Req #67948 [Com]: DOMDocument::saveHTML() should accept a DOMNodeList for the $node argument
| From: | thomas at weinert dot info | Date: | Tue, 02 Sep 2014 14:22:45 +0000 |
| Subject: | Req #67948 [Com]: DOMDocument::saveHTML() should accept a DOMNodeList for the $node argument | ||
| References: | 1 | Groups: | php.bugs |
| Request: | Send a blank email to php-bugs+get-187385@lists.php.net to get a copy of this message | ||
Edit report at https://bugs.php.net/bug.php?id=67948&edit=1
ID: 67948
Comment by: thomas at weinert dot info
Reported by: daverandom@php.net
Summary: DOMDocument::saveHTML() should accept a DOMNodeList
for the $node argument
Status: Open
Type: Feature/Change Request
Package: DOM XML related
Operating System: Any
PHP Version: Irrelevant
Block user comment: N
Private report: N
New Comment:
> This would also potentially have uses with XPath results.
Yes, this would be useful for XPath.
But DOMDocument::saveXml() should get the same functionality.
(DOMDocument::createDocumentFragment() allows to import a fragment. This would allow to export one.)
Previous Comments:
------------------------------------------------------------------------
[2014-09-02 12:48:01] daverandom@php.net
Description:
------------
A common usage of DOM is a pattern roughly like this:
// A HTML fragment as a string, probably loaded from external source
$html = <<<HTML
<div>
<span>Some random data</span>
</div>
HTML;
$doc = new DOMDocument;
$doc->loadHTML($html);
// manipulate the data
$data = $doc->saveHTML();
By default, libxml will wrap this fragment in <html><body> tags, meaning that saveHTML()
exports a whole document and not just a fragment, which is not likely to be the desired result.
While it is possible to avoid this when loading the document using the LIBXML_HTML_NOIMPLIED flag,
it's often desirable to accept a DOMDocument as an argument to a routine that does not know
whether this flag was used or not.
It is possible to pass a node to the saveHTML() routine (e.g. the <body> element) but this
exports the container node as well as it's children. as a result, people often write code which
strips this outer element from the fragment using string manipulation, which is error-prone and less
than ideal.
A good way to make this situation easier to handle would be if saveHTML() could also accept a
DOMNodeList, which would mean that *only* the children of an arbitrary element would be easy to
export, using
$node->childNodes.
This would also potentially have uses with XPath results.
Test script:
---------------
<?php
$html = <<<HTML
<div>
<span>Some random data</span>
</div>
HTML;
$doc = new DOMDocument;
$doc->loadHTML($html);
$body = $doc->getElementsByTagName('body')[0];
$body->appendChild($doc->createElement('span', 'more data'));
$data = $doc->saveHTML($body->childNodes);
echo $data;
Expected result:
----------------
<div>
<span>Some random data</span>
<span>more data</span>
</div>
------------------------------------------------------------------------
--
Edit this bug report at https://bugs.php.net/bug.php?id=67948&edit=1