Improved recognition of different newline types in Office
authorAlexander Ebert <ebert@woltlab.com>
Fri, 10 Nov 2017 16:49:35 +0000 (17:49 +0100)
committerAlexander Ebert <ebert@woltlab.com>
Fri, 10 Nov 2017 16:49:35 +0000 (17:49 +0100)
wcfsetup/install/files/js/3rdParty/redactor2/plugins/WoltLabClean.js

index cdfd850d98ea90505c441cdd58967ea0c1715531..33cf7f26cc0951378c081fcda344fe17b71d70cc 100644 (file)
@@ -253,7 +253,7 @@ $.Redactor.prototype.WoltLabClean = function() {
                                
                                html = mpOnPaste.call(this, div.innerHTML, data, insert);
                                
-                               html = html.replace(/\n*@@@WOLTLAB-BR-MARKER@@@/g, '<woltlab-br-marker></woltlab-br-marker>');
+                               html = html.replace(/\n*@@@WOLTLAB-BR-MARKER@@@\n*/g, '<woltlab-br-marker></woltlab-br-marker>');
                                html = html.replace(/(<p>)?\s*@@@WOLTLAB-P-ALIGN-(left|right|center|justify)@@@/g, function (match, p, alignment) {
                                        if (p) {
                                                return '<p class="text-' + alignment + '">';
@@ -266,12 +266,23 @@ $.Redactor.prototype.WoltLabClean = function() {
                                
                                elBySelAll('woltlab-br-marker', div, function (marker) {
                                        var parent = marker.parentNode;
+                                       if (parent === null) {
+                                               // marker was already removed, ignore it
+                                               return;
+                                       }
                                        
                                        if (parent.nodeName === 'P') {
                                                var p = elCreate('p');
                                                p.innerHTML = '<br>';
                                                
-                                               var emptySiblings = true;
+                                               var isDoubleBr = false;
+                                               var sibling = marker.nextSibling;
+                                               if (sibling && sibling.nodeName === 'WOLTLAB-BR-MARKER') {
+                                                       // equals <br><br> and should be converted into an empty line, splitting the ancestors
+                                                       isDoubleBr = true;
+                                               }
+                                               
+                                               var emptySiblings = !isDoubleBr;
                                                while (marker.nextSibling) {
                                                        if (emptySiblings && marker.nextSibling.textContent.replace(/\u200B/g, '').trim().length !== 0) {
                                                                emptySiblings = false;
@@ -281,7 +292,7 @@ $.Redactor.prototype.WoltLabClean = function() {
                                                }
                                                
                                                if (!emptySiblings) {
-                                                       // the <br> is not required when there is text afterwards
+                                                       // the <br> is not required when there is text afterwards, or if this is a <br><br> match
                                                        elRemove(p.firstElementChild);
                                                }
                                                
@@ -291,6 +302,12 @@ $.Redactor.prototype.WoltLabClean = function() {
                                                }
                                                
                                                parent.parentNode.insertBefore(p, parent.nextSibling);
+                                               
+                                               if (isDoubleBr) {
+                                                       p = elCreate('p');
+                                                       p.innerHTML = '<br>';
+                                                       parent.parentNode.insertBefore(p, parent.nextSibling);
+                                               }
                                        }
                                        else {
                                                parent.insertBefore(elCreate('br'), marker);
@@ -315,6 +332,22 @@ $.Redactor.prototype.WoltLabClean = function() {
                                                        }
                                                });
                                        }
+                                       else if (p.textContent.trim().length === 0) {
+                                               elBySelAll('span', p, function (span) {
+                                                       if (!span.hasAttribute('style') || !span.style.length) {
+                                                               while (span.childNodes.length) {
+                                                                       span.parentNode.insertBefore(span.childNodes[0], span);
+                                                               }
+                                                               
+                                                               elRemove(span);
+                                                       }
+                                               });
+                                               
+                                               if (p.children.length === 0) {
+                                                       // fix <p>&nbsp;</p> pasted from Office
+                                                       p.innerHTML = '<br>';
+                                               }
+                                       }
                                        
                                        if (remove) {
                                                elRemove(p);