Skip to content

Commit dfde0d4

Browse files
committed
Handle dumping node to file
1 parent 0c490ad commit dfde0d4

File tree

5 files changed

+46
-20
lines changed

5 files changed

+46
-20
lines changed

ext/dom/tests/modern/common/namespace_sxe_interaction.phpt

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,11 @@ var_dump($dom->documentElement->firstElementChild->firstElementChild->lookupName
2424
echo "=== serialize SimpleXML ===\n";
2525

2626
echo $sxe->saveXML(), "\n";
27+
echo $sxe->foo->saveXML(), "\n";
28+
$sxe->asXML(__DIR__ . "/namespace_sxe_interaction1.xml");
29+
$sxe->foo->asXML(__DIR__ . "/namespace_sxe_interaction2.xml");
30+
echo file_get_contents(__DIR__ . "/namespace_sxe_interaction1.xml"), "\n";
31+
echo file_get_contents(__DIR__ . "/namespace_sxe_interaction2.xml"), "\n";
2732

2833
echo "=== serialize DOM ===\n";
2934

@@ -36,18 +41,27 @@ $new_dom = Dom\XMLDocument::createEmpty();
3641
$new_dom->append($new_dom->importNode($dom->documentElement, true));
3742
echo $new_dom->saveXML(), "\n";
3843

44+
?>
45+
--CLEAN--
46+
<?php
47+
@unlink(__DIR__ . "/namespace_sxe_interaction1.xml");
48+
@unlink(__DIR__ . "/namespace_sxe_interaction2.xml");
3949
?>
4050
--EXPECT--
4151
namespace c: string(5) "urn:c"
4252
namespace b: string(5) "urn:b"
4353
namespace a: NULL
4454
=== serialize SimpleXML ===
4555
<?xml version="1.0" encoding="UTF-8"?>
46-
<root xmlns:a="urn:a" a:attr="value"><b:child xmlns:b="urn:b">value<c:child xmlns:c="urn:c"/></b:child></root>
56+
<root xmlns:a="urn:a" a:attr="value"><b:child xmlns:b="urn:b">value<c:child xmlns:c="urn:c"/></b:child><foo>value2</foo></root>
57+
<foo>value2</foo>
58+
<?xml version="1.0" encoding="UTF-8"?>
59+
<root xmlns:a="urn:a" a:attr="value"><b:child xmlns:b="urn:b">value<c:child xmlns:c="urn:c"/></b:child><foo>value2</foo></root>
60+
<foo>value2</foo>
4761
=== serialize DOM ===
4862
<?xml version="1.0" encoding="UTF-8"?>
49-
<root xmlns:a="urn:a" a:attr="value"><b:child xmlns:b="urn:b">value<c:child xmlns:c="urn:c"/></b:child></root>
63+
<root xmlns:a="urn:a" a:attr="value"><b:child xmlns:b="urn:b">value<c:child xmlns:c="urn:c"/></b:child><foo>value2</foo></root>
5064

5165
=== serialize imported DOM ===
5266
<?xml version="1.0" encoding="UTF-8"?>
53-
<root xmlns:a="urn:a" a:attr="value"><b:child xmlns:b="urn:b">value<c:child xmlns:c="urn:c"/></b:child></root>
67+
<root xmlns:a="urn:a" a:attr="value"><b:child xmlns:b="urn:b">value<c:child xmlns:c="urn:c"/></b:child><foo>value2</foo></root>

ext/dom/xml_document.c

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -293,7 +293,7 @@ static zend_string *php_new_dom_dump_doc_to_str(xmlDocPtr doc, int options, cons
293293
return php_new_dom_dump_node_to_str(doc, (xmlNodePtr) doc, options & XML_SAVE_FORMAT, encoding);
294294
}
295295

296-
static zend_long php_new_dom_dump_doc_to_file(const char *filename, xmlDocPtr doc, bool format, const char *encoding)
296+
zend_long php_new_dom_dump_node_to_file(const char *filename, xmlDocPtr doc, xmlNodePtr node, bool format, const char *encoding)
297297
{
298298
xmlCharEncodingHandlerPtr handler = xmlFindCharEncodingHandler(encoding);
299299
xmlOutputBufferPtr out = xmlOutputBufferCreateFilename(filename, handler, 0);
@@ -307,7 +307,7 @@ static zend_long php_new_dom_dump_doc_to_file(const char *filename, xmlDocPtr do
307307
int status = -1;
308308
xmlSaveCtxtPtr ctxt = xmlSaveToIO(out->writecallback, NULL, stream, encoding, XML_SAVE_AS_XML);
309309
if (EXPECTED(ctxt != NULL)) {
310-
status = dom_xml_serialize(ctxt, out, (xmlNodePtr) doc, format);
310+
status = dom_xml_serialize(ctxt, out, node, format);
311311
status |= xmlOutputBufferFlush(out);
312312
(void) xmlSaveClose(ctxt);
313313
}
@@ -319,9 +319,15 @@ static zend_long php_new_dom_dump_doc_to_file(const char *filename, xmlDocPtr do
319319
return status < 0 ? status : (zend_long) offset;
320320
}
321321

322+
static zend_long php_new_dom_dump_doc_to_file(const char *filename, xmlDocPtr doc, bool format, const char *encoding)
323+
{
324+
return php_new_dom_dump_node_to_file(filename, doc, (xmlNodePtr) doc, format, encoding);
325+
}
326+
322327
static const php_libxml_document_handlers php_new_dom_default_document_handlers = {
323328
.dump_node_to_str = php_new_dom_dump_node_to_str,
324329
.dump_doc_to_str = php_new_dom_dump_doc_to_str,
330+
.dump_node_to_file = php_new_dom_dump_node_to_file,
325331
.dump_doc_to_file = php_new_dom_dump_doc_to_file,
326332
};
327333

ext/libxml/libxml.c

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,7 @@ static zend_result php_libxml_post_deactivate(void);
8383

8484
static zend_string *php_libxml_default_dump_node_to_str(xmlDocPtr doc, xmlNodePtr node, bool format, const char *encoding);
8585
static zend_string *php_libxml_default_dump_doc_to_str(xmlDocPtr doc, int options, const char *encoding);
86+
static zend_long php_libxml_dump_node_to_file(const char *filename, xmlDocPtr doc, xmlNodePtr node, bool format, const char *encoding);
8687
static zend_long php_libxml_default_dump_doc_to_file(const char *filename, xmlDocPtr doc, bool format, const char *encoding);
8788

8889
/* }}} */
@@ -109,6 +110,7 @@ zend_module_entry libxml_module_entry = {
109110
static const php_libxml_document_handlers php_libxml_default_document_handlers = {
110111
.dump_node_to_str = php_libxml_default_dump_node_to_str,
111112
.dump_doc_to_str = php_libxml_default_dump_doc_to_str,
113+
.dump_node_to_file = php_libxml_dump_node_to_file,
112114
.dump_doc_to_file = php_libxml_default_dump_doc_to_file,
113115
};
114116

@@ -1540,6 +1542,17 @@ static zend_long php_libxml_default_dump_doc_to_file(const char *filename, xmlDo
15401542
return xmlSaveFormatFileEnc(filename, doc, encoding, format);
15411543
}
15421544

1545+
static zend_long php_libxml_dump_node_to_file(const char *filename, xmlDocPtr doc, xmlNodePtr node, bool format, const char *encoding)
1546+
{
1547+
xmlOutputBufferPtr outbuf = xmlOutputBufferCreateFilename(filename, NULL, 0);
1548+
if (!outbuf) {
1549+
return -1;
1550+
}
1551+
1552+
xmlNodeDumpOutput(outbuf, doc, node, 0, format, encoding);
1553+
return xmlOutputBufferClose(outbuf);
1554+
}
1555+
15431556
#if defined(PHP_WIN32) && defined(COMPILE_DL_LIBXML)
15441557
PHP_LIBXML_API BOOL WINAPI DllMain(HINSTANCE hinstDLL, DWORD fdwReason, LPVOID lpvReserved)
15451558
{

ext/libxml/php_libxml.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,7 @@ typedef struct _php_libxml_private_data_header {
7575
typedef struct php_libxml_document_handlers {
7676
zend_string *(*dump_node_to_str)(xmlDocPtr doc, xmlNodePtr node, bool format, const char *encoding);
7777
zend_string *(*dump_doc_to_str)(xmlDocPtr doc, int options, const char *encoding);
78+
zend_long (*dump_node_to_file)(const char *filename, xmlDocPtr doc, xmlNodePtr node, bool format, const char *encoding);
7879
zend_long (*dump_doc_to_file)(const char *filename, xmlDocPtr doc, bool format, const char *encoding);
7980
} php_libxml_document_handlers;
8081

ext/simplexml/simplexml.c

Lines changed: 7 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1342,7 +1342,6 @@ PHP_METHOD(SimpleXMLElement, asXML)
13421342
{
13431343
php_sxe_object *sxe;
13441344
xmlNodePtr node;
1345-
xmlOutputBufferPtr outbuf;
13461345
char *filename = NULL;
13471346
size_t filename_len;
13481347

@@ -1361,22 +1360,15 @@ PHP_METHOD(SimpleXMLElement, asXML)
13611360
xmlDocPtr doc = sxe->document->ptr;
13621361

13631362
if (filename) {
1363+
zend_long bytes;
13641364
if (node->parent && (XML_DOCUMENT_NODE == node->parent->type)) {
1365-
zend_long bytes = sxe->document->handlers->dump_doc_to_file(filename, doc, false, (const char *) doc->encoding);
1366-
if (bytes == -1) {
1367-
RETURN_FALSE;
1368-
} else {
1369-
RETURN_TRUE;
1370-
}
1365+
bytes = sxe->document->handlers->dump_doc_to_file(filename, doc, false, (const char *) doc->encoding);
1366+
} else {
1367+
bytes = sxe->document->handlers->dump_node_to_file(filename, doc, node, false, NULL);
1368+
}
1369+
if (bytes == -1) {
1370+
RETURN_FALSE;
13711371
} else {
1372-
outbuf = xmlOutputBufferCreateFilename(filename, NULL, 0);
1373-
1374-
if (outbuf == NULL) {
1375-
RETURN_FALSE;
1376-
}
1377-
1378-
xmlNodeDumpOutput(outbuf, (xmlDocPtr) sxe->document->ptr, node, 0, 0, NULL);
1379-
xmlOutputBufferClose(outbuf);
13801372
RETURN_TRUE;
13811373
}
13821374
}

0 commit comments

Comments
 (0)