]> BookStack Code Mirror - bookstack/commitdiff
Includes: Added ID de-duplicating and more thorough clean-up
authorDan Brown <redacted>
Mon, 27 Nov 2023 20:16:27 +0000 (20:16 +0000)
committerDan Brown <redacted>
Mon, 27 Nov 2023 20:16:27 +0000 (20:16 +0000)
app/Entities/Tools/PageContent.php
app/Entities/Tools/PageIncludeParser.php
tests/Unit/PageIncludeParserTest.php

index 7f4d695febac8d0880d357aa2e857e807f605dc5..22190f03f61913a48326ce0c24f64bbb12339140 100644 (file)
@@ -5,8 +5,6 @@ namespace BookStack\Entities\Tools;
 use BookStack\Entities\Models\Page;
 use BookStack\Entities\Tools\Markdown\MarkdownToHtml;
 use BookStack\Exceptions\ImageUploadException;
-use BookStack\Facades\Theme;
-use BookStack\Theming\ThemeEvents;
 use BookStack\Uploads\ImageRepo;
 use BookStack\Uploads\ImageService;
 use BookStack\Util\HtmlContentFilter;
@@ -293,10 +291,16 @@ class PageContent
         $parser = new PageIncludeParser($doc, $contentProvider);
         $nodesAdded = 1;
 
-        for ($includeDepth = 0; $includeDepth < 3 && $nodesAdded !== 0; $includeDepth++) {
+        for ($includeDepth = 0; $includeDepth < 1 && $nodesAdded !== 0; $includeDepth++) {
             $nodesAdded = $parser->parse();
         }
 
+        if ($includeDepth > 1) {
+            $idMap = [];
+            $changeMap = [];
+            $this->updateIdsRecursively($doc->getBody(), 0, $idMap, $changeMap);
+        }
+
         if (!config('app.allow_content_scripts')) {
             HtmlContentFilter::removeScriptsFromDocument($doc);
         }
index 660c60372cdc41ebac9b5dbf293e20fa30b7ed11..02af3fce99aef19a751497620fef76e78522469e 100644 (file)
@@ -145,6 +145,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);
@@ -206,8 +207,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;
             }
         }
     }
index c4c12762604c7ded62fbc9fb8919078f1f42b19f..fc071cf79c8a2ed33ec182da5c1f54a0f456f0c0 100644 (file)
@@ -179,6 +179,15 @@ class PageIncludeParserTest extends TestCase
         );
     }
 
+    public function test_multi_source_elem_whole_document_with_nested_content_origin()
+    {
+        $this->runParserTest(
+            '<p><strong>{{@45}}</strong></p>',
+            ['45' => '<p>Testing</p><blockquote>This</blockquote>'],
+            '<p>Testing</p><blockquote>This</blockquote>',
+        );
+    }
+
     public function test_multiple_tags_in_same_origin_with_inline_content()
     {
         $this->runParserTest(
@@ -202,7 +211,7 @@ class PageIncludeParserTest extends TestCase
         $this->runParserTest(
             '<div><p>This <strong>{{@45#content}}</strong> content is {{@45#content}}</p>{{@45#content}}</div>',
             ['45' => '<pre id="content">block</pre>'],
-            '<div><pre id="content">block</pre><p>This <strong></strong> content is </p><pre id="content">block</pre><pre id="content">block</pre></div>',
+            '<div><pre id="content">block</pre><p>This  content is </p><pre id="content">block</pre><pre id="content">block</pre></div>',
         );
     }