xref: /PHP-8.1/ext/dom/documentfragment.c (revision 08c4db7f)
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    | https://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_decrement_resource((php_libxml_node_object *)intern);
54 	}
55 	php_libxml_increment_node_ptr((php_libxml_node_object *)intern, nodep, (void *)intern);
56 }
57 /* }}} end DOMDocumentFragment::__construct */
58 
59 /* php_dom_xmlSetTreeDoc is a custom implementation of xmlSetTreeDoc
60  needed for hack in appendXML due to libxml bug - no need to share this function */
php_dom_xmlSetTreeDoc(xmlNodePtr tree,xmlDocPtr doc)61 static void php_dom_xmlSetTreeDoc(xmlNodePtr tree, xmlDocPtr doc) /* {{{ */
62 {
63 	xmlAttrPtr prop;
64 	xmlNodePtr cur;
65 
66 	if (tree) {
67 		if(tree->type == XML_ELEMENT_NODE) {
68 			prop = tree->properties;
69 			while (prop != NULL) {
70 				prop->doc = doc;
71 				if (prop->children) {
72 					cur = prop->children;
73 					while (cur != NULL) {
74 						php_dom_xmlSetTreeDoc(cur, doc);
75 						cur = cur->next;
76 					}
77 				}
78 				prop = prop->next;
79 			}
80 		}
81 		if (tree->children != NULL) {
82 			cur = tree->children;
83 			while (cur != NULL) {
84 				php_dom_xmlSetTreeDoc(cur, doc);
85 				cur = cur->next;
86 			}
87 		}
88 		tree->doc = doc;
89 	}
90 }
91 /* }}} */
92 
93 /* {{{ */
PHP_METHOD(DOMDocumentFragment,appendXML)94 PHP_METHOD(DOMDocumentFragment, appendXML) {
95 	zval *id;
96 	xmlNode *nodep;
97 	dom_object *intern;
98 	char *data = NULL;
99 	size_t data_len = 0;
100 	int err;
101 	xmlNodePtr lst;
102 
103 	id = ZEND_THIS;
104 	if (zend_parse_parameters(ZEND_NUM_ARGS(), "s", &data, &data_len) == FAILURE) {
105 		RETURN_THROWS();
106 	}
107 
108 	DOM_GET_OBJ(nodep, id, xmlNodePtr, intern);
109 
110 	if (dom_node_is_read_only(nodep) == SUCCESS) {
111 		php_dom_throw_error(NO_MODIFICATION_ALLOWED_ERR, dom_get_strict_error(intern->document));
112 		RETURN_FALSE;
113 	}
114 
115 	if (data) {
116 		PHP_LIBXML_SANITIZE_GLOBALS(parse);
117 		err = xmlParseBalancedChunkMemory(nodep->doc, NULL, NULL, 0, (xmlChar *) data, &lst);
118 		PHP_LIBXML_RESTORE_GLOBALS(parse);
119 		if (err != 0) {
120 			RETURN_FALSE;
121 		}
122 		/* Following needed due to bug in libxml2 <= 2.6.14
123 		ifdef after next libxml release as bug is fixed in their cvs */
124 		php_dom_xmlSetTreeDoc(lst, nodep->doc);
125 		/* End stupid hack */
126 
127 		xmlAddChildList(nodep,lst);
128 	}
129 
130 	RETURN_TRUE;
131 }
132 /* }}} */
133 
134 /* {{{ URL: https://dom.spec.whatwg.org/#dom-parentnode-append
135 Since: DOM Living Standard (DOM4)
136 */
PHP_METHOD(DOMDocumentFragment,append)137 PHP_METHOD(DOMDocumentFragment, append)
138 {
139 	int argc = 0;
140 	zval *args, *id;
141 	dom_object *intern;
142 	xmlNode *context;
143 
144 	if (zend_parse_parameters(ZEND_NUM_ARGS(), "*", &args, &argc) == FAILURE) {
145 		RETURN_THROWS();
146 	}
147 
148 	id = ZEND_THIS;
149 	DOM_GET_OBJ(context, id, xmlNodePtr, intern);
150 
151 	dom_parent_node_append(intern, args, argc);
152 }
153 /* }}} */
154 
155 /* {{{ URL: https://dom.spec.whatwg.org/#dom-parentnode-prepend
156 Since: DOM Living Standard (DOM4)
157 */
PHP_METHOD(DOMDocumentFragment,prepend)158 PHP_METHOD(DOMDocumentFragment, prepend)
159 {
160 	int argc = 0;
161 	zval *args, *id;
162 	dom_object *intern;
163 	xmlNode *context;
164 
165 	if (zend_parse_parameters(ZEND_NUM_ARGS(), "*", &args, &argc) == FAILURE) {
166 		RETURN_THROWS();
167 	}
168 
169 	id = ZEND_THIS;
170 	DOM_GET_OBJ(context, id, xmlNodePtr, intern);
171 
172 	dom_parent_node_prepend(intern, args, argc);
173 }
174 /* }}} */
175 
176 #endif
177