Title: [291136] trunk
Revision
291136
Author
[email protected]
Date
2022-03-10 16:15:39 -0800 (Thu, 10 Mar 2022)

Log Message

[iOS] Add support for -[UITextInput addTextAlternatives:] on WKContentView
https://bugs.webkit.org/show_bug.cgi?id=237691
rdar://89647018

Reviewed by Aditya Keerthi.

Source/WebKit:

Work towards refactoring support for dictation alternatives on iOS; this patch adds support for an optional,
private UITextInput protocol method on WKContentView, which clients can use to directly insert
NSTextAlternatives for a given string of matching text (i.e., if the given string matches text at or before the
current selection range).

Tests:  TextAlternatives.AddTextAlternativesWithMatch
        TextAlternatives.AddTextAlternativesWithSelectedMatch
        TextAlternatives.AddTextAlternativesWithoutMatch

* Scripts/webkit/messages.py:
(headers_for_type):

Add support for sending WebCore::DictationContext as an IPC message argument.

* UIProcess/Cocoa/WebPageProxyCocoa.mm:
(WebKit::WebPageProxy::addDictationAlternative):

Register the dictation alternative (NSTextAlternative) on the page client, and send a message to the web process
to try and insert document markers corresponding to this dictation alternative. If, for whatever reason, we fail
to map dictation alternatives to an editable DOM range, make sure we automatically unregister this text
alternative as well.

* UIProcess/WebPageProxy.h:
* UIProcess/ios/WKContentViewInteraction.mm:
(-[WKContentView addTextAlternatives:]):
* WebProcess/WebPage/Cocoa/WebPageCocoa.mm:
(WebKit::WebPage::addDictationAlternative):

Given a string to match and a dictation alternative identifier context, try to search for the matching string
before (or in) the selection; if found, install the dictation alternatives in the match range, as a document
marker.

* WebProcess/WebPage/WebPage.h:
* WebProcess/WebPage/WebPage.messages.in:

Tools:

Add several basic API tests to exercise the new API; in particular, exercise both cases where the text
alternative's primary text matches before the selection, where it does not match, and where it matches inside
the selection.

* TestWebKitAPI/TestWebKitAPI.xcodeproj/project.pbxproj:
* TestWebKitAPI/Tests/ios/TextAlternatives.mm: Added.
(-[TestWKWebView dictationAlternativesMarkerCount:]):
(TestWebKitAPI::createWebViewForTestingTextAlternatives):
(TestWebKitAPI::TEST):
* TestWebKitAPI/ios/UIKitSPI.h:

Modified Paths

Added Paths

Diff

Modified: trunk/Source/WebKit/ChangeLog (291135 => 291136)


--- trunk/Source/WebKit/ChangeLog	2022-03-10 23:45:46 UTC (rev 291135)
+++ trunk/Source/WebKit/ChangeLog	2022-03-11 00:15:39 UTC (rev 291136)
@@ -1,3 +1,46 @@
+2022-03-10  Wenson Hsieh  <[email protected]>
+
+        [iOS] Add support for -[UITextInput addTextAlternatives:] on WKContentView
+        https://bugs.webkit.org/show_bug.cgi?id=237691
+        rdar://89647018
+
+        Reviewed by Aditya Keerthi.
+
+        Work towards refactoring support for dictation alternatives on iOS; this patch adds support for an optional,
+        private UITextInput protocol method on WKContentView, which clients can use to directly insert
+        NSTextAlternatives for a given string of matching text (i.e., if the given string matches text at or before the
+        current selection range).
+
+        Tests:  TextAlternatives.AddTextAlternativesWithMatch
+                TextAlternatives.AddTextAlternativesWithSelectedMatch
+                TextAlternatives.AddTextAlternativesWithoutMatch
+
+        * Scripts/webkit/messages.py:
+        (headers_for_type):
+
+        Add support for sending WebCore::DictationContext as an IPC message argument.
+
+        * UIProcess/Cocoa/WebPageProxyCocoa.mm:
+        (WebKit::WebPageProxy::addDictationAlternative):
+
+        Register the dictation alternative (NSTextAlternative) on the page client, and send a message to the web process
+        to try and insert document markers corresponding to this dictation alternative. If, for whatever reason, we fail
+        to map dictation alternatives to an editable DOM range, make sure we automatically unregister this text
+        alternative as well.
+
+        * UIProcess/WebPageProxy.h:
+        * UIProcess/ios/WKContentViewInteraction.mm:
+        (-[WKContentView addTextAlternatives:]):
+        * WebProcess/WebPage/Cocoa/WebPageCocoa.mm:
+        (WebKit::WebPage::addDictationAlternative):
+
+        Given a string to match and a dictation alternative identifier context, try to search for the matching string
+        before (or in) the selection; if found, install the dictation alternatives in the match range, as a document
+        marker.
+
+        * WebProcess/WebPage/WebPage.h:
+        * WebProcess/WebPage/WebPage.messages.in:
+
 2022-03-10  Jean-Yves Avenard  <[email protected]>
 
         MediaSampleCursor should iterate over the samples contained in the MediaSamplesBlock

