Title: [154252] trunk
Revision
154252
Author
[email protected]
Date
2013-08-18 11:54:12 -0700 (Sun, 18 Aug 2013)

Log Message

<https://webkit.org/b/119917> Pasting multiple lines into a textarea can introduce extra new lines

Reviewed by Darin Adler.

Source/WebCore:

Inspired by https://chromium.googlesource.com/chromium/blink/+/6152a12f7ace27beea4d284ff8416631e8aa5217.

The bug was caused by createFragmentFromText's falsely assuming that the newline were not preserved
if the first node's renderer didn't exist.

Fixed the bug by obtaining the renderer of the container of the first visible position in the context.

Test: editing/pasteboard/paste-into-textarea-with-new-line.html

* editing/markup.cpp:
(WebCore::contextPreservesNewline):
(WebCore::createFragmentFromText):

LayoutTests:

Add a regression test.

* editing/pasteboard/paste-into-textarea-with-new-line-expected.txt: Added.
* editing/pasteboard/paste-into-textarea-with-new-line.html: Added.

Modified Paths

Added Paths

Diff

Modified: trunk/LayoutTests/ChangeLog (154251 => 154252)


--- trunk/LayoutTests/ChangeLog	2013-08-18 18:44:21 UTC (rev 154251)
+++ trunk/LayoutTests/ChangeLog	2013-08-18 18:54:12 UTC (rev 154252)
@@ -1,3 +1,14 @@
+2013-08-18  Ryosuke Niwa  <[email protected]>
+
+        <https://webkit.org/b/119917> Pasting multiple lines into a textarea can introduce extra new lines
+
+        Reviewed by Darin Adler.
+
+        Add a regression test.
+
+        * editing/pasteboard/paste-into-textarea-with-new-line-expected.txt: Added.
+        * editing/pasteboard/paste-into-textarea-with-new-line.html: Added.
+
 2013-08-18  Antti Koivisto  <[email protected]>
 
         <https://webkit.org/b/119963> Use TextNodeTraversal for getting sheet text in StyleElement

Added: trunk/LayoutTests/editing/pasteboard/paste-into-textarea-with-new-line-expected.txt (0 => 154252)


--- trunk/LayoutTests/editing/pasteboard/paste-into-textarea-with-new-line-expected.txt	                        (rev 0)
+++ trunk/LayoutTests/editing/pasteboard/paste-into-textarea-with-new-line-expected.txt	2013-08-18 18:54:12 UTC (rev 154252)
@@ -0,0 +1,10 @@
+This tests pasting a multi-line text into a textarea that contains a single new line. WebKit should preserve the number of new lines.
+
+On success, you will see a series of "PASS" messages, followed by "TEST COMPLETE".
+
+
+PASS document.execCommand('InsertHTML', false, 'first<br><br>third'); textarea.value is 'first\n\nthird\n'
+PASS successfullyParsed is true
+
+TEST COMPLETE
+

Added: trunk/LayoutTests/editing/pasteboard/paste-into-textarea-with-new-line.html (0 => 154252)


--- trunk/LayoutTests/editing/pasteboard/paste-into-textarea-with-new-line.html	                        (rev 0)
+++ trunk/LayoutTests/editing/pasteboard/paste-into-textarea-with-new-line.html	2013-08-18 18:54:12 UTC (rev 154252)
@@ -0,0 +1,21 @@
+<!DOCTYPE html>
+<html>
+<body>
+<script src=""
+<script>
+
+description("This tests pasting a multi-line text into a textarea that contains a single new line. WebKit should preserve the number of new lines.");
+
+var textarea = document.createElement("textarea");
+textarea.value = "\n";
+textarea.rows = 10;
+document.body.appendChild(textarea);
+textarea.focus();
+shouldBe("document.execCommand('InsertHTML', false, 'first<br><br>third'); textarea.value", "'first\\n\\nthird\\n'");
+
+var successfullyParsed = true;
+
+</script>
+<script src=""
+</body>
+</html>

Modified: trunk/Source/WebCore/ChangeLog (154251 => 154252)


--- trunk/Source/WebCore/ChangeLog	2013-08-18 18:44:21 UTC (rev 154251)
+++ trunk/Source/WebCore/ChangeLog	2013-08-18 18:54:12 UTC (rev 154252)
@@ -1,3 +1,22 @@
+2013-08-18  Ryosuke Niwa  <[email protected]>
+
+        <https://webkit.org/b/119917> Pasting multiple lines into a textarea can introduce extra new lines
+
+        Reviewed by Darin Adler.
+
+        Inspired by https://chromium.googlesource.com/chromium/blink/+/6152a12f7ace27beea4d284ff8416631e8aa5217.
+
+        The bug was caused by createFragmentFromText's falsely assuming that the newline were not preserved
+        if the first node's renderer didn't exist.
+
+        Fixed the bug by obtaining the renderer of the container of the first visible position in the context.
+
+        Test: editing/pasteboard/paste-into-textarea-with-new-line.html
+
+        * editing/markup.cpp:
+        (WebCore::contextPreservesNewline):
+        (WebCore::createFragmentFromText):
+
 2013-08-18  Andreas Kling  <[email protected]>
 
         <https://webkit.org/b/119983> Add two missing RefPtr::release() in HTMLLinkElement.

Modified: trunk/Source/WebCore/editing/markup.cpp (154251 => 154252)


--- trunk/Source/WebCore/editing/markup.cpp	2013-08-18 18:44:21 UTC (rev 154251)
+++ trunk/Source/WebCore/editing/markup.cpp	2013-08-18 18:54:12 UTC (rev 154252)
@@ -823,19 +823,22 @@
     return (node->childNodeCount() == 2 && isTabSpanTextNode(node->firstChild()->firstChild()) && node->firstChild()->nextSibling()->isTextNode());
 }
 
+static bool contextPreservesNewline(const Range& context)
+{
+    VisiblePosition position(context.startPosition());
+    Node* container = position.deepEquivalent().containerNode();
+    if (!container || !container->renderer())
+        return false;
+
+    return container->renderer()->style()->preserveNewline();
+}
+
 PassRefPtr<DocumentFragment> createFragmentFromText(Range* context, const String& text)
 {
     if (!context)
         return 0;
 
-    Node* styleNode = context->firstNode();
-    if (!styleNode) {
-        styleNode = context->startPosition().deprecatedNode();
-        if (!styleNode)
-            return 0;
-    }
-
-    Document* document = styleNode->document();
+    Document* document = context->ownerDocument();
     RefPtr<DocumentFragment> fragment = document->createDocumentFragment();
     
     if (text.isEmpty())
@@ -845,8 +848,7 @@
     string.replace("\r\n", "\n");
     string.replace('\r', '\n');
 
-    RenderObject* renderer = styleNode->renderer();
-    if (renderer && renderer->style()->preserveNewline()) {
+    if (contextPreservesNewline(*context)) {
         fragment->appendChild(document->createTextNode(string), ASSERT_NO_EXCEPTION);
         if (string.endsWith('\n')) {
             RefPtr<Element> element = createBreakElement(document);
_______________________________________________
webkit-changes mailing list
[email protected]
https://lists.webkit.org/mailman/listinfo/webkit-changes

Reply via email to