Skip to content

Commit 70b90c2

Browse files
🩹 Refactor Document class constructor to simplify XML handling and remove unnecessary methods.
1 parent 1db02e2 commit 70b90c2

File tree

1 file changed

+4
-37
lines changed

1 file changed

+4
-37
lines changed

‎src/Document.php‎

Lines changed: 4 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -14,26 +14,17 @@ class Document
1414
*/
1515
protected DOMDocument $document;
1616

17-
/**
18-
* The XML encoding tag.
19-
*/
20-
protected string $encoding = '<?xml encoding="UTF-8">';
21-
2217
/**
2318
* Initialize the Document instance.
2419
*/
2520
public function __construct(string $html)
2621
{
27-
$this->document = new DOMDocument;
28-
29-
$this->suppress();
22+
$this->document = new DOMDocument(encoding: 'UTF-8');
3023

3124
$this->document->loadHTML(
32-
Str::start($html, $this->encoding),
33-
\LIBXML_HTML_NOIMPLIED | \LIBXML_HTML_NODEFDTD
25+
$html,
26+
LIBXML_HTML_NOIMPLIED | LIBXML_HTML_NODEFDTD | LIBXML_NOXMLDECL | LIBXML_NOWARNING | LIBXML_NOERROR
3427
);
35-
36-
$this->clear();
3728
}
3829

3930
/**
@@ -69,31 +60,7 @@ public function xpath(string $expression): DOMNodeList
6960
*/
7061
public function html(): string
7162
{
72-
$html = $this->document->saveHTML();
73-
$html = preg_replace('/^<\?xml[^>]*>\s*/i', '', $html);
74-
$html = preg_replace('/^<!--.*?-->\s*/s', '', $html);
75-
76-
return trim($html);
77-
}
78-
79-
/**
80-
* Suppress the XML errors.
81-
*/
82-
protected function suppress(): self
83-
{
84-
libxml_use_internal_errors(true);
85-
86-
return $this;
87-
}
88-
89-
/**
90-
* Clear the XML errors.
91-
*/
92-
protected function clear(): self
93-
{
94-
libxml_clear_errors();
95-
96-
return $this;
63+
return trim($this->document->saveHTML());
9764
}
9865

9966
/**

0 commit comments

Comments
 (0)