Title: [266075] trunk
Revision
266075
Author
sihui_...@apple.com
Date
2020-08-24 11:06:01 -0700 (Mon, 24 Aug 2020)

Log Message

Source/WebCore:
Text manipulation should not manipulate nodes out of paragraph range
https://bugs.webkit.org/show_bug.cgi?id=215406

Reviewed by Wenson Hsieh.

TextManipulationController currently does not set correct start path for insertion. Therefore, it does not mark
the nodes on the start path but out of range correctly, and may change position of those nodes by mistake. For
example, in the newly added test, text node "zero" can be moved around even though it is not in range.

API test: TextManipulation.CompleteTextManipulationShouldOnlyChangeNodesInParagraphRange

* editing/TextManipulationController.cpp:
(WebCore::TextManipulationController::replace):

Tools:
Text manipulationshould not manipulate nodes out of paragraph range
https://bugs.webkit.org/show_bug.cgi?id=215406

Reviewed by Wenson Hsieh.

* TestWebKitAPI/Tests/WebKitCocoa/TextManipulation.mm:
(TestWebKitAPI::TEST):

Modified Paths

Diff

Modified: trunk/Source/WebCore/ChangeLog (266074 => 266075)


--- trunk/Source/WebCore/ChangeLog	2020-08-24 17:34:12 UTC (rev 266074)
+++ trunk/Source/WebCore/ChangeLog	2020-08-24 18:06:01 UTC (rev 266075)
@@ -1,3 +1,19 @@
+2020-08-24  Sihui Liu  <sihui_...@apple.com>
+
+        Text manipulation should not manipulate nodes out of paragraph range
+        https://bugs.webkit.org/show_bug.cgi?id=215406
+
+        Reviewed by Wenson Hsieh.
+
+        TextManipulationController currently does not set correct start path for insertion. Therefore, it does not mark 
+        the nodes on the start path but out of range correctly, and may change position of those nodes by mistake. For 
+        example, in the newly added test, text node "zero" can be moved around even though it is not in range.
+
+        API test: TextManipulation.CompleteTextManipulationShouldOnlyChangeNodesInParagraphRange
+
+        * editing/TextManipulationController.cpp:
+        (WebCore::TextManipulationController::replace):
+
 2020-08-24  Devin Rousso  <drou...@apple.com>
 
         Web Inspector: allow event breakpoints to be configured

Modified: trunk/Source/WebCore/editing/TextManipulationController.cpp (266074 => 266075)


--- trunk/Source/WebCore/editing/TextManipulationController.cpp	2020-08-24 17:34:12 UTC (rev 266074)
+++ trunk/Source/WebCore/editing/TextManipulationController.cpp	2020-08-24 18:06:01 UTC (rev 266075)
@@ -829,8 +829,20 @@
         nodesToRemove.remove(*node);
 
     HashSet<Ref<Node>> reusedOriginalNodes;
-    Vector<NodeEntry> lastTopDownPath;
     Vector<NodeInsertion> insertions;
