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