Skip to content

Commit 5b19e1e

Browse files
author
Rob Richards
committed
add node->isSupported()
add domimplementation->hasFeature() add formatOutput property (extends DOM) call xmlFreeDoc when doc is no longer referenced rather than custom code save and savexml now format based on formatOutput property
1 parent 48cc4a7 commit 5b19e1e

File tree

5 files changed

+84
-13
lines changed

5 files changed

+84
-13
lines changed

ext/dom/document.c

Lines changed: 32 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,27 @@ zend_function_entry php_dom_document_class_functions[] = {
7575
};
7676

7777

78+
int dom_document_get_formatting(zval *id TSRMLS_DC) {
79+
zval *format, *member;
80+
zend_object_handlers *std_hnd;
81+
int retformat = 0;
82+
83+
MAKE_STD_ZVAL(member);
84+
ZVAL_STRING(member, "formatOutput", 1);
85+
86+
std_hnd = zend_get_std_object_handlers();
87+
format = std_hnd->read_property(id, member TSRMLS_CC);
88+
89+
if (format->type == IS_BOOL) {
90+
retformat = Z_BVAL_P(format);
91+
}
92+
93+
zval_dtor(member);
94+
FREE_ZVAL(member);
95+
96+
return retformat;
97+
}
98+
7899
/* {{{ proto doctype documenttype
79100
readonly=yes
80101
URL: http://www.w3.org/TR/2003/WD-DOM-Level-3-Core-20030226/DOM3-Core.html#core-ID-B63ED1A31
@@ -1070,7 +1091,7 @@ PHP_FUNCTION(dom_document_save)
10701091
{
10711092
zval *id;
10721093
xmlDoc *docp;
1073-
int file_len, bytes;
1094+
int file_len, bytes, format;
10741095
dom_object *intern;
10751096
char *file;
10761097

@@ -1084,7 +1105,9 @@ PHP_FUNCTION(dom_document_save)
10841105
RETURN_FALSE;
10851106
}
10861107

1087-
bytes = xmlSaveFile(file, docp);
1108+
/* encoding handled by property on doc */
1109+
format = dom_document_get_formatting(id TSRMLS_CC);
1110+
bytes = xmlSaveFormatFileEnc(file, docp, NULL, format);
10881111

10891112
if (bytes == -1) {
10901113
RETURN_FALSE;
@@ -1105,14 +1128,16 @@ PHP_FUNCTION(dom_document_savexml)
11051128
xmlBufferPtr buf;
11061129
xmlChar *mem;
11071130
dom_object *intern, *nodeobj;
1108-
int size;
1131+
int size, format;
11091132

11101133
DOM_GET_THIS_OBJ(docp, id, xmlDocPtr, intern);
11111134

11121135
if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "|o", &nodep) == FAILURE) {
11131136
return;
11141137
}
11151138

1139+
format = dom_document_get_formatting(id TSRMLS_CC);
1140+
11161141
if (nodep != NULL) {
11171142
/* Dump contents of Node */
11181143
DOM_GET_OBJ(node, nodep, xmlNodePtr, nodeobj);
@@ -1126,7 +1151,8 @@ PHP_FUNCTION(dom_document_savexml)
11261151
php_error_docref(NULL TSRMLS_CC, E_WARNING, "Could not fetch buffer");
11271152
RETURN_FALSE;
11281153
}
1129-
xmlNodeDump(buf, docp, node, 0, 0);
1154+
1155+
xmlNodeDump(buf, docp, node, 0, format);
11301156
mem = (xmlChar*) xmlBufferContent(buf);
11311157
if (!mem) {
11321158
xmlBufferFree(buf);
@@ -1135,9 +1161,8 @@ PHP_FUNCTION(dom_document_savexml)
11351161
RETVAL_STRING(mem, 1);
11361162
xmlBufferFree(buf);
11371163
} else {
1138-
/* Dump Document Contents
1139-
Encoding is handled from the encoding property set on the document */
1140-
xmlDocDumpMemory(docp, &mem, &size);
1164+
/* Encoding is handled from the encoding property set on the document */
1165+
xmlDocDumpFormatMemory(docp, &mem, &size, format);
11411166
if (!size) {
11421167
RETURN_FALSE;
11431168
}

ext/dom/domimplementation.c

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,18 @@ URL: http://www.w3.org/TR/2003/WD-DOM-Level-3-Core-20030226/DOM3-Core.html#ID-5C
5050
*/
5151
PHP_FUNCTION(dom_domimplementation_has_feature)
5252
{
53-
DOM_NOT_IMPLEMENTED();
53+
int feature_len, version_len;
54+
char *feature, *version;
55+
56+
if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "ss", &feature, &feature_len, &version, &version_len) == FAILURE) {
57+
return;
58+
}
59+
60+
if (dom_has_feature(feature, version)) {
61+
RETURN_TRUE;
62+
} else {
63+
RETURN_FALSE;
64+
}
5465
}
5566
/* }}} end dom_domimplementation_has_feature */
5667

ext/dom/node.c

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1123,12 +1123,23 @@ PHP_FUNCTION(dom_node_normalize)
11231123

