Bug 737014 - Plaintext signatures are not received properly due to lack of BR nodes. r+a=bienvenu.
authorMike Conley <mconley@mozilla.com>
Tue, 20 Mar 2012 12:17:21 -0400
changeset 11123 62d1ea4c543ccee291d168f88cb9b8b8d247d81a
parent 11122 eb3c266e224e47e5dad6ae7dc868886047447740
child 11124 c87d0eee72a8debe0c306872a4ca41574fb12e6c
push id463
push userbugzilla@standard8.plus.com
push dateTue, 24 Apr 2012 17:34:51 +0000
treeherdercomm-beta@e53588e8f7b0 [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
bugs737014
Bug 737014 - Plaintext signatures are not received properly due to lack of BR nodes. r+a=bienvenu.
mail/test/mozmill/composition/test-signature-updating.js
mailnews/compose/src/nsMsgCompose.cpp
--- a/mail/test/mozmill/composition/test-signature-updating.js
+++ b/mail/test/mozmill/composition/test-signature-updating.js
@@ -105,52 +105,64 @@ function plaintextComposeWindowSwitchSig
   node = node.previousSibling;
   assert_equals(node.localName, "br");
   node = node.previousSibling;
 
   // Now we should have the DIV node that contains the signature, with
   // the class moz-signature.
   assert_equals(node.localName, "div");
 
-  const sigClass = "moz-signature";
-  assert_equals(node.className, sigClass);
+  const kSeperator = "-- ";
+  const kSigClass = "moz-signature";
+  assert_equals(node.className, kSigClass);
 
-  let expectedText = "Tinderbox is soo 90ies\n";
+  let sigNode = node.firstChild;
 
-  if (!suppressSigSep)
-    expectedText = "-- \n" + expectedText;
+  if (!suppressSigSep) {
+    assert_equals(sigNode.textContent, kSeperator);
+    let brNode = sigNode.nextSibling;
+    assert_equals(brNode.localName, "br");
+    sigNode = brNode.nextSibling;
+  }
 
-  assert_equals(node.textContent, expectedText);
+  let expectedText = "Tinderbox is soo 90ies";
+  assert_equals(sigNode.textContent, expectedText);
 
   // Now switch identities!
   let menuID = cwc.e("msgIdentity");
   menuID.value = "id2";
   menuID.click();
 
   node = contentFrame.contentDocument.body.lastChild;
 
   // In plaintext compose, the signature is followed by two <br> elements.
   assert_equals(node.localName, "br");
   node = node.previousSibling;
   assert_equals(node.localName, "br");
   node = node.previousSibling;
 
   assert_equals(node.localName, "div");
-  assert_equals(node.className, sigClass);
+  assert_equals(node.className, kSigClass);
 
-  expectedText = "Tinderboxpushlog is the new *hotness!*\n";
+  sigNode = node.firstChild;
 
-  if (!suppressSigSep)
-    expectedText = "-- \n" + expectedText;
+  if (!suppressSigSep) {
+    expectedText = "-- ";
+    assert_equals(sigNode.textContent, kSeperator);
+    let brNode = sigNode.nextSibling;
+    assert_equals(brNode.localName, "br");
+    sigNode = brNode.nextSibling;
+  }
 
-  assert_equals(node.textContent, expectedText);
+  expectedText = "Tinderboxpushlog is the new *hotness!*";
+  assert_equals(sigNode.textContent, expectedText);
 
   // Now check that the original signature has been removed by ensuring
   // that there's only one node with class moz-signature.
-  let sigs = contentFrame.contentDocument.querySelectorAll("." + sigClass);
+  let sigs = contentFrame.contentDocument.querySelectorAll("." + kSigClass);
   assert_equals(sigs.length, 1);
 
   // And ensure that the text we wrote wasn't altered
   let bodyFirstChild =  contentFrame.contentDocument.body.firstChild;
 
   while (node != bodyFirstChild)
     node = node.previousSibling;
 
--- a/mailnews/compose/src/nsMsgCompose.cpp
+++ b/mailnews/compose/src/nsMsgCompose.cpp
@@ -42,16 +42,17 @@
  *
  * ***** END LICENSE BLOCK ***** */
 
 #include "nsMsgCompose.h"
 #include "nsIScriptGlobalObject.h"
 #include "nsIScriptContext.h"
 #include "nsIDOMNode.h"
 #include "nsIDOMNodeList.h"
+#include "nsIDOMText.h"
 #include "nsIDOMHTMLImageElement.h"
 #include "nsIDOMHTMLLinkElement.h"
 #include "nsIDOMHTMLAnchorElement.h"
 #include "nsPIDOMWindow.h"
 #include "nsISelectionController.h"
 #include "nsIDOMNamedNodeMap.h"
 #include "nsMsgI18N.h"
 #include "nsMsgCompCID.h"
@@ -561,17 +562,55 @@ nsMsgCompose::InsertDivWrappedTextAtSele
   nsCOMPtr<nsIPlaintextEditor> textEditor(do_QueryInterface(m_editor));
 
   nsresult rv = htmlEditor->CreateElementWithDefaults(NS_LITERAL_STRING("div"),
                                                       getter_AddRefs(divElem));
 
   NS_ENSURE_SUCCESS(rv,);
 
   nsCOMPtr<nsIDOMNode> divNode (do_QueryInterface(divElem));
-  divNode->SetTextContent(aText);
+
+  // We need the document
+  nsCOMPtr<nsIDOMDocument> doc;
+  rv = m_editor->GetDocument(getter_AddRefs(doc));
+  NS_ENSURE_SUCCESS(rv,);
+
+  // Break up the text by newlines, and then insert text nodes followed
+  // by <br> nodes.
+  nsAString::const_iterator start, end;
+  aText.BeginReading(start);
+  aText.EndReading(end);
+
+  for (;;)
+  {
+    nsAString::const_iterator delimiter = start;
+    FindCharInReadable('\n', delimiter, end);
+
+    nsCOMPtr<nsIDOMText> textNode;
+    rv = doc->CreateTextNode(Substring(start, delimiter), getter_AddRefs(textNode));
+    NS_ENSURE_SUCCESS(rv,);
+
+    nsCOMPtr<nsIDOMNode> newTextNode = do_QueryInterface(textNode);
+    nsCOMPtr<nsIDOMNode> resultNode;
+    rv = divElem->AppendChild(newTextNode, getter_AddRefs(resultNode));
+    NS_ENSURE_SUCCESS(rv,);
+
+    // Now create and insert a BR
+    nsCOMPtr<nsIDOMElement> brElem;
+    rv = htmlEditor->CreateElementWithDefaults(NS_LITERAL_STRING("br"),
+                                               getter_AddRefs(brElem));
+    rv = divElem->AppendChild(brElem, getter_AddRefs(resultNode));
+    NS_ENSURE_SUCCESS(rv,);
+
+    if (delimiter == end)
+      break;
+    start = ++delimiter;
+    if (start == end)
+      break;
+  }
 
   htmlEditor->InsertElementAtSelection(divElem, true);
   nsCOMPtr<nsIDOMNode> parent;
   PRInt32 offset;
 
   rv = GetNodeLocation(divNode, address_of(parent), &offset);
   if (NS_SUCCEEDED(rv))
   {