Skip to content

Commit dff05a7

Browse files
committed
Implement LIBXML_NOXMLDECL
Fixes GH-11792.
1 parent 5da08df commit dff05a7

File tree

2 files changed

+55
-18
lines changed

2 files changed

+55
-18
lines changed

ext/dom/document.c

Lines changed: 36 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
#if defined(HAVE_LIBXML) && defined(HAVE_DOM)
2424
#include "php_dom.h"
2525
#include <libxml/SAX.h>
26+
#include <libxml/xmlsave.h>
2627
#ifdef LIBXML_SCHEMAS_ENABLED
2728
#include <libxml/relaxng.h>
2829
#include <libxml/xmlschemas.h>
@@ -1461,8 +1462,7 @@ PHP_METHOD(DOMDocument, saveXML)
14611462
zval *id, *nodep = NULL;
14621463
xmlDoc *docp;
14631464
xmlNode *node;
1464-
xmlBufferPtr buf;
1465-
xmlChar *mem;
1465+
const char *mem;
14661466
dom_object *intern, *nodeobj;
14671467
int size, format, saveempty = 0;
14681468
zend_long options = 0;
@@ -1477,18 +1477,20 @@ PHP_METHOD(DOMDocument, saveXML)
14771477
libxml_doc_props const* doc_props = dom_get_doc_props_read_only(intern->document);
14781478
format = doc_props->formatoutput;
14791479

1480+
xmlBufferPtr buf = xmlBufferCreate();
1481+
if (!buf) {
1482+
php_error_docref(NULL, E_WARNING, "Could not fetch buffer");
1483+
RETURN_FALSE;
1484+
}
1485+
14801486
if (nodep != NULL) {
14811487
/* Dump contents of Node */
14821488
DOM_GET_OBJ(node, nodep, xmlNodePtr, nodeobj);
14831489
if (node->doc != docp) {
1490+
xmlBufferFree(buf);
14841491
php_dom_throw_error(WRONG_DOCUMENT_ERR, dom_get_strict_error(intern->document));
14851492
RETURN_FALSE;
14861493
}
1487-
buf = xmlBufferCreate();
1488-
if (!buf) {
1489-
php_error_docref(NULL, E_WARNING, "Could not fetch buffer");
1490-
RETURN_FALSE;
1491-
}
14921494
if (options & LIBXML_SAVE_NOEMPTYTAG) {
14931495
saveempty = xmlSaveNoEmptyTags;
14941496
xmlSaveNoEmptyTags = 1;
@@ -1497,29 +1499,45 @@ PHP_METHOD(DOMDocument, saveXML)
14971499
if (options & LIBXML_SAVE_NOEMPTYTAG) {
14981500
xmlSaveNoEmptyTags = saveempty;
14991501
}
1500-
mem = (xmlChar*) xmlBufferContent(buf);
1501-
if (!mem) {
1502-
xmlBufferFree(buf);
1503-
RETURN_FALSE;
1504-
}
1505-
RETVAL_STRING((char *) mem);
1506-
xmlBufferFree(buf);
15071502
} else {
15081503
if (options & LIBXML_SAVE_NOEMPTYTAG) {
15091504
saveempty = xmlSaveNoEmptyTags;
15101505
xmlSaveNoEmptyTags = 1;
15111506
}
1507+
int convertedOptions = XML_SAVE_AS_XML;
1508+
if (options & XML_SAVE_NO_DECL) {
1509+
convertedOptions |= XML_SAVE_NO_DECL;
1510+
}
1511+
if (format) {
1512+
convertedOptions |= XML_SAVE_FORMAT;
1513+
}
15121514
/* Encoding is handled from the encoding property set on the document */
1513-
xmlDocDumpFormatMemory(docp, &mem, &size, format);
1515+
xmlSaveCtxtPtr ctxt = xmlSaveToBuffer(buf, (const char *) docp->encoding, convertedOptions);
15141516
if (options & LIBXML_SAVE_NOEMPTYTAG) {
15151517
xmlSaveNoEmptyTags = saveempty;
15161518
}
1517-
if (!size || !mem) {
1519+
if (UNEXPECTED(!ctxt)) {
1520+
xmlBufferFree(buf);
1521+
php_error_docref(NULL, E_WARNING, "Could not create save context");
15181522
RETURN_FALSE;
15191523
}
1520-
RETVAL_STRINGL((char *) mem, size);
1521-
xmlFree(mem);
1524+
if (UNEXPECTED(xmlSaveDoc(ctxt, docp) < 0)) {
1525+
xmlBufferFree(buf);
1526+
(void) xmlSaveClose(ctxt);
1527+
php_error_docref(NULL, E_WARNING, "Could not save document");
1528+
RETURN_FALSE;
1529+
}
1530+
(void) xmlSaveFlush(ctxt);
1531+
(void) xmlSaveClose(ctxt);
1532+
}
1533+
mem = (const char *) xmlBufferContent(buf);
1534+
if (!mem) {
1535+
xmlBufferFree(buf);
1536+
RETURN_FALSE;
15221537
}
1538+
size = xmlBufferLength(buf);
1539+
RETVAL_STRINGL(mem, size);
1540+
xmlBufferFree(buf);
15231541
}
15241542
/* }}} end dom_document_savexml */
15251543

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
--TEST--
2+
DOMDocument::saveXML(): XML_SAVE_NO_DECL
3+
--EXTENSIONS--
4+
dom
5+
--FILE--
6+
<?php
7+
$doc = new DOMDocument();
8+
$doc->loadXML('<root>é</root>');
9+
10+
echo $doc->saveXML(options: 0);
11+
echo $doc->saveXML(options: LIBXML_NOXMLDECL);
12+
$doc->encoding = "BIG5";
13+
echo $doc->saveXML(options: LIBXML_NOXMLDECL);
14+
?>
15+
--EXPECT--
16+
<?xml version="1.0"?>
17+
<root>&#xE9;</root>
18+
<root>&#xE9;</root>
19+
<root>&#233;</root>

0 commit comments

Comments
 (0)