Make UIWKDocumentContext rects per-character instead of per-word
authortimothy_horton@apple.com <timothy_horton@apple.com@268f45cc-cd09-0410-ab3c-d52691b4dbfc>
Mon, 1 Apr 2019 21:08:31 +0000 (21:08 +0000)
committertimothy_horton@apple.com <timothy_horton@apple.com@268f45cc-cd09-0410-ab3c-d52691b4dbfc>
Mon, 1 Apr 2019 21:08:31 +0000 (21:08 +0000)
https://bugs.webkit.org/show_bug.cgi?id=196459

Reviewed by Wenson Hsieh.

Source/WebCore:

No new tests; adjusted expected results of WebKit.DocumentEditingContext.

* editing/TextIterator.cpp:
(WebCore::CharacterIterator::CharacterIterator):
* editing/TextIterator.h:
(WebCore::CharacterIterator::atEnd const):
(WebCore::CharacterIterator::text const):
Add WEBCORE_EXPORT to some things.
Introduce a CharacterIterator constructor that takes Positions, like one that TextIterator has.
Move initializers to the header.

Source/WebKit:

* WebProcess/WebPage/ios/WebPageIOS.mm:
(WebKit::WebPage::requestDocumentEditingContext):
Switch to CharacterIterator instead of TextIterator directly, to get
per-character rects as the API requests.

Tools:

* TestWebKitAPI/Tests/WebKitCocoa/DocumentEditingContext.mm:
(TEST):
Adjust test results due to switching to per-character rects.

git-svn-id: https://svn.webkit.org/repository/webkit/trunk@243712 268f45cc-cd09-0410-ab3c-d52691b4dbfc

Source/WebCore/ChangeLog
Source/WebCore/editing/TextIterator.cpp
Source/WebCore/editing/TextIterator.h
Source/WebKit/ChangeLog
Source/WebKit/WebProcess/WebPage/ios/WebPageIOS.mm
Tools/ChangeLog
Tools/TestWebKitAPI/Tests/WebKitCocoa/DocumentEditingContext.mm

index 42488f7..d77c42c 100644 (file)
@@ -1,3 +1,21 @@
+2019-04-01  Tim Horton  <timothy_horton@apple.com>
+
+        Make UIWKDocumentContext rects per-character instead of per-word
+        https://bugs.webkit.org/show_bug.cgi?id=196459
+
+        Reviewed by Wenson Hsieh.
+
+        No new tests; adjusted expected results of WebKit.DocumentEditingContext.
+
+        * editing/TextIterator.cpp:
+        (WebCore::CharacterIterator::CharacterIterator):
+        * editing/TextIterator.h:
+        (WebCore::CharacterIterator::atEnd const):
+        (WebCore::CharacterIterator::text const):
+        Add WEBCORE_EXPORT to some things.
+        Introduce a CharacterIterator constructor that takes Positions, like one that TextIterator has.
+        Move initializers to the header.
+
 2019-04-01  Antti Koivisto  <antti@apple.com>
 
         Update event region when toggling pointer-events:none
index d58abad..7e7a1f4 100644 (file)
@@ -1528,9 +1528,13 @@ Ref<Range> SimplifiedBackwardsTextIterator::range() const
 
 CharacterIterator::CharacterIterator(const Range& range, TextIteratorBehavior behavior)
     : m_underlyingIterator(&range, behavior)
