diff options
author | Pierre Joye <pajoye@php.net> | 2007-05-08 21:41:36 +0000 |
---|---|---|
committer | Pierre Joye <pajoye@php.net> | 2007-05-08 21:41:36 +0000 |
commit | 223cfe342f5210fd67cb26b96c0e78b0ad1931d1 (patch) | |
tree | f0273d1c16fe20852d351713bead2d570ec520b0 /ext/xmlwriter | |
parent | 75b5ce3b8ddc035aefedab8ccc505d0ccbac003a (diff) | |
download | php-git-223cfe342f5210fd67cb26b96c0e78b0ad1931d1.tar.gz |
- MFH: #41326, Writing empty tags with Xmlwriter::WriteElement[ns]
Diffstat (limited to 'ext/xmlwriter')
-rw-r--r-- | ext/xmlwriter/php_xmlwriter.c | 42 | ||||
-rw-r--r-- | ext/xmlwriter/tests/bug41287.phpt | 12 | ||||
-rw-r--r-- | ext/xmlwriter/tests/bug41326.phpt | 44 |
3 files changed, 82 insertions, 16 deletions
diff --git a/ext/xmlwriter/php_xmlwriter.c b/ext/xmlwriter/php_xmlwriter.c index 169ae0b1a8..1d8fd99961 100644 --- a/ext/xmlwriter/php_xmlwriter.c +++ b/ext/xmlwriter/php_xmlwriter.c @@ -695,7 +695,6 @@ static PHP_FUNCTION(xmlwriter_start_element) } /* }}} */ - /* {{{ proto bool xmlwriter_start_element_ns(resource xmlwriter, string prefix, string name, string uri) Create start namespaced element tag - returns FALSE on error */ static PHP_FUNCTION(xmlwriter_start_element_ns) @@ -756,20 +755,21 @@ static PHP_FUNCTION(xmlwriter_full_end_element) } /* }}} */ -/* {{{ proto bool xmlwriter_write_element(resource xmlwriter, string name, string content) +/* {{{ proto bool xmlwriter_write_element(resource xmlwriter, string name[, string content]) Write full element tag - returns FALSE on error */ static PHP_FUNCTION(xmlwriter_write_element) { zval *pind; xmlwriter_object *intern; xmlTextWriterPtr ptr; - char *name, *content; + char *name, *content = NULL; int name_len, content_len, retval; + #ifdef ZEND_ENGINE_2 zval *this = getThis(); if (this) { - if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "ss", + if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "s|s", &name, &name_len, &content, &content_len) == FAILURE) { return; } @@ -789,7 +789,18 @@ static PHP_FUNCTION(xmlwriter_write_element) ptr = intern->ptr; if (ptr) { - retval = xmlTextWriterWriteElement(ptr, (xmlChar *)name, (xmlChar *)content); + if (!content || content_len < 1) { + retval = xmlTextWriterStartElement(ptr, (xmlChar *)name); + if (retval == -1) { + RETURN_FALSE; + } + xmlTextWriterEndElement(ptr); + if (retval == -1) { + RETURN_FALSE; + } + } else { + retval = xmlTextWriterWriteElement(ptr, (xmlChar *)name, (xmlChar *)content); + } if (retval != -1) { RETURN_TRUE; } @@ -799,21 +810,21 @@ static PHP_FUNCTION(xmlwriter_write_element) } /* }}} */ -/* {{{ proto bool xmlwriter_write_element_ns(resource xmlwriter, string prefix, string name, string uri, string content) +/* {{{ proto bool xmlwriter_write_element_ns(resource xmlwriter, string prefix, string name, string uri[, string content]) Write full namesapced element tag - returns FALSE on error */ static PHP_FUNCTION(xmlwriter_write_element_ns) { zval *pind; xmlwriter_object *intern; xmlTextWriterPtr ptr; - char *name, *prefix, *uri, *content; + char *name, *prefix, *uri, *content = NULL; int name_len, prefix_len, uri_len, content_len, retval; #ifdef ZEND_ENGINE_2 zval *this = getThis(); if (this) { - if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "s!ss!s", + if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "s!ss!|s", &prefix, &prefix_len, &name, &name_len, &uri, &uri_len, &content, &content_len) == FAILURE) { return; } @@ -821,7 +832,7 @@ static PHP_FUNCTION(xmlwriter_write_element_ns) } else #endif { - if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "rs!ss!s", &pind, + if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "rs!ss!|s", &pind, &prefix, &prefix_len, &name, &name_len, &uri, &uri_len, &content, &content_len) == FAILURE) { return; } @@ -833,7 +844,18 @@ static PHP_FUNCTION(xmlwriter_write_element_ns) ptr = intern->ptr; if (ptr) { - retval = xmlTextWriterWriteElementNS(ptr, (xmlChar *)prefix, (xmlChar *)name, (xmlChar *)uri, (xmlChar *)content); + if (!content || content_len < 1) { + retval = xmlTextWriterStartElementNS(ptr,(xmlChar *)prefix, (xmlChar *)name, (xmlChar *)uri); + if (retval == -1) { + RETURN_FALSE; + } + retval = xmlTextWriterEndElement(ptr); + if (retval == -1) { + RETURN_FALSE; + } + } else { + retval = xmlTextWriterWriteElementNS(ptr, (xmlChar *)prefix, (xmlChar *)name, (xmlChar *)uri, (xmlChar *)content); + } if (retval != -1) { RETURN_TRUE; } diff --git a/ext/xmlwriter/tests/bug41287.phpt b/ext/xmlwriter/tests/bug41287.phpt index 106ac3a3ec..3bd09beb84 100644 --- a/ext/xmlwriter/tests/bug41287.phpt +++ b/ext/xmlwriter/tests/bug41287.phpt @@ -30,14 +30,14 @@ print $xw->flush(true); --EXPECTF-- <?xml version="1.0"?> <test:test xmlns:test="urn:x-test:"> - <test:foo></test:foo> - <bar xmlns="urn:x-test:"></bar> - <bar xmlns=""></bar> + <test:foo/> + <bar xmlns="urn:x-test:"/> + <bar xmlns=""/> </test:test> <?xml version="1.0"?> <test:test xmlns:test="urn:x-test:"> - <test:foo></test:foo> - <bar xmlns="urn:x-test:"></bar> - <bar xmlns=""></bar> + <test:foo/> + <bar xmlns="urn:x-test:"/> + <bar xmlns=""/> </test:test> diff --git a/ext/xmlwriter/tests/bug41326.phpt b/ext/xmlwriter/tests/bug41326.phpt new file mode 100644 index 0000000000..f4a451fa3c --- /dev/null +++ b/ext/xmlwriter/tests/bug41326.phpt @@ -0,0 +1,44 @@ +--TEST-- +Bug #41287 (Writing empty tags with Xmlwriter::WriteElement[ns]) +--FILE-- +<?php +$xml = new XmlWriter(); +$xml->openMemory(); +$xml->setIndent(true); +$xml->startDocument(); +$xml->startElement('test'); +$xml->writeElement('foo', null); +$xml->writeElement('foo2', null); +$xml->startElement('bar'); +$xml->endElement('bar'); +$xml->endElement(); +print $xml->flush(true); + +print "\n"; + +$xw = new XMLWriter(); +$xw->openMemory(); +$xw->setIndent(true); +$xw->startDocument(); +$xw->startElementNS('test', 'test', 'urn:x-test:'); +$xw->writeElementNS('test', 'foo', null, ''); +$xw->writeElementNS(null, 'bar', 'urn:x-test:', ''); +$xw->writeElementNS(null, 'bar', '', ''); +$xw->endElement(); +$xw->endDocument(); +print $xw->flush(true); +?> +--EXPECTF-- +<?xml version="1.0"?> +<test> + <foo/> + <foo2/> + <bar/> +</test> + +<?xml version="1.0"?> +<test:test xmlns:test="urn:x-test:"> + <test:foo/> + <bar xmlns="urn:x-test:"/> + <bar xmlns=""/> +</test:test> |