+    auto startTopDownPath = getPath(commonAncestor.get(), firstContentNode.get());
+    while (!startTopDownPath.isEmpty()) {
+        auto lastNode = startTopDownPath.last();
+        ASSERT(is<ContainerNode>(lastNode.get()));
+        if (!downcast<ContainerNode>(lastNode.get()).hasOneChild())
+            break;
+        nodesToRemove.add(startTopDownPath.takeLast());
+    }
+    auto lastTopDownPath = startTopDownPath.map([&](auto node) -> NodeEntry {
+        reusedOriginalNodes.add(node.copyRef());
+        return { node, node };
+    });
+
     for (size_t index = 0; index < replacementTokens.size(); ++index) {
         auto& replacementToken = replacementTokens[index];
         auto it = tokenExchangeMap.find(replacementToken.identifier);

Modified: trunk/Tools/ChangeLog (266074 => 266075)


--- trunk/Tools/ChangeLog	2020-08-24 17:34:12 UTC (rev 266074)
+++ trunk/Tools/ChangeLog	2020-08-24 18:06:01 UTC (rev 266075)
@@ -1,3 +1,13 @@
+2020-08-24  Sihui Liu  <sihui_...@apple.com>
+
+        Text manipulationshould not manipulate nodes out of paragraph range
+        https://bugs.webkit.org/show_bug.cgi?id=215406
+
+        Reviewed by Wenson Hsieh.
+
+        * TestWebKitAPI/Tests/WebKitCocoa/TextManipulation.mm:
+        (TestWebKitAPI::TEST):
+
 2020-08-24  Aditya Keerthi  <akeer...@apple.com>
 
         [macOS] Show picker for date and datetime-local input types

Modified: trunk/Tools/TestWebKitAPI/Tests/WebKitCocoa/TextManipulation.mm (266074 => 266075)


--- trunk/Tools/TestWebKitAPI/Tests/WebKitCocoa/TextManipulation.mm	2020-08-24 17:34:12 UTC (rev 266074)
+++ trunk/Tools/TestWebKitAPI/Tests/WebKitCocoa/TextManipulation.mm	2020-08-24 18:06:01 UTC (rev 266075)
@@ -3131,6 +3131,54 @@
     EXPECT_WK_STREQ("<div role=\"img\">Images<a>Link</a><img src="" src="" [webView stringByEvaluatingJavaScript:@"document.body.innerHTML"]);
 }
 
+TEST(TextManipulation, CompleteTextManipulationShouldOnlyChangeNodesInParagraphRange)
+{
+    auto delegate = adoptNS([[TextManipulationDelegate alloc] init]);
+    auto webView = adoptNS([[TestWKWebView alloc] initWithFrame:NSMakeRect(0, 0, 400, 400)]);
+    [webView _setTextManipulationDelegate:delegate.get()];
+    [webView synchronouslyLoadHTMLString:@"<!DOCTYPE html>"
+        "<head>"
+            "<style>"
+                "span { white-space:pre; }"
+            "</style>"
+        "</head>"
+        "<body>"
+            "<span>zero &#10;<b>two four</b></span>"
+            "one"
+            "<i>three</i>"
+        "</body>"];
+
+    done = false;
+    [webView _startTextManipulationsWithConfiguration:nil completion:^{
+        done = true;
+    }];
+    TestWebKitAPI::Util::run(&done);
+
+    auto *items = [delegate items];
+    EXPECT_EQ(items.count, 2UL);
+    EXPECT_EQ(items[0].tokens.count, 2UL);
+    EXPECT_WK_STREQ("zero", items[0].tokens[0].content);
+    EXPECT_WK_STREQ(" \n", items[0].tokens[1].content);
+    EXPECT_EQ(items[1].tokens.count, 3UL);
+    EXPECT_WK_STREQ("two four", items[1].tokens[0].content);
+    EXPECT_WK_STREQ("one", items[1].tokens[1].content);
+    EXPECT_WK_STREQ("three", items[1].tokens[2].content);
+
+    done = false;
+    [webView _completeTextManipulationForItems:@[(_WKTextManipulationItem *)createItem(items[1].identifier, {
+        { items[1].tokens[1].identifier, @"one" },
+        { items[1].tokens[0].identifier, @"two" },
+        { items[1].tokens[2].identifier, @"three" },
+        { items[1].tokens[0].identifier, @"four" }
+    }).get()] completion:^(NSArray<NSError *> *errors) {
+        EXPECT_EQ(errors, nil);
+        done = true;
+    }];
+    TestWebKitAPI::Util::run(&done);
+    EXPECT_WK_STREQ("<span>zero \n</span>one<span><b>two</b></span><i>three</i><span><b>four</b></span>",
+        [webView stringByEvaluatingJavaScript:@"document.body.innerHTML"]);
+}
+
 TEST(TextManipulation, TextManipulationTokenDebugDescription)
 {
     auto token = adoptNS([[_WKTextManipulationToken alloc] init]);
_______________________________________________
webkit-changes mailing list
webkit-changes@lists.webkit.org
https://lists.webkit.org/mailman/listinfo/webkit-changes

Reply via email to