11241124

11251125
/* {{{ proto boolean dom_node_is_supported(string feature, string version);
1126-
URL: http://www.w3.org/TR/2003/WD-DOM-Level-3-Core-20030226/DOM3-Core.html#Level-2-Core-Node-supports
1126+
URL: http://www.w3.org/TR/2003/WD-DOM-Level-3-Core-20030226/DOM3-Core.html#core-Level-2-Core-Node-supports
11271127
Since: DOM Level 2
11281128
*/
11291129
PHP_FUNCTION(dom_node_is_supported)
11301130
{
1131-
DOM_NOT_IMPLEMENTED();
1131+
int feature_len, version_len;
1132+
char *feature, *version;
1133+
1134+
if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "ss", &feature, &feature_len, &version, &version_len) == FAILURE) {
1135+
return;
1136+
}
1137+
1138+
if (dom_has_feature(feature, version)) {
1139+
RETURN_TRUE;
1140+
} else {
1141+
RETURN_FALSE;
1142+
}
11321143
}
11331144
/* }}} end dom_node_is_supported */
11341145

ext/dom/php_dom.c

Lines changed: 26 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -102,8 +102,13 @@ int decrement_document_reference(dom_object *object TSRMLS_DC) {
102102
object->document->refcount--;
103103
ret_refcount = object->document->refcount;
104104
if (ret_refcount == 0) {
105-
dom_clean_nodes(object TSRMLS_CC);
106-
node_free_resource(object->document->ptr TSRMLS_CC);
105+
if (object->document->ptr != NULL) {
106+
dom_clean_nodes(object TSRMLS_CC);
107+
/* No references to Doc so can use xmlFreeDoc
108+
node_free_resource(object->document->ptr TSRMLS_CC); */
109+
xmlFreeDoc((xmlDoc *) object->document->ptr);
110+
object->document->ptr = NULL;
111+
}
107112
efree(object->document);
108113
object->document = NULL;
109114
}
@@ -590,7 +595,7 @@ PHP_MINFO_FUNCTION(dom)
590595
php_info_print_table_start();
591596
php_info_print_table_row(2, "DOM/XML", "enabled");
592597
php_info_print_table_row(2, "DOM/XML API Version", DOM_API_VERSION);
593-
php_info_print_table_row(2, "libxml Version", xmlParserVersion);
598+
php_info_print_table_row(2, "libxml Version", LIBXML_DOTTED_VERSION);
594599
#if defined(LIBXML_HTML_ENABLED)
595600
php_info_print_table_row(2, "HTML Support", "enabled");
596601
#endif
@@ -979,6 +984,10 @@ zval *php_dom_create_object(xmlNodePtr obj, int *found, zval *wrapper_in, zval *
979984

980985

981986
object_init_ex(wrapper, ce);
987+
/* Add object properties not needing function calls */
988+
if (obj->type == XML_DOCUMENT_NODE || obj->type == XML_HTML_DOCUMENT_NODE) {
989+
add_property_bool(wrapper, "formatOutput", 0);
990+
}
982991
intern = (dom_object *)zend_objects_get_address(wrapper TSRMLS_CC);
983992
if (obj->doc != NULL) {
984993
if (domobj != NULL) {
@@ -1019,6 +1028,20 @@ int dom_hierarchy(xmlNodePtr parent, xmlNodePtr child)
10191028
}
10201029
/* }}} end dom_hierarchy */
10211030

1031+
/* {{{ dom_has_feature(char *feature, char *version) */
1032+
int dom_has_feature(char *feature, char *version)
1033+
{
1034+
int retval = 0;
1035+
1036+
if (!(strcmp (version, "1.0") && strcmp (version,"2.0") && strcmp(version, ""))) {
1037+
if ((!strcasecmp(feature, "Core") && strcmp (version, "1.0")) || !strcasecmp(feature, "XML"))
1038+
retval = 1;
1039+
}
1040+
1041+
return retval;
1042+
}
1043+
/* }}} end dom_has_feature */
1044+
10221045
/* {{{ void dom_element_get_elements_by_tag_name_ns_raw(xmlNodePtr nodep, char *ns, char *local, zval **retval TSRMLS_DC) */
10231046
void dom_get_elements_by_tag_name_ns_raw(xmlNodePtr nodep, char *ns, char *local, zval **retval, dom_object *intern TSRMLS_DC)
10241047
{

ext/dom/php_dom.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,7 @@ void dom_normalize (xmlNodePtr nodep TSRMLS_DC);
7373
void dom_get_elements_by_tag_name_ns_raw(xmlNodePtr nodep, char *ns, char *local, zval **retval, dom_object *intern TSRMLS_DC);
7474
void php_dom_create_implementation(zval **retval TSRMLS_DC);
7575
int dom_hierarchy(xmlNodePtr parent, xmlNodePtr child);
76+
int dom_has_feature(char *feature, char *version);
7677

7778
#define DOM_NO_ARGS() \
7879
if (ZEND_NUM_ARGS() != 0) { \

0 commit comments

Comments
 (0)