]> BookStack Code Mirror - bookstack/blobdiff - app/Entities/Tools/PageIncludeParser.php
Perms: Fixed some issues made when adding transactions
[bookstack] / app / Entities / Tools / PageIncludeParser.php
index 660c60372cdc41ebac9b5dbf293e20fa30b7ed11..e0b89f158704298702122f9839898be6c9e6ffde 100644 (file)
@@ -19,6 +19,9 @@ class PageIncludeParser
      */
     protected array $toCleanup = [];
 
+    /**
+     * @param Closure(PageIncludeTag $tag): PageContent $pageContentForId
+     */
     public function __construct(
         protected HtmlDocument $doc,
         protected Closure $pageContentForId,
@@ -35,8 +38,8 @@ class PageIncludeParser
         $tags = $this->locateAndIsolateIncludeTags();
 
         foreach ($tags as $tag) {
-            $htmlContent = $this->pageContentForId->call($this, $tag->getPageId());
-            $content = new PageIncludeContent($htmlContent, $tag);
+            /** @var PageIncludeContent $content */
+            $content = $this->pageContentForId->call($this, $tag);
 
             if (!$content->isInline()) {
                 $parentP = $this->getParentParagraph($tag->domNode);
@@ -69,8 +72,8 @@ class PageIncludeParser
         $includeTags = [];
 
         /** @var DOMNode $node */
-        /** @var DOMNode $childNode */
         foreach ($includeHosts as $node) {
+            /** @var DOMNode $childNode */
             foreach ($node->childNodes as $childNode) {
                 if ($childNode->nodeName === '#text') {
                     array_push($includeTags, ...$this->splitTextNodesAtTags($childNode));
@@ -101,10 +104,10 @@ class PageIncludeParser
 
             if ($currentOffset < $tagStartOffset) {
                 $previousText = substr($text, $currentOffset, $tagStartOffset - $currentOffset);
-                $textNode->parentNode->insertBefore(new DOMText($previousText), $textNode);
+                $textNode->parentNode->insertBefore($this->doc->createTextNode($previousText), $textNode);
             }
 
-            $node = $textNode->parentNode->insertBefore(new DOMText($tagOuterContent), $textNode);
+            $node = $textNode->parentNode->insertBefore($this->doc->createTextNode($tagOuterContent), $textNode);
             $includeTags[] = new PageIncludeTag($tagInnerContent, $node);
             $currentOffset = $tagStartOffset + strlen($tagOuterContent);
         }
@@ -145,6 +148,7 @@ class PageIncludeParser
         $parentText = $parent->textContent;
         $tagPos = strpos($parentText, $tag->tagContent);
         $before = $tagPos < (strlen($parentText) / 2);
+        $this->toCleanup[] = $tag->domNode->parentNode;
 
         if ($before) {
             $parent->parentNode->insertBefore($tag->domNode, $parent);
@@ -170,8 +174,8 @@ class PageIncludeParser
         $parentNode->parentNode->insertBefore($parentClone, $parentNode);
         $parentClone->removeAttribute('id');
 
-        /** @var DOMNode $child */
         for ($i = 0; $i < $splitPos; $i++) {
+            /** @var DOMNode $child */
             $child = $children[$i];
             $parentClone->appendChild($child);
         }
@@ -206,8 +210,10 @@ class PageIncludeParser
     {
         foreach ($this->toCleanup as $element) {
             $element->normalize();
-            if ($element->parentNode && !$element->hasChildNodes()) {
-                $element->parentNode->removeChild($element);
+            while ($element->parentNode && !$element->hasChildNodes()) {
+                $parent = $element->parentNode;
+                $parent->removeChild($element);
+                $element = $parent;
             }
         }
     }