Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
105 changes: 94 additions & 11 deletions ext/dom/document.c
Original file line number Diff line number Diff line change
Expand Up @@ -132,7 +132,6 @@ ZEND_BEGIN_ARG_INFO_EX(arginfo_dom_document_loadxml, 0, 0, 1)
ZEND_END_ARG_INFO();

ZEND_BEGIN_ARG_INFO_EX(arginfo_dom_document_savexml, 0, 0, 0)
ZEND_ARG_OBJ_INFO(0, node, DOMNode, 1)
ZEND_END_ARG_INFO();

ZEND_BEGIN_ARG_INFO_EX(arginfo_dom_document_construct, 0, 0, 0)
Expand Down Expand Up @@ -1605,19 +1604,31 @@ PHP_FUNCTION(dom_document_savexml)
xmlChar *mem;
dom_object *intern, *nodeobj;
dom_doc_propsptr doc_props;
int size, format, saveempty = 0;
int size, format, saveempty = 0, type = 0, one_size;
zend_long options = 0;
dom_nnodemap_object *objmap;

if (zend_parse_method_parameters(ZEND_NUM_ARGS() TSRMLS_CC, getThis(), "O|O!l", &id, dom_document_class_entry, &nodep, dom_node_class_entry, &options) == FAILURE) {
if (zend_parse_method_parameters(ZEND_NUM_ARGS() TSRMLS_CC, getThis(), "O|o!l", &id, dom_document_class_entry, &nodep, &options) == FAILURE) {
return;
}

if (nodep != NULL) {
if (instanceof_function(Z_OBJCE_P(nodep), dom_node_class_entry TSRMLS_CC)) {
type = 1;
} else if (instanceof_function(Z_OBJCE_P(nodep), dom_nodelist_class_entry TSRMLS_CC)) {
type = 2;
} else {
php_error_docref(NULL TSRMLS_CC, E_WARNING, "Invalid argument, expect DOMNode or DOMNodeList");
RETURN_FALSE;
}
}

DOM_GET_OBJ(docp, id, xmlDocPtr, intern);

doc_props = dom_get_doc_props(intern->document);
format = doc_props->formatoutput;

if (nodep != NULL) {
if (type == 1) {
/* Dump contents of Node */
DOM_GET_OBJ(node, nodep, xmlNodePtr, nodeobj);
if (node->doc != docp) {
Expand All @@ -1644,6 +1655,38 @@ PHP_FUNCTION(dom_document_savexml)
}
RETVAL_STRING(mem);
xmlBufferFree(buf);
} else if (type == 2) {
intern = Z_DOMOBJ_P(nodep);
objmap = (dom_nnodemap_object *)intern->ptr;
node = dom_object_get_node(objmap->baseobj);

buf = xmlBufferCreate();
if (!buf) {
php_error_docref(NULL TSRMLS_CC, E_WARNING, "Could not fetch buffer");
RETURN_FALSE;
}

if (node && node->children) {
node = node->children;

while (node = xmlNextElementSibling(node)) {
one_size = xmlNodeDump(buf, NULL, node, 0, format);
if (one_size >= 0) {
size += one_size;
} else {
size = -1;
break;
}
}
}

if (size == -1) {
RETVAL_FALSE;
} else {
mem = (xmlChar*) xmlBufferContent(buf);
RETVAL_STRINGL((const char*) mem, size);
}
xmlBufferFree(buf);
} else {
if (options & LIBXML_SAVE_NOEMPTYTAG) {
saveempty = xmlSaveNoEmptyTags;
Expand Down Expand Up @@ -2118,21 +2161,31 @@ PHP_FUNCTION(dom_document_save_html)
xmlBufferPtr buf;
dom_object *intern, *nodeobj;
xmlChar *mem = NULL;
int size = 0, format;
int size = 0, format, type = 0, one_size;
dom_doc_propsptr doc_props;
dom_nnodemap_object *objmap;

if (zend_parse_method_parameters(ZEND_NUM_ARGS() TSRMLS_CC, getThis(),
"O|O!", &id, dom_document_class_entry, &nodep, dom_node_class_entry)
== FAILURE) {
if (zend_parse_method_parameters(ZEND_NUM_ARGS() TSRMLS_CC, getThis(), "O|o!", &id, dom_document_class_entry, &nodep) == FAILURE) {
return;
}

if (nodep != NULL) {
if (instanceof_function(Z_OBJCE_P(nodep), dom_node_class_entry TSRMLS_CC)) {
type = 1;
} else if (instanceof_function(Z_OBJCE_P(nodep), dom_nodelist_class_entry TSRMLS_CC)) {
type = 2;
} else {
php_error_docref(NULL TSRMLS_CC, E_WARNING, "Invalid argument, expect DOMNode or DOMNodeList");
RETURN_FALSE;
}
}

DOM_GET_OBJ(docp, id, xmlDocPtr, intern);

doc_props = dom_get_doc_props(intern->document);
format = doc_props->formatoutput;

if (nodep != NULL) {
if (type == 1) {
/* Dump contents of Node */
DOM_GET_OBJ(node, nodep, xmlNodePtr, nodeobj);
if (node->doc != docp) {
Expand All @@ -2147,8 +2200,6 @@ PHP_FUNCTION(dom_document_save_html)
}

if (node->type == XML_DOCUMENT_FRAG_NODE) {
int one_size;

for (node = node->children; node; node = node->next) {
one_size = htmlNodeDump(buf, docp, node);

Expand All @@ -2174,6 +2225,38 @@ PHP_FUNCTION(dom_document_save_html)
RETVAL_FALSE;
}
xmlBufferFree(buf);
} else if (type == 2) {
intern = Z_DOMOBJ_P(nodep);
objmap = (dom_nnodemap_object *)intern->ptr;
node = dom_object_get_node(objmap->baseobj);

buf = xmlBufferCreate();
if (!buf) {
php_error_docref(NULL TSRMLS_CC, E_WARNING, "Could not fetch buffer");
RETURN_FALSE;
}

if (node && node->children) {
node = node->children;

while (node = xmlNextElementSibling(node)) {
one_size = htmlNodeDump(buf, NULL, node);
if (one_size >= 0) {
size += one_size;
} else {
size = -1;
break;
}
}
}

if (size == -1) {
RETVAL_FALSE;
} else {
mem = (xmlChar*) xmlBufferContent(buf);
RETVAL_STRINGL((const char*) mem, size);
}
xmlBufferFree(buf);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This code is nearly identical to the other one, except for htmlNodeDump() vs xmlNodeDump() ... I think we can carve out a static inline xmlBufferPtr *dumpNodeList(...) :)

} else {
#if LIBXML_VERSION >= 20623
htmlDocDumpMemoryFormat(docp, &mem, &size, format);
Expand Down
35 changes: 35 additions & 0 deletions ext/dom/tests/bug67948.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
--TEST--
Test for feature #67948: DOMDocument::saveHTML() should accept a DOMNodeList for the $node argument
--CREDITS--
Boro Sitnikovski <buritomath@yahoo.com>
--FILE--
<?php
$html = <<<HTML
<div>
<span>Some random data</span>
</div>
HTML;

$xml = <<<XML
<div>
<span>Some random data</span>
</div>
XML;

$doc = new DOMDocument;
$doc->loadHTML($html);
$div = $doc->getElementsByTagName('div')->item(0);
$div->appendChild($doc->createElement('span', 'more data'));
$data = $doc->saveHTML($div->childNodes);
var_dump($data);

$doc = new DOMDocument;
$doc->loadXML($xml);
$div = $doc->getElementsByTagName('div')->item(0);
$div->appendChild($doc->createElement('span', 'more data'));
$data = $doc->saveXML($div->childNodes);
var_dump($data);
?>
--EXPECT--
string(51) "<span>Some random data</span><span>more data</span>"
string(51) "<span>Some random data</span><span>more data</span>"