diff --git a/app/Entities/Tools/PageContent.php b/app/Entities/Tools/PageContent.php index 7f4d695fe..22190f03f 100644 --- a/app/Entities/Tools/PageContent.php +++ b/app/Entities/Tools/PageContent.php @@ -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); } diff --git a/app/Entities/Tools/PageIncludeParser.php b/app/Entities/Tools/PageIncludeParser.php index 660c60372..02af3fce9 100644 --- a/app/Entities/Tools/PageIncludeParser.php +++ b/app/Entities/Tools/PageIncludeParser.php @@ -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; } } } diff --git a/tests/Unit/PageIncludeParserTest.php b/tests/Unit/PageIncludeParserTest.php index c4c127626..fc071cf79 100644 --- a/tests/Unit/PageIncludeParserTest.php +++ b/tests/Unit/PageIncludeParserTest.php @@ -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>', ); }