Skip to content

Commit a6db950

Browse files
dustingrofthenumberLog1xQWp6t
authored
Fix problem with Document class and html() function (#19)
* 🩹 Remove XML declaration and comments from HTML output in Document class. * 🩹 Improve comment removal in Document class by using a non-greedy regex match. * 🎨 Remove comments * 🩹 Refactor Document class constructor to simplify XML handling and remove unnecessary methods. * 🩹 Remove unused Str import from Document class --------- Co-authored-by: Brandon <[email protected]> Co-authored-by: QWp6t <[email protected]>
1 parent 708b689 commit a6db950

File tree

1 file changed

+4
-34
lines changed

1 file changed

+4
-34
lines changed

src/Document.php

Lines changed: 4 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@
55
use DOMDocument;
66
use DOMNodeList;
77
use DOMXPath;
8-
use Illuminate\Support\Str;
98

109
class Document
1110
{
@@ -14,26 +13,17 @@ class Document
1413
*/
1514
protected DOMDocument $document;
1615

17-
/**
18-
* The XML encoding tag.
19-
*/
20-
protected string $encoding = '<?xml encoding="UTF-8">';
21-
2216
/**
2317
* Initialize the Document instance.
2418
*/
2519
public function __construct(string $html)
2620
{
27-
$this->document = new DOMDocument;
28-
29-
$this->suppress();
21+
$this->document = new DOMDocument(encoding: 'UTF-8');
3022

3123
$this->document->loadHTML(
32-
Str::start($html, $this->encoding),
33-
\LIBXML_HTML_NOIMPLIED | \LIBXML_HTML_NODEFDTD
24+
$html,
25+
LIBXML_HTML_NOIMPLIED | LIBXML_HTML_NODEFDTD | LIBXML_NOXMLDECL | LIBXML_NOWARNING | LIBXML_NOERROR
3426
);
35-
36-
$this->clear();
3727
}
3828

3929
/**
@@ -69,27 +59,7 @@ public function xpath(string $expression): DOMNodeList
6959
*/
7060
public function html(): string
7161
{
72-
return trim(substr($this->document->saveHTML(), 23));
73-
}
74-
75-
/**
76-
* Suppress the XML errors.
77-
*/
78-
protected function suppress(): self
79-
{
80-
libxml_use_internal_errors(true);
81-
82-
return $this;
83-
}
84-
85-
/**
86-
* Clear the XML errors.
87-
*/
88-
protected function clear(): self
89-
{
90-
libxml_clear_errors();
91-
92-
return $this;
62+
return trim($this->document->saveHTML());
9363
}
9464

9565
/**

0 commit comments

Comments
 (0)