Modified: trunk/Source/WebKit/Scripts/webkit/messages.py (291135 => 291136)


--- trunk/Source/WebKit/Scripts/webkit/messages.py	2022-03-10 23:45:46 UTC (rev 291135)
+++ trunk/Source/WebKit/Scripts/webkit/messages.py	2022-03-11 00:15:39 UTC (rev 291136)
@@ -774,6 +774,7 @@
         'WebCore::DOMPasteAccessCategory': ['<WebCore/DOMPasteAccess.h>'],
         'WebCore::DOMPasteAccessResponse': ['<WebCore/DOMPasteAccess.h>'],
         'WebCore::DestinationColorSpace': ['<WebCore/ColorSpace.h>'],
+        'WebCore::DictationContext': ['<WebCore/DictationContext.h>'],
         'WebCore::DisplayList::ItemBufferIdentifier': ['<WebCore/DisplayList.h>'],
         'WebCore::DocumentMarkerLineStyle': ['<WebCore/GraphicsTypes.h>'],
         'WebCore::DragApplicationFlags': ['<WebCore/DragData.h>'],

Modified: trunk/Source/WebKit/UIProcess/Cocoa/WebPageProxyCocoa.mm (291135 => 291136)


--- trunk/Source/WebKit/UIProcess/Cocoa/WebPageProxyCocoa.mm	2022-03-10 23:45:46 UTC (rev 291135)
+++ trunk/Source/WebKit/UIProcess/Cocoa/WebPageProxyCocoa.mm	2022-03-11 00:15:39 UTC (rev 291136)
@@ -373,6 +373,19 @@
     send(Messages::WebPage::InsertDictatedTextAsync { text, replacementRange, dictationAlternatives, WTFMove(options) });
 }
 
+void WebPageProxy::addDictationAlternative(TextAlternativeWithRange&& alternative)
+{
+    if (!hasRunningProcess())
+        return;
+
+    auto nsAlternatives = alternative.alternatives.get();
+    auto context = pageClient().addDictationAlternatives(nsAlternatives);
+    sendWithAsyncReply(Messages::WebPage::AddDictationAlternative { nsAlternatives.primaryString, context }, [context, weakThis = WeakPtr { *this }](bool success) {
+        if (RefPtr protectedThis = weakThis.get(); protectedThis && !success)
+            protectedThis->removeDictationAlternatives(context);
+    });
+}
+
 #if USE(DICTATION_ALTERNATIVES)
 
 NSTextAlternatives *WebPageProxy::platformDictationAlternatives(WebCore::DictationContext dictationContext)

Modified: trunk/Source/WebKit/UIProcess/WebPageProxy.h (291135 => 291136)


--- trunk/Source/WebKit/UIProcess/WebPageProxy.h	2022-03-10 23:45:46 UTC (rev 291135)
+++ trunk/Source/WebKit/UIProcess/WebPageProxy.h	2022-03-11 00:15:39 UTC (rev 291136)
@@ -973,6 +973,8 @@
     void insertTextAsync(const String&, const EditingRange& replacementRange, InsertTextOptions&&);
     void insertDictatedTextAsync(const String&, const EditingRange& replacementRange, const Vector<WebCore::TextAlternativeWithRange>&, InsertTextOptions&&);
 
+    void addDictationAlternative(WebCore::TextAlternativeWithRange&&);
+
 #if USE(DICTATION_ALTERNATIVES)
     NSTextAlternatives *platformDictationAlternatives(WebCore::DictationContext);
 #endif

Modified: trunk/Source/WebKit/UIProcess/ios/WKContentViewInteraction.mm (291135 => 291136)


