Open
Description
Description
XPath
queries don't work with <template>
tags.
The following code:
<?php
use Dom\HTMLDocument;
use Dom\XPath;
$html = '<html><body><template><p>Hello World</p></template></body></html>';
$dom = HTMLDocument::createFromString($html, LIBXML_NOERROR);
$xpath = new XPath($dom);
$xpath->registerNamespace("html", "http://www.w3.org/1999/xhtml");
$query = "//html:body//text();"
foreach ($xpath->query($query) as $text) { echo $text->nodeValue . "\n"; }
Resulted in this output:
Nothing
But I expected this output instead:
Hello World
I understand that the new HTMLDocument
implementation adheres more closely to HTML5 standards, meaning that the content of <template>
tags is considered a document fragment and not part of the main DOM. However, the current workaround is cumbersome:
- Query all
<template>
tags directly. - Create separate
HTMLDocument
instances for their content. - Query the content of each
HTMLDocument
individually.
This process becomes increasingly complex with nested templates. I believe this could be simplified with a flag in either HTMLDocument
or XPath
.