Bug #76285 [Opn->Csd]: DOMDocument::formatOutput attribute sometimes ignored
| From: | ab@php.net | Date: | Thu, 28 Jun 2018 09:43:34 +0000 |
| Subject: | Bug #76285 [Opn->Csd]: DOMDocument::formatOutput attribute sometimes ignored | ||
| References: | 1 | Groups: | php.bugs |
| Request: | Send a blank email to php-bugs+get-216004@lists.php.net to get a copy of this message | ||
Edit report at https://bugs.php.net/bug.php?id=76285&edit=1
ID: 76285
Updated by: ab@php.net
Reported by: daniel dot hemberger at gmail dot com
Summary: DOMDocument::formatOutput attribute sometimes
ignored
-Status: Open
+Status: Closed
Type: Bug
Package: DOM XML related
PHP Version: Irrelevant
-Assigned To:
+Assigned To: ab
Block user comment: N
Private report: N
New Comment:
The linked PR has been merged.
Thanks.
Previous Comments:
------------------------------------------------------------------------
[2018-05-01 12:08:01] andrew dot nester dot dev at gmail dot com
Thanks for reporting the issue!
I added PR fixing this: https://github.com/php/php-src/pull/3229
------------------------------------------------------------------------
[2018-04-29 06:23:16] daniel dot hemberger at gmail dot com
Description:
------------
When using
DOMDocument::saveHTML ([ DOMNode $node = NULL ] ), whether or not the
formatOutput attribute of the class instance is respected depends on whether or not the
$node argument is specified when calling the method.
We can see where the problem arises in the source code (here: https://github.com/php/php-src/blob/701437a94872853026c97b225f9882ed7fa3164c/ext/dom/document.c).
If nodep == NULL (i.e. $node is NOT passed to saveHTML) then
the following function call is made in dom_document_save_html:
htmlDocDumpMemoryFormat(docp, &mem, &size, format);
But if nodep != NULL (i.e. $node IS passed to saveHTML), it
calls:
htmlNodeDump(buf, docp, node);
We can see already that the format variable is not passed into
htmlNodeDump, and thus the output cannot respect the
DOMDocument::formatOutput attribute.
This should be fairly simple to fix, because there is a similar function
htmlNodeDumpFormatOutput which does take an argument specifying the formatting. I think
that the problem is solved by replacing the instances of htmlNodeDump with
htmlNodeDumpFormatOutput and passing the appropriate formatting arguments (see: http://xmlsoft.org/html/libxml-HTMLtree.html#htmlNodeDumpFormatOutput).
Thank you for your time!
Test script:
---------------
<?php
$dom = new DOMDocument();
$dom->formatOutput = false;
$html =
'<div><div><a>test</a></div><div><a>test2</a></div></div>';
$dom->loadHTML($html);
$rootNode = $dom->documentElement;
foreach ($rootNode->firstChild->childNodes as $child) {
var_dump($dom->saveHTML($child));
$dom2 = new DOMDocument();
$dom2->formatOutput = false;
$child2 = $dom2->importNode($child, true);
$dom2->appendChild($child2);
echo "\n";
var_dump($dom2->saveHTML());
}
Expected result:
----------------
string(57)
"<div><div><a>test</a></div><div><a>test2</a></div></div>
"
string(57)
"<div><div><a>test</a></div><div><a>test2</a></div></div>
"
Actual result:
--------------
string(59) "<div>
<div><a>test</a></div>
<div><a>test2</a></div>
</div>"
string(57)
"<div><div><a>test</a></div><div><a>test2</a></div></div>
"
------------------------------------------------------------------------
--
Edit this bug report at https://bugs.php.net/bug.php?id=76285&edit=1