@@ -419,20 +419,18 @@ private object CoerceText(object baseValue)
419
419
420
420
if ( MaxLength > 0 && baseString . Length > MaxLength )
421
421
{
422
+ // Reject the new string if it's longer than the MaxLength
423
+ #if __SKIA__
424
+ _pendingSelection = null ;
425
+ #endif
422
426
return DependencyProperty . UnsetValue ;
423
427
}
424
428
425
- #if __SKIA__
426
429
if ( ! AcceptsReturn )
427
430
{
428
431
baseString = GetFirstLine ( baseString ) ;
429
- if ( _pendingSelection is { } selection )
430
- {
431
- var start = Math . Min ( selection . start , baseString . Length ) ;
432
- var end = Math . Min ( selection . start + selection . length , baseString . Length ) ;
433
- _pendingSelection = ( start , end - start ) ;
434
- }
435
432
}
433
+ #if __SKIA__
436
434
else if ( _isSkiaTextBox )
437
435
{
438
436
// WinUI replaces all \n's and and \r\n's by \r. This is annoying because
@@ -441,10 +439,13 @@ private object CoerceText(object baseValue)
441
439
// the native input and the managed representation.
442
440
baseString = RemoveLF ( baseString ) ;
443
441
}
444
- #else
445
- if ( ! AcceptsReturn )
442
+
443
+ // make sure this coercion doesn't cause the pending selection to be out of range
444
+ if ( _pendingSelection is { } selection2 )
446
445
{
447
- baseString = GetFirstLine ( baseString ) ;
446
+ var start = Math . Min ( selection2 . start , baseString . Length ) ;
447
+ var end = Math . Min ( selection2 . start + selection2 . length , baseString . Length ) ;
448
+ _pendingSelection = ( start , end - start ) ;
448
449
}
449
450
#endif
450
451
@@ -1430,15 +1431,21 @@ internal void PasteFromClipboard(string clipboardText)
1430
1431
var selectionStart = SelectionStart ;
1431
1432
var selectionLength = SelectionLength ;
1432
1433
var currentText = Text ;
1434
+ var adjustedClipboardText = clipboardText ;
1433
1435
1434
1436
if ( selectionLength > 0 )
1435
1437
{
1436
1438
currentText = currentText . Remove ( selectionStart , selectionLength ) ;
1437
1439
}
1438
1440
1439
- currentText = currentText . Insert ( selectionStart , clipboardText ) ;
1441
+ if ( MaxLength > 0 )
1442
+ {
1443
+ var clipboardRangeToBePasted = Math . Max ( 0 , Math . Min ( clipboardText . Length , MaxLength - currentText . Length ) ) ;
1444
+ adjustedClipboardText = clipboardText [ ..clipboardRangeToBePasted ] ;
1445
+ }
1440
1446
1441
- PasteFromClipboardPartial ( clipboardText , selectionStart , selectionLength , currentText ) ;
1447
+ currentText = currentText . Insert ( selectionStart , adjustedClipboardText ) ;
1448
+ PasteFromClipboardPartial ( adjustedClipboardText , selectionStart , currentText ) ;
1442
1449
1443
1450
#if __SKIA__
1444
1451
try
@@ -1468,7 +1475,7 @@ internal void PasteFromClipboard(string clipboardText)
1468
1475
#endif
1469
1476
}
1470
1477
1471
- partial void PasteFromClipboardPartial ( string clipboardText , int selectionStart , int selectionLength , string newText ) ;
1478
+ partial void PasteFromClipboardPartial ( string adjustedClipboardText , int selectionStart , string newText ) ;
1472
1479
1473
1480
/// <summary>
1474
1481
/// Copies the selected content to the OS clipboard.
0 commit comments