Commit 52d55d36 authored by Yoshifumi Inoue's avatar Yoshifumi Inoue Committed by Commit Bot

Make InsertText command not to split SPAN element containing TAB character

Before this patch, "InsertText" command splits SPAN element containing TAB
character when inserting text into it.

For example:
Before: <span>&#9;a|c</span>
Command: insertText "B"
After: <span>&#9;a</span>B|<span>c</span>

This behavior is introduced by the patch[1], which replaces
"class=apple-tab-span" to "style=white-space:pre".

This patch changes |IsTabHTMLSpanElement()| to check "white-space:pre" CSS
property to make "InsertText" command not to split SPAN element containing
TAB character with "white-space: pre".


Note: "apple-tab-span" CSS class is MacOS specific feature[2]. On MacOS,
UITextView puts following HTML into pasteboard:

<style>.Apple-Tab-Span { white-space: pre; }</style>
<span class=Apple-Tab-Span>&#9;</span>

[1] http://crrev.com/2718543003 Remove EditingAppleTabSpan class handling
[2] https://www.cocoanetics.com/2013/06/apple-tab-span/

Bug: 741826
Change-Id: I42d8d388e306b613fbe8ffa614b9e974db254c22
Reviewed-on: https://chromium-review.googlesource.com/697044
Commit-Queue: Yoshifumi Inoue <yosin@chromium.org>
Reviewed-by: default avatarXiaocheng Hu <xiaochengh@chromium.org>
Cr-Commit-Position: refs/heads/master@{#506683}
parent 4a3dd816
......@@ -1559,12 +1559,14 @@ HTMLElement* CreateHTMLElement(Document& document, const QualifiedName& name) {
}
bool IsTabHTMLSpanElement(const Node* node) {
if (!IsHTMLSpanElement(node) || !node->firstChild())
if (!IsHTMLSpanElement(node))
return false;
if (node->firstChild()->IsCharacterDataNode() &&
ToCharacterData(node->firstChild())->data().Contains('\t'))
return true;
return false;
const Node* const first_child = NodeTraversal::FirstChild(*node);
if (!first_child || !first_child->IsTextNode())
return false;
if (!ToText(first_child)->data().Contains('\t'))
return false;
return node->GetComputedStyle()->WhiteSpace() == EWhiteSpace::kPre;
}
bool IsTabHTMLSpanElementTextNode(const Node* node) {
......
......@@ -32,4 +32,75 @@ TEST_F(InsertTextCommandTest, WithTypingStyle) {
"without applying typing style.";
}
// http://crbug.com/741826
TEST_F(InsertTextCommandTest, InsertChar) {
Selection().SetSelection(
SetSelectionTextToBody("<p contenteditable><span>\ta|c</span></p>"));
GetDocument().execCommand("insertText", false, "B", ASSERT_NO_EXCEPTION);
EXPECT_EQ("<p contenteditable><span>\taB|c</span></p>",
GetSelectionTextFromBody(Selection().GetSelectionInDOMTree()))
<< "We should not split Text node";
}
// http://crbug.com/741826
TEST_F(InsertTextCommandTest, InsertCharToWhiteSpacePre) {
Selection().SetSelection(SetSelectionTextToBody(
"<p contenteditable><span style='white-space:pre'>\ta|c</span></p>"));
GetDocument().execCommand("insertText", false, "B", ASSERT_NO_EXCEPTION);
EXPECT_EQ(
"<p contenteditable>"
"<span style=\"white-space:pre\">\ta</span>"
"B|"
"<span style=\"white-space:pre\">c</span>"
"</p>",
GetSelectionTextFromBody(Selection().GetSelectionInDOMTree()))
<< "This is a just record current behavior. We should not split SPAN.";
}
// http://crbug.com/741826
TEST_F(InsertTextCommandTest, InsertSpace) {
Selection().SetSelection(
SetSelectionTextToBody("<p contenteditable><span>\ta|c</span></p>"));
GetDocument().execCommand("insertText", false, " ", ASSERT_NO_EXCEPTION);
EXPECT_EQ("<p contenteditable><span>\ta\xC2\xA0 |c</span></p>",
GetSelectionTextFromBody(Selection().GetSelectionInDOMTree()))
<< "We should insert U+0020 without splitting SPAN";
}
// http://crbug.com/741826
TEST_F(InsertTextCommandTest, InsertSpaceToWhiteSpacePre) {
Selection().SetSelection(SetSelectionTextToBody(
"<p contenteditable><span style='white-space:pre'>\ta|c</span></p>"));
GetDocument().execCommand("insertText", false, " ", ASSERT_NO_EXCEPTION);
EXPECT_EQ(
"<p contenteditable>"
"<span style=\"white-space:pre\">\ta</span>"
"\xC2\xA0\xC2\xA0|"
"<span style=\"white-space:pre\">c</span></p>",
GetSelectionTextFromBody(Selection().GetSelectionInDOMTree()))
<< "We should insert U+0020 without splitting SPAN";
}
// http://crbug.com/741826
TEST_F(InsertTextCommandTest, InsertTab) {
Selection().SetSelection(
SetSelectionTextToBody("<p contenteditable><span>\ta|c</span></p>"));
GetDocument().execCommand("insertText", false, "\t", ASSERT_NO_EXCEPTION);
EXPECT_EQ(
"<p contenteditable>"
"<span>\ta<span style=\"white-space:pre\">\t|</span>c</span>"
"</p>",
GetSelectionTextFromBody(Selection().GetSelectionInDOMTree()));
}
// http://crbug.com/741826
TEST_F(InsertTextCommandTest, InsertTabToWhiteSpacePre) {
Selection().SetSelection(SetSelectionTextToBody(
"<p contenteditable><span style='white-space:pre'>\ta|c</span></p>"));
GetDocument().execCommand("insertText", false, "\t", ASSERT_NO_EXCEPTION);
EXPECT_EQ(
"<p contenteditable><span style=\"white-space:pre\">\ta\t|c</span></p>",
GetSelectionTextFromBody(Selection().GetSelectionInDOMTree()));
}
} // namespace blink
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