--- trunk/Source/WebKit/UIProcess/ios/WKContentViewInteraction.mm	2022-03-10 23:45:46 UTC (rev 291135)
+++ trunk/Source/WebKit/UIProcess/ios/WKContentViewInteraction.mm	2022-03-11 00:15:39 UTC (rev 291136)
@@ -5642,6 +5642,11 @@
     return !editorState.isMissingPostLayoutData && editorState.postLayoutData().hasPlainText;
 }
 
+- (void)addTextAlternatives:(NSTextAlternatives *)alternatives
+{
+    _page->addDictationAlternative({ alternatives, NSMakeRange(0, alternatives.primaryString.length) });
+}
+
 // end of UITextInput protocol implementation
 
 static UITextAutocapitalizationType toUITextAutocapitalize(WebCore::AutocapitalizeType webkitType)

Modified: trunk/Source/WebKit/WebProcess/WebPage/Cocoa/WebPageCocoa.mm (291135 => 291136)


--- trunk/Source/WebKit/WebProcess/WebPage/Cocoa/WebPageCocoa.mm	2022-03-10 23:45:46 UTC (rev 291135)
+++ trunk/Source/WebKit/WebProcess/WebPage/Cocoa/WebPageCocoa.mm	2022-03-11 00:15:39 UTC (rev 291136)
@@ -37,6 +37,7 @@
 #import "WebRemoteObjectRegistry.h"
 #import <pal/spi/cocoa/LaunchServicesSPI.h>
 #import <WebCore/DictionaryLookup.h>
+#import <WebCore/DocumentMarkerController.h>
 #import <WebCore/Editing.h>
 #import <WebCore/Editor.h>
 #import <WebCore/EventHandler.h>
@@ -247,6 +248,42 @@
     }
 }
 
