xref: /PHP-5.3/ext/dom/documentfragment.c (revision a2045ff3)
1 /*
2    +----------------------------------------------------------------------+
3    | PHP Version 5                                                        |
4    +----------------------------------------------------------------------+
5    | Copyright (c) 1997-2013 The PHP Group                                |
6    +----------------------------------------------------------------------+
7    | This source file is subject to version 3.01 of the PHP license,      |
8    | that is bundled with this package in the file LICENSE, and is        |
9    | available through the world-wide-web at the following url:           |
10    | http://www.php.net/license/3_01.txt                                  |
11    | If you did not receive a copy of the PHP license and are unable to   |
12    | obtain it through the world-wide-web, please send a note to          |
13    | license@php.net so we can mail you a copy immediately.               |
14    +----------------------------------------------------------------------+
15    | Authors: Christian Stocker <chregu@php.net>                          |
16    |          Rob Richards <rrichards@php.net>                            |
17    +----------------------------------------------------------------------+
18 */
19 
20 /* $Id$ */
21 
22 #ifdef HAVE_CONFIG_H
23 #include "config.h"
24 #endif
25 
26 #include "php.h"
27 #if HAVE_LIBXML && HAVE_DOM
28 #include "php_dom.h"
29 
30 /* {{{ arginfo */
31 ZEND_BEGIN_ARG_INFO_EX(arginfo_dom_documentfragement_construct, 0, 0, 0)
32 ZEND_END_ARG_INFO();
33 
34 ZEND_BEGIN_ARG_INFO_EX(arginfo_dom_documentfragement_appendXML, 0, 0, 1)
35 	ZEND_ARG_INFO(0, data)
36 ZEND_END_ARG_INFO();
37 /* }}} */
38 
39 /*
40 * class DOMDocumentFragment extends DOMNode
41 *
42 * URL: http://www.w3.org/TR/2003/WD-DOM-Level-3-Core-20030226/DOM3-Core.html#ID-B63ED1A3
43 * Since:
44 */
45 
46 const zend_function_entry php_dom_documentfragment_class_functions[] = {
47 	PHP_ME(domdocumentfragment, __construct, arginfo_dom_documentfragement_construct, ZEND_ACC_PUBLIC)
48 	PHP_ME(domdocumentfragment, appendXML, arginfo_dom_documentfragement_appendXML, ZEND_ACC_PUBLIC)
49 	PHP_FE_END
50 };
51 
52 /* {{{ proto void DOMDocumentFragment::__construct(); */
PHP_METHOD(domdocumentfragment,__construct)53 PHP_METHOD(domdocumentfragment, __construct)
54 {
55 
56 	zval *id;
57 	xmlNodePtr nodep = NULL, oldnode = NULL;
58 	dom_object *intern;
59 	zend_error_handling error_handling;
60 
61 	zend_replace_error_handling(EH_THROW, dom_domexception_class_entry, &error_handling TSRMLS_CC);
62 	if (zend_parse_method_parameters(ZEND_NUM_ARGS() TSRMLS_CC, getThis(), "O", &id, dom_documentfragment_class_entry) == FAILURE) {
63 		zend_restore_error_handling(&error_handling TSRMLS_CC);
64 		return;
65 	}
66 
67 	zend_restore_error_handling(&error_handling TSRMLS_CC);
68 	nodep = xmlNewDocFragment(NULL);
69 
70 	if (!nodep) {
71 		php_dom_throw_error(INVALID_STATE_ERR, 1 TSRMLS_CC);
72 		RETURN_FALSE;
73 	}
74 
75 	intern = (dom_object *)zend_object_store_get_object(id TSRMLS_CC);
76 	if (intern != NULL) {
77 		oldnode = dom_object_get_node(intern);
78 		if (oldnode != NULL) {
79 			php_libxml_node_free_resource(oldnode  TSRMLS_CC);
80 		}
81 		/* php_dom_set_object(intern, nodep TSRMLS_CC); */
82 		php_libxml_increment_node_ptr((php_libxml_node_object *)intern, nodep, (void *)intern TSRMLS_CC);
83 	}
84 }
85 /* }}} end DOMDocumentFragment::__construct */
86 
87 /* php_dom_xmlSetTreeDoc is a custom implementation of xmlSetTreeDoc
88  needed for hack in appendXML due to libxml bug - no need to share this function */
php_dom_xmlSetTreeDoc(xmlNodePtr tree,xmlDocPtr doc)89 static void php_dom_xmlSetTreeDoc(xmlNodePtr tree, xmlDocPtr doc) /* {{{ */
90 {
91     xmlAttrPtr prop;
92 	xmlNodePtr cur;
93 
94     if (tree) {
95 		if(tree->type == XML_ELEMENT_NODE) {
96 			prop = tree->properties;
97 			while (prop != NULL) {
98 				prop->doc = doc;
99 				if (prop->children) {
100 					cur = prop->children;
101 					while (cur != NULL) {
102 						php_dom_xmlSetTreeDoc(cur, doc);
103 						cur = cur->next;
104 					}
105 				}
106 				prop = prop->next;
107 			}
108 		}
109 		if (tree->children != NULL) {
110 			cur = tree->children;
111 			while (cur != NULL) {
112 				php_dom_xmlSetTreeDoc(cur, doc);
113 				cur = cur->next;
114 			}
115 		}
116 		tree->doc = doc;
117     }
118 }
119 /* }}} */
120 
121 /* {{{ proto void DOMDocumentFragment::appendXML(string data); */
PHP_METHOD(domdocumentfragment,appendXML)122 PHP_METHOD(domdocumentfragment, appendXML) {
123 	zval *id;
124 	xmlNode *nodep;
125 	dom_object *intern;
126 	char *data = NULL;
127 	int data_len = 0;
128 	int err;
129 	xmlNodePtr lst;
130 
131 	if (zend_parse_method_parameters(ZEND_NUM_ARGS() TSRMLS_CC, getThis(), "Os", &id, dom_documentfragment_class_entry, &data, &data_len) == FAILURE) {
132 		return;
133 	}
134 
135 	DOM_GET_OBJ(nodep, id, xmlNodePtr, intern);
136 
137 	if (dom_node_is_read_only(nodep) == SUCCESS) {
138 		php_dom_throw_error(NO_MODIFICATION_ALLOWED_ERR, dom_get_strict_error(intern->document) TSRMLS_CC);
139 		RETURN_FALSE;
140 	}
141 
142 	if (data) {
143 		err = xmlParseBalancedChunkMemory(nodep->doc, NULL, NULL, 0, data, &lst);
144 		if (err != 0) {
145 			RETURN_FALSE;
146 		}
147 		/* Following needed due to bug in libxml2 <= 2.6.14
148 		ifdef after next libxml release as bug is fixed in their cvs */
149 		php_dom_xmlSetTreeDoc(lst, nodep->doc);
150 		/* End stupid hack */
151 
152 		xmlAddChildList(nodep,lst);
153 	}
154 
155 	RETURN_TRUE;
156 }
157 /* }}} */
158 
159 #endif
160 
161 /*
162  * Local variables:
163  * tab-width: 4
164  * c-basic-offset: 4
165  * End:
166  * vim600: noet sw=4 ts=4 fdm=marker
167  * vim<600: noet sw=4 ts=4
168  */
169