Modified: trunk/Source/WebCore/ChangeLog (260577 => 260578)
--- 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)
@@ -1,3 +1,34 @@
+2020-04-23 Sihui Liu <sihui_...@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.
+
2020-04-23 Chris Dumez <cdu...@apple.com>
[ Mac wk2 ] imported/w3c/web-platform-tests/notifications/event-onclose.html is flaky failing.
Modified: trunk/Source/WebCore/editing/TextManipulationController.cpp (260577 => 260578)
--- 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)
@@ -30,8 +30,10 @@
#include "Editing.h"
#include "ElementAncestorIterator.h"
#include "EventLoop.h"
+#include "HTMLBRElement.h"
#include "HTMLElement.h"
#include "HTMLNames.h"
+#include "HTMLParserIdioms.h"
#include "NodeTraversal.h"
#include "PseudoElement.h"
#include "Range.h"
@@ -207,6 +209,9 @@
private:
void moveCurrentNodeForward()
{
+ if (m_currentNodeForFindingInvisibleContent == m_pastEndNode)
+ return;
+
m_currentNodeForFindingInvisibleContent = NodeTraversal::next(*m_currentNodeForFindingInvisibleContent);
if (!m_currentNodeForFindingInvisibleContent)
m_currentNodeForFindingInvisibleContent = m_pastEndNode;
@@ -242,6 +247,16 @@
return element.hasTagName(HTMLNames::titleTag) || element.hasTagName(HTMLNames::optionTag);
}
+static bool containsOnlyHTMLSpaces(StringView text)
+{
+ for (unsigned index = 0; index < text.length(); ++index) {
+ if (isNotHTMLSpace(text[index]))
+ return false;
+ }
+
+ return true;
+}
+
void TextManipulationController::observeParagraphs(const Position& start, const Position& end)
{
if (start.isNull() || end.isNull())
@@ -257,7 +272,8 @@
ExclusionRuleMatcher exclusionRuleMatcher(m_exclusionRules);
Vector<ManipulationToken> tokensInCurrentParagraph;
- Position startOfCurrentParagraph = visibleStart.deepEquivalent();
+ Position startOfCurrentParagraph;
+ Position endOfCurrentParagraph;
for (; !iterator.atEnd(); iterator.advance()) {
auto content = iterator.currentContent();
if (content.node) {
@@ -281,15 +297,20 @@
}
}
}
-
- if (startOfCurrentParagraph.isNull() && content.isTextContent)
- startOfCurrentParagraph = iterator.startPosition();
}
if (content.isReplacedContent) {
- if (startOfCurrentParagraph.isNull())
- startOfCurrentParagraph = positionBeforeNode(content.node.get());
- tokensInCurrentParagraph.append(ManipulationToken { m_tokenIdentifier.generate(), "[]", true /* isExcluded */});
+ 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 });
+
continue;
}
@@ -300,31 +321,39 @@
size_t offsetOfNextNewLine = 0;
StringView currentText = content.text;
while ((offsetOfNextNewLine = currentText.find('\n', startOfCurrentLine)) != notFound) {
- if (startOfCurrentLine < offsetOfNextNewLine) {
+ if (is<Text>(content.node) && startOfCurrentLine < offsetOfNextNewLine) {
auto stringUntilEndOfLine = currentText.substring(startOfCurrentLine, offsetOfNextNewLine - startOfCurrentLine).toString();
+ auto& textNode = downcast<Text>(*content.node);
+ endOfCurrentParagraph = Position(&textNode, offsetOfNextNewLine);
+ if (tokensInCurrentParagraph.isEmpty())
+ startOfCurrentParagraph = Position(&textNode, startOfCurrentLine);
+
tokensInCurrentParagraph.append(ManipulationToken { m_tokenIdentifier.generate(), stringUntilEndOfLine, exclusionRuleMatcher.isExcluded(content.node.get()) });
}
if (!tokensInCurrentParagraph.isEmpty()) {
- Position endOfCurrentParagraph = iterator.endPosition();
- if (is<Text>(content.node)) {
- auto& textNode = downcast<Text>(*content.node);
- endOfCurrentParagraph = Position(&textNode, offsetOfNextNewLine);
- startOfCurrentParagraph = Position(&textNode, offsetOfNextNewLine + 1);
- }
+ if (is<HTMLBRElement>(content.node))
+ endOfCurrentParagraph = positionAfterNode(content.node.get());
addItem(ManipulationItemData { startOfCurrentParagraph, endOfCurrentParagraph, nullptr, nullQName(), std::exchange(tokensInCurrentParagraph, { }) });
- startOfCurrentParagraph.clear();
}
startOfCurrentLine = offsetOfNextNewLine + 1;
}
auto remainingText = currentText.substring(startOfCurrentLine);
- if (remainingText.length())
+ 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();
tokensInCurrentParagraph.append(ManipulationToken { m_tokenIdentifier.generate(), remainingText.toString(), exclusionRuleMatcher.isExcluded(content.node.get()) });
+ }
}
if (!tokensInCurrentParagraph.isEmpty())
- addItem(ManipulationItemData { startOfCurrentParagraph, visibleEnd.deepEquivalent(), nullptr, nullQName(), WTFMove(tokensInCurrentParagraph) });
+ addItem(ManipulationItemData { startOfCurrentParagraph, endOfCurrentParagraph, nullptr, nullQName(), WTFMove(tokensInCurrentParagraph) });
}
void TextManipulationController::didCreateRendererForElement(Element& element)
@@ -519,6 +548,13 @@
return ManipulationFailureType::ContentChanged;
}
+ 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;
+ }
+
auto& currentToken = item.tokens[currentTokenIndex];
if (!content.isReplacedContent && content.text != currentToken.content)
return ManipulationFailureType::ContentChanged;
Modified: trunk/Tools/TestWebKitAPI/Tests/WebKitCocoa/TextManipulation.mm (260577 => 260578)
--- 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)
@@ -988,22 +988,14 @@
TestWebKitAPI::Util::run(&done);
auto *items = [delegate items];
- 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);
+ 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);
- auto *tokens = items[1].tokens;
- EXPECT_EQ(tokens.count, 1UL);
- EXPECT_STREQ("helllo world", tokens[0].content.UTF8String);
- EXPECT_FALSE(tokens[0].isExcluded);
-
done = false;
[webView _completeTextManipulationForItems:@[
- (_WKTextManipulationItem *)createItem(items[0].identifier, { { items[0].tokens[0].identifier, nil } }),
- (_WKTextManipulationItem *)createItem(items[1].identifier, { { items[1].tokens[0].identifier, @"hello, world" } }),
+ (_WKTextManipulationItem *)createItem(items[0].identifier, { { items[0].tokens[0].identifier, @"hello, world" } }),
] completion:^(NSArray<NSError *> *errors) {
EXPECT_EQ(errors, nil);
done = true;
@@ -1076,16 +1068,13 @@
TestWebKitAPI::Util::run(&done);
auto *items = [delegate items];
- EXPECT_EQ(items.count, 2UL);
- EXPECT_EQ(items[1].tokens.count, 1UL);
- EXPECT_STREQ("[]", items[0].tokens[0].content.UTF8String);
+ EXPECT_EQ(items.count, 1UL);
EXPECT_EQ(items[0].tokens.count, 1UL);
- EXPECT_STREQ("hello world", items[1].tokens[0].content.UTF8String);
+ EXPECT_STREQ("hello world", items[0].tokens[0].content.UTF8String);
done = false;
[webView _completeTextManipulationForItems:@[
- (_WKTextManipulationItem *)createItem(items[0].identifier, { { items[0].tokens[0].identifier, nil } }),
- (_WKTextManipulationItem *)createItem(items[1].identifier, { { items[1].tokens[0].identifier, @"hello, world" } }),
+ (_WKTextManipulationItem *)createItem(items[0].identifier, { { items[0].tokens[0].identifier, @"hello, world" } }),
] completion:^(NSArray<NSError *> *errors) {
EXPECT_EQ(errors, nil);
done = true;
@@ -1162,19 +1151,16 @@
TestWebKitAPI::Util::run(&done);
auto *items = [delegate items];
- EXPECT_EQ(items.count, 3UL);
+ EXPECT_EQ(items.count, 2UL);
EXPECT_EQ(items[0].tokens.count, 1UL);
EXPECT_STREQ("heeey", items[0].tokens[0].content.UTF8String);
EXPECT_EQ(items[1].tokens.count, 1UL);
- EXPECT_STREQ("[]", items[1].tokens[0].content.UTF8String);
- EXPECT_EQ(items[2].tokens.count, 1UL);
- EXPECT_STREQ("woorld", items[2].tokens[0].content.UTF8String);
+ EXPECT_STREQ("woorld", items[1].tokens[0].content.UTF8String);
done = false;
[webView _completeTextManipulationForItems:@[
(_WKTextManipulationItem *)createItem(items[0].identifier, { { items[0].tokens[0].identifier, @"hello" } }),
- (_WKTextManipulationItem *)createItem(items[1].identifier, { { items[1].tokens[0].identifier, nil } }),
- (_WKTextManipulationItem *)createItem(items[2].identifier, { { items[2].tokens[0].identifier, @"world" } }),
+ (_WKTextManipulationItem *)createItem(items[1].identifier, { { items[1].tokens[0].identifier, @"world" } }),
] completion:^(NSArray<NSError *> *errors) {
EXPECT_EQ(errors, nil);
done = true;
@@ -1684,6 +1670,40 @@
EXPECT_WK_STREQ("<p>bar <strong>garply</strong> foo</p>", [webView stringByEvaluatingJavaScript:@"document.body.innerHTML"]);
}
+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="" href="" class=\"frame\"><div class=\"subframe\"></div></div>", [webView stringByEvaluatingJavaScript:@"document.body.innerHTML"]);
+}
+
TEST(TextManipulation, InsertingContentIntoAlreadyManipulatedContentDoesNotCreateTextManipulationItem)
{
auto delegate = adoptNS([[TextManipulationDelegate alloc] init]);