Description
Description
The following code:
<?php
try {
\Dom\HTMLDocument::createFromString('', options: LIBXML_HTML_NODEFDTD);
} catch (\Throwable $e) {
echo $e->__toString();
}
\Dom\HTMLDocument::createFromString('', options: Dom\NO_DEFAULT_NS);
https://3v4l.org/pZj10/rfc#vgit.master
Resulted in this output:
ValueError: Dom\HTMLDocument::createFromString(): Argument #2 ($options) contains invalid flags (allowed flags: LIBXML_NOERROR, LIBXML_COMPACT, LIBXML_HTML_NOIMPLIED, Dom\NO_DEFAULT_NS) in /in/pZj10:4
Stack trace:
#0 /in/pZj10(4): Dom\HTMLDocument::createFromString('', 4)
#1 {main}
Fatal error: Uncaught Error: Undefined constant "Dom\NO_DEFAULT_NS" in /in/pZj10:8
But I expected this output instead:
ValueError: Dom\HTMLDocument::createFromString(): Argument #2 ($options) contains invalid flags (allowed flags: LIBXML_NOERROR, LIBXML_COMPACT, LIBXML_HTML_NOIMPLIED, Dom\HTML_NO_DEFAULT_NS) in /in/pZj10:4
Stack trace:
#0 /in/pZj10(4): Dom\HTMLDocument::createFromString('', 4)
#1 {main}
Fatal error: Uncaught Error: Undefined constant "Dom\NO_DEFAULT_NS" in /in/pZj10:8
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 ?
var_dump(\Dom\HTMLDocument::createFromString('<!DOCTYPE html>
<html>
<body>abc<br>def</body></html>', options: \Dom\HTML_NO_DEFAULT_NS)->saveHtml());
Output
<!DOCTYPE html><html><head></head><body>abc<br></br>def</body></html>
https://3v4l.org/UJ8fL/rfc#vgit.master
Edit: the goal is to not add a doctype here:
$html = 'abc<br>def';
var_dump(\Dom\HTMLDocument::createFromString($html, options: LIBXML_NOERROR|\Dom\HTML_NO_DEFAULT_NS)->saveHtml());
$dom = new \DOMDocument(encoding: 'UTF-8');
$dom->loadHTML($html, LIBXML_NOERROR| LIBXML_HTML_NODEFDTD);
var_dump($dom->saveHtml());
Output :
string(54) "<html><head></head><body>abc<br></br>def</body></html>"
string(44) "<html><body><p>abc<br>def</p></body></html>
"
https://3v4l.org/AuPs9/rfc#vgit.master
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
PHP Version
8.4.2
Operating System
No response