Edit report at https://bugs.php.net/bug.php?id=65196&edit=1
ID: 65196 Updated by: yohg...@php.net Reported by: baileyp at comcast dot net Summary: Passing DOMDocumentFragment to DOMDocument::saveHTML() Produces invalid Markup Status: Open Type: Bug Package: DOM XML related -Operating System: OSX 10.8.4 & CentOS 6.4 +Operating System: any PHP Version: master-Git-2013-07-03 (Git) Block user comment: N Private report: N New Comment: Reproduced with Windows7 & Fedora19 & PHP5.5 Previous Comments: ------------------------------------------------------------------------ [2013-07-03 20:34:21] baileyp at comcast dot net Bug also reproducible on GNU/Linux CentOS 6.4. ------------------------------------------------------------------------ [2013-07-03 19:50:29] baileyp at comcast dot net It seems that this is a discrepancy between how saveHTML() and saveXML() function, because replacing saveHTML() in the above code with saveXML() produces the expected result: var_dump($dom->saveXML($frag1)); // string(0) "" var_dump($dom->saveXML($frag2)); // string(6) "<div/>" ------------------------------------------------------------------------ [2013-07-03 19:41:28] baileyp at comcast dot net Description: ------------ When trying to save the contents of a HTML document's body without the surrounding <body></body> tags I had the idea of appending all of the childNodes to a DOMDocumentFragment and then passing it as the first argument ($node) to DOMDocument::saveHTML(). However, I discovered a bug in doing so. Saving returns invalid markup with empty tags (<></>) surrounding the content. I have reproduced this is both 5.4.11 (with libxml 2.7.8) and 5.5.0 (with libxml 2.7.6). Test script: --------------- <?php $dom = new DOMDocument(); $frag1 = $dom->createDocumentFragment(); var_dump($dom->saveHTML($frag1)); $frag2 = $dom->createDocumentFragment(); $frag2->appendChild($dom->createElement('div')); var_dump($dom->saveHTML($frag2)); Expected result: ---------------- string(0) "" string(11) "<div></div>" Actual result: -------------- string(5) "<></>" string(16) "<><div></div></>" ------------------------------------------------------------------------ -- Edit this bug report at https://bugs.php.net/bug.php?id=65196&edit=1