Bug #65196 [Opn->Csd]: Passing DOMDocumentFragment to DOMDocument::saveHTML() Produces invalid Markup

From: Date: Mon, 02 Dec 2013 16:04:19 +0000
Subject: Bug #65196 [Opn->Csd]: Passing DOMDocumentFragment to DOMDocument::saveHTML() Produces invalid Markup
References: 1  Groups: php.bugs 
Request: Send a blank email to php-bugs+get-183067@lists.php.net to get a copy of this message
Edit report at https://bugs.php.net/bug.php?id=65196&edit=1

 ID:                 65196
 Updated by:         mike@php.net
 Reported by:        baileyp at comcast dot net
 Summary:            Passing DOMDocumentFragment to
                     DOMDocument::saveHTML() Produces invalid Markup
-Status:             Open
+Status:             Closed
 Type:               Bug
 Package:            DOM XML related
 Operating System:   any
 PHP Version:        master-Git-2013-07-03 (Git)
 Block user comment: N
 Private report:     N

 New Comment:

Automatic comment on behalf of mike
Revision: http://git.php.net/?p=php-src.git;a=commit;h=22fa3fbc5ffe75349c0edb6b776b6fb1168cb21c
Log: Fix bug #65196


Previous Comments:
------------------------------------------------------------------------
[2013-10-10 18:43:31] 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-08-01 23:10:34] yohgaki@php.net

Reproduced with Windows7 & Fedora19 & PHP5.5

------------------------------------------------------------------------
[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


Thread (6 messages)

« previous php.bugs (#183067) next »