php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #65196 Passing DOMDocumentFragment to DOMDocument::saveHTML() Produces invalid Markup
Submitted: 2013-07-03 19:41 UTC Modified: 2013-08-01 23:10 UTC
Votes:3
Avg. Score:5.0 ± 0.0
Reproduced:3 of 3 (100.0%)
Same Version:2 (66.7%)
Same OS:1 (33.3%)
From: baileyp at comcast dot net Assigned:
Status: Closed Package: DOM XML related
PHP Version: master-Git-2013-07-03 (Git) OS: any
Private report: No CVE-ID:
 [2013-07-03 19:41 UTC] 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></>"

Patches

Add a Patch

Pull Requests

Add a Pull Request

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2013-07-03 19:50 UTC] 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 20:34 UTC] baileyp at comcast dot net
-Operating System: OSX 10.8.4 +Operating System: OSX 10.8.4 & CentOS 6.4
 [2013-07-03 20:34 UTC] baileyp at comcast dot net
Bug also reproducible on GNU/Linux CentOS 6.4.
 [2013-08-01 23:10 UTC] yohgaki@php.net
-Operating System: OSX 10.8.4 & CentOS 6.4 +Operating System: any
 [2013-08-01 23:10 UTC] yohgaki@php.net
Reproduced with Windows7 & Fedora19 & PHP5.5
 [2013-10-10 18:43 UTC] soulodd at yahoo dot com
Reproduced this with latest 5.5 branch.

$doc = new DOMDocument();
$frag = $doc -> createDocumentFragment();
$frag -> appendXML('yyy');

echo $doc -> saveHTML($frag);
// outputs <>yyy

workaround is to loop through childnodes of the domdocumentfragment and savehtml to each.
 [2013-12-02 16:04 UTC] mike@php.net
Automatic comment on behalf of mike
Revision: http://git.php.net/?p=php-src.git;a=commit;h=22fa3fbc5ffe75349c0edb6b776b6fb1168cb21c
Log: Fix bug #65196
 [2013-12-02 16:04 UTC] mike@php.net
-Status: Open +Status: Closed
 [2013-12-02 16:51 UTC] ab@php.net
Automatic comment on behalf of mike
Revision: http://git.php.net/?p=php-src.git;a=commit;h=22fa3fbc5ffe75349c0edb6b776b6fb1168cb21c
Log: Fix bug #65196
 
PHP Copyright © 2001-2014 The PHP Group
All rights reserved.
Last updated: Wed Apr 16 16:02:23 2014 UTC