Skip to content

Commit 6cb942e

Browse files
authored
[iOS] Double dash in input field crash fix (#20584)
* [iOS] Double dash in input field fix (#20439) * Added a UiTest (#20439) * Double dash in input field fix - improvements(#20439)
1 parent 1252f00 commit 6cb942e

File tree

4 files changed

+84
-2
lines changed

4 files changed

+84
-2
lines changed
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
<?xml version="1.0" encoding="utf-8" ?>
2+
<Shell xmlns="http://schemas.microsoft.com/dotnet/2021/maui"
3+
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
4+
x:Class="Maui.Controls.Sample.Issues.Issue20439"
5+
xmlns:ns="clr-namespace:Maui.Controls.Sample.Issues">
6+
<ShellContent
7+
Title="Home">
8+
<ContentPage>
9+
<StackLayout VerticalOptions="Center">
10+
<Entry AutomationId="entry" Text="Tap here"/>
11+
<Button AutomationId="button" Text="Open the input page" Clicked="Button_Clicked"/>
12+
</StackLayout>
13+
</ContentPage>
14+
</ShellContent>
15+
16+
<ShellContent
17+
Title="Text input page">
18+
<ContentPage>
19+
<Editor VerticalOptions="Center"
20+
AutomationId="editor"
21+
HeightRequest="100"
22+
Text="Potential auto correcting words: -- :-) ... omw \n" />
23+
</ContentPage>
24+
</ShellContent>
25+
</Shell>
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
using Microsoft.Maui.Controls;
2+
using Microsoft.Maui.Controls.Xaml;
3+
4+
namespace Maui.Controls.Sample.Issues
5+
{
6+
[XamlCompilation(XamlCompilationOptions.Compile)]
7+
[Issue(IssueTracker.Github, 20439, "[iOS] Double dash in Entry or Editor crashes the app", PlatformAffected.iOS)]
8+
public partial class Issue20439 : Shell
9+
{
10+
public Issue20439()
11+
{
12+
InitializeComponent();
13+
}
14+
15+
void Button_Clicked(object sender, System.EventArgs e)
16+
{
17+
CurrentItem = Items[1];
18+
}
19+
}
20+
}

src/Controls/src/Core/Platform/iOS/Extensions/TextExtensions.cs

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -41,17 +41,26 @@ static void UpdateText(this IUITextInput textInput, InputView inputView, bool is
4141
// position if needed when the text was modified by a Converter.
4242
var textRange = textInput.GetTextRange(textInput.BeginningOfDocument, textInput.EndOfDocument);
4343
var oldText = textInput.TextInRange(textRange) ?? string.Empty;
44+
45+
// We need this variable because in some cases because of the iOS's
46+
// auto correction eg. eg '--' => '—' the actual text in the input might have
47+
// a different length that the one that has been set in the control.
48+
var newTextLength = !string.IsNullOrWhiteSpace(inputView.Text) ? textInput.TextInRange(textRange)?.Length ?? 0 : 0;
49+
4450
var newText = TextTransformUtilites.GetTransformedText(
4551
inputView?.Text,
4652
textInput.GetSecureTextEntry() ? TextTransform.Default : inputView.TextTransform
4753
);
4854

55+
if (!string.IsNullOrWhiteSpace(oldText))
56+
newTextLength = newText.Length;
57+
4958
if (oldText != newText)
5059
{
5160
// Re-calculate the cursor offset position if the text was modified by a Converter.
5261
// but if the text is being set by code, let's just move the cursor to the end.
53-
var cursorOffset = newText.Length - oldText.Length;
54-
var cursorPosition = isEditing ? textInput.GetCursorPosition(cursorOffset) : newText.Length;
62+
var cursorOffset = newTextLength - oldText.Length;
63+
var cursorPosition = isEditing ? textInput.GetCursorPosition(cursorOffset) : newTextLength;
5564

5665
textInput.ReplaceText(textRange, newText);
5766

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
using NUnit.Framework;
2+
using UITest.Appium;
3+
using UITest.Core;
4+
5+
namespace Microsoft.Maui.AppiumTests.Issues
6+
{
7+
public class Issue20439 : _IssuesUITest
8+
{
9+
public override string Issue => "[iOS] Double dash in Entry or Editor crashes the app";
10+
11+
public Issue20439(TestDevice device) : base(device)
12+
{
13+
}
14+
15+
[Test]
16+
public void ErrorShouldNotBeThrown()
17+
{
18+
this.IgnoreIfPlatforms(new TestDevice[] { TestDevice.Android, TestDevice.Mac, TestDevice.Windows });
19+
20+
_ = App.WaitForElement("entry");
21+
App.Click("entry");
22+
App.Click("button");
23+
24+
// The test passes if no crash is observed
25+
App.FindElement("editor");
26+
}
27+
}
28+
}

0 commit comments

Comments
 (0)