From d4c86003d9ef3eac00ead82cf2a09636e033f4f1 Mon Sep 17 00:00:00 2001 From: NabilaWorks Date: Fri, 21 Feb 2025 01:04:00 +0100 Subject: [PATCH] WebKit should respect TextCapitalization.words (#161027) Fixes https://github.com/flutter/flutter/issues/151118 https://github.com/user-attachments/assets/88a9baf9-8f2c-48fb-ad9c-1618fbce0795 Flutter web apps on iOS were forced to use `sentences` instead of `words` when `TextCapitalization.words` was specified, in order to work around this particular bug : https://bugs.webkit.org/show_bug.cgi?id=148504 That bug was fixed more than 4 years ago so it should be safe to use `words` now. With this change, legacy devices that are still on specific versions on iOS 13 or lower might experience the old WebKit bug when `TextCapitalization.words` is used, however the Flutter developer who expects such users can work around that bug by detecting the user agent and using `TextCapitalization.sentences` conditionally, in order to get the existing behavior prior to this change. ## Pre-launch Checklist - [X] I read the [Contributor Guide] and followed the process outlined there for submitting PRs. - [X] I read the [Tree Hygiene] wiki page, which explains my responsibilities. - [X] I read and followed the [Flutter Style Guide], including [Features we expect every widget to implement]. - [X] I signed the [CLA]. - [X] I listed at least one issue that this PR fixes in the description above. - [X] I updated/added relevant documentation (doc comments with `///`). - [X] I added new tests to check the change I am making, or this PR is [test-exempt]. - [X] I followed the [breaking change policy] and added [Data Driven Fixes] where supported. - [ ] All existing and new tests are passing. If you need help, consider asking for advice on the #hackers-new channel on [Discord]. [Contributor Guide]: https://github.com/flutter/flutter/blob/main/docs/contributing/Tree-hygiene.md#overview [Tree Hygiene]: https://github.com/flutter/flutter/blob/main/docs/contributing/Tree-hygiene.md [test-exempt]: https://github.com/flutter/flutter/blob/main/docs/contributing/Tree-hygiene.md#tests [Flutter Style Guide]: https://github.com/flutter/flutter/blob/main/docs/contributing/Style-guide-for-Flutter-repo.md [Features we expect every widget to implement]: https://github.com/flutter/flutter/blob/main/docs/contributing/Style-guide-for-Flutter-repo.md#features-we-expect-every-widget-to-implement [CLA]: https://cla.developers.google.com/ [flutter/tests]: https://github.com/flutter/tests [breaking change policy]: https://github.com/flutter/flutter/blob/main/docs/contributing/Tree-hygiene.md#handling-breaking-changes [Discord]: https://github.com/flutter/flutter/blob/main/docs/contributing/Chat.md [Data Driven Fixes]: https://github.com/flutter/flutter/blob/main/docs/contributing/Data-driven-Fixes.md --------- Co-authored-by: Mouad Debbar --- .../text_editing/text_capitalization.dart | 11 +------ .../web_ui/test/engine/text_editing_test.dart | 33 +++++++++++++++++++ 2 files changed, 34 insertions(+), 10 deletions(-) diff --git a/engine/src/flutter/lib/web_ui/lib/src/engine/text_editing/text_capitalization.dart b/engine/src/flutter/lib/web_ui/lib/src/engine/text_editing/text_capitalization.dart index 1de39a3f02..5bf69ade83 100644 --- a/engine/src/flutter/lib/web_ui/lib/src/engine/text_editing/text_capitalization.dart +++ b/engine/src/flutter/lib/web_ui/lib/src/engine/text_editing/text_capitalization.dart @@ -2,8 +2,6 @@ // Use of this source code is governed by a BSD-style license that can be // found in the LICENSE file. -import 'package:ui/ui_web/src/ui_web.dart' as ui_web; - import '../dom.dart'; /// Controls the capitalization of the text. @@ -62,14 +60,7 @@ class TextCapitalizationConfig { String autocapitalize = ''; switch (textCapitalization) { case TextCapitalization.words: - // TODO(mdebbar): There is a bug for `words` level capitalization in IOS now. - // For now go back to default. Remove the check after bug is resolved. - // https://bugs.webkit.org/show_bug.cgi?id=148504 - if (ui_web.browser.browserEngine == ui_web.BrowserEngine.webkit) { - autocapitalize = 'sentences'; - } else { - autocapitalize = 'words'; - } + autocapitalize = 'words'; case TextCapitalization.characters: autocapitalize = 'characters'; case TextCapitalization.sentences: diff --git a/engine/src/flutter/lib/web_ui/test/engine/text_editing_test.dart b/engine/src/flutter/lib/web_ui/test/engine/text_editing_test.dart index be8d0c715d..3ca4d0c063 100644 --- a/engine/src/flutter/lib/web_ui/test/engine/text_editing_test.dart +++ b/engine/src/flutter/lib/web_ui/test/engine/text_editing_test.dart @@ -1545,6 +1545,39 @@ Future testMain() async { hideKeyboard(); }); + test('Words capitalization: setClient, setEditingState, show', () { + // Create a configuration with an AutofillGroup of four text fields. + final Map capitalizeWordsConfig = createFlutterConfig( + 'text', + textCapitalization: 'TextCapitalization.words', + ); + final MethodCall setClient = MethodCall('TextInput.setClient', [ + 123, + capitalizeWordsConfig, + ]); + sendFrameworkMessage(codec.encodeMethodCall(setClient)); + + const MethodCall setEditingState1 = MethodCall('TextInput.setEditingState', { + 'text': '', + 'selectionBase': 0, + 'selectionExtent': 0, + }); + sendFrameworkMessage(codec.encodeMethodCall(setEditingState1)); + + const MethodCall show = MethodCall('TextInput.show'); + sendFrameworkMessage(codec.encodeMethodCall(show)); + spy.messages.clear(); + + // Test for mobile Safari. + if (ui_web.browser.browserEngine == ui_web.BrowserEngine.webkit && + ui_web.browser.operatingSystem == ui_web.OperatingSystem.iOs) { + expect(textEditing!.strategy.domElement!.getAttribute('autocapitalize'), 'words'); + } + + spy.messages.clear(); + hideKeyboard(); + }); + test( 'setClient, setEditableSizeAndTransform, setStyle, setEditingState, show, clearClient', () {