Commit fa583784 authored by Sunny's avatar Sunny Committed by Commit Bot

Align some set innerText behavior with standard

According to spec[1], when a string starts with CR/LF set as innerText,
the first child node should not be an empty text node. So we adjust the
logic in HTMLElement::TextToFragment to make sure it follows the spec.

Besides, same in spec[1], there is no special procedure for setting
innerText when the element is being rendered and styled as preserving
new line, so corresponding code was removed in this commit.

15 failed wpt tests could be passed after this change.

[1] https://html.spec.whatwg.org/multipage/dom.html#the-innertext-idl-attribute

Bug: 573309, 705478
Cq-Include-Trybots: luci.chromium.try:linux_layout_tests_layout_ng;luci.chromium.try:linux_layout_tests_slimming_paint_v2;master.tryserver.blink:linux_trusty_blink_rel
Change-Id: I6765601a14c2e95cec3841e084046da79aca065c
Reviewed-on: https://chromium-review.googlesource.com/c/1266728Reviewed-by: default avatarKent Tamura <tkent@chromium.org>
Commit-Queue: Kent Tamura <tkent@chromium.org>
Cr-Commit-Position: refs/heads/master@{#601480}
parent ffadc322
This is a testharness.js-based test. This is a testharness.js-based test.
Found 126 tests; 80 PASS, 46 FAIL, 0 TIMEOUT, 0 NOTRUN. Found 126 tests; 95 PASS, 31 FAIL, 0 TIMEOUT, 0 NOTRUN.
PASS Simplest possible test PASS Simplest possible test
PASS Simplest possible test, detached PASS Simplest possible test, detached
PASS Newlines convert to <br> in non-white-space:pre elements PASS Newlines convert to <br> in non-white-space:pre elements
PASS Newlines convert to <br> in non-white-space:pre elements, detached PASS Newlines convert to <br> in non-white-space:pre elements, detached
FAIL Newlines convert to <br> in <pre> element assert_equals: expected "abc<br>def" but got "abc\ndef" PASS Newlines convert to <br> in <pre> element
PASS Newlines convert to <br> in <pre> element, detached PASS Newlines convert to <br> in <pre> element, detached
FAIL Newlines convert to <br> in <textarea> element assert_equals: expected "abc<br>def" but got "abc\ndef" PASS Newlines convert to <br> in <textarea> element
PASS Newlines convert to <br> in <textarea> element, detached PASS Newlines convert to <br> in <textarea> element, detached
FAIL Newlines convert to <br> in white-space:pre element assert_equals: expected "abc<br>def" but got "abc\ndef" PASS Newlines convert to <br> in white-space:pre element
PASS Newlines convert to <br> in white-space:pre element, detached PASS Newlines convert to <br> in white-space:pre element, detached
PASS CRs convert to <br> in non-white-space:pre elements PASS CRs convert to <br> in non-white-space:pre elements
PASS CRs convert to <br> in non-white-space:pre elements, detached PASS CRs convert to <br> in non-white-space:pre elements, detached
FAIL CRs convert to <br> in <pre> element assert_equals: expected "abc<br>def" but got "abc\ndef" PASS CRs convert to <br> in <pre> element
PASS CRs convert to <br> in <pre> element, detached PASS CRs convert to <br> in <pre> element, detached
PASS Newline/CR pair converts to <br> in non-white-space:pre element PASS Newline/CR pair converts to <br> in non-white-space:pre element
PASS Newline/CR pair converts to <br> in non-white-space:pre element, detached PASS Newline/CR pair converts to <br> in non-white-space:pre element, detached
FAIL Newline/newline pair converts to two <br>s in non-white-space:pre element assert_not_equals: Should not have empty text nodes got disallowed value "" PASS Newline/newline pair converts to two <br>s in non-white-space:pre element
FAIL Newline/newline pair converts to two <br>s in non-white-space:pre element, detached assert_not_equals: Should not have empty text nodes got disallowed value "" PASS Newline/newline pair converts to two <br>s in non-white-space:pre element, detached
FAIL CR/CR pair converts to two <br>s in non-white-space:pre element assert_not_equals: Should not have empty text nodes got disallowed value "" PASS CR/CR pair converts to two <br>s in non-white-space:pre element
FAIL CR/CR pair converts to two <br>s in non-white-space:pre element, detached assert_not_equals: Should not have empty text nodes got disallowed value "" PASS CR/CR pair converts to two <br>s in non-white-space:pre element, detached
FAIL CRs convert to <br> in white-space:pre element assert_equals: expected "abc<br>def" but got "abc\ndef" PASS CRs convert to <br> in white-space:pre element
PASS CRs convert to <br> in white-space:pre element, detached PASS CRs convert to <br> in white-space:pre element, detached
PASS < preserved PASS < preserved
PASS < preserved, detached PASS < preserved, detached
...@@ -56,12 +56,12 @@ PASS Assigning null ...@@ -56,12 +56,12 @@ PASS Assigning null
PASS Assigning null, detached PASS Assigning null, detached
PASS Assigning undefined PASS Assigning undefined
PASS Assigning undefined, detached PASS Assigning undefined, detached
FAIL Start with CR assert_not_equals: Should not have empty text nodes got disallowed value "" PASS Start with CR
FAIL Start with CR, detached assert_not_equals: Should not have empty text nodes got disallowed value "" PASS Start with CR, detached
FAIL Start with LF assert_not_equals: Should not have empty text nodes got disallowed value "" PASS Start with LF
FAIL Start with LF, detached assert_not_equals: Should not have empty text nodes got disallowed value "" PASS Start with LF, detached
FAIL Start with CRLF assert_not_equals: Should not have empty text nodes got disallowed value "" PASS Start with CRLF
FAIL Start with CRLF, detached assert_not_equals: Should not have empty text nodes got disallowed value "" PASS Start with CRLF, detached
PASS End with CR PASS End with CR
PASS End with CR, detached PASS End with CR, detached
PASS End with LF PASS End with LF
......
...@@ -626,22 +626,26 @@ DocumentFragment* HTMLElement::TextToFragment(const String& text, ...@@ -626,22 +626,26 @@ DocumentFragment* HTMLElement::TextToFragment(const String& text,
break; break;
} }
fragment->AppendChild( if (i > start) {
Text::Create(GetDocument(), text.Substring(start, i - start)), fragment->AppendChild(
exception_state); Text::Create(GetDocument(), text.Substring(start, i - start)),
if (exception_state.HadException()) exception_state);
return nullptr;
if (c == '\r' || c == '\n') {
fragment->AppendChild(HTMLBRElement::Create(GetDocument()),
exception_state);
if (exception_state.HadException()) if (exception_state.HadException())
return nullptr; return nullptr;
// Make sure \r\n doesn't result in two line breaks.
if (c == '\r' && i + 1 < length && text[i + 1] == '\n')
i++;
} }
if (i == length)
break;
fragment->AppendChild(HTMLBRElement::Create(GetDocument()),
exception_state);
if (exception_state.HadException())
return nullptr;
// Make sure \r\n doesn't result in two line breaks.
if (c == '\r' && i + 1 < length && text[i + 1] == '\n')
i++;
start = i + 1; // Character after line break. start = i + 1; // Character after line break.
} }
...@@ -661,25 +665,6 @@ void HTMLElement::setInnerText(const String& text, ...@@ -661,25 +665,6 @@ void HTMLElement::setInnerText(const String& text,
return; return;
} }
// FIXME: Do we need to be able to detect preserveNewline style even when
// there's no layoutObject?
// FIXME: Can the layoutObject be out of date here? Do we need to call
// updateStyleIfNeeded? For example, for the contents of textarea elements
// that are display:none?
LayoutObject* r = GetLayoutObject();
if (r && r->Style()->PreserveNewline()) {
if (!text.Contains('\r')) {
ReplaceChildrenWithText(this, text, exception_state);
return;
}
String text_with_consistent_line_breaks = text;
text_with_consistent_line_breaks.Replace("\r\n", "\n");
text_with_consistent_line_breaks.Replace('\r', '\n');
ReplaceChildrenWithText(this, text_with_consistent_line_breaks,
exception_state);
return;
}
// Add text nodes and <br> elements. // Add text nodes and <br> elements.
DocumentFragment* fragment = TextToFragment(text, exception_state); DocumentFragment* fragment = TextToFragment(text, exception_state);
if (!exception_state.HadException()) if (!exception_state.HadException())
......
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