<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN"
"http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head><meta http-equiv="content-type" content="text/html; charset=utf-8" />
<title>[260578] trunk</title>
</head>
<body>

<style type="text/css"><!--
#msg dl.meta { border: 1px #006 solid; background: #369; padding: 6px; color: #fff; }
#msg dl.meta dt { float: left; width: 6em; font-weight: bold; }
#msg dt:after { content:':';}
#msg dl, #msg dt, #msg ul, #msg li, #header, #footer, #logmsg { font-family: verdana,arial,helvetica,sans-serif; font-size: 10pt;  }
#msg dl a { font-weight: bold}
#msg dl a:link    { color:#fc3; }
#msg dl a:active  { color:#ff0; }
#msg dl a:visited { color:#cc6; }
h3 { font-family: verdana,arial,helvetica,sans-serif; font-size: 10pt; font-weight: bold; }
#msg pre { overflow: auto; background: #ffc; border: 1px #fa0 solid; padding: 6px; }
#logmsg { background: #ffc; border: 1px #fa0 solid; padding: 1em 1em 0 1em; }
#logmsg p, #logmsg pre, #logmsg blockquote { margin: 0 0 1em 0; }
#logmsg p, #logmsg li, #logmsg dt, #logmsg dd { line-height: 14pt; }
#logmsg h1, #logmsg h2, #logmsg h3, #logmsg h4, #logmsg h5, #logmsg h6 { margin: .5em 0; }
#logmsg h1:first-child, #logmsg h2:first-child, #logmsg h3:first-child, #logmsg h4:first-child, #logmsg h5:first-child, #logmsg h6:first-child { margin-top: 0; }
#logmsg ul, #logmsg ol { padding: 0; list-style-position: inside; margin: 0 0 0 1em; }
#logmsg ul { text-indent: -1em; padding-left: 1em; }#logmsg ol { text-indent: -1.5em; padding-left: 1.5em; }
#logmsg > ul, #logmsg > ol { margin: 0 0 1em 0; }
#logmsg pre { background: #eee; padding: 1em; }
#logmsg blockquote { border: 1px solid #fa0; border-left-width: 10px; padding: 1em 1em 0 1em; background: white;}
#logmsg dl { margin: 0; }
#logmsg dt { font-weight: bold; }
#logmsg dd { margin: 0; padding: 0 0 0.5em 0; }
#logmsg dd:before { content:'\00bb';}
#logmsg table { border-spacing: 0px; border-collapse: collapse; border-top: 4px solid #fa0; border-bottom: 1px solid #fa0; background: #fff; }
#logmsg table th { text-align: left; font-weight: normal; padding: 0.2em 0.5em; border-top: 1px dotted #fa0; }
#logmsg table td { text-align: right; border-top: 1px dotted #fa0; padding: 0.2em 0.5em; }
#logmsg table thead th { text-align: center; border-bottom: 1px solid #fa0; }
#logmsg table th.Corner { text-align: left; }
#logmsg hr { border: none 0; border-top: 2px dashed #fa0; height: 1px; }
#header, #footer { color: #fff; background: #636; border: 1px #300 solid; padding: 6px; }
#patch { width: 100%; }
#patch h4 {font-family: verdana,arial,helvetica,sans-serif;font-size:10pt;padding:8px;background:#369;color:#fff;margin:0;}
#patch .propset h4, #patch .binary h4 {margin:0;}
#patch pre {padding:0;line-height:1.2em;margin:0;}
#patch .diff {width:100%;background:#eee;padding: 0 0 10px 0;overflow:auto;}
#patch .propset .diff, #patch .binary .diff  {padding:10px 0;}
#patch span {display:block;padding:0 10px;}
#patch .modfile, #patch .addfile, #patch .delfile, #patch .propset, #patch .binary, #patch .copfile {border:1px solid #ccc;margin:10px 0;}
#patch ins {background:#dfd;text-decoration:none;display:block;padding:0 10px;}
#patch del {background:#fdd;text-decoration:none;display:block;padding:0 10px;}
#patch .lines, .info {color:#888;background:#fff;}
--></style>
<div id="msg">
<dl class="meta">
<dt>Revision</dt> <dd><a href="http://trac.webkit.org/projects/webkit/changeset/260578">260578</a></dd>
<dt>Author</dt> <dd>sihui_liu@apple.com</dd>
<dt>Date</dt> <dd>2020-04-23 10:23:54 -0700 (Thu, 23 Apr 2020)</dd>
</dl>

<h3>Log Message</h3>
<pre>TextManipulationController should set range of paragraph using token's positions
https://bugs.webkit.org/show_bug.cgi?id=210866
<rdar://problem/60646283>

Reviewed by Wenson Hsieh.

Source/WebCore:

Set the range of paragraph using positions of first token and last token in the paragraph because:
1. Accurate range makes token matching in TextManipulationController::replace() easier, as TextIterator could
visit different positions with different ranges or different conditions. For example, in our previous
implementation, start of a paragraph can be set as the first visible position of document, while position of
first token is after that. Then in replace(), TextManipulationController may extract a word before the position
of first token and return error. See added test TextManipulation.CompleteTextManipulationCorrectParagraphRange.
2. TextManipulationController can handle fewer content and this is less error-prone. For example, svg elements
before/after the paragraph text will not be identified as tokens [] in a paragraph now. See updated API tests
for example.

New test: TextManipulation.CompleteTextManipulationCorrectParagraphRange

* editing/TextManipulationController.cpp:
(WebCore::ParagraphContentIterator::moveCurrentNodeForward): m_currentNodeForFindingInvisibleContent should not
be advanced if it is already at the end.
(WebCore::containsOnlyHTMLSpaces):
(WebCore::TextManipulationController::observeParagraphs):Set the paragraph start as the position of the first
token and end as the position of last token. If the paragraph is split with <br>, the end will be extended to
position of <br> so that we can add this node back later; otherwise, <br> can be removed after original
text of paragraph is removed in TextManipulationController::replace(). Also, stop identifying spaces as tokens
because non-text Node can emit spaces.
(WebCore::TextManipulationController::replace): Only identify tokens from content with meaningful text.

Tools:

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

<h3>Modified Paths</h3>
<ul>
<li><a href="#trunkSourceWebCoreChangeLog">trunk/Source/WebCore/ChangeLog</a></li>
<li><a href="#trunkSourceWebCoreeditingTextManipulationControllercpp">trunk/Source/WebCore/editing/TextManipulationController.cpp</a></li>
<li><a href="#trunkToolsChangeLog">trunk/Tools/ChangeLog</a></li>
<li><a href="#trunkToolsTestWebKitAPITestsWebKitCocoaTextManipulationmm">trunk/Tools/TestWebKitAPI/Tests/WebKitCocoa/TextManipulation.mm</a></li>
</ul>

