Skip to content

Commit 366f838

Browse files
committed
Merge branch 'PHP-7.0' into PHP-7.1
* PHP-7.0: fixed bug #50989 (DOM support for LIBXML_NOXMLDECL)
2 parents 19fff2e + 2fcf125 commit 366f838

File tree

3 files changed

+35
-29
lines changed

3 files changed

+35
-29
lines changed

NEWS

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ PHP NEWS
1717
- DOM:
1818
. Fixed bug #54382 (getAttributeNodeNS doesn't get xmlns* attributes).
1919
(aboks)
20+
. Fixed bug #50989 (support for LIBXML_NOXMLDECL). (jhdxr)
2021

2122
- DTrace:
2223
. Fixed bug #73965 (DTrace reported as enabled when disabled). (Remi)

ext/dom/document.c

Lines changed: 22 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626
#include "php.h"
2727
#if HAVE_LIBXML && HAVE_DOM
2828
#include "php_dom.h"
29+
#include <libxml/xmlsave.h>
2930
#include <libxml/SAX.h>
3031
#ifdef LIBXML_SCHEMAS_ENABLED
3132
#include <libxml/relaxng.h>
@@ -1616,59 +1617,51 @@ PHP_FUNCTION(dom_document_savexml)
16161617
dom_doc_propsptr doc_props;
16171618
int size, format, saveempty = 0;
16181619
zend_long options = 0;
1620+
xmlSaveCtxtPtr xscp;
16191621

16201622
if (zend_parse_method_parameters(ZEND_NUM_ARGS(), getThis(), "O|O!l", &id, dom_document_class_entry, &nodep, dom_node_class_entry, &options) == FAILURE) {
16211623
return;
16221624
}
1625+
options = options | XML_SAVE_AS_XML;
16231626

16241627
DOM_GET_OBJ(docp, id, xmlDocPtr, intern);
16251628

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

1632+
buf = xmlBufferCreate();
1633+
if (!buf) {
1634+
php_error_docref(NULL, E_WARNING, "Could not fetch buffer");
1635+
RETURN_FALSE;
1636+
}
1637+
xscp = xmlSaveToBuffer(buf, docp->encoding, options);
1638+
16291639
if (nodep != NULL) {
16301640
/* Dump contents of Node */
16311641
DOM_GET_OBJ(node, nodep, xmlNodePtr, nodeobj);
16321642
if (node->doc != docp) {
16331643
php_dom_throw_error(WRONG_DOCUMENT_ERR, dom_get_strict_error(intern->document));
1644+
xmlBufferFree(buf);
16341645
RETURN_FALSE;
16351646
}
1636-
buf = xmlBufferCreate();
1637-
if (!buf) {
1638-
php_error_docref(NULL, E_WARNING, "Could not fetch buffer");
1639-
RETURN_FALSE;
1640-
}
1641-
if (options & LIBXML_SAVE_NOEMPTYTAG) {
1642-
saveempty = xmlSaveNoEmptyTags;
1643-
xmlSaveNoEmptyTags = 1;
1644-
}
1645-
xmlNodeDump(buf, docp, node, 0, format);
1646-
if (options & LIBXML_SAVE_NOEMPTYTAG) {
1647-
xmlSaveNoEmptyTags = saveempty;
1648-
}
1649-
mem = (xmlChar*) xmlBufferContent(buf);
1650-
if (!mem) {
1647+
if(xmlSaveTree(xscp, node) < 0) {
16511648
xmlBufferFree(buf);
16521649
RETURN_FALSE;
16531650
}
1654-
RETVAL_STRING((char *) mem);
1655-
xmlBufferFree(buf);
16561651
} else {
1657-
if (options & LIBXML_SAVE_NOEMPTYTAG) {
1658-
saveempty = xmlSaveNoEmptyTags;
1659-
xmlSaveNoEmptyTags = 1;
1660-
}
1661-
/* Encoding is handled from the encoding property set on the document */
1662-
xmlDocDumpFormatMemory(docp, &mem, &size, format);
1663-
if (options & LIBXML_SAVE_NOEMPTYTAG) {
1664-
xmlSaveNoEmptyTags = saveempty;
1665-
}
1666-
if (!size || !mem) {
1652+
if(xmlSaveDoc(xscp, docp) < 0) {
1653+
xmlBufferFree(buf);
16671654
RETURN_FALSE;
16681655
}
1669-
RETVAL_STRINGL((char *) mem, size);
1670-
xmlFree(mem);
16711656
}
1657+
xmlSaveClose(xscp);
1658+
mem = (xmlChar*) xmlBufferContent(buf);
1659+
if (!mem) {
1660+
xmlBufferFree(buf);
1661+
RETURN_FALSE;
1662+
}
1663+
RETVAL_STRING((char *) mem);
1664+
xmlBufferFree(buf);
16721665
}
16731666
/* }}} end dom_document_savexml */
16741667

ext/dom/tests/bug50989.phpt

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
--TEST--
2+
Bug #50989 add support LIBXML_NOXMLDECL for DOMDocument::saveXML()
3+
--SKIPIF--
4+
<?php require_once('skipif.inc'); ?>
5+
--FILE--
6+
<?php
7+
$dom = new DomDocument();
8+
$dom->loadXML("<foo />");
9+
10+
print $dom->saveXML(null,LIBXML_NOXMLDECL);
11+
--EXPECT--
12+
<foo/>

0 commit comments

Comments
 (0)