+void WebPage::addDictationAlternative(const String& text, DictationContext context, CompletionHandler<void(bool)>&& completion)
+{
+    Ref frame = CheckedRef(m_page->focusController())->focusedOrMainFrame();
+    RefPtr document = frame->document();
+    if (!document) {
+        completion(false);
+        return;
+    }
+
+    auto selection = frame->selection().selection();
+    RefPtr editableRoot = selection.rootEditableElement();
+    if (!editableRoot) {
+        completion(false);
+        return;
+    }
+
+    auto firstEditablePosition = firstPositionInNode(editableRoot.get());
+    auto selectionEnd = selection.end();
+    auto searchRange = makeSimpleRange(firstEditablePosition, selectionEnd);
+    if (!searchRange) {
+        completion(false);
+        return;
+    }
+
+    auto targetOffset = characterCount(*searchRange);
+    targetOffset -= std::min<uint64_t>(targetOffset, text.length());
+    auto matchRange = findClosestPlainText(*searchRange, text, { Backwards, DoNotRevealSelection }, targetOffset);
+    if (matchRange.collapsed()) {
+        completion(false);
+        return;
+    }
+
+    document->markers().addMarker(matchRange, DocumentMarker::DictationAlternatives, { DocumentMarker::DictationData { context, text } });
+    completion(true);
+}
+
 void WebPage::accessibilityTransferRemoteToken(RetainPtr<NSData> remoteToken)
 {
     IPC::DataReference dataToken = IPC::DataReference(reinterpret_cast<const uint8_t*>([remoteToken bytes]), [remoteToken length]);

Modified: trunk/Source/WebKit/WebProcess/WebPage/WebPage.h (291135 => 291136)


--- trunk/Source/WebKit/WebProcess/WebPage/WebPage.h	2022-03-10 23:45:46 UTC (rev 291135)
+++ trunk/Source/WebKit/WebProcess/WebPage/WebPage.h	2022-03-11 00:15:39 UTC (rev 291136)
@@ -969,7 +969,8 @@
     bool performNonEditingBehaviorForSelector(const String&, WebCore::KeyboardEvent*);
 
     void insertDictatedTextAsync(const String& text, const EditingRange& replacementRange, const Vector<WebCore::DictationAlternative>& dictationAlternativeLocations, InsertTextOptions&&);
-#endif
+    void addDictationAlternative(const String& text, WebCore::DictationContext, CompletionHandler<void(bool)>&&);
+#endif // PLATFORM(COCOA)
 
 #if PLATFORM(MAC)
     void attributedSubstringForCharacterRangeAsync(const EditingRange&, CompletionHandler<void(const WebCore::AttributedString&, const EditingRange&)>&&);

Modified: trunk/Source/WebKit/WebProcess/WebPage/WebPage.messages.in (291135 => 291136)


--- trunk/Source/WebKit/WebProcess/WebPage/WebPage.messages.in	2022-03-10 23:45:46 UTC (rev 291135)
+++ trunk/Source/WebKit/WebProcess/WebPage/WebPage.messages.in	2022-03-11 00:15:39 UTC (rev 291136)
@@ -478,6 +478,7 @@
 
     InsertTextAsync(String text, struct WebKit::EditingRange replacementRange, struct WebKit::InsertTextOptions options)
     InsertDictatedTextAsync(String text, struct WebKit::EditingRange replacementRange, Vector<WebCore::DictationAlternative> dictationAlternatives, struct WebKit::InsertTextOptions options)
+    AddDictationAlternative(String text, WebCore::DictationContext context) -> (bool success) Async
 
     HasMarkedText() -> (bool hasMarkedText)
     GetMarkedRangeAsync() -> (struct WebKit::EditingRange range)

Modified: trunk/Tools/ChangeLog (291135 => 291136)


--- trunk/Tools/ChangeLog	2022-03-10 23:45:46 UTC (rev 291135)
+++ trunk/Tools/ChangeLog	2022-03-11 00:15:39 UTC (rev 291136)
@@ -1,3 +1,22 @@
+2022-03-10  Wenson Hsieh  <[email protected]>
+
+        [iOS] Add support for -[UITextInput addTextAlternatives:] on WKContentView
+        https://bugs.webkit.org/show_bug.cgi?id=237691
+        rdar://89647018
+
+        Reviewed by Aditya Keerthi.
+
+        Add several basic API tests to exercise the new API; in particular, exercise both cases where the text
+        alternative's primary text matches before the selection, where it does not match, and where it matches inside
+        the selection.
+
+        * TestWebKitAPI/TestWebKitAPI.xcodeproj/project.pbxproj:
+        * TestWebKitAPI/Tests/ios/TextAlternatives.mm: Added.
+        (-[TestWKWebView dictationAlternativesMarkerCount:]):
+        (TestWebKitAPI::createWebViewForTestingTextAlternatives):
+        (TestWebKitAPI::TEST):
+        * TestWebKitAPI/ios/UIKitSPI.h:
+
 2022-03-10  Elliott Williams  <[email protected]>
 
         [XCBuild] Fix GeneratedSources dependency validation errors

Modified: trunk/Tools/TestWebKitAPI/TestWebKitAPI.xcodeproj/project.pbxproj (291135 => 291136)


--- trunk/Tools/TestWebKitAPI/TestWebKitAPI.xcodeproj/project.pbxproj	2022-03-10 23:45:46 UTC (rev 291135)
+++ trunk/Tools/TestWebKitAPI/TestWebKitAPI.xcodeproj/project.pbxproj	2022-03-11 00:15:39 UTC (rev 291136)
@@ -1090,6 +1090,7 @@
 		F46C45C427D42A2F00ECED2C /* ApplicationStateTracking.mm in Sources */ = {isa = PBXBuildFile; fileRef = F46C45C327D42A2F00ECED2C /* ApplicationStateTracking.mm */; };
 		F46D43AB26D7092800969E5E /* test.jpg in Copy Resources */ = {isa = PBXBuildFile; fileRef = F46D43AA26D7090300969E5E /* test.jpg */; };
 		F472874727816BCE003EBE7F /* NSResponderTests.mm in Sources */ = {isa = PBXBuildFile; fileRef = F43C3823278133190099ABCE /* NSResponderTests.mm */; };
+		F472E00327D966F200F3A172 /* TextAlternatives.mm in Sources */ = {isa = PBXBuildFile; fileRef = F472E00227D966F200F3A172 /* TextAlternatives.mm */; };
 		F47728991E4AE3C1007ABF6A /* full-page-contenteditable.html in Copy Resources */ = {isa = PBXBuildFile; fileRef = F47728981E4AE3AD007ABF6A /* full-page-contenteditable.html */; };
 		F47DFB2621A878DF00021FB6 /* data-detectors.html in Copy Resources */ = {isa = PBXBuildFile; fileRef = F47DFB2421A8704A00021FB6 /* data-detectors.html */; };
 		F481C028277945B800E470CF /* modal-container-with-overlay.html in Copy Resources */ = {isa = PBXBuildFile; fileRef = F481C0272779459700E470CF /* modal-container-with-overlay.html */; };
@@ -3107,6 +3108,7 @@
 		F46BD5682487062D008282D6 /* dragstart-data.html */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = text.html; path = "dragstart-data.html"; sourceTree = "<group>"; };
 		F46C45C327D42A2F00ECED2C /* ApplicationStateTracking.mm */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.cpp.objcpp; path = ApplicationStateTracking.mm; sourceTree = "<group>"; };
 		F46D43AA26D7090300969E5E /* test.jpg */ = {isa = PBXFileReference; lastKnownFileType = image.jpeg; path = test.jpg; sourceTree = "<group>"; };