</div>
<div id="patch">
<h3>Diff</h3>
<a id="trunkSourceWebCoreChangeLog"></a>
<div class="modfile"><h4>Modified: trunk/Source/WebCore/ChangeLog (260577 => 260578)</h4>
<pre class="diff"><span>
<span class="info">--- trunk/Source/WebCore/ChangeLog   2020-04-23 17:23:18 UTC (rev 260577)
+++ trunk/Source/WebCore/ChangeLog      2020-04-23 17:23:54 UTC (rev 260578)
</span><span class="lines">@@ -1,3 +1,34 @@
</span><ins>+2020-04-23  Sihui Liu  <sihui_liu@apple.com>
+
+        TextManipulationController should set range of paragraph using token's positions
+        https://bugs.webkit.org/show_bug.cgi?id=210866
+        <rdar://problem/60646283>
+
+        Reviewed by Wenson Hsieh.
+
+        Set the range of paragraph using positions of first token and last token in the paragraph because:
+        1. Accurate range makes token matching in TextManipulationController::replace() easier, as TextIterator could 
+        visit different positions with different ranges or different conditions. For example, in our previous 
+        implementation, start of a paragraph can be set as the first visible position of document, while position of 
+        first token is after that. Then in replace(), TextManipulationController may extract a word before the position 
+        of first token and return error. See added test TextManipulation.CompleteTextManipulationCorrectParagraphRange.
+        2. TextManipulationController can handle fewer content and this is less error-prone. For example, svg elements 
+        before/after the paragraph text will not be identified as tokens [] in a paragraph now. See updated API tests 
+        for example.
+
+        New test: TextManipulation.CompleteTextManipulationCorrectParagraphRange
+
+        * editing/TextManipulationController.cpp:
+        (WebCore::ParagraphContentIterator::moveCurrentNodeForward): m_currentNodeForFindingInvisibleContent should not 
+        be advanced if it is already at the end.
+        (WebCore::containsOnlyHTMLSpaces):
+        (WebCore::TextManipulationController::observeParagraphs):Set the paragraph start as the position of the first 
+        token and end as the position of last token. If the paragraph is split with <br>, the end will be extended to 
+        position of <br> so that we can add this node back later; otherwise, <br> can be removed after original 
+        text of paragraph is removed in TextManipulationController::replace(). Also, stop identifying spaces as tokens 
+        because non-text Node can emit spaces.
+        (WebCore::TextManipulationController::replace): Only identify tokens from content with meaningful text.
+
</ins><span class="cx"> 2020-04-23  Chris Dumez  <cdumez@apple.com>
</span><span class="cx"> 
</span><span class="cx">         [ Mac wk2 ] imported/w3c/web-platform-tests/notifications/event-onclose.html is flaky failing.
</span></span></pre></div>
<a id="trunkSourceWebCoreeditingTextManipulationControllercpp"></a>
<div class="modfile"><h4>Modified: trunk/Source/WebCore/editing/TextManipulationController.cpp (260577 => 260578)</h4>
<pre class="diff"><span>
<span class="info">--- trunk/Source/WebCore/editing/TextManipulationController.cpp      2020-04-23 17:23:18 UTC (rev 260577)
+++ trunk/Source/WebCore/editing/TextManipulationController.cpp 2020-04-23 17:23:54 UTC (rev 260578)
</span><span class="lines">@@ -30,8 +30,10 @@
</span><span class="cx"> #include "Editing.h"
</span><span class="cx"> #include "ElementAncestorIterator.h"
</span><span class="cx"> #include "EventLoop.h"
</span><ins>+#include "HTMLBRElement.h"
</ins><span class="cx"> #include "HTMLElement.h"
</span><span class="cx"> #include "HTMLNames.h"
</span><ins>+#include "HTMLParserIdioms.h"
</ins><span class="cx"> #include "NodeTraversal.h"
</span><span class="cx"> #include "PseudoElement.h"
</span><span class="cx"> #include "Range.h"
</span><span class="lines">@@ -207,6 +209,9 @@
</span><span class="cx"> private:
</span><span class="cx">     void moveCurrentNodeForward()
</span><span class="cx">     {
</span><ins>+        if (m_currentNodeForFindingInvisibleContent == m_pastEndNode)
+            return;
+
</ins><span class="cx">         m_currentNodeForFindingInvisibleContent = NodeTraversal::next(*m_currentNodeForFindingInvisibleContent);
</span><span class="cx">         if (!m_currentNodeForFindingInvisibleContent)
</span><span class="cx">             m_currentNodeForFindingInvisibleContent = m_pastEndNode;
</span><span class="lines">@@ -242,6 +247,16 @@
</span><span class="cx">     return element.hasTagName(HTMLNames::titleTag) || element.hasTagName(HTMLNames::optionTag);
</span><span class="cx"> }
</span><span class="cx"> 
</span><ins>+static bool containsOnlyHTMLSpaces(StringView text)
+{
+    for (unsigned index = 0; index < text.length(); ++index) {
+        if (isNotHTMLSpace(text[index]))
+            return false;
+    }
+
+    return true;
+}
+
</ins><span class="cx"> void TextManipulationController::observeParagraphs(const Position& start, const Position& end)
</span><span class="cx"> {
</span><span class="cx">     if (start.isNull() || end.isNull())
</span><span class="lines">@@ -257,7 +272,8 @@
</span><span class="cx"> 
</span><span class="cx">     ExclusionRuleMatcher exclusionRuleMatcher(m_exclusionRules);
</span><span class="cx">     Vector<ManipulationToken> tokensInCurrentParagraph;
</span><del>-    Position startOfCurrentParagraph = visibleStart.deepEquivalent();
</del><ins>+    Position startOfCurrentParagraph;
+    Position endOfCurrentParagraph;
</ins><span class="cx">     for (; !iterator.atEnd(); iterator.advance()) {
</span><span class="cx">         auto content = iterator.currentContent();
</span><span class="cx">         if (content.node) {
</span><span class="lines">@@ -281,15 +297,20 @@
</span><span class="cx">                     }
</span><span class="cx">                 }
</span><span class="cx">             }
</span><del>-
-            if (startOfCurrentParagraph.isNull() && content.isTextContent)
-                startOfCurrentParagraph = iterator.startPosition();
</del><span class="cx">         }
</span><span class="cx"> 
</span><span class="cx">         if (content.isReplacedContent) {
</span><del>-            if (startOfCurrentParagraph.isNull())
-                startOfCurrentParagraph = positionBeforeNode(content.node.get());
-            tokensInCurrentParagraph.append(ManipulationToken { m_tokenIdentifier.generate(), "[]", true /* isExcluded */});
</del><ins>+            if (tokensInCurrentParagraph.isEmpty())
+                continue;
+
+            auto currentEndOfCurrentParagraph = positionAfterNode(content.node.get());
+            // This is at the same Node as last token, so it is already included in current range.
+            if (!is<Text>(content.node) && currentEndOfCurrentParagraph.equals(endOfCurrentParagraph))
+                continue;
+
+            endOfCurrentParagraph = currentEndOfCurrentParagraph;
+            tokensInCurrentParagraph.append(ManipulationToken { m_tokenIdentifier.generate(), "[]", true });
+
</ins><span class="cx">             continue;
</span><span class="cx">         }
</span><span class="cx"> 
</span><span class="lines">@@ -300,31 +321,39 @@
</span><span class="cx">         size_t offsetOfNextNewLine = 0;
</span><span class="cx">         StringView currentText = content.text;
</span><span class="cx">         while ((offsetOfNextNewLine = currentText.find('\n', startOfCurrentLine)) != notFound) {
</span><del>-            if (startOfCurrentLine < offsetOfNextNewLine) {
</del><ins>+            if (is<Text>(content.node) && startOfCurrentLine < offsetOfNextNewLine) {
</ins><span class="cx">                 auto stringUntilEndOfLine = currentText.substring(startOfCurrentLine, offsetOfNextNewLine - startOfCurrentLine).toString();
</span><ins>+                auto& textNode = downcast<Text>(*content.node);
+                endOfCurrentParagraph = Position(&textNode, offsetOfNextNewLine);
+                if (tokensInCurrentParagraph.isEmpty())
+                    startOfCurrentParagraph = Position(&textNode, startOfCurrentLine);
+
</ins><span class="cx">                 tokensInCurrentParagraph.append(ManipulationToken { m_tokenIdentifier.generate(), stringUntilEndOfLine, exclusionRuleMatcher.isExcluded(content.node.get()) });
</span><span class="cx">             }
</span><span class="cx"> 
</span><span class="cx">             if (!tokensInCurrentParagraph.isEmpty()) {
</span><del>-                Position endOfCurrentParagraph = iterator.endPosition();
-                if (is<Text>(content.node)) {
-                    auto& textNode = downcast<Text>(*content.node);
-                    endOfCurrentParagraph = Position(&textNode, offsetOfNextNewLine);
-                    startOfCurrentParagraph = Position(&textNode, offsetOfNextNewLine + 1);
-                }
</del><ins>+                if (is<HTMLBRElement>(content.node))
+                    endOfCurrentParagraph = positionAfterNode(content.node.get());
</ins><span class="cx">                 addItem(ManipulationItemData { startOfCurrentParagraph, endOfCurrentParagraph, nullptr, nullQName(), std::exchange(tokensInCurrentParagraph, { }) });
</span><del>-                startOfCurrentParagraph.clear();
</del><span class="cx">             }
</span><span class="cx">             startOfCurrentLine = offsetOfNextNewLine + 1;
</span><span class="cx">         }
</span><span class="cx"> 
</span><span class="cx">         auto remainingText = currentText.substring(startOfCurrentLine);
</span><del>-        if (remainingText.length())
</del><ins>+        if (!containsOnlyHTMLSpaces(remainingText)) {
+            if (tokensInCurrentParagraph.isEmpty()) {
+                if (startOfCurrentLine && is<Text>(content.node))
+                    startOfCurrentParagraph = Position(&downcast<Text>(*content.node), startOfCurrentLine);
+                else
+                    startOfCurrentParagraph = iterator.startPosition();
+            }
+            endOfCurrentParagraph = iterator.endPosition();
</ins><span class="cx">             tokensInCurrentParagraph.append(ManipulationToken { m_tokenIdentifier.generate(), remainingText.toString(), exclusionRuleMatcher.isExcluded(content.node.get()) });
</span><ins>+        }
</ins><span class="cx">     }
</span><span class="cx"> 
</span><span class="cx">     if (!tokensInCurrentParagraph.isEmpty())
</span><del>-        addItem(ManipulationItemData { startOfCurrentParagraph, visibleEnd.deepEquivalent(), nullptr, nullQName(), WTFMove(tokensInCurrentParagraph) });
</del><ins>+        addItem(ManipulationItemData { startOfCurrentParagraph, endOfCurrentParagraph, nullptr, nullQName(), WTFMove(tokensInCurrentParagraph) });
</ins><span class="cx"> }
</span><span class="cx"> 
</span><span class="cx"> void TextManipulationController::didCreateRendererForElement(Element& element)
</span><span class="lines">@@ -519,6 +548,13 @@
</span><span class="cx">             return ManipulationFailureType::ContentChanged;
</span><span class="cx">         }
</span><span class="cx"> 
</span><ins>+        if (content.isTextContent && containsOnlyHTMLSpaces(content.text)) {
+            // <br> should not exist in the middle of a paragraph.
+            if (is<HTMLBRElement>(content.node))
+                return ManipulationFailureType::ContentChanged;
+            continue;
+        }
+
</ins><span class="cx">         auto& currentToken = item.tokens[currentTokenIndex];
</span><span class="cx">         if (!content.isReplacedContent && content.text != currentToken.content)
</span><span class="cx">             return ManipulationFailureType::ContentChanged;
</span></span></pre></div>
<a id="trunkToolsChangeLog"></a>
<div class="modfile"><h4>Modified: trunk/Tools/ChangeLog (260577 => 260578)</h4>
<pre class="diff"><span>
<span class="info">--- trunk/Tools/ChangeLog    2020-04-23 17:23:18 UTC (rev 260577)
+++ trunk/Tools/ChangeLog       2020-04-23 17:23:54 UTC (rev 260578)
</span><span class="lines">@@ -1,3 +1,14 @@
</span><ins>+2020-04-23  Sihui Liu  <sihui_liu@apple.com>
+
+        TextManipulationController should set range of paragraph using token's positions
+        https://bugs.webkit.org/show_bug.cgi?id=210866
+        <rdar://problem/60646283>
+
+        Reviewed by Wenson Hsieh.
+
+        * TestWebKitAPI/Tests/WebKitCocoa/TextManipulation.mm:
+        (TestWebKitAPI::TEST):
+
</ins><span class="cx"> 2020-04-23  Emilio Cobos Ãlvarez  <emilio@crisal.io>
</span><span class="cx"> 
</span><span class="cx">         Unreviewed, add my bugzilla / slack nick to contributors.json
</span></span></pre></div>
<a id="trunkToolsTestWebKitAPITestsWebKitCocoaTextManipulationmm"></a>
<div class="modfile"><h4>Modified: trunk/Tools/TestWebKitAPI/Tests/WebKitCocoa/TextManipulation.mm (260577 => 260578)</h4>
<pre class="diff"><span>
<span class="info">--- trunk/Tools/TestWebKitAPI/Tests/WebKitCocoa/TextManipulation.mm  2020-04-23 17:23:18 UTC (rev 260577)
+++ trunk/Tools/TestWebKitAPI/Tests/WebKitCocoa/TextManipulation.mm     2020-04-23 17:23:54 UTC (rev 260578)
</span><span class="lines">@@ -988,22 +988,14 @@
</span><span class="cx">     TestWebKitAPI::Util::run(&done);
</span><span class="cx"> 
</span><span class="cx">     auto *items = [delegate items];
</span><del>-    EXPECT_EQ(items.count, 2UL);
-    EXPECT_EQ(items[0].tokens.count, 2UL);
-    EXPECT_STREQ("[]", items[0].tokens[0].content.UTF8String);
-    EXPECT_TRUE(items[0].tokens[0].isExcluded);
-    EXPECT_STREQ("[]", items[0].tokens[1].content.UTF8String);
-    EXPECT_TRUE(items[0].tokens[1].isExcluded);
</del><ins>+    EXPECT_EQ(items.count, 1UL);
+    EXPECT_EQ(items[0].tokens.count, 1UL);
+    EXPECT_STREQ("helllo world", items[0].tokens[0].content.UTF8String);
+    EXPECT_TRUE(!items[0].tokens[0].isExcluded);
</ins><span class="cx"> 
</span><del>-    auto *tokens = items[1].tokens;
-    EXPECT_EQ(tokens.count, 1UL);
-    EXPECT_STREQ("helllo world", tokens[0].content.UTF8String);
-    EXPECT_FALSE(tokens[0].isExcluded);
-
</del><span class="cx">     done = false;
</span><span class="cx">     [webView _completeTextManipulationForItems:@[
</span><del>-        (_WKTextManipulationItem *)createItem(items[0].identifier, { { items[0].tokens[0].identifier, nil } }),
-        (_WKTextManipulationItem *)createItem(items[1].identifier, { { items[1].tokens[0].identifier, @"hello, world" } }),
</del><ins>+        (_WKTextManipulationItem *)createItem(items[0].identifier, { { items[0].tokens[0].identifier, @"hello, world" } }),
</ins><span class="cx">     ] completion:^(NSArray<NSError *> *errors) {
</span><span class="cx">         EXPECT_EQ(errors, nil);
</span><span class="cx">         done = true;
</span><span class="lines">@@ -1076,16 +1068,13 @@
</span><span class="cx">     TestWebKitAPI::Util::run(&done);
</span><span class="cx"> 
</span><span class="cx">     auto *items = [delegate items];
</span><del>-    EXPECT_EQ(items.count, 2UL);
-    EXPECT_EQ(items[1].tokens.count, 1UL);
-    EXPECT_STREQ("[]", items[0].tokens[0].content.UTF8String);
</del><ins>+    EXPECT_EQ(items.count, 1UL);
</ins><span class="cx">     EXPECT_EQ(items[0].tokens.count, 1UL);
</span><del>-    EXPECT_STREQ("hello world", items[1].tokens[0].content.UTF8String);
</del><ins>+    EXPECT_STREQ("hello world", items[0].tokens[0].content.UTF8String);
</ins><span class="cx"> 
</span><span class="cx">     done = false;
</span><span class="cx">     [webView _completeTextManipulationForItems:@[
</span><del>-        (_WKTextManipulationItem *)createItem(items[0].identifier, { { items[0].tokens[0].identifier, nil } }),
-        (_WKTextManipulationItem *)createItem(items[1].identifier, { { items[1].tokens[0].identifier, @"hello, world" } }),
</del><ins>+        (_WKTextManipulationItem *)createItem(items[0].identifier, { { items[0].tokens[0].identifier, @"hello, world" } }),
</ins><span class="cx">     ] completion:^(NSArray<NSError *> *errors) {
</span><span class="cx">         EXPECT_EQ(errors, nil);
</span><span class="cx">         done = true;
</span><span class="lines">@@ -1162,19 +1151,16 @@
</span><span class="cx">     TestWebKitAPI::Util::run(&done);
</span><span class="cx"> 
</span><span class="cx">     auto *items = [delegate items];
</span><del>-    EXPECT_EQ(items.count, 3UL);
</del><ins>+    EXPECT_EQ(items.count, 2UL);
</ins><span class="cx">     EXPECT_EQ(items[0].tokens.count, 1UL);
</span><span class="cx">     EXPECT_STREQ("heeey", items[0].tokens[0].content.UTF8String);
</span><span class="cx">     EXPECT_EQ(items[1].tokens.count, 1UL);
</span><del>-    EXPECT_STREQ("[]", items[1].tokens[0].content.UTF8String);
-    EXPECT_EQ(items[2].tokens.count, 1UL);
-    EXPECT_STREQ("woorld", items[2].tokens[0].content.UTF8String);
</del><ins>+    EXPECT_STREQ("woorld", items[1].tokens[0].content.UTF8String);
</ins><span class="cx"> 
</span><span class="cx">     done = false;
</span><span class="cx">     [webView _completeTextManipulationForItems:@[
</span><span class="cx">         (_WKTextManipulationItem *)createItem(items[0].identifier, { { items[0].tokens[0].identifier, @"hello" } }),
</span><del>-        (_WKTextManipulationItem *)createItem(items[1].identifier, { { items[1].tokens[0].identifier, nil } }),
-        (_WKTextManipulationItem *)createItem(items[2].identifier, { { items[2].tokens[0].identifier, @"world" } }),
</del><ins>+        (_WKTextManipulationItem *)createItem(items[1].identifier, { { items[1].tokens[0].identifier, @"world" } }),
</ins><span class="cx">     ] completion:^(NSArray<NSError *> *errors) {
</span><span class="cx">         EXPECT_EQ(errors, nil);
</span><span class="cx">         done = true;
</span><span class="lines">@@ -1684,6 +1670,40 @@
</span><span class="cx">     EXPECT_WK_STREQ("<p>bar <strong>garply</strong> foo</p>", [webView stringByEvaluatingJavaScript:@"document.body.innerHTML"]);
</span><span class="cx"> }
</span><span class="cx"> 
</span><ins>+TEST(TextManipulation, CompleteTextManipulationCorrectParagraphRange)
+{
+    auto delegate = adoptNS([[TextManipulationDelegate alloc] init]);
+    auto webView = adoptNS([[TestWKWebView alloc] initWithFrame:NSMakeRect(0, 0, 400, 400)]);
+    [webView _setTextManipulationDelegate:delegate.get()];
+
+    [webView synchronouslyLoadHTMLString:@"<head><style>ul{display:block}li{display:inline-block}.inline {float: left;}.subframe {height: 42px;}.frame {position: absolute;top: -9999px;}</style></head><body><div class='frame'><div class='subframe'></div></div><style></style><div class='inline'><div><li><a href='#'>holle</a></li><li><a href='#'>wdrlo</a></li></div></div><div class='frame'><div class='subframe'></div></div></body>"];
+
+    RetainPtr<_WKTextManipulationConfiguration> configuration = adoptNS([[_WKTextManipulationConfiguration alloc] init]);
+    done = false;
+    [webView _startTextManipulationsWithConfiguration:configuration.get() completion:^{
+        done = true;
+    }];
+    TestWebKitAPI::Util::run(&done);
+
+    auto *items = [delegate items];
+    EXPECT_EQ(items.count, 1UL);
+    EXPECT_EQ(items[0].tokens.count, 2UL);
+    EXPECT_STREQ("holle", items[0].tokens[0].content.UTF8String);
+    EXPECT_STREQ("wdrlo", items[0].tokens[1].content.UTF8String);
+
+    done = false;
+    [webView _completeTextManipulationForItems:@[(_WKTextManipulationItem *)createItem(items[0].identifier, {
+        { items[0].tokens[0].identifier, @"hello" },
+        { items[0].tokens[1].identifier, @"world" },
+    })] completion:^(NSArray<NSError *> *errors) {
+        EXPECT_EQ(errors, nil);
+        done = true;
+    }];
+    TestWebKitAPI::Util::run(&done);
+
+    EXPECT_WK_STREQ("<div class=\"frame\"><div class=\"subframe\"></div></div><style></style><div class=\"inline\"><div><li><a href=\"#\">hello</a></li><li><a href=\"#\">world</a></li></div></div><div class=\"frame\"><div class=\"subframe\"></div></div>", [webView stringByEvaluatingJavaScript:@"document.body.innerHTML"]);
+}
+
</ins><span class="cx"> TEST(TextManipulation, InsertingContentIntoAlreadyManipulatedContentDoesNotCreateTextManipulationItem)
</span><span class="cx"> {
</span><span class="cx">     auto delegate = adoptNS([[TextManipulationDelegate alloc] init]);
</span></span></pre>
</div>
</div>

</body>
</html>