diff --git a/ext/dom/document.c b/ext/dom/document.c index 1a8e5623006..b2d21c1ebf3 100644 --- a/ext/dom/document.c +++ b/ext/dom/document.c @@ -2150,6 +2150,7 @@ PHP_FUNCTION(dom_document_save_html) zval *id, *nodep = NULL; xmlDoc *docp; xmlNode *node; + xmlOutputBufferPtr outBuf; xmlBufferPtr buf; dom_object *intern, *nodeobj; xmlChar *mem = NULL; @@ -2176,7 +2177,8 @@ PHP_FUNCTION(dom_document_save_html) } buf = xmlBufferCreate(); - if (!buf) { + outBuf = xmlOutputBufferCreateBuffer(buf, NULL); + if (!outBuf || !buf) { php_error_docref(NULL, E_WARNING, "Could not fetch buffer"); RETURN_FALSE; } @@ -2185,20 +2187,21 @@ PHP_FUNCTION(dom_document_save_html) int one_size; for (node = node->children; node; node = node->next) { - one_size = htmlNodeDump(buf, docp, node); - + htmlNodeDumpFormatOutput(outBuf, docp, node, NULL, format); + one_size = !outBuf->error ? xmlOutputBufferGetSize(outBuf) : -1; if (one_size >= 0) { - size += one_size; + size = one_size; } else { size = -1; break; } } } else { - size = htmlNodeDump(buf, docp, node); + htmlNodeDumpFormatOutput(outBuf, docp, node, NULL, format); + size = !outBuf->error ? xmlOutputBufferGetSize(outBuf): -1; } if (size >= 0) { - mem = (xmlChar*) xmlBufferContent(buf); + mem = (xmlChar*) xmlOutputBufferGetContent(outBuf); if (!mem) { RETVAL_FALSE; } else { @@ -2208,7 +2211,7 @@ PHP_FUNCTION(dom_document_save_html) php_error_docref(NULL, E_WARNING, "Error dumping HTML node"); RETVAL_FALSE; } - xmlBufferFree(buf); + xmlOutputBufferClose(outBuf); } else { #if LIBXML_VERSION >= 20623 htmlDocDumpMemoryFormat(docp, &mem, &size, format); diff --git a/ext/dom/tests/bug76285.phpt b/ext/dom/tests/bug76285.phpt new file mode 100644 index 00000000000..e6668b10d97 --- /dev/null +++ b/ext/dom/tests/bug76285.phpt @@ -0,0 +1,18 @@ +--TEST-- +Bug #76285 DOMDocument::formatOutput attribute sometimes ignored +--FILE-- +formatOutput = false; +$html = '
test
test2
'; +$dom->loadHTML($html, LIBXML_HTML_NOIMPLIED | LIBXML_HTML_NODEFDTD); +$rootNode = $dom->documentElement; +var_dump($dom->saveHTML($rootNode)); +var_dump($dom->saveHTML()); + +?> +--EXPECT-- +string(56) "
test
test2
" +string(57) "
test
test2
+" \ No newline at end of file