Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Template contents participate in the DOM tree #252

Open
nielsdos opened this issue Jul 31, 2024 · 1 comment
Open

Template contents participate in the DOM tree #252

nielsdos opened this issue Jul 31, 2024 · 1 comment

Comments

@nielsdos
Copy link

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
Copy link
Member

goetas commented Jul 31, 2024

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants