xref: /PHP-8.3/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_METHOD(DOMDocumentFragment,appendXML)60 PHP_METHOD(DOMDocumentFragment, appendXML) {
61 	zval *id;
62 	xmlNode *nodep;
63 	dom_object *intern;
64 	char *data = NULL;
65 	size_t data_len = 0;
66 	int err;
67 	xmlNodePtr lst;
68 
69 	id = ZEND_THIS;
70 	if (zend_parse_parameters(ZEND_NUM_ARGS(), "s", &data, &data_len) == FAILURE) {
71 		RETURN_THROWS();
72 	}
73 
74 	DOM_GET_OBJ(nodep, id, xmlNodePtr, intern);
75 
76 	if (dom_node_is_read_only(nodep) == SUCCESS) {
77 		php_dom_throw_error(NO_MODIFICATION_ALLOWED_ERR, dom_get_strict_error(intern->document));
78 		RETURN_FALSE;
79 	}
80 
81 	if (data) {
82 		PHP_LIBXML_SANITIZE_GLOBALS(parse);
83 		err = xmlParseBalancedChunkMemory(nodep->doc, NULL, NULL, 0, (xmlChar *) data, &lst);
84 		PHP_LIBXML_RESTORE_GLOBALS(parse);
85 		if (err != 0) {
86 			RETURN_FALSE;
87 		}
88 
89 		xmlAddChildList(nodep,lst);
90 	}
91 
92 	RETURN_TRUE;
93 }
94 /* }}} */
95 
96 /* {{{ URL: https://dom.spec.whatwg.org/#dom-parentnode-append
97 Since: DOM Living Standard (DOM4)
98 */
PHP_METHOD(DOMDocumentFragment,append)99 PHP_METHOD(DOMDocumentFragment, append)
100 {
101 	uint32_t argc = 0;
102 	zval *args;
103 	dom_object *intern;
104 
105 	if (zend_parse_parameters(ZEND_NUM_ARGS(), "*", &args, &argc) == FAILURE) {
106 		RETURN_THROWS();
107 	}
108 
109 	DOM_GET_THIS_INTERN(intern);
110 
111 	dom_parent_node_append(intern, args, argc);
112 }
113 /* }}} */
114 
115 /* {{{ URL: https://dom.spec.whatwg.org/#dom-parentnode-prepend
116 Since: DOM Living Standard (DOM4)
117 */
PHP_METHOD(DOMDocumentFragment,prepend)118 PHP_METHOD(DOMDocumentFragment, prepend)
119 {
120 	uint32_t argc = 0;
121 	zval *args;
122 	dom_object *intern;
123 
124 	if (zend_parse_parameters(ZEND_NUM_ARGS(), "*", &args, &argc) == FAILURE) {
125 		RETURN_THROWS();
126 	}
127 
128 	DOM_GET_THIS_INTERN(intern);
129 
130 	dom_parent_node_prepend(intern, args, argc);
131 }
132 /* }}} */
133 
134 /* {{{ URL: https://dom.spec.whatwg.org/#dom-parentnode-replacechildren
135 Since:
136 */
PHP_METHOD(DOMDocumentFragment,replaceChildren)137 PHP_METHOD(DOMDocumentFragment, replaceChildren)
138 {
139 	uint32_t argc = 0;
140 	zval *args;
141 	dom_object *intern;
142 
143 	if (zend_parse_parameters(ZEND_NUM_ARGS(), "*", &args, &argc) == FAILURE) {
144 		RETURN_THROWS();
145 	}
146 
147 	DOM_GET_THIS_INTERN(intern);
148 
149 	dom_parent_node_replace_children(intern, args, argc);
150 }
151 /* }}} */
152 
153 #endif
154