Improved handling of empty `<pre>` blocks
authorAlexander Ebert <ebert@woltlab.com>
Tue, 5 Dec 2017 10:53:57 +0000 (11:53 +0100)
committerAlexander Ebert <ebert@woltlab.com>
Tue, 5 Dec 2017 10:53:57 +0000 (11:53 +0100)
wcfsetup/install/files/js/3rdParty/redactor2/plugins/WoltLabClean.js
wcfsetup/install/files/js/3rdParty/redactor2/plugins/WoltLabSource.js

index 044a18d7344af9644b400eb1d89d43079bcafe99..283038494138f0d945bd9bcf0dbf6a4bfa6bef11 100644 (file)
@@ -42,6 +42,13 @@ $.Redactor.prototype.WoltLabClean = function() {
                                        }
                                });
                                
+                               // enforce at least a single whitespace inside certain block elements
+                               elBySelAll('pre, woltlab-quote, woltlab-spoiler', div, function (element) {
+                                       if (element.childElementCount === 0 && (element.textContent.length === 0 || element.textContent.match(/^\r?\n$/))) {
+                                               element.textContent = '\u200B';
+                                       }
+                               });
+                               
                                html = div.innerHTML;
                                
                                return html;
index bfbe6b51dcb99db9308b3f5de926779b06e994ea..0b38ce076fcb460abf1acf135e7cfe140acb47b5 100644 (file)
@@ -67,6 +67,7 @@ $.Redactor.prototype.WoltLabSource = function() {
                                // use jQuery to parse, its parser is much more graceful
                                var div = $('<div />').html(this.source.$textarea.val());
                                stripIcons(div[0]);
+                               
                                this.source.$textarea.val(div[0].innerHTML);
                                
                                mpHide.call(this);
@@ -198,6 +199,9 @@ $.Redactor.prototype.WoltLabSource = function() {
                                html = html.replace('@@@WCF_PRE_BACKUP_' + i + '@@@', backup[i]);
                        }
                        
+                       // remove the trailing newline in front of <pre>
+                       html = html.replace(/\r?\n<\/pre>/g, '</pre>');
+                       
                        return html.trim();
                }
        };