From 72be74508f8e8d39150fa58c1572e41a7791c02f Mon Sep 17 00:00:00 2001
From: Aleksander Machniak <alec@alec.pl>
Date: Wed, 28 Oct 2015 04:40:42 -0400
Subject: [PATCH] Fix redundant blank lines when using HTML and top posting (#1490576)

---
 program/js/editor.js |   19 ++++++++-----------
 1 files changed, 8 insertions(+), 11 deletions(-)

diff --git a/program/js/editor.js b/program/js/editor.js
index abd800c..50b7228 100644
--- a/program/js/editor.js
+++ b/program/js/editor.js
@@ -527,20 +527,19 @@
 
       // Append the signature as a div within the body
       if (!sigElem) {
-        var body = this.editor.getBody(),
-          doc = this.editor.getDoc();
+        var body = this.editor.getBody();
 
-        sigElem = doc.createElement('div');
-        sigElem.setAttribute('id', '_rc_sig');
+        sigElem = $('<div id="_rc_sig"></div>').get(0);
 
-        if (rcmail.env.top_posting && !rcmail.env.sig_below) {
+        // insert at start or at cursor position in top-posting mode
+        // (but not if the content is empty)
+        if (rcmail.env.top_posting && !rcmail.env.sig_below && (body.childNodes.length > 1 || $(body).text())) {
           this.editor.getWin().focus(); // correct focus in IE & Chrome
 
           var node = this.editor.selection.getNode();
 
-          // insert at start or at cursor position if found
-          body.insertBefore(sigElem, node.nodeName == 'BODY' ? body.firstChild : node.nextSibling);
-          body.insertBefore(doc.createElement('p'), sigElem);
+          $(sigElem).insertBefore(node.nodeName == 'BODY' ? body.firstChild : node.nextSibling);
+          $('<p>').append($('<br>')).insertBefore(sigElem);
         }
         else {
           body.appendChild(sigElem);
@@ -548,9 +547,7 @@
         }
       }
 
-      if (rcmail.env.signatures[id]) {
-        sigElem.innerHTML = rcmail.env.signatures[id].html;
-      }
+      sigElem.innerHTML = rcmail.env.signatures[id] ? rcmail.env.signatures[id].html : '';
     }
     else if (!rcmail.env.top_posting) {
       position_element = $(this.editor.getBody()).children().last();

--
Gitblit v1.9.1