Bug 1385725 - Port bug 1336531 to SM: No <br>-Tag before signatures in paragraph mode. r=frg a=IanN DONTBUILD RELEASE_56_END
authorJorg K <jorgk@jorgk.com>
Tue, 17 Oct 2017 17:56:25 +0200
changeset 29445 b1a70a8dd3684cb9300dfda1a5fb15f6cc408896
parent 29444 5d4cbfdbc82944a96809505b226a25c57be3d8d5
child 29446 47a22611c960a776471eccc2dcb10aecb8f861e5
push id377
push userfrgrahl@gmx.net
push dateTue, 17 Oct 2017 15:57:27 +0000
reviewersfrg, IanN
bugs1385725, 1336531
Bug 1385725 - Port bug 1336531 to SM: No <br>-Tag before signatures in paragraph mode. r=frg a=IanN DONTBUILD
mailnews/compose/src/nsMsgCompose.cpp
suite/mailnews/compose/MsgComposeCommands.js
--- a/mailnews/compose/src/nsMsgCompose.cpp
+++ b/mailnews/compose/src/nsMsgCompose.cpp
@@ -4452,30 +4452,26 @@ nsMsgCompose::ProcessSignature(nsIMsgIde
   static const char      preclose[] = "</pre>";
 
   int32_t wrapLength = 72; // setup default value in case GetWrapLength failed
   GetWrapLength(&wrapLength);
   preopen = PR_smprintf(_preopen, wrapLength);
   if (!preopen)
     return NS_ERROR_OUT_OF_MEMORY;
 
-#ifdef MOZ_THUNDERBIRD
   bool paragraphMode =
     mozilla::Preferences::GetBool("mail.compose.default_to_paragraph", false);
-#endif
 
   if (imageSig)
   {
     // We have an image signature. If we're using the in HTML composer, we
     // should put in the appropriate HTML for inclusion, otherwise, do nothing.
     if (m_composeHTML)
     {
-#ifdef MOZ_THUNDERBIRD
       if (!paragraphMode)
-#endif
         sigOutput.AppendLiteral(htmlBreak);
       sigOutput.AppendLiteral(htmlsigopen);
       if ((mType == nsIMsgCompType::NewsPost || !suppressSigSep) &&
           (reply_on_top != 1 || sig_bottom || !aQuoted)) {
         sigOutput.AppendLiteral(dashes);
       }
 
       sigOutput.AppendLiteral(htmlBreak);
@@ -4556,19 +4552,17 @@ nsMsgCompose::ProcessSignature(nsIMsgIde
   }
 
   // Now that sigData holds data...if any, append it to the body in a nice
   // looking manner
   if (!sigData.IsEmpty())
   {
     if (m_composeHTML)
     {
-#ifdef MOZ_THUNDERBIRD
       if (!paragraphMode)
-#endif
         sigOutput.AppendLiteral(htmlBreak);
 
       if (htmlSig)
         sigOutput.AppendLiteral(htmlsigopen);
       else
         sigOutput.Append(NS_ConvertASCIItoUTF16(preopen));
     }
 
--- a/suite/mailnews/compose/MsgComposeCommands.js
+++ b/suite/mailnews/compose/MsgComposeCommands.js
@@ -146,16 +146,25 @@ function enableEditableFields()
 {
   gMsgCompose.editor.flags &= ~nsIPlaintextEditorMail.eEditorReadonlyMask;
   var enableElements = document.getElementsByAttribute("disableonsend", "true");
   for (let i = 0; i < enableElements.length; i++)
     enableElements[i].removeAttribute('disabled');
 
 }
 
+/**
+ * Small helper function to check whether the node passed in is a signature.
+ * Note that a text node is not a DOM element, hence .localName can't be used.
+ */
+function isSignature(aNode) {
+  return ["DIV","PRE"].includes(aNode.nodeName) &&
+         aNode.classList.contains("moz-signature");
+}
+
 var stateListener = {
   NotifyComposeFieldsReady: function() {
     ComposeFieldsReady();
   },
 
   NotifyComposeBodyReady: function() {
     this.useParagraph = gMsgCompose.composeHTML &&
                         Services.prefs.getBoolPref("mail.compose.default_to_paragraph");
@@ -213,16 +222,19 @@ var stateListener = {
     AdjustFocus();
   },
 
   NotifyComposeBodyReadyNew: function() {
     // Control insertion of line breaks.
     if (this.useParagraph) {
       this.editor.enableUndo(false);
 
+      let mailDoc = document.getElementById("content-frame").contentDocument;
+      let mailBody = mailDoc.querySelector("body");
+      this.editor.selection.collapse(mailBody, 0);
       let pElement = this.editor.createElementWithDefaults("p");
       let brElement = this.editor.createElementWithDefaults("br");
       pElement.appendChild(brElement);
       this.editor.insertElementAtSelection(pElement, false);
 
       this.paragraphState.setAttribute("state", "p");
 
       this.editor.beginningOfDocument();
@@ -234,16 +246,21 @@ var stateListener = {
   },
 
   NotifyComposeBodyReadyReply: function() {
     // Control insertion of line breaks.
     if (this.useParagraph) {
       let mailDoc = document.getElementById("content-frame").contentDocument;
       let mailBody = mailDoc.querySelector("body");
       let selection = this.editor.selection;
+
+      // Make sure the selection isn't inside the signature.
+      if (isSignature(mailBody.firstChild))
+        selection.collapse(mailBody, 0);
+
       let range = selection.getRangeAt(0);
       let start = range.startOffset;
 
       if (start != range.endOffset) {
         // The selection is not collapsed, most likely due to the
         // "select the quote" option. In this case we do nothing.
         return;
       }