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

---
 CHANGELOG            |    1 +
 program/js/editor.js |   15 +++++++--------
 2 files changed, 8 insertions(+), 8 deletions(-)

diff --git a/CHANGELOG b/CHANGELOG
index 2c262ae..0149437 100644
--- a/CHANGELOG
+++ b/CHANGELOG
@@ -46,6 +46,7 @@
 - Fix responses list update issue after response name change (#1490555)
 - Fix bug where message preview was unintentionally reset on check-recent action (#1490563)
 - Fix bug where HTML messages with invalid/excessive css styles couldn't be displayed (#1490539)
+- Fix redundant blank lines when using HTML and top posting (#1490576)
 
 RELEASE 1.1.3
 -------------
diff --git a/program/js/editor.js b/program/js/editor.js
index 50ba03d..cd16158 100644
--- a/program/js/editor.js
+++ b/program/js/editor.js
@@ -547,20 +547,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);

--
Gitblit v1.9.1