Skip to content

Template contents participate in the DOM tree #252

Description

@ndossche

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

Activity

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

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions