From 28a5166c72b8c6ed1a6650ab49387a210195212e Mon Sep 17 00:00:00 2001 From: Alexander Ebert Date: Sat, 22 Aug 2020 02:14:17 +0200 Subject: [PATCH] Workaround for custom elements being chopped up by outdenting an empty `li` Reproducible test case: ``` ``` --- .../files/js/3rdParty/redactor2/redactor.js | 27 +++++++++++++++++++ 1 file changed, 27 insertions(+) diff --git a/wcfsetup/install/files/js/3rdParty/redactor2/redactor.js b/wcfsetup/install/files/js/3rdParty/redactor2/redactor.js index 8fb05c90ce..ea84419f40 100644 --- a/wcfsetup/install/files/js/3rdParty/redactor2/redactor.js +++ b/wcfsetup/install/files/js/3rdParty/redactor2/redactor.js @@ -4559,7 +4559,25 @@ this.indent.repositionItem($item); } + var tmpWrapper = null; if ($item.length === 0) { + // `formatBlock` does not handle custom elements gracefully, always + // treating them as inline elements, causing these to be chopped up + // into separate elements. We can mitigate this problem by introducing + // a temporary intermediate `
` which will serve as a block wrapper. + var block = this.selection.block(); + if (block && block.nodeName.indexOf('-') !== -1) { + this.selection.save(); + + tmpWrapper = elCreate('div'); + while (block.childNodes.length) { + tmpWrapper.appendChild(block.childNodes[0]); + } + block.appendChild(tmpWrapper); + + this.selection.restore(); + } + document.execCommand('formatblock', false, 'p'); $item = $(this.selection.current()); var $next = $item.next(); @@ -4570,6 +4588,15 @@ // normalize this.selection.save(); + + if (tmpWrapper !== null) { + var parent = tmpWrapper.parentNode; + while (tmpWrapper.childNodes.length) { + parent.insertBefore(tmpWrapper.childNodes[0], tmpWrapper); + } + parent.removeChild(tmpWrapper); + } + this.indent.removeEmpty(); this.indent.normalize(); this.selection.restore(); -- 2.20.1