|  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #76285 DOMDocument::formatOutput attribute sometimes ignored
Submitted: 2018-04-29 06:23 UTC Modified: 2018-06-28 09:43 UTC
From: daniel dot hemberger at gmail dot com Assigned: ab (profile)
Status: Closed Package: DOM XML related
PHP Version: Irrelevant OS:
Private report: No CVE-ID: None
 [2018-04-29 06:23 UTC] daniel dot hemberger at gmail dot com
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: 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:

Thank you for your time!

Test script:
$dom = new DOMDocument();
$dom->formatOutput = false;
$html = '<div><div><a>test</a></div><div><a>test2</a></div></div>';
$rootNode = $dom->documentElement;
foreach ($rootNode->firstChild->childNodes as $child) {
  $dom2 = new DOMDocument();
  $dom2->formatOutput = false;
  $child2 = $dom2->importNode($child, true);
  echo "\n";

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>

string(57) "<div><div><a>test</a></div><div><a>test2</a></div></div>


Add a Patch

Pull Requests

Add a Pull Request


AllCommentsChangesGit/SVN commitsRelated reports
 [2018-05-01 12:08 UTC] andrew dot nester dot dev at gmail dot com
Thanks for reporting the issue!

I added PR fixing this:
 [2018-06-28 09:43 UTC]
-Status: Open +Status: Closed -Assigned To: +Assigned To: ab
 [2018-06-28 09:43 UTC]
The linked PR has been merged.

PHP Copyright © 2001-2023 The PHP Group
All rights reserved.
Last updated: Sat Sep 30 17:01:24 2023 UTC