2011-05-12 Ryosuke Niwa <rniwa@webkit.org>
authorrniwa@webkit.org <rniwa@webkit.org@268f45cc-cd09-0410-ab3c-d52691b4dbfc>
Thu, 12 May 2011 18:21:57 +0000 (18:21 +0000)
committerrniwa@webkit.org <rniwa@webkit.org@268f45cc-cd09-0410-ab3c-d52691b4dbfc>
Thu, 12 May 2011 18:21:57 +0000 (18:21 +0000)
        Reviewed by Darin Adler.

        WebKit inserts base, link, meta, style, and title elements into an editable region
        when pasting table cells from Excel
        https://bugs.webkit.org/show_bug.cgi?id=60644

        Added a test to ensure WebKit strips the said elements prior to inserting HTML.

        * editing/pasteboard/paste-head-contents-expected.txt: Added.
        * editing/pasteboard/paste-head-contents.html: Added.
2011-05-12  Ryosuke Niwa  <rniwa@webkit.org>

        Reviewed by Darin Adler.

        WebKit inserts base, link, meta, style, and title elements into an editable region
        when pasting table cells from Excel
        https://bugs.webkit.org/show_bug.cgi?id=60644

        Strip base, link, meta, style, and tile elements from the fragment to be pasted prior
        to the merge. We don't have to strip these elements in performTrivialReplace because
        the function only handles a fragment that consists of exactly one text node.

        Test: editing/pasteboard/paste-head-contents.html

        * editing/ReplaceSelectionCommand.cpp:
        (WebCore::removeHeadContents): Added.
        (WebCore::ReplaceSelectionCommand::doApply): Calls removeHeadContents before handing
        style spans.

git-svn-id: https://svn.webkit.org/repository/webkit/trunk@86360 268f45cc-cd09-0410-ab3c-d52691b4dbfc

LayoutTests/ChangeLog
LayoutTests/editing/pasteboard/paste-head-contents-expected.txt [new file with mode: 0644]
LayoutTests/editing/pasteboard/paste-head-contents.html [new file with mode: 0644]
Source/WebCore/ChangeLog
Source/WebCore/editing/ReplaceSelectionCommand.cpp

index 3e481d6..293a8c0 100644 (file)
@@ -1,3 +1,16 @@
+2011-05-12  Ryosuke Niwa  <rniwa@webkit.org>
+
+        Reviewed by Darin Adler.
+
+        WebKit inserts base, link, meta, style, and title elements into an editable region
+        when pasting table cells from Excel
+        https://bugs.webkit.org/show_bug.cgi?id=60644
+
+        Added a test to ensure WebKit strips the said elements prior to inserting HTML.
+
+        * editing/pasteboard/paste-head-contents-expected.txt: Added.
+        * editing/pasteboard/paste-head-contents.html: Added.
+
 2011-05-08 Abhishek Arya <inferno@chromium.org> and Carol Szabo  <carol@webkit.org>
 
         Reviewed by David Hyatt.