-    , m_offset(0)
-    , m_runOffset(0)
-    , m_atBreak(true)
+{
+    while (!atEnd() && !m_underlyingIterator.text().length())
+        m_underlyingIterator.advance();
+}
+
+CharacterIterator::CharacterIterator(Position start, Position end, TextIteratorBehavior behavior)
+    : m_underlyingIterator(start, end, behavior)
 {
     while (!atEnd() && !m_underlyingIterator.text().length())
         m_underlyingIterator.advance();
index e4a5e10..5917a30 100644 (file)
@@ -255,12 +255,13 @@ private:
 class CharacterIterator {
 public:
     explicit CharacterIterator(const Range&, TextIteratorBehavior = TextIteratorDefaultBehavior);
+    WEBCORE_EXPORT explicit CharacterIterator(Position start, Position end, TextIteratorBehavior = TextIteratorDefaultBehavior);
     
     bool atEnd() const { return m_underlyingIterator.atEnd(); }
-    void advance(int numCharacters);
+    WEBCORE_EXPORT void advance(int numCharacters);
     
     StringView text() const { return m_underlyingIterator.text().substring(m_runOffset); }
-    Ref<Range> range() const;
+    WEBCORE_EXPORT Ref<Range> range() const;
 
     bool atBreak() const { return m_atBreak; }
     int characterOffset() const { return m_offset; }
@@ -268,9 +269,9 @@ public:
 private:
     TextIterator m_underlyingIterator;
 
-    int m_offset;
-    int m_runOffset;
-    bool m_atBreak;
+    int m_offset { 0 };
+    int m_runOffset { 0 };
+    bool m_atBreak { true };
 };
     
 class BackwardsCharacterIterator {
index 34e81cd..b910fa4 100644 (file)
@@ -1,3 +1,15 @@
+2019-04-01  Tim Horton  <timothy_horton@apple.com>
+
+        Make UIWKDocumentContext rects per-character instead of per-word
+        https://bugs.webkit.org/show_bug.cgi?id=196459
+
+        Reviewed by Wenson Hsieh.
+
+        * WebProcess/WebPage/ios/WebPageIOS.mm:
+        (WebKit::WebPage::requestDocumentEditingContext):
+        Switch to CharacterIterator instead of TextIterator directly, to get
+        per-character rects as the API requests.
+
 2019-04-01  Chris Dumez  <cdumez@apple.com>
 
         UIProcess crash when a prewarmed process is terminated
index a96d2e7..26122a8 100644 (file)
@@ -3518,22 +3518,22 @@ void WebPage::requestDocumentEditingContext(DocumentEditingContextRequest reques
     }
 
     if (wantsRects) {
-        TextIterator contextIterator(contextBeforeStart.deepEquivalent(), contextAfterEnd.deepEquivalent());
+        CharacterIterator contextIterator(contextBeforeStart.deepEquivalent(), contextAfterEnd.deepEquivalent());
         unsigned currentLocation = 0;
         while (!contextIterator.atEnd()) {
             unsigned length = contextIterator.text().length();
             if (!length) {
-                contextIterator.advance();
+                contextIterator.advance(1);
                 continue;
             }
 
             DocumentEditingContext::TextRectAndRange rect;
             rect.rect = contextIterator.range()->absoluteBoundingBox();
-            rect.range = { currentLocation, length };
+            rect.range = { currentLocation, 1 };
             context.textRects.append(rect);
 
-            currentLocation += length;
-            contextIterator.advance();
+            currentLocation++;
+            contextIterator.advance(1);
         }
     }
 
index cb9ecc2..0220864 100644 (file)
@@ -1,3 +1,14 @@
+2019-04-01  Tim Horton  <timothy_horton@apple.com>
+
+        Make UIWKDocumentContext rects per-character instead of per-word
+        https://bugs.webkit.org/show_bug.cgi?id=196459
+
+        Reviewed by Wenson Hsieh.
+
+        * TestWebKitAPI/Tests/WebKitCocoa/DocumentEditingContext.mm:
+        (TEST):
+        Adjust test results due to switching to per-character rects.
+
 2019-04-01  Chris Dumez  <cdumez@apple.com>
 
         UIProcess crash when a prewarmed process is terminated
index 4ba37f6..271ce8c 100644 (file)
@@ -206,9 +206,14 @@ TEST(WebKit, DocumentEditingContext)
     EXPECT_NSSTRING_EQ("MMMM", context.selectedText);
     EXPECT_NULL(context.contextAfter);
 
-    NSArray<NSValue *> *rects = [context characterRectsForCharacterRange:NSMakeRange(0, 1)];
-    EXPECT_EQ(1UL, rects.count);
-    EXPECT_RECT_EQ(0, 0, 92, 24, rects.firstObject.CGRectValue);
+    NSArray<NSValue *> *rects = [[context characterRectsForCharacterRange:NSMakeRange(0, 4)] sortedArrayUsingComparator:^(NSValue *a, NSValue *b) {
+        return [@(a.CGRectValue.origin.x) compare:@(b.CGRectValue.origin.x)];
+    }];
+    EXPECT_EQ(4UL, rects.count);
+    EXPECT_RECT_EQ(0, 0, 23, 24, rects[0].CGRectValue);
+    EXPECT_RECT_EQ(23, 0, 23, 24, rects[1].CGRectValue);
+    EXPECT_RECT_EQ(46, 0, 23, 24, rects[2].CGRectValue);
+    EXPECT_RECT_EQ(69, 0, 23, 24, rects[3].CGRectValue);
     rects = [context characterRectsForCharacterRange:NSMakeRange(5, 1)];
     EXPECT_EQ(0UL, rects.count);
 
@@ -216,10 +221,10 @@ TEST(WebKit, DocumentEditingContext)
     EXPECT_NSSTRING_EQ(" MMM", context.contextAfter);
     rects = [context characterRectsForCharacterRange:NSMakeRange(0, 1)];
     EXPECT_EQ(1UL, rects.count);
-    EXPECT_RECT_EQ(0, 0, 92, 24, rects.firstObject.CGRectValue);
+    EXPECT_RECT_EQ(0, 0, 23, 24, rects.firstObject.CGRectValue);
     rects = [context characterRectsForCharacterRange:NSMakeRange(6, 1)];
     EXPECT_EQ(1UL, rects.count);
-    EXPECT_RECT_EQ(92, 0, 92, 24, rects.firstObject.CGRectValue);
+    EXPECT_RECT_EQ(138, 0, 23, 24, rects.firstObject.CGRectValue);
 
     // Text Input Context
     [webView synchronouslyLoadHTMLString:applyStyle(@"<input type='text' style='width: 50px; height: 50px;' value='hello, world'>")];