From 2072d377d3f0380f1f77768746d196106673db8d Mon Sep 17 00:00:00 2001 From: Andrew Nester Date: Tue, 1 May 2018 12:04:46 +0000 Subject: [PATCH 1/5] DOMDocument::formatOutput attribute sometimes ignored (cherry picked from commit ef9ed19ec7f141311feea1d42467f5773cfc09bc) --- 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 2b029dc4037..9c3ece5ac3d 100644 --- a/ext/dom/document.c +++ b/ext/dom/document.c @@ -2148,6 +2148,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; @@ -2174,7 +2175,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; } @@ -2183,20 +2185,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 { @@ -2206,7 +2209,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 From 10d724d82d6471a7808b46c186b5b8ded68e79d5 Mon Sep 17 00:00:00 2001 From: Xinchen Hui Date: Wed, 4 Jul 2018 11:15:32 +0800 Subject: [PATCH 2/5] Fixed build (cherry picked from commit 36f05a80d7cf11fffb827c7f0b6c8e73d3846e8e) --- ext/dom/document.c | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/ext/dom/document.c b/ext/dom/document.c index 9c3ece5ac3d..6bbc0500b91 100644 --- a/ext/dom/document.c +++ b/ext/dom/document.c @@ -2186,7 +2186,11 @@ PHP_FUNCTION(dom_document_save_html) for (node = node->children; node; node = node->next) { htmlNodeDumpFormatOutput(outBuf, docp, node, NULL, format); +#ifdef LIBXML2_NEW_BUFFER one_size = !outBuf->error ? xmlOutputBufferGetSize(outBuf) : -1; +#else + one_size = !outBuf->error ? outBuf->buffer->use : -1; +#endif if (one_size >= 0) { size = one_size; } else { @@ -2196,10 +2200,18 @@ PHP_FUNCTION(dom_document_save_html) } } else { htmlNodeDumpFormatOutput(outBuf, docp, node, NULL, format); +#ifdef LIBXML2_NEW_BUFFER size = !outBuf->error ? xmlOutputBufferGetSize(outBuf): -1; +#else + size = !outBuf->error ? outBuf->buffer->use : -1; +#endif } if (size >= 0) { +#ifdef LIBXML2_NEW_BUFFER mem = (xmlChar*) xmlOutputBufferGetContent(outBuf); +#else + mem = (xmlChar*) outBuf->buffer->content; +#endif if (!mem) { RETVAL_FALSE; } else { From ddd73d4fa8155f8fa8d0d29082136d5719afd1f4 Mon Sep 17 00:00:00 2001 From: Anatol Belski Date: Tue, 14 Aug 2018 14:14:05 +0200 Subject: [PATCH 3/5] Followup fix for ef9ed19e, see also bug #76738 (cherry picked from commit 083285f22a74989689f97d1d53476e7eaec35acc) --- ext/dom/document.c | 26 +++++++------------------- 1 file changed, 7 insertions(+), 19 deletions(-) diff --git a/ext/dom/document.c b/ext/dom/document.c index 6bbc0500b91..e16788072b5 100644 --- a/ext/dom/document.c +++ b/ext/dom/document.c @@ -2152,7 +2152,7 @@ PHP_FUNCTION(dom_document_save_html) xmlBufferPtr buf; dom_object *intern, *nodeobj; xmlChar *mem = NULL; - int size = 0, format; + int format; dom_doc_propsptr doc_props; if (zend_parse_method_parameters(ZEND_NUM_ARGS(), getThis(), @@ -2182,39 +2182,26 @@ PHP_FUNCTION(dom_document_save_html) } if (node->type == XML_DOCUMENT_FRAG_NODE) { - int one_size; - for (node = node->children; node; node = node->next) { htmlNodeDumpFormatOutput(outBuf, docp, node, NULL, format); -#ifdef LIBXML2_NEW_BUFFER - one_size = !outBuf->error ? xmlOutputBufferGetSize(outBuf) : -1; -#else - one_size = !outBuf->error ? outBuf->buffer->use : -1; -#endif - if (one_size >= 0) { - size = one_size; - } else { - size = -1; + if (outBuf->error) { break; } } } else { htmlNodeDumpFormatOutput(outBuf, docp, node, NULL, format); -#ifdef LIBXML2_NEW_BUFFER - size = !outBuf->error ? xmlOutputBufferGetSize(outBuf): -1; -#else - size = !outBuf->error ? outBuf->buffer->use : -1; -#endif } - if (size >= 0) { + if (!outBuf->error) { + xmlOutputBufferFlush(outBuf); #ifdef LIBXML2_NEW_BUFFER - mem = (xmlChar*) xmlOutputBufferGetContent(outBuf); + mem = (xmlChar*) xmlBufferContent(buf); #else mem = (xmlChar*) outBuf->buffer->content; #endif if (!mem) { RETVAL_FALSE; } else { + int size = xmlBufferLength(buf); RETVAL_STRINGL((const char*) mem, size); } } else { @@ -2223,6 +2210,7 @@ PHP_FUNCTION(dom_document_save_html) } xmlOutputBufferClose(outBuf); } else { + int size = 0; #if LIBXML_VERSION >= 20623 htmlDocDumpMemoryFormat(docp, &mem, &size, format); #else From 0c2327b4723c4b05cafea16140fe2c5c3de73599 Mon Sep 17 00:00:00 2001 From: Anatol Belski Date: Tue, 14 Aug 2018 14:33:19 +0200 Subject: [PATCH 4/5] Cleanup (cherry picked from commit 8b3174f256147a1708821621a8cbe2b257fca737) --- ext/dom/document.c | 4 ---- 1 file changed, 4 deletions(-) diff --git a/ext/dom/document.c b/ext/dom/document.c index e16788072b5..aba43f13858 100644 --- a/ext/dom/document.c +++ b/ext/dom/document.c @@ -2193,11 +2193,7 @@ PHP_FUNCTION(dom_document_save_html) } if (!outBuf->error) { xmlOutputBufferFlush(outBuf); -#ifdef LIBXML2_NEW_BUFFER mem = (xmlChar*) xmlBufferContent(buf); -#else - mem = (xmlChar*) outBuf->buffer->content; -#endif if (!mem) { RETVAL_FALSE; } else { From a097f926c8849ca541e989cac27762c92e6797ac Mon Sep 17 00:00:00 2001 From: "Christoph M. Becker" Date: Fri, 17 Aug 2018 13:06:27 +0200 Subject: [PATCH 5/5] [ci skip] Update NEWS Bug #76285 was supposed to have been fixed for 7.3.0alpha3, but that has been reverted for 7.3.0beta2 due to bug #76738. Now that we have a working fix in master, we backport the respective commits. --- NEWS | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/NEWS b/NEWS index 66a302e516e..971a92fb735 100644 --- a/NEWS +++ b/NEWS @@ -8,6 +8,10 @@ PHP NEWS . Fixed bug #76752 (Crash in ZEND_COALESCE_SPEC_TMP_HANDLER - assertion in _get_zval_ptr_tmp failed). (Laruence) +- DOM: + . Fixed bug #76285 (DOMDocument::formatOutput attribute sometimes ignored). + (Andrew Nester, Laruence, Anatol) + - Opcache: . Fixed bug #76747 (Opcache treats path containing "test.pharma.tld" as a phar file). (Laruence)