Skip to content

Commit 2072d37

Browse files
andrewnestercmb69
authored andcommitted
DOMDocument::formatOutput attribute sometimes ignored
(cherry picked from commit ef9ed19)
1 parent 5c1a2d8 commit 2072d37

File tree

2 files changed

+28
-7
lines changed

2 files changed

+28
-7
lines changed

ext/dom/document.c

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2148,6 +2148,7 @@ PHP_FUNCTION(dom_document_save_html)
21482148
zval *id, *nodep = NULL;
21492149
xmlDoc *docp;
21502150
xmlNode *node;
2151+
xmlOutputBufferPtr outBuf;
21512152
xmlBufferPtr buf;
21522153
dom_object *intern, *nodeobj;
21532154
xmlChar *mem = NULL;
@@ -2174,7 +2175,8 @@ PHP_FUNCTION(dom_document_save_html)
21742175
}
21752176

21762177
buf = xmlBufferCreate();
2177-
if (!buf) {
2178+
outBuf = xmlOutputBufferCreateBuffer(buf, NULL);
2179+
if (!outBuf || !buf) {
21782180
php_error_docref(NULL, E_WARNING, "Could not fetch buffer");
21792181
RETURN_FALSE;
21802182
}
@@ -2183,20 +2185,21 @@ PHP_FUNCTION(dom_document_save_html)
21832185
int one_size;
21842186

21852187
for (node = node->children; node; node = node->next) {
2186-
one_size = htmlNodeDump(buf, docp, node);
2187-
2188+
htmlNodeDumpFormatOutput(outBuf, docp, node, NULL, format);
2189+
one_size = !outBuf->error ? xmlOutputBufferGetSize(outBuf) : -1;
21882190
if (one_size >= 0) {
2189-
size += one_size;
2191+
size = one_size;
21902192
} else {
21912193
size = -1;
21922194
break;
21932195
}
21942196
}
21952197
} else {
2196-
size = htmlNodeDump(buf, docp, node);
2198+
htmlNodeDumpFormatOutput(outBuf, docp, node, NULL, format);
2199+
size = !outBuf->error ? xmlOutputBufferGetSize(outBuf): -1;
21972200
}
21982201
if (size >= 0) {
2199-
mem = (xmlChar*) xmlBufferContent(buf);
2202+
mem = (xmlChar*) xmlOutputBufferGetContent(outBuf);
22002203
if (!mem) {
22012204
RETVAL_FALSE;
22022205
} else {
@@ -2206,7 +2209,7 @@ PHP_FUNCTION(dom_document_save_html)
22062209
php_error_docref(NULL, E_WARNING, "Error dumping HTML node");
22072210
RETVAL_FALSE;
22082211
}
2209-
xmlBufferFree(buf);
2212+
xmlOutputBufferClose(outBuf);
22102213
} else {
22112214
#if LIBXML_VERSION >= 20623
22122215
htmlDocDumpMemoryFormat(docp, &mem, &size, format);

ext/dom/tests/bug76285.phpt

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
--TEST--
2+
Bug #76285 DOMDocument::formatOutput attribute sometimes ignored
3+
--FILE--
4+
<?php
5+
6+
$dom = new DOMDocument();
7+
$dom->formatOutput = false;
8+
$html = '<div><div><a>test</a></div><div><a>test2</a></div></div>';
9+
$dom->loadHTML($html, LIBXML_HTML_NOIMPLIED | LIBXML_HTML_NODEFDTD);
10+
$rootNode = $dom->documentElement;
11+
var_dump($dom->saveHTML($rootNode));
12+
var_dump($dom->saveHTML());
13+
14+
?>
15+
--EXPECT--
16+
string(56) "<div><div><a>test</a></div><div><a>test2</a></div></div>"
17+
string(57) "<div><div><a>test</a></div><div><a>test2</a></div></div>
18+
"

0 commit comments

Comments
 (0)