Title: [276441] trunk
Revision
276441
Author
[email protected]
Date
2021-04-22 09:38:08 -0700 (Thu, 22 Apr 2021)

Log Message

[iOS] The Look Up text service popover should avoid covering selected text
https://bugs.webkit.org/show_bug.cgi?id=224915
rdar://75891746

Reviewed by Megan Gardner.

Source/WebKit:

Adjust the presentation rect when showing the Look Up text service popup using the callout bar on iOS so that
we use the entire bounds of the selection. Currently, we only use the first selection rect (top to bottom) as
the presentation rect, which may cause the popup to obscure selected text.

This seems to have been derived from iOS legacy web view behavior that dating back to rdar://7475904; while the
exact reasoning for this is effectively lost to time, it's likely due to how there used to only be a single
block selection rect in non-editable content in iOS 5 anyways, so taking only the first selection rect would not
be incorrect. However, we now use character granularity selection everywhere in WebKit2, so this logic is no
longer sensible.

Test: TextServicesTests.LookUpPresentationRect

* UIProcess/ios/WKContentViewInteraction.mm:
(-[WKContentView _lookupForWebView:]):
(-[WKContentView _translateForWebView:]):

Make a similar adjustment here, since this presentation rect is intended to match that of the Look Up popover.

Tools:

Add a new API test to exercise the change.

* TestWebKitAPI/TestWebKitAPI.xcodeproj/project.pbxproj:
* TestWebKitAPI/Tests/ios/TextServicesTests.mm: Added.
(TestWebKitAPI::handleLookup):
(TestWebKitAPI::TEST):
* TestWebKitAPI/ios/UIKitSPI.h: Add SPI declarations for UIWKTextInteractionAssistant.

Modified Paths

Added Paths

Diff

Modified: trunk/Source/WebKit/ChangeLog (276440 => 276441)


--- trunk/Source/WebKit/ChangeLog	2021-04-22 16:35:13 UTC (rev 276440)
+++ trunk/Source/WebKit/ChangeLog	2021-04-22 16:38:08 UTC (rev 276441)
@@ -1,3 +1,29 @@
+2021-04-21  Wenson Hsieh  <[email protected]>
+
+        [iOS] The Look Up text service popover should avoid covering selected text
+        https://bugs.webkit.org/show_bug.cgi?id=224915
+        rdar://75891746
+
+        Reviewed by Megan Gardner.
+
+        Adjust the presentation rect when showing the Look Up text service popup using the callout bar on iOS so that
+        we use the entire bounds of the selection. Currently, we only use the first selection rect (top to bottom) as
+        the presentation rect, which may cause the popup to obscure selected text.
+
+        This seems to have been derived from iOS legacy web view behavior that dating back to rdar://7475904; while the
+        exact reasoning for this is effectively lost to time, it's likely due to how there used to only be a single
+        block selection rect in non-editable content in iOS 5 anyways, so taking only the first selection rect would not
+        be incorrect. However, we now use character granularity selection everywhere in WebKit2, so this logic is no
+        longer sensible.
+
+        Test: TextServicesTests.LookUpPresentationRect
+
+        * UIProcess/ios/WKContentViewInteraction.mm:
+        (-[WKContentView _lookupForWebView:]):
+        (-[WKContentView _translateForWebView:]):
+
+        Make a similar adjustment here, since this presentation rect is intended to match that of the Look Up popover.
+
 2021-04-22  Brent Fulgham  <[email protected]>
 
         [macOS] Re-block access to SYS_setattrlist

Modified: trunk/Source/WebKit/UIProcess/ios/WKContentViewInteraction.mm (276440 => 276441)


--- trunk/Source/WebKit/UIProcess/ios/WKContentViewInteraction.mm	2021-04-22 16:35:13 UTC (rev 276440)
+++ trunk/Source/WebKit/UIProcess/ios/WKContentViewInteraction.mm	2021-04-22 16:38:08 UTC (rev 276441)
@@ -3284,7 +3284,7 @@
         auto& postLayoutData = editorState.postLayoutData();
         CGRect presentationRect;
         if (editorState.selectionIsRange && !postLayoutData.selectionGeometries.isEmpty())
