Req #67948 [Com]: DOMDocument::saveHTML() should accept a DOMNodeList for the $node argument

From: Date: Sat, 22 Apr 2023 07:30:22 +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-244248@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: buzysreports at gmail dot com 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: Buzyrepoters are sharing latest news about Business, Finance, Education, Entertainment, Gaming, Mobile, tech, Realstate etc. More info to visit: (https://buzyrepoters.com)github.com Previous Comments: ------------------------------------------------------------------------ [2021-10-11 15:50:03] cmb@php.net A working userland alternative is to append all child nodes to a new DOMDocumentFragment, and to export that: <?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')); $frag = $doc->createDocumentFragment(); foreach ($body->childNodes as $node) { $frag->appendChild($node); } $data = $doc->saveHTML($frag); echo $data; ?> ------------------------------------------------------------------------ [2014-09-02 14:22:45] thomas at weinert dot info > 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.) ------------------------------------------------------------------------ [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

« previous php.bugs (#244248) next »