Fix problem with Document class and html() function#19
Conversation
Log1x
left a comment
There was a problem hiding this comment.
Appreciate the PR! Seeing an initial issue with the Regex for comments. Can we double check these handle every condition?
|
I encountered the same problem; the code in the PR seems to solve my problem. Have you got any update on this? |
|
I believe you should be able to resolve this without regex just by using LIBXML_NOXMLDECL. You should also be able to suppress errors using a flag as well. I haven't tested this, but it should theoretically work as a drop-in replacement for this class cc @Log1x @retlehs class Document
{
/**
* The DOMDocument instance.
*/
protected DOMDocument $document;
/**
* Initialize the Document instance.
*/
public function __construct(string $html)
{
$this->document = new DOMDocument(encoding: 'UTF-8');
$this->document->loadHTML($html,
LIBXML_HTML_NOIMPLIED | LIBXML_HTML_NODEFDTD | LIBXML_NOXMLDECL | LIBXML_NOWARNING | LIBXML_NOERROR
);
}
/**
* Make a new instance of the Document.
*/
public static function make(string $html): self
{
return new static($html);
}
/**
* Loop through each node in the document and execute the provided callback.
*/
public function each(callable $callback): self
{
foreach ($this->xpath('//*') as $node) {
$callback($node);
}
return $this;
}
/**
* Evaluate the given XPath expression.
*/
public function xpath(string $expression): DOMNodeList
{
return (new DOMXPath($this->document))->query($expression);
}
/**
* Get the saved document HTML.
*/
public function html(): string
{
return trim($this->document->saveHTML());
}
/**
* Call the given method on the root document.
*/
public function __call(string $name, array $arguments): mixed
{
return $this->document->{$name}(...$arguments);
}
/**
* Get the given property from the root document.
*/
public function __get(string $name): mixed
{
return $this->document->{$name};
}
} |
@QWp6t I've tried your code and it's fixing the issue for me. I've also removed suppress() and clear() using the suggested flags. |
|
Same problem on macOS with Herd PHP 8.3. libxml2 Version => 2.15.1 |
Sounds good to me! Appreciate it. |
…move unnecessary methods.
|
Done! @Log1x I hope this does it :) |
There was a problem hiding this comment.
Not sure @dustingrofthenumber, but I think you should remove:
use Illuminate\Support\Str;
|
Yep, you are right, it is unused, removed! |
Resolves #18