-            presentationRect = postLayoutData.selectionGeometries[0].rect();
+            presentationRect = view->_page->selectionBoundingRectInRootViewCoordinates();
         else
             presentationRect = postLayoutData.caretRectAtStart;
         
@@ -3324,12 +3324,11 @@
         if (strongSelf->_page->editorState().isMissingPostLayoutData)
             return;
 
-        auto& selectionGeometries = strongSelf->_page->editorState().postLayoutData().selectionGeometries;
-        if (selectionGeometries.isEmpty())
+        if (strongSelf->_page->editorState().postLayoutData().selectionGeometries.isEmpty())
             return;
 
         if ([strongSelf->_textInteractionAssistant respondsToSelector:@selector(translate:fromRect:)])
-            [strongSelf->_textInteractionAssistant translate:string fromRect:selectionGeometries.first().rect()];
+            [strongSelf->_textInteractionAssistant translate:string fromRect:strongSelf->_page->selectionBoundingRectInRootViewCoordinates()];
     });
 }
 

Modified: trunk/Tools/ChangeLog (276440 => 276441)


--- trunk/Tools/ChangeLog	2021-04-22 16:35:13 UTC (rev 276440)
+++ trunk/Tools/ChangeLog	2021-04-22 16:38:08 UTC (rev 276441)
@@ -1,3 +1,19 @@
+2021-04-21  Wenson Hsieh  <[email protected]>
+
+        [iOS] The Look Up text service popover should avoid covering selected text
+        https://bugs.webkit.org/show_bug.cgi?id=224915
+        rdar://75891746
+
+        Reviewed by Megan Gardner.
+
+        Add a new API test to exercise the change.
+
+        * TestWebKitAPI/TestWebKitAPI.xcodeproj/project.pbxproj:
+        * TestWebKitAPI/Tests/ios/TextServicesTests.mm: Added.
+        (TestWebKitAPI::handleLookup):
+        (TestWebKitAPI::TEST):
+        * TestWebKitAPI/ios/UIKitSPI.h: Add SPI declarations for UIWKTextInteractionAssistant.
+
 2021-04-22  Aakash Jain  <[email protected]>
 
         Services EWS should run for webkitscmpy and webkitcorepy changes

Modified: trunk/Tools/TestWebKitAPI/TestWebKitAPI.xcodeproj/project.pbxproj (276440 => 276441)


--- trunk/Tools/TestWebKitAPI/TestWebKitAPI.xcodeproj/project.pbxproj	2021-04-22 16:35:13 UTC (rev 276440)
+++ trunk/Tools/TestWebKitAPI/TestWebKitAPI.xcodeproj/project.pbxproj	2021-04-22 16:38:08 UTC (rev 276441)
@@ -1212,6 +1212,7 @@
 		F4856CA31E649EA8009D7EE7 /* attachment-element.html in Copy Resources */ = {isa = PBXBuildFile; fileRef = F4856CA21E6498A8009D7EE7 /* attachment-element.html */; };
 		F486B1D01F67952300F34BDD /* DataTransfer-setDragImage.html in Copy Resources */ = {isa = PBXBuildFile; fileRef = F486B1CF1F6794FF00F34BDD /* DataTransfer-setDragImage.html */; };
 		F48D6C10224B377000E3E2FB /* PreferredContentMode.mm in Sources */ = {isa = PBXBuildFile; fileRef = F48D6C0F224B377000E3E2FB /* PreferredContentMode.mm */; };
