From 51a7b13dde6a67202173f0b396f06c5ce6b46fec Mon Sep 17 00:00:00 2001 From: Andrew Nester Date: Tue, 1 May 2018 12:04:46 +0000 Subject: [PATCH] DOMDocument::formatOutput attribute sometimes ignored --- ext/dom/document.c | 17 ++++++++++------- ext/dom/tests/bug76285.phpt | 18 ++++++++++++++++++ 2 files changed, 28 insertions(+), 7 deletions(-) create mode 100644 ext/dom/tests/bug76285.phpt diff --git a/ext/dom/document.c b/ext/dom/document.c index fdb71f67389dd..7bc7b60558ca2 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 0000000000000..e6668b10d972f --- /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