Skip to content

fix(TextBox): fix MaxLength to work correctly with paste #19665

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -4160,6 +4160,41 @@ public async Task When_Setting_Short_Text_And_Previous_Selection_Is_OutOfBounds(
SUT.RaiseEvent(UIElement.KeyUpEvent, new KeyRoutedEventArgs(SUT, VirtualKey.Escape, VirtualKeyModifiers.None));
}

[TestMethod]
public async Task When_MaxLine_Paste()
{
using var _ = new TextBoxFeatureConfigDisposable();

var SUT = new TextBox
{
MaxLength = 10,
Text = "0123456789",
SelectionStart = 4,
SelectionLength = 2
};

WindowHelper.WindowContent = SUT;

await WindowHelper.WaitForIdle();
await WindowHelper.WaitForLoaded(SUT);

SUT.Focus(FocusState.Programmatic);
await WindowHelper.WaitForIdle();

var dp = new DataPackage();
var text = "abcdefgh";
dp.SetText(text);
Clipboard.SetContent(dp);
await WindowHelper.WaitForIdle();

SUT.PasteFromClipboard();
await WindowHelper.WaitForIdle();

Assert.AreEqual("0123ab6789", SUT.Text);
Assert.AreEqual(6, SUT.SelectionStart);
Assert.AreEqual(0, SUT.SelectionLength);
}

private static bool HasColorInRectangle(RawBitmap screenshot, Rectangle rect, Color expectedColor)
{
for (var x = rect.Left; x < rect.Right; x++)
Expand Down
33 changes: 20 additions & 13 deletions src/Uno.UI/UI/Xaml/Controls/TextBox/TextBox.cs
Original file line number Diff line number Diff line change
Expand Up @@ -417,20 +417,18 @@ private object CoerceText(object baseValue)

if (MaxLength > 0 && baseString.Length > MaxLength)
{
// Reject the new string if it's longer than the MaxLength
#if __SKIA__
_pendingSelection = null;
#endif
return DependencyProperty.UnsetValue;
}

#if __SKIA__
if (!AcceptsReturn)
{
baseString = GetFirstLine(baseString);
if (_pendingSelection is { } selection)
{
var start = Math.Min(selection.start, baseString.Length);
var end = Math.Min(selection.start + selection.length, baseString.Length);
_pendingSelection = (start, end - start);
}
}
#if __SKIA__
else if (_isSkiaTextBox)
{
// WinUI replaces all \n's and and \r\n's by \r. This is annoying because
Expand All @@ -439,10 +437,13 @@ private object CoerceText(object baseValue)
// the native input and the managed representation.
baseString = RemoveLF(baseString);
}
#else
if (!AcceptsReturn)

// make sure this coercion doesn't cause the pending selection to be out of range
if (_pendingSelection is { } selection2)
{
baseString = GetFirstLine(baseString);
var start = Math.Min(selection2.start, baseString.Length);
var end = Math.Min(selection2.start + selection2.length, baseString.Length);
_pendingSelection = (start, end - start);
}
#endif

Expand Down Expand Up @@ -1404,15 +1405,21 @@ internal void PasteFromClipboard(string clipboardText)
var selectionStart = SelectionStart;
var selectionLength = SelectionLength;
var currentText = Text;
var adjustedClipboardText = clipboardText;

if (selectionLength > 0)
{
currentText = currentText.Remove(selectionStart, selectionLength);
}

currentText = currentText.Insert(selectionStart, clipboardText);
if (MaxLength > 0)
{
var clipboardRangeToBePasted = Math.Max(0, Math.Min(clipboardText.Length, MaxLength - currentText.Length));
adjustedClipboardText = clipboardText[..clipboardRangeToBePasted];
}

PasteFromClipboardPartial(clipboardText, selectionStart, selectionLength, currentText);
currentText = currentText.Insert(selectionStart, adjustedClipboardText);
PasteFromClipboardPartial(adjustedClipboardText, selectionStart, currentText);

#if __SKIA__
try
Expand Down Expand Up @@ -1442,7 +1449,7 @@ internal void PasteFromClipboard(string clipboardText)
#endif
}

partial void PasteFromClipboardPartial(string clipboardText, int selectionStart, int selectionLength, string newText);
partial void PasteFromClipboardPartial(string adjustedClipboardText, int selectionStart, string newText);

/// <summary>
/// Copies the selected content to the OS clipboard.
Expand Down
4 changes: 2 additions & 2 deletions src/Uno.UI/UI/Xaml/Controls/TextBox/TextBox.skia.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1212,7 +1212,7 @@ private string RemoveLF(string baseString)
return baseString;
}

partial void PasteFromClipboardPartial(string clipboardText, int selectionStart, int selectionLength, string newText)
partial void PasteFromClipboardPartial(string adjustedClipboardText, int selectionStart, string newText)
{
if (_isSkiaTextBox)
{
Expand All @@ -1227,7 +1227,7 @@ partial void PasteFromClipboardPartial(string clipboardText, int selectionStart,
CommitAction(new ReplaceAction(Text, newText, selectionStart));
}

_pendingSelection = (selectionStart + clipboardText.Length, 0);
_pendingSelection = (selectionStart + adjustedClipboardText.Length, 0);
}
}

Expand Down
Loading