Title: [244570] trunk/Source/WebKit
Revision
244570
Author
[email protected]
Date
2019-04-23 16:46:46 -0700 (Tue, 23 Apr 2019)

Log Message

Return annotated text checking strings via UIWKDocumentContext
https://bugs.webkit.org/show_bug.cgi?id=197177
<rdar://problem/49064839>

Reviewed by Ryosuke Niwa.

* WebProcess/WebPage/Cocoa/TextCheckingControllerProxy.h:
* WebProcess/WebPage/Cocoa/TextCheckingControllerProxy.mm:
(WebKit::TextCheckingControllerProxy::annotatedSubstringBetweenPositions):
* WebProcess/WebPage/ios/WebPageIOS.mm:
(WebKit::WebPage::requestDocumentEditingContext):
Respect the UIWKDocumentRequestAnnotation flag, returning an attributed
string containing the platform text checking annotations.

Modified Paths

Diff

Modified: trunk/Source/WebKit/ChangeLog (244569 => 244570)


--- trunk/Source/WebKit/ChangeLog	2019-04-23 22:30:06 UTC (rev 244569)
+++ trunk/Source/WebKit/ChangeLog	2019-04-23 23:46:46 UTC (rev 244570)
@@ -1,3 +1,19 @@
+2019-04-23  Tim Horton  <[email protected]>
+
+        Return annotated text checking strings via UIWKDocumentContext
+        https://bugs.webkit.org/show_bug.cgi?id=197177
+        <rdar://problem/49064839>
+
+        Reviewed by Ryosuke Niwa.
+
+        * WebProcess/WebPage/Cocoa/TextCheckingControllerProxy.h:
+        * WebProcess/WebPage/Cocoa/TextCheckingControllerProxy.mm:
+        (WebKit::TextCheckingControllerProxy::annotatedSubstringBetweenPositions):
+        * WebProcess/WebPage/ios/WebPageIOS.mm:
+        (WebKit::WebPage::requestDocumentEditingContext):
+        Respect the UIWKDocumentRequestAnnotation flag, returning an attributed
+        string containing the platform text checking annotations.
+
 2019-04-23  Commit Queue  <[email protected]>
 
         Unreviewed, rolling out r244556.

Modified: trunk/Source/WebKit/WebProcess/WebPage/Cocoa/TextCheckingControllerProxy.h (244569 => 244570)


--- trunk/Source/WebKit/WebProcess/WebPage/Cocoa/TextCheckingControllerProxy.h	2019-04-23 22:30:06 UTC (rev 244569)
+++ trunk/Source/WebKit/WebProcess/WebPage/Cocoa/TextCheckingControllerProxy.h	2019-04-23 23:46:46 UTC (rev 244570)
@@ -38,6 +38,10 @@
 class Encoder;
 }
 
+namespace WebCore {
+class VisiblePosition;
+}
+
 namespace WebKit {
 
 class WebPage;
@@ -47,6 +51,8 @@
     TextCheckingControllerProxy(WebPage&);
     ~TextCheckingControllerProxy();
 
+    AttributedString annotatedSubstringBetweenPositions(const WebCore::VisiblePosition&, const WebCore::VisiblePosition&);
+
 private:
     // IPC::MessageReceiver
     void didReceiveMessage(IPC::Connection&, IPC::Decoder&) override;

Modified: trunk/Source/WebKit/WebProcess/WebPage/Cocoa/TextCheckingControllerProxy.mm (244569 => 244570)


--- trunk/Source/WebKit/WebProcess/WebPage/Cocoa/TextCheckingControllerProxy.mm	2019-04-23 22:30:06 UTC (rev 244569)
+++ trunk/Source/WebKit/WebProcess/WebPage/Cocoa/TextCheckingControllerProxy.mm	2019-04-23 23:46:46 UTC (rev 244570)
@@ -163,6 +163,55 @@
     }, removeCoreSpellingMarkers ? relevantMarkerTypes() : DocumentMarker::PlatformTextChecking);
 }
 
