Commit 397365d2 authored by Yoshifumi Inoue's avatar Yoshifumi Inoue Committed by Chromium LUCI CQ

Avoid having LayoutText for Text::splitText()

This patch changes |Text:splitText()| to avoid having empty |LayoutText|
as other text modifying functions, e.g. deleteData(), replaceData() to
reduce changes that AX tree having AX objects with empty |LayoutText|.

Note: The root cause of issue 1149171 is |Range#insertNodes()| calling
|Text#splitText()| with offset 0.

Bug: 1149171
Change-Id: Ibfecb14111ff16f3901fe23cdba6d3fcae1ddda1
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/2623371
Commit-Queue: Kent Tamura <tkent@chromium.org>
Reviewed-by: default avatarKent Tamura <tkent@chromium.org>
Auto-Submit: Yoshifumi Inoue <yosin@chromium.org>
Cr-Commit-Position: refs/heads/master@{#842890}
parent 3627e79f
...@@ -124,8 +124,13 @@ Text* Text::splitText(unsigned offset, ExceptionState& exception_state) { ...@@ -124,8 +124,13 @@ Text* Text::splitText(unsigned offset, ExceptionState& exception_state) {
if (exception_state.HadException()) if (exception_state.HadException())
return nullptr; return nullptr;
if (GetLayoutObject()) if (GetLayoutObject()) {
GetLayoutObject()->SetTextWithOffset(DataImpl(), 0, old_str.length()); GetLayoutObject()->SetTextWithOffset(DataImpl(), 0, old_str.length());
if (data().IsEmpty()) {
// To avoid |LayoutText| has empty text, we rebuild layout tree.
SetForceReattachLayoutTree();
}
}
if (parentNode()) if (parentNode())
GetDocument().DidSplitTextNode(*this); GetDocument().DidSplitTextNode(*this);
......
...@@ -4,6 +4,7 @@ ...@@ -4,6 +4,7 @@
#include "third_party/blink/renderer/core/dom/text.h" #include "third_party/blink/renderer/core/dom/text.h"
#include "third_party/blink/renderer/bindings/core/v8/v8_binding_for_testing.h"
#include "third_party/blink/renderer/core/dom/range.h" #include "third_party/blink/renderer/core/dom/range.h"
#include "third_party/blink/renderer/core/editing/testing/editing_test_base.h" #include "third_party/blink/renderer/core/editing/testing/editing_test_base.h"
#include "third_party/blink/renderer/core/html/html_pre_element.h" #include "third_party/blink/renderer/core/html/html_pre_element.h"
...@@ -40,6 +41,22 @@ TEST_F(TextTest, RemoveFirstLetterPseudoElementWhenNoLetter) { ...@@ -40,6 +41,22 @@ TEST_F(TextTest, RemoveFirstLetterPseudoElementWhenNoLetter) {
EXPECT_FALSE(text->GetLayoutObject()->IsTextFragment()); EXPECT_FALSE(text->GetLayoutObject()->IsTextFragment());
} }
TEST_F(TextTest, splitTextToEmpty) {
V8TestingScope scope;
SetBodyContent("<p id=sample>ab</p>");
const Element& sample = *GetElementById("sample");
Text& text = *To<Text>(sample.firstChild());
// |new_text| is after |text|.
Text& new_text = *text.splitText(0, ASSERT_NO_EXCEPTION);
UpdateAllLifecyclePhasesForTest();
EXPECT_EQ("", text.data());
EXPECT_FALSE(text.GetLayoutObject());
EXPECT_EQ("ab", new_text.data());
EXPECT_TRUE(new_text.GetLayoutObject());
}
TEST_F(TextTest, TextLayoutObjectIsNeeded_CannotHaveChildren) { TEST_F(TextTest, TextLayoutObjectIsNeeded_CannotHaveChildren) {
SetBodyContent("<img id=image>"); SetBodyContent("<img id=image>");
UpdateAllLifecyclePhasesForTest(); UpdateAllLifecyclePhasesForTest();
......
...@@ -424,8 +424,8 @@ TEST_P(ParameterizedLayoutTextFragmentTest, SetTextWithFirstLetter) { ...@@ -424,8 +424,8 @@ TEST_P(ParameterizedLayoutTextFragmentTest, SetTextWithFirstLetter) {
->IsRemainingTextLayoutObject()); ->IsRemainingTextLayoutObject());
ASSERT_TRUE(letter_a.GetLayoutObject()->GetFirstLetterPart()); ASSERT_TRUE(letter_a.GetLayoutObject()->GetFirstLetterPart());
EXPECT_EQ("a", letter_a.GetLayoutObject()->GetFirstLetterPart()->GetText()); EXPECT_EQ("a", letter_a.GetLayoutObject()->GetFirstLetterPart()->GetText());
EXPECT_TRUE(letter_x.GetLayoutObject()->IsTextFragment()); EXPECT_FALSE(letter_x.GetLayoutObject())
EXPECT_FALSE(letter_x.GetLayoutObject()->GetFirstLetterPart()); << "We don't have layout text for empty Text node.";
// Make <div>"x" "a"</div> // Make <div>"x" "a"</div>
letter_x.setTextContent("x"); letter_x.setTextContent("x");
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment