Bug #76285 [NEW]: DOMDocument::formatOutput attribute sometimes ignored
| From: | daniel dot hemberger at gmail dot com | Date: | Sun, 29 Apr 2018 06:23:21 +0000 |
| Subject: | Bug #76285 [NEW]: DOMDocument::formatOutput attribute sometimes ignored | ||
| Groups: | php.bugs | ||
| Request: | Send a blank email to php-bugs+get-214977@lists.php.net to get a copy of this message | ||
From: daniel dot hemberger at gmail dot com
Operating system:
PHP version: Irrelevant
Package: DOM XML related
Bug Type: Bug
Bug description:DOMDocument::formatOutput attribute sometimes ignored
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 bug report at https://bugs.php.net/bug.php?id=76285&edit=1
--
Try a snapshot (PHP 5.4): https://bugs.php.net/fix.php?id=76285&r=trysnapshot54
Try a snapshot (PHP 5.5): https://bugs.php.net/fix.php?id=76285&r=trysnapshot55
Try a snapshot (trunk): https://bugs.php.net/fix.php?id=76285&r=trysnapshottrunk
Fixed in SVN: https://bugs.php.net/fix.php?id=76285&r=fixed
Fixed in release: https://bugs.php.net/fix.php?id=76285&r=alreadyfixed
Need backtrace: https://bugs.php.net/fix.php?id=76285&r=needtrace
Need Reproduce Script: https://bugs.php.net/fix.php?id=76285&r=needscript
Try newer version: https://bugs.php.net/fix.php?id=76285&r=oldversion
Not developer issue: https://bugs.php.net/fix.php?id=76285&r=support
Expected behavior: https://bugs.php.net/fix.php?id=76285&r=notwrong
Not enough info: https://bugs.php.net/fix.php?id=76285&r=notenoughinfo
Submitted twice: https://bugs.php.net/fix.php?id=76285&r=submittedtwice
register_globals: https://bugs.php.net/fix.php?id=76285&r=globals
PHP 4 support discontinued: https://bugs.php.net/fix.php?id=76285&r=php4
Daylight Savings: https://bugs.php.net/fix.php?id=76285&r=dst
IIS Stability: https://bugs.php.net/fix.php?id=76285&r=isapi
Install GNU Sed: https://bugs.php.net/fix.php?id=76285&r=gnused
Floating point limitations: https://bugs.php.net/fix.php?id=76285&r=float
No Zend Extensions: https://bugs.php.net/fix.php?id=76285&r=nozend
MySQL Configuration Error: https://bugs.php.net/fix.php?id=76285&r=mysqlcfg