+AttributedString TextCheckingControllerProxy::annotatedSubstringBetweenPositions(const WebCore::VisiblePosition& start, const WebCore::VisiblePosition& end)
+{
+    RetainPtr<NSMutableAttributedString> string = adoptNS([[NSMutableAttributedString alloc] init]);
+    NSUInteger stringLength = 0;
+
+    RefPtr<Document> document = start.deepEquivalent().document();
+    if (!document)
+        return { };
+
+    auto entireRange = makeRange(start, end);
+    if (!entireRange)
+        return { };
+
+    RefPtr<Node> commonAncestor = entireRange->commonAncestorContainer();
+    size_t entireRangeLocation;
+    size_t entireRangeLength;
+    TextIterator::getLocationAndLengthFromRange(commonAncestor.get(), entireRange.get(), entireRangeLocation, entireRangeLength);
+
+    for (TextIterator it(start.deepEquivalent(), end.deepEquivalent()); !it.atEnd(); it.advance()) {
+        int currentTextLength = it.text().length();
+        if (!currentTextLength)
+            continue;
+
+        [string appendAttributedString:[[[NSAttributedString alloc] initWithString:it.text().createNSStringWithoutCopying().get()] autorelease]];
+
+        RefPtr<Range> currentTextRange = it.range();
+        auto markers = document->markers().markersInRange(*currentTextRange, DocumentMarker::PlatformTextChecking);
+        for (const auto* marker : markers) {
+            if (!WTF::holds_alternative<DocumentMarker::PlatformTextCheckingData>(marker->data()))
+                continue;
+
+            auto& textCheckingData = WTF::get<DocumentMarker::PlatformTextCheckingData>(marker->data());
+            auto subrange = TextIterator::subrange(*currentTextRange, marker->startOffset(), marker->endOffset() - marker->startOffset());
+
+            size_t subrangeLocation;
+            size_t subrangeLength;
+            TextIterator::getLocationAndLengthFromRange(commonAncestor.get(), &subrange.get(), subrangeLocation, subrangeLength);
+
+            ASSERT(subrangeLocation > entireRangeLocation);
+            ASSERT(subrangeLocation + subrangeLength < entireRangeLength);
+            [string addAttribute:textCheckingData.key value:textCheckingData.value range:NSMakeRange(subrangeLocation - entireRangeLocation, subrangeLength)];
+        }
+
+        stringLength += currentTextLength;
+    }
+
+    return string.autorelease();
+}
+
 } // namespace WebKit
 
 #endif // ENABLE(PLATFORM_DRIVEN_TEXT_CHECKING)

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


--- trunk/Source/WebKit/WebProcess/WebPage/ios/WebPageIOS.mm	2019-04-23 22:30:06 UTC (rev 244569)
+++ trunk/Source/WebKit/WebProcess/WebPage/ios/WebPageIOS.mm	2019-04-23 23:46:46 UTC (rev 244570)
@@ -42,6 +42,7 @@
 #import "PrintInfo.h"
 #import "RemoteLayerTreeDrawingArea.h"
 #import "SandboxUtilities.h"
+#import "TextCheckingControllerProxy.h"
 #import "UIKitSPI.h"
 #import "UserData.h"
 #import "ViewGestureGeometryCollector.h"
@@ -3573,7 +3574,10 @@
         }
     }
 
-    // FIXME: Support Annotation option.
+#if ENABLE(PLATFORM_DRIVEN_TEXT_CHECKING)
+    if (request.options.contains(DocumentEditingContextRequest::Options::Annotation))
+        context.annotatedText = m_textCheckingControllerProxy->annotatedSubstringBetweenPositions(contextBeforeStart, contextAfterEnd);
+#endif
 
     completionHandler(context);
 }
_______________________________________________
webkit-changes mailing list
[email protected]
https://lists.webkit.org/mailman/listinfo/webkit-changes

Reply via email to