Skip to content

Commit

Permalink
[iOS] Double dash in input field crash fix (#20584)
Browse files Browse the repository at this point in the history
* [iOS] Double dash in input field fix (#20439)

* Added a UiTest (#20439)

* Double dash in input field fix - improvements(#20439)
  • Loading branch information
kubaflo committed Mar 4, 2024
1 parent 1252f00 commit 6cb942e
Show file tree
Hide file tree
Showing 4 changed files with 84 additions and 2 deletions.
@@ -0,0 +1,25 @@
<?xml version="1.0" encoding="utf-8" ?>
<Shell xmlns="http://schemas.microsoft.com/dotnet/2021/maui"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
x:Class="Maui.Controls.Sample.Issues.Issue20439"
xmlns:ns="clr-namespace:Maui.Controls.Sample.Issues">
<ShellContent
Title="Home">
<ContentPage>
<StackLayout VerticalOptions="Center">
<Entry AutomationId="entry" Text="Tap here"/>
<Button AutomationId="button" Text="Open the input page" Clicked="Button_Clicked"/>
</StackLayout>
</ContentPage>
</ShellContent>

<ShellContent
Title="Text input page">
<ContentPage>
<Editor VerticalOptions="Center"
AutomationId="editor"
HeightRequest="100"
Text="Potential auto correcting words: -- :-) ... omw \n" />
</ContentPage>
</ShellContent>
</Shell>
@@ -0,0 +1,20 @@
using Microsoft.Maui.Controls;
using Microsoft.Maui.Controls.Xaml;

namespace Maui.Controls.Sample.Issues
{
[XamlCompilation(XamlCompilationOptions.Compile)]
[Issue(IssueTracker.Github, 20439, "[iOS] Double dash in Entry or Editor crashes the app", PlatformAffected.iOS)]
public partial class Issue20439 : Shell
{
public Issue20439()
{
InitializeComponent();
}

void Button_Clicked(object sender, System.EventArgs e)
{
CurrentItem = Items[1];
}
}
}
13 changes: 11 additions & 2 deletions src/Controls/src/Core/Platform/iOS/Extensions/TextExtensions.cs
Expand Up @@ -41,17 +41,26 @@ static void UpdateText(this IUITextInput textInput, InputView inputView, bool is
// position if needed when the text was modified by a Converter.
var textRange = textInput.GetTextRange(textInput.BeginningOfDocument, textInput.EndOfDocument);
var oldText = textInput.TextInRange(textRange) ?? string.Empty;

// We need this variable because in some cases because of the iOS's
// auto correction eg. eg '--' => '—' the actual text in the input might have
// a different length that the one that has been set in the control.
var newTextLength = !string.IsNullOrWhiteSpace(inputView.Text) ? textInput.TextInRange(textRange)?.Length ?? 0 : 0;

var newText = TextTransformUtilites.GetTransformedText(
inputView?.Text,
textInput.GetSecureTextEntry() ? TextTransform.Default : inputView.TextTransform
);

if (!string.IsNullOrWhiteSpace(oldText))
newTextLength = newText.Length;

if (oldText != newText)
{
// Re-calculate the cursor offset position if the text was modified by a Converter.
// but if the text is being set by code, let's just move the cursor to the end.
var cursorOffset = newText.Length - oldText.Length;
var cursorPosition = isEditing ? textInput.GetCursorPosition(cursorOffset) : newText.Length;
var cursorOffset = newTextLength - oldText.Length;
var cursorPosition = isEditing ? textInput.GetCursorPosition(cursorOffset) : newTextLength;

textInput.ReplaceText(textRange, newText);

Expand Down
28 changes: 28 additions & 0 deletions src/Controls/tests/UITests/Tests/Issues/Issue20439.cs
@@ -0,0 +1,28 @@
using NUnit.Framework;
using UITest.Appium;
using UITest.Core;

namespace Microsoft.Maui.AppiumTests.Issues
{
public class Issue20439 : _IssuesUITest
{
public override string Issue => "[iOS] Double dash in Entry or Editor crashes the app";

public Issue20439(TestDevice device) : base(device)
{
}

[Test]
public void ErrorShouldNotBeThrown()
{
this.IgnoreIfPlatforms(new TestDevice[] { TestDevice.Android, TestDevice.Mac, TestDevice.Windows });

_ = App.WaitForElement("entry");
App.Click("entry");
App.Click("button");

// The test passes if no crash is observed
App.FindElement("editor");
}
}
}

0 comments on commit 6cb942e

Please sign in to comment.