3 namespace BookStack\Entities\Tools;
5 use BookStack\Util\HtmlDocument;
8 class PageIncludeContent
10 protected static array $topLevelTags = ['table', 'ul', 'ol', 'pre'];
13 * @param DOMNode[] $contents
14 * @param bool $isInline
16 public function __construct(
17 protected array $contents,
18 protected bool $isInline,
22 public static function fromHtmlAndTag(string $html, PageIncludeTag $tag): self
25 return new self([], true);
28 $doc = new HtmlDocument($html);
30 $sectionId = $tag->getSectionId();
32 $contents = [...$doc->getBodyChildren()];
33 return new self($contents, false);
36 $section = $doc->getElementById($sectionId);
38 return new self([], true);
41 $isTopLevel = in_array(strtolower($section->nodeName), static::$topLevelTags);
42 $contents = $isTopLevel ? [$section] : [...$section->childNodes];
43 return new self($contents, !$isTopLevel);
46 public static function fromInlineHtml(string $html): self
49 return new self([], true);
52 $doc = new HtmlDocument($html);
54 return new self([...$doc->getBodyChildren()], true);
57 public function isInline(): bool
59 return $this->isInline;
62 public function isEmpty(): bool
64 return empty($this->contents);
70 public function toDomNodes(): array
72 return $this->contents;
75 public function toHtml(): string
79 foreach ($this->contents as $content) {
80 $html .= $content->ownerDocument->saveHTML($content);