From 18eddcae0e80ad0e8c686b97f0552055f7f38d3c Mon Sep 17 00:00:00 2001 From: Alexander Ebert Date: Mon, 8 May 2023 12:05:44 +0200 Subject: [PATCH] Replace the recursive call in `unwrapBr` with a loop --- .../Core/Component/Ckeditor/Cleanup.ts | 41 ++++++++++--------- .../Core/Component/Ckeditor/Cleanup.js | 39 ++++++++++-------- .../node/HtmlOutputNodeNormalizer.class.php | 41 ++++++++++--------- 3 files changed, 65 insertions(+), 56 deletions(-) diff --git a/ts/WoltLabSuite/Core/Component/Ckeditor/Cleanup.ts b/ts/WoltLabSuite/Core/Component/Ckeditor/Cleanup.ts index 14719cde89..fee08a6a5e 100644 --- a/ts/WoltLabSuite/Core/Component/Ckeditor/Cleanup.ts +++ b/ts/WoltLabSuite/Core/Component/Ckeditor/Cleanup.ts @@ -21,26 +21,29 @@ function normalizeBr(div: HTMLElement): void { } function unwrapBr(br: HTMLElement): void { - if (br.previousSibling || br.nextSibling) { - return; - } + for (;;) { + if (br.previousSibling || br.nextSibling) { + return; + } - const parent = br.parentElement!; - switch (parent.tagName) { - case "B": - case "DEL": - case "EM": - case "I": - case "STRONG": - case "SUB": - case "SUP": - case "SPAN": - case "U": - parent.insertAdjacentElement("afterend", br); - parent.remove(); - - unwrapBr(br); - break; + const parent = br.parentElement!; + switch (parent.tagName) { + case "B": + case "DEL": + case "EM": + case "I": + case "STRONG": + case "SUB": + case "SUP": + case "SPAN": + case "U": + parent.insertAdjacentElement("afterend", br); + parent.remove(); + break; + + default: + return; + } } } diff --git a/wcfsetup/install/files/js/WoltLabSuite/Core/Component/Ckeditor/Cleanup.js b/wcfsetup/install/files/js/WoltLabSuite/Core/Component/Ckeditor/Cleanup.js index 925cfcf603..3e0f18b1d0 100644 --- a/wcfsetup/install/files/js/WoltLabSuite/Core/Component/Ckeditor/Cleanup.js +++ b/wcfsetup/install/files/js/WoltLabSuite/Core/Component/Ckeditor/Cleanup.js @@ -22,24 +22,27 @@ define(["require", "exports", "tslib", "../../Dom/Util"], function (require, exp }); } function unwrapBr(br) { - if (br.previousSibling || br.nextSibling) { - return; - } - const parent = br.parentElement; - switch (parent.tagName) { - case "B": - case "DEL": - case "EM": - case "I": - case "STRONG": - case "SUB": - case "SUP": - case "SPAN": - case "U": - parent.insertAdjacentElement("afterend", br); - parent.remove(); - unwrapBr(br); - break; + for (;;) { + if (br.previousSibling || br.nextSibling) { + return; + } + const parent = br.parentElement; + switch (parent.tagName) { + case "B": + case "DEL": + case "EM": + case "I": + case "STRONG": + case "SUB": + case "SUP": + case "SPAN": + case "U": + parent.insertAdjacentElement("afterend", br); + parent.remove(); + break; + default: + return; + } } } function removeTrailingBr(br) { diff --git a/wcfsetup/install/files/lib/system/html/output/node/HtmlOutputNodeNormalizer.class.php b/wcfsetup/install/files/lib/system/html/output/node/HtmlOutputNodeNormalizer.class.php index 85dc429555..d6c3437958 100644 --- a/wcfsetup/install/files/lib/system/html/output/node/HtmlOutputNodeNormalizer.class.php +++ b/wcfsetup/install/files/lib/system/html/output/node/HtmlOutputNodeNormalizer.class.php @@ -104,26 +104,29 @@ final class HtmlOutputNodeNormalizer private function unwrapBr(\DOMElement $br): void { - if ($br->previousSibling || $br->nextSibling) { - return; - } + for (;;) { + if ($br->previousSibling || $br->nextSibling) { + return; + } - $parent = $br->parentNode; - switch ($parent->nodeName) { - case "b": - case "del": - case "em": - case "i": - case "strong": - case "sub": - case "sup": - case "span": - case "u": - $parent->parentNode->insertBefore($br, $parent); - $parent->parentNode->removeChild($parent); - - $this->unwrapBr($br); - break; + $parent = $br->parentNode; + switch ($parent->nodeName) { + case "b": + case "del": + case "em": + case "i": + case "strong": + case "sub": + case "sup": + case "span": + case "u": + $parent->parentNode->insertBefore($br, $parent); + $parent->parentNode->removeChild($parent); + break; + + default: + return; + } } } -- 2.20.1