diff --git a/LayoutTests/editing/pasteboard/paste-head-contents-expected.txt b/LayoutTests/editing/pasteboard/paste-head-contents-expected.txt
new file mode 100644 (file)
index 0000000..3e123b6
--- /dev/null
@@ -0,0 +1,4 @@
+This test ensures WebKit strips away base, link, meta, style, and title elements before inserting HTML.
+
+PASS
+
diff --git a/LayoutTests/editing/pasteboard/paste-head-contents.html b/LayoutTests/editing/pasteboard/paste-head-contents.html
new file mode 100644 (file)
index 0000000..cde8462
--- /dev/null
@@ -0,0 +1,102 @@
+<!DOCTYPE html>
+<html>
+<body>
+<script type="data:text/html">
+<html xmlns:v="urn:schemas-microsoft-com:vml"
+xmlns:o="urn:schemas-microsoft-com:office:office"
+xmlns:x="urn:schemas-microsoft-com:office:excel"
+xmlns="http://www.w3.org/TR/REC-html40">
+<head>
+<meta http-equiv=Content-Type content="text/html; charset=utf-8">
+<meta name=ProgId content=Excel.Sheet>
+<meta name=Generator content="Microsoft Excel 14">
+<link id=Main-File rel=Main-File href="dummy_path">
+<link rel=File-List href="dummy_path2">
+<base href="dummy-base">
+<title>Dummy Title</title>
+<style>
+<!--table
+       {mso-displayed-decimal-separator:"\.";
+       mso-displayed-thousand-separator:"\,";}
+@page
+       {margin:1.0in .75in 1.0in .75in;
+       mso-header-margin:.5in;
+       mso-footer-margin:.5in;}
+td
+       {padding-top:1px;
+       padding-right:1px;
+       padding-left:1px;
+       mso-ignore:padding;
+       color:black;
+       font-size:12.0pt;
+       font-weight:400;
+       font-style:normal;
+       text-decoration:none;
+       font-family:Calibri, sans-serif;
+       mso-font-charset:0;
+       mso-number-format:General;
+       text-align:general;
+       vertical-align:bottom;
+       border:none;
+       mso-background-source:auto;
+       mso-pattern:auto;
+       mso-protection:locked visible;
+       white-space:nowrap;
+       mso-rotate:0;}
+-->
+</style>
+</head>
+<body link=blue vlink=purple>
+<table border=0 cellpadding=0 cellspacing=0 width=130 style='border-collapse:
+ collapse;width:130pt'>
+<!--StartFragment-->
+ <col width=65 span=2 style='width:65pt'>
+ <tr height=15 style='height:15.0pt'>
+  <td height=15 width=65 style='height:15.0pt;width:65pt'>hello</td>
+  <td width=65 style='width:65pt'>world</td>
+ </tr>
+ <tr height=15 style='height:15.0pt'>
+  <td height=15 style='height:15.0pt'></td>
+  <td>webkit</td>
+ </tr>
+<!--EndFragment-->
+</table>
+</body>
+</html>
+</script>
+<p>This test ensures WebKit strips away base, link, meta, style, and title elements before inserting HTML.</p>
+<div id="test" contenteditable></div>
+<pre><script type="text/javascript">
+
+var htmlInPasteboard = document.getElementsByTagName('script')[0].firstChild.textContent;
+document.getElementById('test').focus();
+document.execCommand('InsertHTML', false, htmlInPasteboard);
+
+var passed = true;
+function expectNoInstanceOf(elementName) {
+    var elements = document.body.getElementsByTagName(elementName);
+    if (elements.length <= 0)
+        return;
+
+    document.write('FAIL - found ' + elements.length + ' ');
+    document.write(elements.length == 1 ? 'instance' : 'instances');
+    document.writeln(' of ' + elementName + ' element');
+    passed = false;
+}
+
+if (window.layoutTestController)
+    layoutTestController.dumpAsText();
+
+expectNoInstanceOf('base');
+expectNoInstanceOf('meta');
+expectNoInstanceOf('link');
+expectNoInstanceOf('title');
+expectNoInstanceOf('style');
+if (passed)
+    document.writeln('PASS');
+
+document.getElementById('test').innerHTML = '';
+
+</script></pre>
+</body>
+</html>
\ No newline at end of file
index 77d666c..186e405 100644 (file)
@@ -1,3 +1,22 @@
+2011-05-12  Ryosuke Niwa  <rniwa@webkit.org>
+
+        Reviewed by Darin Adler.
+
+        WebKit inserts base, link, meta, style, and title elements into an editable region
+        when pasting table cells from Excel
+        https://bugs.webkit.org/show_bug.cgi?id=60644
+
+        Strip base, link, meta, style, and tile elements from the fragment to be pasted prior
+        to the merge. We don't have to strip these elements in performTrivialReplace because
+        the function only handles a fragment that consists of exactly one text node.
+
+        Test: editing/pasteboard/paste-head-contents.html
+
+        * editing/ReplaceSelectionCommand.cpp:
+        (WebCore::removeHeadContents): Added.
+        (WebCore::ReplaceSelectionCommand::doApply): Calls removeHeadContents before handing
+        style spans.
+
 2011-05-12  Levi Weintraub  <leviw@chromium.org>
 
         Reviewed by Simon Fraser.
index 0810a1e..31caa9c 100644 (file)
@@ -551,6 +551,22 @@ VisiblePosition ReplaceSelectionCommand::positionAtStartOfInsertedContent()
     return VisiblePosition(nextCandidate(positionInParentBeforeNode(m_firstNodeInserted.get())));
 }
 
+static void removeHeadContents(ReplacementFragment& fragment)
+{
+    Node* next = 0;
+    for (Node* node = fragment.firstChild(); node; node = next) {
+        if (node->hasTagName(baseTag)
+            || node->hasTagName(linkTag)
+            || node->hasTagName(metaTag)
+            || node->hasTagName(styleTag)
+            || node->hasTagName(titleTag)) {
+            next = node->traverseNextSibling();
+            fragment.removeNode(node);
+        } else
+            next = node->traverseNextNode();
+    }
+}
+
 // Remove style spans before insertion if they are unnecessary.  It's faster because we'll 
 // avoid doing a layout.
 static bool handleStyleSpansBeforeInsertion(ReplacementFragment& fragment, const Position& insertionPos)
@@ -937,7 +953,9 @@ void ReplaceSelectionCommand::doApply()
     // any work performed after this that queries or uses the typing style.
     if (Frame* frame = document()->frame())
         frame->selection()->clearTypingStyle();
-    
+
+    removeHeadContents(fragment);
+
     bool handledStyleSpans = handleStyleSpansBeforeInsertion(fragment, insertionPos);
 
     // We don't want the destination to end up inside nodes that weren't selected.  To avoid that, we move the