+		F472E00227D966F200F3A172 /* TextAlternatives.mm */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.cpp.objcpp; path = TextAlternatives.mm; sourceTree = "<group>"; };
 		F47728981E4AE3AD007ABF6A /* full-page-contenteditable.html */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = text.html; path = "full-page-contenteditable.html"; sourceTree = "<group>"; };
 		F47D30EB1ED28619000482E1 /* apple.gif */ = {isa = PBXFileReference; lastKnownFileType = image.gif; path = apple.gif; sourceTree = "<group>"; };
 		F47D30ED1ED28A6C000482E1 /* gif-and-file-input.html */ = {isa = PBXFileReference; lastKnownFileType = text.html; path = "gif-and-file-input.html"; sourceTree = "<group>"; };
@@ -3980,6 +3982,7 @@
 				4433A395208044130091ED57 /* SynchronousTimeoutTests.mm */,
 				F45E15742112CE6200307E82 /* TestInputDelegate.h */,
 				F45E15752112CE6200307E82 /* TestInputDelegate.mm */,
+				F472E00227D966F200F3A172 /* TextAlternatives.mm */,
 				F45033F4206BEC95009351CE /* TextAutosizingBoost.mm */,
 				F494B369263120780060A310 /* TextServicesTests.mm */,
 				1C24DEEC263001DE00450D07 /* TextStyleFontSize.mm */,
@@ -5831,6 +5834,7 @@
 				7B774906267CCE72009873B4 /* TestRunnerTests.cpp in Sources */,
 				F4D5C55827C6FF4800ED1C23 /* TestUIMenuBuilder.mm in Sources */,
 				F4517B672054C49500C26721 /* TestWKWebViewController.mm in Sources */,
+				F472E00327D966F200F3A172 /* TextAlternatives.mm in Sources */,
 				F45033F5206BEC95009351CE /* TextAutosizingBoost.mm in Sources */,
 				93E6193B1F931B3A00AF245E /* TextCodec.cpp in Sources */,
 				CE3524F91B1441C40028A7C5 /* TextFieldDidBeginAndEndEditing.cpp in Sources */,

Added: trunk/Tools/TestWebKitAPI/Tests/ios/TextAlternatives.mm (0 => 291136)


