xref: /PHP-7.3/ext/dom/documentfragment.c (revision 8d3f8ca1)
1 /*
2    +----------------------------------------------------------------------+
3    | PHP Version 7                                                        |
4    +----------------------------------------------------------------------+
5    | Copyright (c) 1997-2018 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 #ifdef HAVE_CONFIG_H
21 #include "config.h"
22 #endif
23 
24 #include "php.h"
25 #if HAVE_LIBXML && HAVE_DOM
26 #include "php_dom.h"
27 
28 /* {{{ arginfo */
29 ZEND_BEGIN_ARG_INFO_EX(arginfo_dom_documentfragement_construct, 0, 0, 0)
30 ZEND_END_ARG_INFO();
31 
32 ZEND_BEGIN_ARG_INFO_EX(arginfo_dom_documentfragement_appendXML, 0, 0, 1)
33 	ZEND_ARG_INFO(0, data)
34 ZEND_END_ARG_INFO();
35 /* }}} */
36 
37 /*
38 * class DOMDocumentFragment extends DOMNode
39 *
40 * URL: https://www.w3.org/TR/2003/WD-DOM-Level-3-Core-20030226/DOM3-Core.html#ID-B63ED1A3
41 * Since:
42 */
43 
44 const zend_function_entry php_dom_documentfragment_class_functions[] = {
45 	PHP_ME(domdocumentfragment, __construct, arginfo_dom_documentfragement_construct, ZEND_ACC_PUBLIC)
46 	PHP_ME(domdocumentfragment, appendXML, arginfo_dom_documentfragement_appendXML, ZEND_ACC_PUBLIC)
47 	PHP_FE_END
48 };
49 
50 /* {{{ proto DOMDocumentFragment::__construct() */
PHP_METHOD(domdocumentfragment,__construct)51 PHP_METHOD(domdocumentfragment, __construct)
52 {
53 
54 	zval *id = getThis();
55 	xmlNodePtr nodep = NULL, oldnode = NULL;
56 	dom_object *intern;
57 
58 	if (zend_parse_parameters_none_throw() == FAILURE) {
59 		return;
60 	}
61 
62 	nodep = xmlNewDocFragment(NULL);
63 
64 	if (!nodep) {
65 		php_dom_throw_error(INVALID_STATE_ERR, 1);
66 		RETURN_FALSE;
67 	}
68 
69 	intern = Z_DOMOBJ_P(id);
70 	oldnode = dom_object_get_node(intern);
71 	if (oldnode != NULL) {
72 		php_libxml_node_free_resource(oldnode );
73 	}
74 	/* php_dom_set_object(intern, nodep); */
75 	php_libxml_increment_node_ptr((php_libxml_node_object *)intern, nodep, (void *)intern);
76 }
77 /* }}} end DOMDocumentFragment::__construct */
78 
79 /* php_dom_xmlSetTreeDoc is a custom implementation of xmlSetTreeDoc
80  needed for hack in appendXML due to libxml bug - no need to share this function */
php_dom_xmlSetTreeDoc(xmlNodePtr tree,xmlDocPtr doc)81 static void php_dom_xmlSetTreeDoc(xmlNodePtr tree, xmlDocPtr doc) /* {{{ */
82 {
83     xmlAttrPtr prop;
84 	xmlNodePtr cur;
85 
86     if (tree) {
87 		if(tree->type == XML_ELEMENT_NODE) {
88 			prop = tree->properties;
89 			while (prop != NULL) {
90 				prop->doc = doc;
91 				if (prop->children) {
92 					cur = prop->children;
93 					while (cur != NULL) {
94 						php_dom_xmlSetTreeDoc(cur, doc);
95 						cur = cur->next;
96 					}
97 				}
98 				prop = prop->next;
99 			}
100 		}
101 		if (tree->children != NULL) {
102 			cur = tree->children;
103 			while (cur != NULL) {
104 				php_dom_xmlSetTreeDoc(cur, doc);
105 				cur = cur->next;
106 			}
107 		}
108 		tree->doc = doc;
109     }
110 }
111 /* }}} */
112 
113 /* {{{ proto void DOMDocumentFragment::appendXML(string data) */
PHP_METHOD(domdocumentfragment,appendXML)114 PHP_METHOD(domdocumentfragment, appendXML) {
115 	zval *id;
116 	xmlNode *nodep;
117 	dom_object *intern;
118 	char *data = NULL;
119 	size_t data_len = 0;
120 	int err;
121 	xmlNodePtr lst;
122 
123 	if (zend_parse_method_parameters(ZEND_NUM_ARGS(), getThis(), "Os", &id, dom_documentfragment_class_entry, &data, &data_len) == FAILURE) {
124 		return;
125 	}
126 
127 	DOM_GET_OBJ(nodep, id, xmlNodePtr, intern);
128 
129 	if (dom_node_is_read_only(nodep) == SUCCESS) {
130 		php_dom_throw_error(NO_MODIFICATION_ALLOWED_ERR, dom_get_strict_error(intern->document));
131 		RETURN_FALSE;
132 	}
133 
134 	if (data) {
135 		err = xmlParseBalancedChunkMemory(nodep->doc, NULL, NULL, 0, (xmlChar *) data, &lst);
136 		if (err != 0) {
137 			RETURN_FALSE;
138 		}
139 		/* Following needed due to bug in libxml2 <= 2.6.14
140 		ifdef after next libxml release as bug is fixed in their cvs */
141 		php_dom_xmlSetTreeDoc(lst, nodep->doc);
142 		/* End stupid hack */
143 
144 		xmlAddChildList(nodep,lst);
145 	}
146 
147 	RETURN_TRUE;
148 }
149 /* }}} */
150 
151 #endif
152 
153 /*
154  * Local variables:
155  * tab-width: 4
156  * c-basic-offset: 4
157  * End:
158  * vim600: noet sw=4 ts=4 fdm=marker
159  * vim<600: noet sw=4 ts=4
160  */
161