Bug #65196 [Com]: Passing DOMDocumentFragment to DOMDocument::saveHTML() Produces invalid Markup

From: Date: Thu, 10 Oct 2013 18:43:31 +0000
Subject: Bug #65196 [Com]: Passing DOMDocumentFragment to DOMDocument::saveHTML() Produces invalid Markup
References: 1  Groups: php.bugs 
Request: Send a blank email to php-bugs+get-182121@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
 Comment by:         soulodd at yahoo dot com
 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:   any
 PHP Version:        master-Git-2013-07-03 (Git)
 Block user comment: N
 Private report:     N

 New Comment:

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.


Previous Comments:
------------------------------------------------------------------------
[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 (#182121) next »