Masterminds / html5-php

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

Template contents participate in the DOM tree #252

Open nielsdos opened 1 month ago

nielsdos commented 1 month ago

The following code:

<?php
require 'vendor/autoload.php';
use Masterminds\HTML5;
$html = new HTML5(array(
    "disable_html_ns" => false
));

$dom = $html->loadHTML(<<<HTML
<!DOCTYPE html>
<html>
<body>
  <template><div>foo</div></template>
</body>
</html>
HTML);
echo $dom->saveHTML();

$xp = new DOMXPath($dom);
$xp->registerNamespace('html', 'http://www.w3.org/1999/xhtml');
var_dump($xp->query('//html:div'));

Resulted in the following output:

<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml"><body>
  <template><div>foo</div></template>
</body>
</html>
object(DOMNodeList)#8 (1) {
  ["length"]=>
  int(1)
}

But I expected this instead:

<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml"><body>
  <template><div>foo</div></template>
</body>
</html>
object(DOMNodeList)#8 (1) {
  ["length"]=>
  int(0)
}

While template contents should be serialized via saveHTML(), they should not participate in the DOM tree and therefore not show up in the DOMNodeList.

Equivalent JS:

dom=(new DOMParser).parseFromString(`<!DOCTYPE html>
<html>
<body>
  <template><div>foo</div></template>
</body>
</html>`,'text/html');
console.log(dom.getElementsByTagName('template')[0].firstChild); // null
console.log(dom.documentElement.outerHTML); // Shows template contents
goetas commented 1 month ago

This library was created ~2014-2015, The template tag was not yet a thing (despite the fact that the first proposal was in 2013). So the tag is not supported. It would be great if someone could provide a pull request to add support for it