You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Typo in error message: Dom\NO_DEFAULT_NS instead of Dom\HTML_NO_DEFAULT_NS #17609
The name is HTML_NO_DEFAULT_NS and not NO_DEFAULT_NS
By the way, as LIBXML_HTML_NODEFDTD does not work with \Dom\HtmlDocument, we need to use \Dom\HTML_NO_DEFAULT_NS, and when using that option, void tags are considered as not void and <br> is converted to <br></br>. I don't know if it's expected, and I don't know what to do to prevent this when trying to get an output without added doctype when one isn't provided.
Should I make another issue ?
edit2: i notice that I can use LIBXML_HTML_NOIMPLIED instead of \Dom\HTML_NO_DEFAULT_NS, but then the problem is xpath does not work https://3v4l.org/Tgm1W/rfc#vgit.master
and registering the namespace didn't helped
another problem with HTML_NO_DEFAULT_NS, as it transforms <br> or </br> into <br></br>, every time I parse it again, it double the number of <br> https://3v4l.org/IAVTg/rfc#vgit.master
I believe you misunderstand what \Dom\HTML_NO_DEFAULT_NS means. That option only really exists as a BC option. It prevents putting the elements in the HTML namespace (NS=namespace); so they won't actually be HTML elements. This has consequences to serialization and to how some properties and methods behave. Generally, if you don't know the implications already you should probably not use this.
, and when using that option, void tags are considered as not void and <br> is converted to <br></br>. I don't know if it's expected
Yes this is expected: whether a tag is void or not depends on its name and its namespace, but because HTML_NO_DEFAULT_NS disables using the HTML namespace, this is no longer a void tag and is therefore rendered as <br></br>.
and I don't know what to do to prevent this when trying to get an output without added doctype when one isn't provided.
The default behaviour is to not add a doctype when none is found, but in your example code you explicitly added <!DOCTYPE html> in the input string.
If you want to remove a doctype from a document afterwards, you can use $dom->removeChild($dom->doctype);. This works in both the old DOM and new DOM classes.
Edit: the goal is to not add a doctype here:
If I remove \Dom\HTML_NO_DEFAULT_NS from the options this has the desired result and still does not include a doctype.
edit2: i notice that I can use LIBXML_HTML_NOIMPLIED instead of \Dom\HTML_NO_DEFAULT_NS, but then the problem is xpath does not work https://3v4l.org/Tgm1W/rfc#vgit.master
and registering the namespace didn't helped
You not only have to register the namespace, you also need to specify the prefix in the xpath query, like so: var_dump($xpath->query('//xhtml:div')->length);.
Also, you can just use querySelectorAll which would be simpler 🙂
another problem with HTML_NO_DEFAULT_NS, as it transforms <br> or </br> into <br></br>, every time I parse it again, it double the number of <br>
Why it is turned into <br></br> was already explained above. The reason it then parses it like <br><br> again is because <br></br> isn't valid HTML 5.
changed the title [-]Dom\NO_DEFAULT_NS does not exist[/-][+]Typo in error message: Dom\NO_DEFAULT_NS instead of Dom\HTML_NO_DEFAULT_NS[/+]on Jan 28, 2025
I've pushed the fix for the typo in the error message.
The other things you noted are not bugs; but feel free to use this thread to discuss if you still have questions.
Activity
momala454 commentedon Jan 28, 2025
another problem with HTML_NO_DEFAULT_NS, as it transforms
<br>or</br>into<br></br>, every time I parse it again, it double the number of<br>https://3v4l.org/IAVTg/rfc#vgit.master
ndossche commentedon Jan 28, 2025
Alright there's a lot to unpack here.
Indeed, this appears to be a typo in the error message, nice spot!
Indeed it does not, because this is the default behaviour.
See also https://wiki.php.net/rfc/domdocument_html5_parser#the_options_argument if you want to know the background about some of the options being left out.
I believe you misunderstand what
\Dom\HTML_NO_DEFAULT_NSmeans. That option only really exists as a BC option. It prevents putting the elements in the HTML namespace (NS=namespace); so they won't actually be HTML elements. This has consequences to serialization and to how some properties and methods behave. Generally, if you don't know the implications already you should probably not use this.Yes this is expected: whether a tag is void or not depends on its name and its namespace, but because
HTML_NO_DEFAULT_NSdisables using the HTML namespace, this is no longer a void tag and is therefore rendered as<br></br>.The default behaviour is to not add a doctype when none is found, but in your example code you explicitly added
<!DOCTYPE html>in the input string.If you want to remove a doctype from a document afterwards, you can use
$dom->removeChild($dom->doctype);. This works in both the old DOM and new DOM classes.If I remove
\Dom\HTML_NO_DEFAULT_NSfrom the options this has the desired result and still does not include a doctype.You not only have to register the namespace, you also need to specify the prefix in the xpath query, like so:
var_dump($xpath->query('//xhtml:div')->length);.Also, you can just use
querySelectorAllwhich would be simpler 🙂Why it is turned into
<br></br>was already explained above. The reason it then parses it like<br><br>again is because<br></br>isn't valid HTML 5.[-]Dom\NO_DEFAULT_NS does not exist[/-][+]Typo in error message: Dom\NO_DEFAULT_NS instead of Dom\HTML_NO_DEFAULT_NS[/+]Fix GH-17609: Typo in error message: Dom\NO_DEFAULT_NS instead of Dom…
Merge branch 'PHP-8.4'
ndossche commentedon Jan 28, 2025
I've pushed the fix for the typo in the error message.
The other things you noted are not bugs; but feel free to use this thread to discuss if you still have questions.
momala454 commentedon Jan 28, 2025
thank you !