xref: /PHP-7.0/ext/dom/documentfragment.c (revision 478f119a)
1 /*
2    +----------------------------------------------------------------------+
3    | PHP Version 7                                                        |
4    +----------------------------------------------------------------------+
5    | Copyright (c) 1997-2017 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 = getThis();
57 	xmlNodePtr nodep = NULL, oldnode = NULL;
58 	dom_object *intern;
59 
60 	if (zend_parse_parameters_throw(ZEND_NUM_ARGS(), "") == FAILURE) {
61 		return;
62 	}
63 
64 	nodep = xmlNewDocFragment(NULL);
65 
66 	if (!nodep) {
67 		php_dom_throw_error(INVALID_STATE_ERR, 1);
68 		RETURN_FALSE;
69 	}
70 
71 	intern = Z_DOMOBJ_P(id);
72 	oldnode = dom_object_get_node(intern);
73 	if (oldnode != NULL) {
74 		php_libxml_node_free_resource(oldnode );
75 	}
76 	/* php_dom_set_object(intern, nodep); */
77 	php_libxml_increment_node_ptr((php_libxml_node_object *)intern, nodep, (void *)intern);
78 }
79 /* }}} end DOMDocumentFragment::__construct */
80 
81 /* php_dom_xmlSetTreeDoc is a custom implementation of xmlSetTreeDoc
82  needed for hack in appendXML due to libxml bug - no need to share this function */
php_dom_xmlSetTreeDoc(xmlNodePtr tree,xmlDocPtr doc)83 static void php_dom_xmlSetTreeDoc(xmlNodePtr tree, xmlDocPtr doc) /* {{{ */
84 {
85     xmlAttrPtr prop;
86 	xmlNodePtr cur;
87 
88     if (tree) {
89 		if(tree->type == XML_ELEMENT_NODE) {
90 			prop = tree->properties;
91 			while (prop != NULL) {
92 				prop->doc = doc;
93 				if (prop->children) {
94 					cur = prop->children;
95 					while (cur != NULL) {
96 						php_dom_xmlSetTreeDoc(cur, doc);
97 						cur = cur->next;
98 					}
99 				}
100 				prop = prop->next;
101 			}
102 		}
103 		if (tree->children != NULL) {
104 			cur = tree->children;
105 			while (cur != NULL) {
106 				php_dom_xmlSetTreeDoc(cur, doc);
107 				cur = cur->next;
108 			}
109 		}
110 		tree->doc = doc;
111     }
112 }
113 /* }}} */
114 
115 /* {{{ proto void DOMDocumentFragment::appendXML(string data) */
PHP_METHOD(domdocumentfragment,appendXML)116 PHP_METHOD(domdocumentfragment, appendXML) {
117 	zval *id;
118 	xmlNode *nodep;
119 	dom_object *intern;
120 	char *data = NULL;
121 	size_t data_len = 0;
122 	int err;
123 	xmlNodePtr lst;
124 
125 	if (zend_parse_method_parameters(ZEND_NUM_ARGS(), getThis(), "Os", &id, dom_documentfragment_class_entry, &data, &data_len) == FAILURE) {
126 		return;
127 	}
128 
129 	DOM_GET_OBJ(nodep, id, xmlNodePtr, intern);
130 
131 	if (dom_node_is_read_only(nodep) == SUCCESS) {
132 		php_dom_throw_error(NO_MODIFICATION_ALLOWED_ERR, dom_get_strict_error(intern->document));
133 		RETURN_FALSE;
134 	}
135 
136 	if (data) {
137 		err = xmlParseBalancedChunkMemory(nodep->doc, NULL, NULL, 0, (xmlChar *) data, &lst);
138 		if (err != 0) {
139 			RETURN_FALSE;
140 		}
141 		/* Following needed due to bug in libxml2 <= 2.6.14
142 		ifdef after next libxml release as bug is fixed in their cvs */
143 		php_dom_xmlSetTreeDoc(lst, nodep->doc);
144 		/* End stupid hack */
145 
146 		xmlAddChildList(nodep,lst);
147 	}
148 
149 	RETURN_TRUE;
150 }
151 /* }}} */
152 
153 #endif
154 
155 /*
156  * Local variables:
157  * tab-width: 4
158  * c-basic-offset: 4
159  * End:
160  * vim600: noet sw=4 ts=4 fdm=marker
161  * vim<600: noet sw=4 ts=4
162  */
163