Bug #80665 [Com]: DOMDocument object corruption during cloning
| From: | glash dot gnome at gmail dot com | Date: | Tue, 26 Jan 2021 07:11:37 +0000 |
| Subject: | Bug #80665 [Com]: DOMDocument object corruption during cloning | ||
| References: | 1 | Groups: | php.bugs |
| Request: | Send a blank email to php-bugs+get-231774@lists.php.net to get a copy of this message | ||
Edit report at https://bugs.php.net/bug.php?id=80665&edit=1
ID: 80665
Comment by: glash dot gnome at gmail dot com
Reported by: andrey at email dot dp dot ua
Summary: DOMDocument object corruption during cloning
Status: Open
Type: Bug
Package: DOM XML related
Operating System: Debian Linux
PHP Version: Irrelevant
Block user comment: N
Private report: N
New Comment:
Dear,
Thank you for reporting this issue. You make php better.
You found a libxml2 bug.
I fixed it and i requested a merge.
https://gitlab.gnome.org/GNOME/libxml2/-/merge_requests/99
-----------------------------------------------------------------
One question remains.
Should the $document->xmlVersion be feeded after loadHTML() / loadXML() when not declared in xml
? Or keep the non-declaration ?
Previous Comments:
------------------------------------------------------------------------
[2021-01-25 10:18:50] andrey at email dot dp dot ua
Description:
------------
Description
------------
When DOMDocument is cloned, properties are cloned incorrectly.
saveHTML method of the cloned object provides different results as the same method of original
object.
saveHTML of the cloned object launched with additional documentElement parameter provides result
with symbols converted to numeric character references. But saveHTML launched without parameters
returns correct result
Properties that are corrupted during cloning
---------------------------------------------
$DOMDocument->nodeType
if original object has nodeType XML_HTML_DOCUMENT_NODE, after cloning it will be set to
XML_DOCUMENT_NODE
$DOMDocument->baseURI
value is lost during cloning
$DOMDocument->version
if not set on original object will be set to 1.0
$DOMDocument->xmlVersion
if not set on original object will be set to 1.0
Methods that has different result on cloned object
-------------------------------------------------
"Carriage-return" symbols in original document correctly returned by
$DOMDocument->saveHTML() method, but replaced with on when used
$DOMDocument->saveHTML($DOMDocument->documentElement) on cloned object.
Test script:
---------------
<?php
$html = "<html><head><base href='https://php.net'></head><body>\r</body></html>";
$dom = new DOMDocument();
$dom->loadHTML($html);
$arr = array(
'DOMDocument' => $dom,
'cloned by clone' => clone $dom,
'cloned by cloneNode' => $dom->cloneNode(true)
);
foreach ($arr as $descr=>$obj)
{
echo $descr.":\n";
echo "--------------------------\n";
echo "saveHTML:\n";
echo $obj->saveHTML()."\n\n";
echo "saveHTML via DOMDocument::documentElement:\n";
echo $obj->saveHTML($obj->documentElement)."\n\n";
echo "\$DOMDocument->nodeType = ".$obj->nodeType."\n";
echo "\$DOMDocument->baseURI = ".$obj->baseURI."\n";
echo "\$DOMDocument->version = ".$obj->version."\n";
echo "\$DOMDocument->xmlVersion = ".$obj->xmlVersion."\n\n\n";
}
Expected result:
----------------
[ three times ]
saveHTML:
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" "http://www.w3.org/TR/REC-html40/loose.dtd">
<html><head><base href="https://php.net"></head><body>
</body></html>
saveHTML(documentElement):
<html><head><base href="https://php.net"></head><body>
</body></html>
$DOMDocument->nodeType = 13
$DOMDocument->baseURI = https://php.net
$DOMDocument->version =
$DOMDocument->xmlVersion =
Actual result:
--------------
DOMDocument:
--------------------------
saveHTML:
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" "http://www.w3.org/TR/REC-html40/loose.dtd">
</body></html>ase href="https://php.net"></head><body>
saveHTML(documentElement):
</body></html>ase href="https://php.net"></head><body>
$DOMDocument->nodeType = 13
$DOMDocument->baseURI = https://php.net
$DOMDocument->version =
$DOMDocument->xmlVersion =
cloned by clone:
--------------------------
saveHTML:
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" "http://www.w3.org/TR/REC-html40/loose.dtd">
</body></html>ase href="https://php.net"></head><body>
saveHTML(documentElement):
<html><head><base href="https://php.net"></head><body> </body></html>
$DOMDocument->nodeType = 9
$DOMDocument->baseURI =
$DOMDocument->version = 1.0
$DOMDocument->xmlVersion = 1.0
cloned by cloneNode:
--------------------------
saveHTML:
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" "http://www.w3.org/TR/REC-html40/loose.dtd">
</body></html>ase href="https://php.net"></head><body>
saveHTML(documentElement):
<html><head><base href="https://php.net"></head><body> </body></html>
$DOMDocument->nodeType = 9
$DOMDocument->baseURI =
$DOMDocument->version = 1.0
$DOMDocument->xmlVersion = 1.0
------------------------------------------------------------------------
--
Edit this bug report at https://bugs.php.net/bug.php?id=80665&edit=1