Fixed text nodes being direct editor childNodes breaking stuff
authorAlexander Ebert <ebert@woltlab.com>
Wed, 17 Dec 2014 00:13:44 +0000 (01:13 +0100)
committerAlexander Ebert <ebert@woltlab.com>
Wed, 17 Dec 2014 00:14:37 +0000 (01:14 +0100)
wcfsetup/install/files/js/3rdParty/redactor/plugins/wbbcode.js
wcfsetup/install/files/js/3rdParty/redactor/plugins/wmonkeypatch.js
wcfsetup/install/files/js/3rdParty/redactor/plugins/wutil.js

index 33411316cf7002005f8d6b728ec37b21c2a42b8b..d09207e764534dfb299e1e0fed919b5a17d58c57 100644 (file)
@@ -108,6 +108,7 @@ RedactorPlugins.wbbcode = function() {
                                        
                                        this.button.get('html').children('i').removeClass('fa-square').addClass('fa-square-o');
                                        $tooltip.text(WCF.Language.get('wcf.bbcode.button.toggleBBCode'));
+                                       this.wutil.fixDOM();
                                        $fixBR(this.$editor);
                                        
                                        this.wutil.saveSelection();
index b3424b15bc291ff56b51c171f951a6b8be3862e7..b1f0afb63d9257cd70250d85a073177ecbff5996 100644 (file)
@@ -759,44 +759,13 @@ RedactorPlugins.wmonkeypatch = function() {
                 *  - fixes text pasting in Internet Explorer 11 (#2040) 
                 */
                paste: function() {
-                       var $fixDOM = (function() {
-                               var $current = this.$editor[0].childNodes[0];
-                               var $nextSibling = $current;
-                               var $p = null;
-                               
-                               while ($nextSibling) {
-                                       $current = $nextSibling;
-                                       $nextSibling = $current.nextSibling;
-                                       
-                                       if ($current.nodeType === Element.ELEMENT_NODE) {
-                                               if (this.reIsBlock.test($current.tagName)) {
-                                                       $p = null;
-                                               }
-                                               else {
-                                                       if ($p === null) {
-                                                               $p = $('<p />').insertBefore($current);
-                                                       }
-                                                       
-                                                       $p.append($current);
-                                               }
-                                       }
-                                       else if ($current.nodeType === Element.TEXT_NODE) {
-                                               if ($p === null) {
-                                                       $p = $('<p />').insertBefore($current);
-                                               }
-                                               
-                                               $p.append($current);
-                                       }
-                               }
-                       }).bind(this);
-                       
                        // paste.insert
                        var $mpInsert = this.paste.insert;
                        this.paste.insert = (function(html) {
                                $mpInsert.call(this, html);
                                
                                setTimeout((function() {
-                                       $fixDOM();
+                                       this.wutil.fixDOM();
                                        
                                        if ($.browser.msie) {
                                                getSelection().getRangeAt(0).collapse(false);
index 9386feb203998979b366667b5af652364bf7ccb4..bc92f6c084b5a339021f7ce9a60f61c7ba146dbb 100644 (file)
@@ -758,6 +758,49 @@ RedactorPlugins.wutil = function() {
                        var $node = $(this.opts.emptyHtml);
                        $node[(setBefore ? 'insertBefore' : 'insertAfter')](element);
                        this.caret.setEnd($node[0]);
+               },
+               
+               /**
+                * Fixes the DOM by moving all non-element children of the editor into a paragraph.
+                */
+               fixDOM: function() {
+                       var $current = this.$editor[0].childNodes[0];
+                       var $nextSibling = $current;
+                       var $p = null;
+                       
+                       while ($nextSibling) {
+                               $current = $nextSibling;
+                               $nextSibling = $current.nextSibling;
+                               
+                               if ($current.nodeType === Element.ELEMENT_NODE) {
+                                       if (this.reIsBlock.test($current.tagName)) {
+                                               $p = null;
+                                       }
+                                       else {
+                                               if ($p === null) {
+                                                       $p = $('<p />').insertBefore($current);
+                                               }
+                                               
+                                               $p.append($current);
+                                       }
+                               }
+                               else if ($current.nodeType === Element.TEXT_NODE) {
+                                       if ($p === null) {
+                                               // check for ghost paragraphs next
+                                               if ($nextSibling) {
+                                                       if ($nextSibling.nodeType === Element.ELEMENT_NODE && $nextSibling.tagName === 'P' && $nextSibling.innerHTML === '\u200B') {
+                                                               var $afterNextSibling = $nextSibling.nextSibling;
+                                                               this.$editor[0].removeChild($nextSibling);
+                                                               $nextSibling = $afterNextSibling;
+                                                       }
+                                               }
+                                               
+                                               $p = $('<p />').insertBefore($current);
+                                       }
+                                       
+                                       $p.append($current);
+                               }
+                       }
                }
        };
 };