+		F494B36A263120780060A310 /* TextServicesTests.mm in Sources */ = {isa = PBXBuildFile; fileRef = F494B369263120780060A310 /* TextServicesTests.mm */; };
 		F49992C6248DABFD00034167 /* overflow-hidden.html in Copy Resources */ = {isa = PBXBuildFile; fileRef = F49992C5248DABE400034167 /* overflow-hidden.html */; };
 		F4A32EC41F05F3850047C544 /* dragstart-change-selection-offscreen.html in Copy Resources */ = {isa = PBXBuildFile; fileRef = F4A32EC31F05F3780047C544 /* dragstart-change-selection-offscreen.html */; };
 		F4A32ECB1F0643370047C544 /* contenteditable-in-iframe.html in Copy Resources */ = {isa = PBXBuildFile; fileRef = F4A32ECA1F0642F40047C544 /* contenteditable-in-iframe.html */; };
@@ -3034,6 +3035,7 @@
 		F486B1CF1F6794FF00F34BDD /* DataTransfer-setDragImage.html */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = text.html; path = "DataTransfer-setDragImage.html"; sourceTree = "<group>"; };
 		F48D6C0F224B377000E3E2FB /* PreferredContentMode.mm */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.cpp.objcpp; path = PreferredContentMode.mm; sourceTree = "<group>"; };
 		F493247C1F44DF8D006F4336 /* UIKitSPI.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = UIKitSPI.h; sourceTree = "<group>"; };
+		F494B369263120780060A310 /* TextServicesTests.mm */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.cpp.objcpp; path = TextServicesTests.mm; sourceTree = "<group>"; };
 		F49992C5248DABE400034167 /* overflow-hidden.html */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = text.html; path = "overflow-hidden.html"; sourceTree = "<group>"; };
 		F4A32EC31F05F3780047C544 /* dragstart-change-selection-offscreen.html */ = {isa = PBXFileReference; lastKnownFileType = text.html; path = "dragstart-change-selection-offscreen.html"; sourceTree = "<group>"; };
 		F4A32ECA1F0642F40047C544 /* contenteditable-in-iframe.html */ = {isa = PBXFileReference; lastKnownFileType = text.html; path = "contenteditable-in-iframe.html"; sourceTree = "<group>"; };
@@ -3793,6 +3795,7 @@
 				F45E15742112CE6200307E82 /* TestInputDelegate.h */,
 				F45E15752112CE6200307E82 /* TestInputDelegate.mm */,
 				F45033F4206BEC95009351CE /* TextAutosizingBoost.mm */,
+				F494B369263120780060A310 /* TextServicesTests.mm */,
 				1C24DEEC263001DE00450D07 /* TextStyleFontSize.mm */,
 				F460F6742614DE2F0064F2B6 /* UIFocusTests.mm */,
 				F46849BD1EEF58E400B937FE /* UIPasteboardTests.mm */,
@@ -5704,6 +5707,7 @@
 				93E6193B1F931B3A00AF245E /* TextCodec.cpp in Sources */,
 				CE3524F91B1441C40028A7C5 /* TextFieldDidBeginAndEndEditing.cpp in Sources */,
 				9B02E0D6235FA47D004044B2 /* TextManipulation.mm in Sources */,
+				F494B36A263120780060A310 /* TextServicesTests.mm in Sources */,
 				5C16F8FC230C94370074C4A8 /* TextSize.mm in Sources */,
 				1C24DEED263001DE00450D07 /* TextStyleFontSize.mm in Sources */,
 				C22FA32B228F8708009D7988 /* TextWidth.mm in Sources */,

Added: trunk/Tools/TestWebKitAPI/Tests/ios/TextServicesTests.mm (0 => 276441)


