protected array $toCleanup = [];
public function __construct(
- protected string $pageHtml,
+ protected HtmlDocument $doc,
protected Closure $pageContentForId,
) {
}
/**
* Parse out the include tags.
+ * Returns the count of new content DOM nodes added to the document.
*/
- public function parse(): string
+ public function parse(): int
{
- $doc = new HtmlDocument($this->pageHtml);
-
- $tags = $this->locateAndIsolateIncludeTags($doc);
+ $nodesAdded = 0;
+ $tags = $this->locateAndIsolateIncludeTags();
foreach ($tags as $tag) {
$htmlContent = $this->pageContentForId->call($this, $tag->getPageId());
}
}
- $this->replaceNodeWithNodes($tag->domNode, $content->toDomNodes());
+ $replacementNodes = $content->toDomNodes();
+ $nodesAdded += count($replacementNodes);
+ $this->replaceNodeWithNodes($tag->domNode, $replacementNodes);
}
$this->cleanup();
- return $doc->getBodyInnerHtml();
+ return $nodesAdded;
}
/**
* own nodes in the DOM for future targeted manipulation.
* @return PageIncludeTag[]
*/
- protected function locateAndIsolateIncludeTags(HtmlDocument $doc): array
+ protected function locateAndIsolateIncludeTags(): array
{
- $includeHosts = $doc->queryXPath("//body//*[text()[contains(., '{{@')]]");
+ $includeHosts = $this->doc->queryXPath("//*[text()[contains(., '{{@')]]");
$includeTags = [];
/** @var DOMNode $node */
foreach ($replacements as $replacement) {
if ($replacement->ownerDocument !== $targetDoc) {
- $replacement = $targetDoc->adoptNode($replacement);
+ $replacement = $targetDoc->importNode($replacement, true);
}
$toReplace->parentNode->insertBefore($replacement, $toReplace);
return $parent;
}
- $parent = $parent->parentElement;
+ $parent = $parent->parentNode;
} while ($parent !== null);
return null;