Title: [253602] trunk
Revision
253602
Author
[email protected]
Date
2019-12-16 17:16:44 -0800 (Mon, 16 Dec 2019)

Log Message

-[UIWKDocumentContext markedTextRange] is wrong when the caret is not at the start of marked text
https://bugs.webkit.org/show_bug.cgi?id=205302

Reviewed by Tim Horton.

Source/WebKit:

Makes a few minor adjustments around marked text handling in document editing context request code.

1. In the case where markedTextRects are requested, automatically expand the context range (i.e. contextBefore
and contextAfter) to encompass the marked text. This fixes UIWKDocumentContext's logic that computes the
marked text range by subtracting `contextBefore` string's length from `selectedRangeInMarkedText`'s location.

(Note that this still requires an adjustment in UIKit to actually respect `selectedRangeInMarkedText` when
computing `markedTextRange`. This is tracked in <rdar://problem/57338528>).

2. Stop clamping compositionStart and compositionEnd to the range of interest (in this case, the selection
range, which is a collapsed caret selection). This makes the composition range seem as if it were empty, which
prevents us from computing the marked text string (and importantly, its length).

3. Flip the arguments to `distanceBetweenPositions`, such that we end up with a positive value for
`selectedRangeInMarkedText` in the case where `compositionStart` is before `startOfRangeOfInterestInSelection`.

Test: DocumentEditingContext.RequestMarkedTextRectsAndTextOnly

* WebProcess/WebPage/ios/WebPageIOS.mm:
(WebKit::WebPage::requestDocumentEditingContext):

Tools:

Add an API test to exercise the case where the options `UIWKDocumentRequestMarkedTextRects` and
`UIWKDocumentRequestText` are used to grab marked text rects.

* TestWebKitAPI/Tests/WebKitCocoa/DocumentEditingContext.mm:
(-[UIWKDocumentContext contextBeforeLength]):
(-[UIWKDocumentContext markedTextLength]):
(-[UIWKDocumentContext markedTextRange]):

Work around a bug that should be fixed by <rdar://problem/57338528>, so that the new API test can pass on
shipping builds of iOS.

Modified Paths

Diff

Modified: trunk/Source/WebKit/ChangeLog (253601 => 253602)


--- trunk/Source/WebKit/ChangeLog	2019-12-17 01:12:22 UTC (rev 253601)
+++ trunk/Source/WebKit/ChangeLog	2019-12-17 01:16:44 UTC (rev 253602)
@@ -1,3 +1,31 @@
+2019-12-16  Wenson Hsieh  <[email protected]>
+
+        -[UIWKDocumentContext markedTextRange] is wrong when the caret is not at the start of marked text
+        https://bugs.webkit.org/show_bug.cgi?id=205302
+
+        Reviewed by Tim Horton.
+
+        Makes a few minor adjustments around marked text handling in document editing context request code.
+
+        1. In the case where markedTextRects are requested, automatically expand the context range (i.e. contextBefore
+        and contextAfter) to encompass the marked text. This fixes UIWKDocumentContext's logic that computes the
+        marked text range by subtracting `contextBefore` string's length from `selectedRangeInMarkedText`'s location.
+
+        (Note that this still requires an adjustment in UIKit to actually respect `selectedRangeInMarkedText` when
+        computing `markedTextRange`. This is tracked in <rdar://problem/57338528>).
+
+        2. Stop clamping compositionStart and compositionEnd to the range of interest (in this case, the selection
+        range, which is a collapsed caret selection). This makes the composition range seem as if it were empty, which
+        prevents us from computing the marked text string (and importantly, its length).
+
+        3. Flip the arguments to `distanceBetweenPositions`, such that we end up with a positive value for
+        `selectedRangeInMarkedText` in the case where `compositionStart` is before `startOfRangeOfInterestInSelection`.
+
+        Test: DocumentEditingContext.RequestMarkedTextRectsAndTextOnly
+
+        * WebProcess/WebPage/ios/WebPageIOS.mm:
+        (WebKit::WebPage::requestDocumentEditingContext):
+
 2019-12-16  Per Arne Vollan  <[email protected]>
 
         [iOS] Issue mach lookup extension to diagnostics daemon

Modified: trunk/Source/WebKit/WebProcess/WebPage/ios/WebPageIOS.mm (253601 => 253602)


--- trunk/Source/WebKit/WebProcess/WebPage/ios/WebPageIOS.mm	2019-12-17 01:12:22 UTC (rev 253601)
+++ trunk/Source/WebKit/WebProcess/WebPage/ios/WebPageIOS.mm	2019-12-17 01:16:44 UTC (rev 253602)
@@ -3999,6 +3999,7 @@
 
     VisiblePosition contextBeforeStart;
     VisiblePosition contextAfterEnd;
+    auto compositionRange = frame->editor().compositionRange();
     if (request.granularityCount) {
         contextBeforeStart = moveByGranularityRespectingWordBoundary(rangeOfInterestStart, request.surroundingGranularity, request.granularityCount, DirectionBackward);
         contextAfterEnd = moveByGranularityRespectingWordBoundary(rangeOfInterestEnd, request.surroundingGranularity, request.granularityCount, DirectionForward);
@@ -4005,6 +4006,14 @@
     } else {
         contextBeforeStart = rangeOfInterestStart;
         contextAfterEnd = rangeOfInterestEnd;
+        if (wantsMarkedTextRects && compositionRange) {
+            // In the case where the client has requested marked text rects, additionally make sure that the
+            // context range encompasses the entire marked text range.
+            auto compositionStart = compositionRange->startPosition();
+            auto compositionEnd = compositionRange->endPosition();
+            contextBeforeStart = contextBeforeStart > compositionStart ? compositionStart : contextBeforeStart;
+            contextAfterEnd = contextAfterEnd < compositionEnd ? compositionEnd : contextAfterEnd;
+        }
     }
 
     auto makeString = [&](VisiblePosition& start, VisiblePosition& end) -> NSAttributedString * {
@@ -4017,17 +4026,11 @@
     context.contextBefore = makeString(contextBeforeStart, startOfRangeOfInterestInSelection);
     context.selectedText = makeString(startOfRangeOfInterestInSelection, endOfRangeOfInterestInSelection);
     context.contextAfter = makeString(endOfRangeOfInterestInSelection, contextAfterEnd);
-
-    auto compositionRange = frame->editor().compositionRange();
     if (compositionRange && rangesOverlap(rangeOfInterest.get(), compositionRange.get())) {
         VisiblePosition compositionStart(compositionRange->startPosition());
         VisiblePosition compositionEnd(compositionRange->endPosition());
-
-        VisiblePosition relevantCompositionStart = rangeOfInterestStart > compositionStart ? rangeOfInterestStart : compositionStart;
-        VisiblePosition relevantCompositionEnd = rangeOfInterestEnd < compositionEnd ? rangeOfInterestEnd : compositionEnd;
-
-        context.markedText = makeString(relevantCompositionStart, relevantCompositionEnd);
-        context.selectedRangeInMarkedText.location = distanceBetweenPositions(relevantCompositionStart, startOfRangeOfInterestInSelection);
+        context.markedText = makeString(compositionStart, compositionEnd);
+        context.selectedRangeInMarkedText.location = distanceBetweenPositions(startOfRangeOfInterestInSelection, compositionStart);
         context.selectedRangeInMarkedText.length = [context.selectedText.string length];
     }
 

Modified: trunk/Tools/ChangeLog (253601 => 253602)


--- trunk/Tools/ChangeLog	2019-12-17 01:12:22 UTC (rev 253601)
+++ trunk/Tools/ChangeLog	2019-12-17 01:16:44 UTC (rev 253602)
@@ -1,3 +1,21 @@
+2019-12-16  Wenson Hsieh  <[email protected]>
+
+        -[UIWKDocumentContext markedTextRange] is wrong when the caret is not at the start of marked text
+        https://bugs.webkit.org/show_bug.cgi?id=205302
+
+        Reviewed by Tim Horton.
+
+        Add an API test to exercise the case where the options `UIWKDocumentRequestMarkedTextRects` and
+        `UIWKDocumentRequestText` are used to grab marked text rects.
+
+        * TestWebKitAPI/Tests/WebKitCocoa/DocumentEditingContext.mm:
+        (-[UIWKDocumentContext contextBeforeLength]):
+        (-[UIWKDocumentContext markedTextLength]):
+        (-[UIWKDocumentContext markedTextRange]):
+
+        Work around a bug that should be fixed by <rdar://problem/57338528>, so that the new API test can pass on
+        shipping builds of iOS.
+
 2019-12-16  Keith Rollin  <[email protected]>
 
         Unreviewed follow-up fix.

Modified: trunk/Tools/TestWebKitAPI/Tests/WebKitCocoa/DocumentEditingContext.mm (253601 => 253602)


--- trunk/Tools/TestWebKitAPI/Tests/WebKitCocoa/DocumentEditingContext.mm	2019-12-17 01:12:22 UTC (rev 253601)
+++ trunk/Tools/TestWebKitAPI/Tests/WebKitCocoa/DocumentEditingContext.mm	2019-12-17 01:16:44 UTC (rev 253602)
@@ -81,6 +81,30 @@
     return rects;
 }
 
+// FIXME: These workarounds can be removed once <rdar://problem/57338528> is fixed.
+- (NSUInteger)contextBeforeLength
+{
+    if ([self.contextBefore isKindOfClass:NSString.class])
+        return [(NSString *)self.contextBefore length];
+    if ([self.contextBefore isKindOfClass:NSAttributedString.class])
+        return [(NSAttributedString *)self.contextBefore length];
+    return 0;
+}
+
+- (NSUInteger)markedTextLength
+{
+    if ([self.markedText isKindOfClass:NSString.class])
+        return [(NSString *)self.markedText length];
+    if ([self.markedText isKindOfClass:NSAttributedString.class])
+        return [(NSAttributedString *)self.markedText length];
+    return 0;
+}
+
+- (NSRange)markedTextRange
+{
+    return NSMakeRange(self.contextBeforeLength - self.selectedRangeInMarkedText.location, self.markedTextLength);
+}
+
 - (NSArray<NSValue *> *)textRects
 {
     Vector<std::pair<NSRange, CGRect>> rangesAndRects;
@@ -370,6 +394,30 @@
     }
 }
 
+TEST(DocumentEditingContext, RequestMarkedTextRectsAndTextOnly)
+{
+    auto webView = adoptNS([[TestWKWebView alloc] initWithFrame:NSMakeRect(0, 0, 800, 600)]);
+    [webView synchronouslyLoadHTMLString:applyAhemStyle(@"<input />")];
+    [webView stringByEvaluatingJavaScript:@"document.querySelector('input').focus()"];
+    [webView _synchronouslyExecuteEditCommand:@"InsertText" argument:@"Hello world"];
+
+    auto *contentView = [webView textInputContentView];
+    [contentView selectWordBackward];
+    [contentView setMarkedText:@"world" selectedRange:NSMakeRange(0, 5)];
+    [webView collapseToEnd];
+
+    auto request = adoptNS([[UIWKDocumentRequest alloc] init]);
+    [request setFlags:UIWKDocumentRequestMarkedTextRects | UIWKDocumentRequestText];
+
+    auto context = retainPtr([webView synchronouslyRequestDocumentContext:request.get()]);
+    auto *rectValues = [context markedTextRects];
+    EXPECT_EQ(CGRectMake(165, 8, 26, 25), [rectValues[0] CGRectValue]);
+    EXPECT_EQ(CGRectMake(190, 8, 26, 25), [rectValues[1] CGRectValue]);
+    EXPECT_EQ(CGRectMake(215, 8, 26, 25), [rectValues[2] CGRectValue]);
+    EXPECT_EQ(CGRectMake(240, 8, 26, 25), [rectValues[3] CGRectValue]);
+    EXPECT_EQ(CGRectMake(265, 8, 26, 25), [rectValues[4] CGRectValue]);
+}
+
 TEST(DocumentEditingContext, SpatialRequestInTextField)
 {
     auto webView = adoptNS([[TestWKWebView alloc] initWithFrame:NSMakeRect(0, 0, 800, 600)]);
_______________________________________________
webkit-changes mailing list
[email protected]
https://lists.webkit.org/mailman/listinfo/webkit-changes

Reply via email to