--- trunk/Tools/TestWebKitAPI/Tests/ios/TextServicesTests.mm	                        (rev 0)
+++ trunk/Tools/TestWebKitAPI/Tests/ios/TextServicesTests.mm	2021-04-22 16:38:08 UTC (rev 276441)
@@ -0,0 +1,87 @@
+/*
+ * Copyright (C) 2021 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 "InstanceMethodSwizzler.h"
+#import "PlatformUtilities.h"
+#import "TestWKWebView.h"
+#import "UIKitSPI.h"
+#import <wtf/RetainPtr.h>
+
+@interface WKWebView (TextServices)
+- (void)_lookup:(id)sender;
+@end
+
+namespace TestWebKitAPI {
+
+static bool doneWaitingForLookup = false;
+static NSRange lastLookupRange = NSMakeRange(0, 0);
+static CGRect lastLookupPresentationRect = CGRectNull;
+
+static void handleLookup(id, SEL, NSString *, NSRange range, CGRect presentationRect)
+{
+    lastLookupRange = range;
+    lastLookupPresentationRect = presentationRect;
+    doneWaitingForLookup = true;
+}
+
+TEST(TextServicesTests, LookUpPresentationRect)
+{
+    InstanceMethodSwizzler swizzler { UIWKTextInteractionAssistant.class, @selector(lookup:withRange:fromRect:), reinterpret_cast<IMP>(handleLookup) };
+
+    auto webView = adoptNS([[TestWKWebView alloc] initWithFrame:CGRectMake(0, 0, 320, 500)]);
+    [webView synchronouslyLoadHTMLString:@"<meta name='viewport' content='width=device-width'><style>p { font-size: 20px; }</style><p>Foo</p><p>Hello World</p>"];
+    [webView stringByEvaluatingJavaScript:@"getSelection().selectAllChildren(document.querySelector('p'))"];
+    [webView waitForNextPresentationUpdate];
+
+    doneWaitingForLookup = false;
+    [webView _lookup:nil];
+    TestWebKitAPI::Util::run(&doneWaitingForLookup);
+
+    EXPECT_EQ(lastLookupRange.location, 0U);
+    EXPECT_EQ(lastLookupRange.length, 3U);
+    auto rectWhenLookingUpFirstLine = lastLookupPresentationRect;
+
+    [[webView textInputContentView] selectAll:nil];
+    [webView waitForNextPresentationUpdate];
+
+    doneWaitingForLookup = false;
+    [webView _lookup:nil];
+    TestWebKitAPI::Util::run(&doneWaitingForLookup);
+
+    EXPECT_EQ(lastLookupRange.location, 0U);
+    EXPECT_EQ(lastLookupRange.length, 16U);
+    auto rectWhenLookingUpBothLines = lastLookupPresentationRect;
+
+    EXPECT_GT(rectWhenLookingUpBothLines.size.width, rectWhenLookingUpFirstLine.size.width);
+    EXPECT_GT(rectWhenLookingUpBothLines.size.height, rectWhenLookingUpFirstLine.size.height);
+}
+
+} // namespace TestWebKitAPI
+
+#endif // PLATFORM(IOS_FAMILY)

Modified: trunk/Tools/TestWebKitAPI/ios/UIKitSPI.h (276440 => 276441)


--- trunk/Tools/TestWebKitAPI/ios/UIKitSPI.h	2021-04-22 16:35:13 UTC (rev 276440)
+++ trunk/Tools/TestWebKitAPI/ios/UIKitSPI.h	2021-04-22 16:38:08 UTC (rev 276441)
@@ -249,6 +249,13 @@
 - (void)_scrollView:(UIScrollView *)scrollView asynchronouslyHandleScrollEvent:(UIScrollEvent *)scrollEvent completion:(void (^)(BOOL handled))completion;
 @end
 
+@interface UITextInteractionAssistant : NSObject <UIResponderStandardEditActions>
+@end
+
+@interface UIWKTextInteractionAssistant : UITextInteractionAssistant
+- (void)lookup:(NSString *)textWithContext withRange:(NSRange)range fromRect:(CGRect)presentationRect;
+@end
+
 #endif // USE(APPLE_INTERNAL_SDK)
 
 #define UIWKDocumentRequestMarkedTextRects (1 << 5)
_______________________________________________
webkit-changes mailing list
[email protected]
https://lists.webkit.org/mailman/listinfo/webkit-changes

Reply via email to