diff --git a/src/Framework/DomQuery.php b/src/Framework/DomQuery.php index 621baefa..acad1e6e 100644 --- a/src/Framework/DomQuery.php +++ b/src/Framework/DomQuery.php @@ -9,6 +9,8 @@ namespace Tester; +use Dom; + /** * DomQuery simplifies querying (X)HTML documents. @@ -20,24 +22,33 @@ class DomQuery extends \SimpleXMLElement */ public static function fromHtml(string $html): self { - if (!str_contains($html, '<')) { - $html = '' . $html; - } + $old = libxml_use_internal_errors(true); + libxml_clear_errors(); - // parse these elements as void - $html = preg_replace('#<(keygen|source|track|wbr)(?=\s|>)((?:"[^"]*"|\'[^\']*\'|[^"\'>])*+)(?#', '<$1$2 />', $html); + if (PHP_VERSION_ID < 80400) { + if (!str_contains($html, '<')) { + $html = '' . $html; + } - // fix parsing of )(?:"[^"]*"|\'[^\']*\'|[^"\'>])*+>)(.*?)()#s', - fn(array $m): string => $m[1] . str_replace(')((?:"[^"]*"|\'[^\']*\'|[^"\'>])*+)(?#', '<$1$2 />', $html); + + // fix parsing of )(?:"[^"]*"|\'[^\']*\'|[^"\'>])*+>)(.*?)()#s', + fn(array $m): string => $m[1] . str_replace('loadHTML($html); + } else { + if (!preg_match('~' . $html; + } + $dom = Dom\HTMLDocument::createFromString($html, Dom\HTML_NO_DEFAULT_NS); + } - $dom = new \DOMDocument; - $old = libxml_use_internal_errors(true); - libxml_clear_errors(); - $dom->loadHTML($html); $errors = libxml_get_errors(); libxml_use_internal_errors($old);