3 namespace BookStack\Util;
12 * HtmlDocument is a thin wrapper around DOMDocument built
13 * specifically for loading, querying and generating HTML content.
17 protected DOMDocument $document;
18 protected ?DOMXPath $xpath = null;
19 protected int $loadOptions;
21 public function __construct(string $partialHtml = '', int $loadOptions = 0)
23 libxml_use_internal_errors(true);
24 $this->document = new DOMDocument();
25 $this->loadOptions = $loadOptions;
28 $this->loadPartialHtml($partialHtml);
33 * Load some HTML content that's part of a document (e.g. body content)
34 * into the current document.
36 public function loadPartialHtml(string $html): void
38 $html = '<?xml encoding="utf-8" ?><body>' . $html . '</body>';
39 $this->document->loadHTML($html, $this->loadOptions);
44 * Load a complete page of HTML content into the document.
46 public function loadCompleteHtml(string $html): void
48 $html = '<?xml encoding="utf-8" ?>' . $html;
49 $this->document->loadHTML($html, $this->loadOptions);
54 * Start an XPath query on the current document.
56 public function queryXPath(string $expression): DOMNodeList
58 if (is_null($this->xpath)) {
59 $this->xpath = new DOMXPath($this->document);
62 $result = $this->xpath->query($expression);
63 if ($result === false) {
64 throw new \InvalidArgumentException("XPath query for expression [$expression] failed to execute");
71 * Create a new DOMElement instance within the document.
73 public function createElement(string $localName, string $value = ''): DOMElement
75 $element = $this->document->createElement($localName, $value);
77 if ($element === false) {
78 throw new \InvalidArgumentException("Failed to create element of name [$localName] and value [$value]");
85 * Get an element within the document of the given ID.
87 public function getElementById(string $elementId): ?DOMElement
89 return $this->document->getElementById($elementId);
93 * Get the DOMNode that represents the HTML body.
95 public function getBody(): DOMNode
97 return $this->document->getElementsByTagName('body')[0];
101 * Get the nodes that are a direct child of the body.
102 * This is usually all the content nodes if loaded partially.
104 public function getBodyChildren(): DOMNodeList
106 return $this->getBody()->childNodes;
110 * Get the inner HTML content of the body.
111 * This is usually all the content if loaded partially.
113 public function getBodyInnerHtml(): string
116 foreach ($this->getBodyChildren() as $child) {
117 $html .= $this->document->saveHTML($child);
124 * Get the HTML content of the whole document.
126 public function getHtml(): string
128 return $this->document->saveHTML($this->document->documentElement);
132 * Get the inner HTML for the given node.
134 public function getNodeInnerHtml(DOMNode $node): string
138 foreach ($node->childNodes as $childNode) {
139 $html .= $this->document->saveHTML($childNode);
146 * Get the outer HTML for the given node.
148 public function getNodeOuterHtml(DOMNode $node): string
150 return $this->document->saveHTML($node);