Bug 1214377 - Hack to solve Thunderbird's need to detect style pre-wrap. rs=ehsan a=rkent THUNDERBIRD_38_VERBRANCH
authorJorg K <mozilla@jorgk.com>
Thu, 26 Nov 2015 09:21:00 +0100
branchTHUNDERBIRD_38_VERBRANCH
changeset 261030 8cc8fa60f0f6c3fc8cdb31af8575e53069c90fe5
parent 261029 a0390f2afafd142ad6ebf85e4d5f1aed17cff57d
child 261031 81ba3d3afe08034a086f0fd9636d32cb44c3747f
push id321
push userkent@caspia.com
push dateFri, 29 Apr 2016 21:03:17 +0000
treeherdermozilla-esr38@8cc8fa60f0f6 [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewersehsan, rkent
bugs1214377
milestone38.8.0esrpre
Bug 1214377 - Hack to solve Thunderbird's need to detect style pre-wrap. rs=ehsan a=rkent
dom/base/nsDocumentEncoder.cpp
--- a/dom/base/nsDocumentEncoder.cpp
+++ b/dom/base/nsDocumentEncoder.cpp
@@ -1439,28 +1439,54 @@ nsHTMLCopyEncoder::SetSelection(nsISelec
     nsIAtom *atom = selContent->Tag();
     if (atom == nsGkAtoms::input ||
         atom == nsGkAtoms::textarea)
     {
       mIsTextWidget = true;
       break;
     }
 #ifdef MOZ_THUNDERBIRD
-    else if (selContent->IsElement()) {
-      RefPtr<nsStyleContext> styleContext =
-        nsComputedDOMStyle::GetStyleContextForElementNoFlush(
-          selContent->AsElement(), nullptr, nullptr);
-      if (styleContext) {
-        const nsStyleText* textStyle = styleContext->StyleText();
-        if (textStyle->mWhiteSpace == NS_STYLE_WHITESPACE_PRE_WRAP) {
-          mIsTextWidget = true;
-        }
+    else if (selContent->IsHTMLElement(nsGkAtoms::body)) {
+      // Currently, setting mIsTextWidget to 'true' will result in the selection
+      // being encoded/copied as pre-formatted plain text.
+      // This is fine for copying pre-formatted plain text with Firefox, it is
+      // already not correct for copying pre-formatted "rich" text (bold, colour)
+      // with Firefox. As long as the serialisers aren't fixed, copying
+      // pre-formatted text in Firefox is broken. If we set mIsTextWidget,
+      // pre-formatted plain text is copied, but pre-formatted "rich" text loses
+      // the "rich" formatting. If we don't set mIsTextWidget, "rich" text
+      // attributes aren't lost, but white-space is lost.
+      // So far the story for Firefox.
+      //
+      // Thunderbird has two *conflicting* requirements.
+      // Case 1:
+      // When selecting and copying text, even pre-formatted text, as a quote
+      // to be placed into a reply, we *always* expect HTML to be copied.
+      // Case 2:
+      // When copying text in a so-called "plain text" message, that is
+      // one where the body carries style "white-space:pre-wrap", the text should
+      // be copied as pre-formatted plain text.
+      //
+      // Therefore the following code checks for "pre-wrap" on the body.
+      // This is a terrible hack.
+      //
+      // The proper fix would be this:
+      // For case 1:
+      // Communicate the fact that HTML is required to EncodeToString(),
+      // bug 1141786.
+      // For case 2:
+      // Wait for Firefox to get fixed to detect pre-formatting correctly,
+      // bug 1174452.
+      nsAutoString styleVal;
+      if (selContent->GetAttr(kNameSpaceID_None, nsGkAtoms::style, styleVal) &&
+          styleVal.Find(NS_LITERAL_STRING("pre-wrap")) != kNotFound) {
+        mIsTextWidget = true;
+        break;
       }
     }
-    break;
 #endif
   }
 
   // normalize selection if we are not in a widget
   if (mIsTextWidget) 
   {
     mSelection = aSelection;
     mMimeType.AssignLiteral("text/plain");