From a427d433c8ae9c19858344709eb0380fa889b602 Mon Sep 17 00:00:00 2001 From: Alexander Ebert Date: Wed, 17 Dec 2014 01:13:44 +0100 Subject: [PATCH] Fixed text nodes being direct editor childNodes breaking stuff --- .../js/3rdParty/redactor/plugins/wbbcode.js | 1 + .../3rdParty/redactor/plugins/wmonkeypatch.js | 33 +------------- .../js/3rdParty/redactor/plugins/wutil.js | 43 +++++++++++++++++++ 3 files changed, 45 insertions(+), 32 deletions(-) diff --git a/wcfsetup/install/files/js/3rdParty/redactor/plugins/wbbcode.js b/wcfsetup/install/files/js/3rdParty/redactor/plugins/wbbcode.js index 33411316cf..d09207e764 100644 --- a/wcfsetup/install/files/js/3rdParty/redactor/plugins/wbbcode.js +++ b/wcfsetup/install/files/js/3rdParty/redactor/plugins/wbbcode.js @@ -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(); diff --git a/wcfsetup/install/files/js/3rdParty/redactor/plugins/wmonkeypatch.js b/wcfsetup/install/files/js/3rdParty/redactor/plugins/wmonkeypatch.js index b3424b15bc..b1f0afb63d 100644 --- a/wcfsetup/install/files/js/3rdParty/redactor/plugins/wmonkeypatch.js +++ b/wcfsetup/install/files/js/3rdParty/redactor/plugins/wmonkeypatch.js @@ -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 = $('

').insertBefore($current); - } - - $p.append($current); - } - } - else if ($current.nodeType === Element.TEXT_NODE) { - if ($p === null) { - $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); diff --git a/wcfsetup/install/files/js/3rdParty/redactor/plugins/wutil.js b/wcfsetup/install/files/js/3rdParty/redactor/plugins/wutil.js index 9386feb203..bc92f6c084 100644 --- a/wcfsetup/install/files/js/3rdParty/redactor/plugins/wutil.js +++ b/wcfsetup/install/files/js/3rdParty/redactor/plugins/wutil.js @@ -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 = $('

').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 = $('

').insertBefore($current); + } + + $p.append($current); + } + } } }; }; -- 2.20.1