Bug 1151873 - Stop forcing text/plain-only content being copied to the clipboard when an ancestor of the selected node has significant whitespace. r=roc, a=sledru
authorEhsan Akhgari <ehsan@mozilla.com>
Wed, 08 Apr 2015 21:24:56 -0400
changeset 258527 7e31d76c4d7b
parent 258526 125ec6c54576
child 258528 38e095acde46
push id4690
push userryanvm@gmail.com
push date2015-04-20 16:04 +0000
treeherdermozilla-beta@eb5e2063637b [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewersroc, sledru
bugs1151873, 116083
milestone38.0
Bug 1151873 - Stop forcing text/plain-only content being copied to the clipboard when an ancestor of the selected node has significant whitespace. r=roc, a=sledru When fixing bug 116083, I didn't understand the purpose of the original code correctly. Setting mIsTextWidget to true will force a text/plain MIME type, and therefore disabling the copying of any HTML content. I don't know why this path in the code used to exist, but it is basically the cause of this bug, so we should remove it.
dom/base/nsDocumentEncoder.cpp
dom/base/test/test_bug116083.html
--- a/dom/base/nsDocumentEncoder.cpp
+++ b/dom/base/nsDocumentEncoder.cpp
@@ -1380,29 +1380,16 @@ nsHTMLCopyEncoder::SetSelection(nsISelec
     // checking for selection inside a plaintext form widget
     nsIAtom *atom = selContent->Tag();
     if (atom == nsGkAtoms::input ||
         atom == nsGkAtoms::textarea)
     {
       mIsTextWidget = true;
       break;
     }
-    else if (selContent->IsElement()) {
-      nsRefPtr<nsStyleContext> styleContext =
-        nsComputedDOMStyle::GetStyleContextForElementNoFlush(selContent->AsElement(),
-                                                             nullptr, nullptr);
-      if (styleContext) {
-        const nsStyleText* textStyle = styleContext->StyleText();
-        if (textStyle->WhiteSpaceOrNewlineIsSignificant()) {
-          // Copy as plaintext for all preformatted elements
-          mIsTextWidget = true;
-        }
-        break;
-      }
-    }
   }
 
   // normalize selection if we are not in a widget
   if (mIsTextWidget) 
   {
     mSelection = aSelection;
     mMimeType.AssignLiteral("text/plain");
     return NS_OK;
--- a/dom/base/test/test_bug116083.html
+++ b/dom/base/test/test_bug116083.html
@@ -27,31 +27,47 @@ https://bugzilla.mozilla.org/show_bug.cg
 <div data-result="foo  &#10;  bar&#10;&#10;!&#10;&#10;&#10;baz" style="white-space: pre-line"><div>foo  </div><div>  bar</div><div><br></div><div>!</div><div><br><br></div><div>baz</div></div>
 <div data-result="foo &#10; bar&#10;&#10;!&#10;&#10;&#10;baz" style="white-space: pre-line" contenteditable><div>foo </div><div> bar</div><div><br></div><div>!</div><div><br><br></div><div>baz</div></div>
 <div data-result="foo  &#10;  bar&#10;&#10;!&#10;&#10;&#10;baz" style="white-space: -moz-pre-space"><div>foo  </div><div>  bar</div><div><br></div><div>!</div><div><br><br></div><div>baz</div></div>
 <div data-result="foo &#10; bar&#10;&#10;!&#10;&#10;&#10;baz" style="white-space: -moz-pre-space" contenteditable><div>foo </div><div> bar</div><div><br></div><div>!</div><div><br><br></div><div>baz</div></div>
 <div data-result="&#10;foo bar&#10;">foo  bar</div>
 </div>
 <script type="application/javascript">
 
+const Cc = SpecialPowers.Cc;
+const Ci = SpecialPowers.Ci;
+
+function hasExpectedFlavors() {
+  var flavors = [
+    "text/plain",
+    "text/html",
+    "application/x-moz-nativehtml",
+  ];
+  var cb = Cc["@mozilla.org/widget/clipboard;1"].
+           getService(Ci.nsIClipboard);
+  return cb.hasDataMatchingFlavors(flavors, flavors.length,
+                                   cb.kGlobalClipboard);
+}
+
 SimpleTest.waitForExplicitFinish();
 SimpleTest.waitForFocus(function nextTest() {
   var div = document.querySelector("#content>div");
   if (!div) {
     SimpleTest.finish();
     return;
   }
   getSelection().selectAllChildren(div);
   var expected = div.hasAttribute("data-result") ?
                  div.getAttribute("data-result") :
                  div.textContent;
   SimpleTest.waitForClipboard(expected, function() {
     synthesizeKey("C", {accelKey: true});
   }, function() {
     ok(true, div.getAttribute("style") + " passed");
+    ok(hasExpectedFlavors(), "The clipboard has the expected flavors");
     div.parentNode.removeChild(div);
     nextTest();
   }, function() {
     ok(false, "failed to copy the expected content to the clipboard");
     SimpleTest.finish();
   });
 });