Replace the recursive call in `unwrapBr` with a loop
authorAlexander Ebert <ebert@woltlab.com>
Mon, 8 May 2023 10:05:44 +0000 (12:05 +0200)
committerAlexander Ebert <ebert@woltlab.com>
Mon, 8 May 2023 16:34:04 +0000 (18:34 +0200)
ts/WoltLabSuite/Core/Component/Ckeditor/Cleanup.ts
wcfsetup/install/files/js/WoltLabSuite/Core/Component/Ckeditor/Cleanup.js
wcfsetup/install/files/lib/system/html/output/node/HtmlOutputNodeNormalizer.class.php

index 14719cde89a2475ee16722418dc61a81ae8e37df..fee08a6a5e0eab9a0faa07c48cac7e875600bef2 100644 (file)
@@ -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;
+    }
   }
 }
 
index 925cfcf603518d369f79d9872e45f77fc1c0a220..3e0f18b1d0eed125f5b1f93f02d2db59e8904142 100644 (file)
@@ -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) {
index 85dc4295557cd1a850ff777beab7d0e46d8799f4..d6c3437958ed7b2d4c8ec0aaa89ecff2eaf71b9d 100644 (file)
@@ -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;
+            }
         }
     }