--- trunk/Tools/TestWebKitAPI/Tests/ios/TextAlternatives.mm	                        (rev 0)
+++ trunk/Tools/TestWebKitAPI/Tests/ios/TextAlternatives.mm	2022-03-11 00:15:39 UTC (rev 291136)
@@ -0,0 +1,101 @@
+/*
+ * Copyright (C) 2022 Apple Inc. All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ * 1. Redistributions of source code must retain the above copyright
+ *    notice, this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ *    notice, this list of conditions and the following disclaimer in the
+ *    documentation and/or other materials provided with the distribution.
+ *
+ * THIS SOFTWARE IS PROVIDED BY APPLE INC. AND ITS CONTRIBUTORS ``AS IS''
+ * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
+ * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
+ * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR ITS CONTRIBUTORS
+ * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
+ * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
+ * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
+ * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
+ * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
+ * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
+ * THE POSSIBILITY OF SUCH DAMAGE.
+ */
+
+#import "config.h"
+
+#if PLATFORM(IOS_FAMILY)
+
+#import "TestWKWebView.h"
+#import "UIKitSPI.h"
+#import "WKWebViewConfigurationExtras.h"
+#import <WebKit/WKWebViewPrivate.h>
+
+@interface TestWKWebView (TextAlternatives)
+- (NSUInteger)dictationAlternativesMarkerCount:(NSString *)evaluateNodeExpression;
+@end
+
+@implementation TestWKWebView (TextAlternatives)
+
+- (NSUInteger)dictationAlternativesMarkerCount:(NSString *)evaluateNodeExpression
+{
+    NSString *scriptToEvaluate = [NSString stringWithFormat:@"internals.markerCountForNode((() => %@)(), 'dictationalternatives')", evaluateNodeExpression];
+    NSNumber *result = [self objectByEvaluatingJavaScript:scriptToEvaluate];
+    return result.unsignedIntegerValue;
+}
+
+@end
+
+namespace TestWebKitAPI {
+
+static RetainPtr<TestWKWebView> createWebViewForTestingTextAlternatives()
+{
+    auto configuration = [WKWebViewConfiguration _test_configurationWithTestPlugInClassName:@"WebProcessPlugInWithInternals" configureJSCForTesting:YES];
+    auto webView = adoptNS([[TestWKWebView alloc] initWithFrame:CGRectMake(0, 0, 320, 568) configuration:configuration]);
+    [webView _setEditable:YES];
+    return webView;
+}
+
+TEST(TextAlternatives, AddTextAlternativesWithMatch)
+{
+    auto webView = createWebViewForTestingTextAlternatives();
+    [webView synchronouslyLoadHTMLString:@"<body>hello world&nbsp;</body>"];
+    [webView stringByEvaluatingJavaScript:@"getSelection().setPosition(document.body, 1)"];
+
+    auto alternatives = adoptNS([[NSTextAlternatives alloc] initWithPrimaryString:@"hello world" alternativeStrings:@[ @"👋🌎" ]]);
+    [[webView textInputContentView] addTextAlternatives:alternatives.get()];
+    [webView waitForNextPresentationUpdate];
+
+    EXPECT_EQ(1U, [webView dictationAlternativesMarkerCount:@"document.body.childNodes[0]"]);
+}
+
+TEST(TextAlternatives, AddTextAlternativesWithSelectedMatch)
+{
+    auto webView = createWebViewForTestingTextAlternatives();
+    [webView synchronouslyLoadHTMLString:@"<body>hello world&nbsp;</body>"];
+    [webView stringByEvaluatingJavaScript:@"getSelection().selectAllChildren(document.body)"];
+
+    auto alternatives = adoptNS([[NSTextAlternatives alloc] initWithPrimaryString:@"hello world" alternativeStrings:@[ @"👋🌎" ]]);
+    [[webView textInputContentView] addTextAlternatives:alternatives.get()];
+    [webView waitForNextPresentationUpdate];
+
+    EXPECT_EQ(1U, [webView dictationAlternativesMarkerCount:@"document.body.childNodes[0]"]);
+}
+
+TEST(TextAlternatives, AddTextAlternativesWithoutMatch)
+{
+    auto webView = createWebViewForTestingTextAlternatives();
+    [webView synchronouslyLoadHTMLString:@"<body>hello world&nbsp;</body>"];
+    [webView stringByEvaluatingJavaScript:@"getSelection().setPosition(document.body, 1)"];
+
+    auto alternatives = adoptNS([[NSTextAlternatives alloc] initWithPrimaryString:@"goodbye world" alternativeStrings:@[ @"👋🌎" ]]);
+    [[webView textInputContentView] addTextAlternatives:alternatives.get()];
+    [webView waitForNextPresentationUpdate];
+
+    EXPECT_EQ(0U, [webView dictationAlternativesMarkerCount:@"document.body.childNodes[0]"]);
+}
+
+} // namespace TestWebKitAPI
+
+#endif // PLATFORM(IOS_FAMILY)

Modified: trunk/Tools/TestWebKitAPI/ios/UIKitSPI.h (291135 => 291136)


--- trunk/Tools/TestWebKitAPI/ios/UIKitSPI.h	2022-03-10 23:45:46 UTC (rev 291135)
+++ trunk/Tools/TestWebKitAPI/ios/UIKitSPI.h	2022-03-11 00:15:39 UTC (rev 291136)
@@ -67,6 +67,7 @@
 #else // USE(APPLE_INTERNAL_SDK)
 
 @interface NSTextAlternatives : NSObject
+- (id)initWithPrimaryString:(NSString *)primaryString alternativeStrings:(NSArray<NSString *> *)alternativeStrings;
 @property (readonly) NSString *primaryString;
 @property (readonly) NSArray<NSString *> *alternativeStrings;
 @property (readonly) BOOL isLowConfidence;
@@ -320,6 +321,8 @@
 
 @protocol UITextInputInternal
 - (CGRect)_selectionClipRect;
+@optional
+- (void)addTextAlternatives:(NSTextAlternatives *)alternatives;
 @end
 
 @interface UIResponder (Internal)
_______________________________________________
webkit-changes mailing list
[email protected]
https://lists.webkit.org/mailman/listinfo/webkit-changes

Reply via email to