Masterminds / html5-php

An HTML5 parser and serializer for PHP.
http://masterminds.github.io/html5-php/
Other
1.56k stars 115 forks source link

Add option to parse html without adding doctype + html + body #180

Closed erropix closed 4 years ago

erropix commented 4 years ago

loadHTML() force adding of doctype, HTML tag, and body tag when loading partial HTML string

Is there any way to do the same as

$dom->loadHTML($output, LIBXML_HTML_NODEFDTD | LIBXML_HTML_NOIMPLIED);
goetas commented 4 years ago

have you tried loadHTMLFragment()?

erropix commented 4 years ago

@goetas yes, The issue is that it create a DOMDocumentFragment instead of DOMDocument, and I can't use DOMXpath query on it

is there a way to apply XPath::query?

goetas commented 4 years ago

You can do this:

$doc = new DOMDocument();

$htm5 = new Html5([
  'target_document' => $doc
]);

$frag = $htm5->loadHTMLFragment('html here...');

$doc->appendChild($frag);

$xpath = new DOMXpath($doc);

$el = $xpath->query('/div');
mundschenk-at commented 4 years ago

@ERROPiX Take a look at my library mundschenk-at/php-typography for another workaround.