xref: /PHP-8.4/ext/dom/element.c (revision 63e1ebe7)
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 "zend_enum.h"
25 #include "php_dom.h"
26 #include "namespace_compat.h"
27 #include "private_data.h"
28 #include "internal_helpers.h"
29 #include "dom_properties.h"
30 #include "token_list.h"
31 
32 /*
33 * class DOMElement extends DOMNode
34 *
35 * URL: https://www.w3.org/TR/2003/WD-DOM-Level-3-Core-20030226/DOM3-Core.html#core-ID-745549614
36 * Since:
37 */
38 
39 /* {{{ */
PHP_METHOD(DOMElement,__construct)40 PHP_METHOD(DOMElement, __construct)
41 {
42 	xmlNodePtr nodep = NULL, oldnode = NULL;
43 	dom_object *intern;
44 	char *name, *value = NULL, *uri = NULL;
45 	char *localname = NULL, *prefix = NULL;
46 	int errorcode = 0;
47 	size_t name_len, value_len = 0, uri_len = 0;
48 	int name_valid;
49 	xmlNsPtr nsptr = NULL;
50 
51 	if (zend_parse_parameters(ZEND_NUM_ARGS(), "s|s!s", &name, &name_len, &value, &value_len, &uri, &uri_len) == FAILURE) {
52 		RETURN_THROWS();
53 	}
54 
55 	name_valid = xmlValidateName(BAD_CAST name, 0);
56 	if (name_valid != 0) {
57 		php_dom_throw_error(INVALID_CHARACTER_ERR, true);
58 		RETURN_THROWS();
59 	}
60 
61 	/* Namespace logic is separate and only when uri passed in to insure no BC breakage */
62 	if (uri_len > 0) {
63 		errorcode = dom_check_qname(name, &localname, &prefix, uri_len, name_len);
64 		if (errorcode == 0) {
65 			nodep = xmlNewNode (NULL, BAD_CAST localname);
66 			if (nodep != NULL && uri != NULL) {
67 				nsptr = dom_get_ns(nodep, uri, &errorcode, prefix);
68 				xmlSetNs(nodep, nsptr);
69 			}
70 		}
71 		xmlFree(localname);
72 		if (prefix != NULL) {
73 			xmlFree(prefix);
74 		}
75 		if (errorcode != 0) {
76 			if (nodep != NULL) {
77 				xmlFreeNode(nodep);
78 			}
79 			php_dom_throw_error(errorcode, true);
80 			RETURN_THROWS();
81 		}
82 	} else {
83 	    /* If you don't pass a namespace uri, then you can't set a prefix */
84 	    localname = (char *) xmlSplitQName2(BAD_CAST name, (xmlChar **) &prefix);
85 	    if (prefix != NULL) {
86 			xmlFree(localname);
87 			xmlFree(prefix);
88 	        php_dom_throw_error(NAMESPACE_ERR, true);
89 	        RETURN_THROWS();
90 	    }
91 		nodep = xmlNewNode(NULL, BAD_CAST name);
92 	}
93 
94 	if (!nodep) {
95 		php_dom_throw_error(INVALID_STATE_ERR, true);
96 		RETURN_THROWS();
97 	}
98 
99 	if (value_len > 0) {
100 		xmlNodeSetContentLen(nodep, BAD_CAST value, value_len);
101 	}
102 
103 	intern = Z_DOMOBJ_P(ZEND_THIS);
104 	oldnode = dom_object_get_node(intern);
105 	if (oldnode != NULL) {
106 		php_libxml_node_decrement_resource((php_libxml_node_object *)intern);
107 	}
108 	php_libxml_increment_node_ptr((php_libxml_node_object *)intern, nodep, (void *)intern);
109 }
110 /* }}} end DOMElement::__construct */
111 
112 /* {{{ tagName	string
113 readonly=yes
114 URL: http://www.w3.org/TR/2003/WD-DOM-Level-3-Core-20030226/DOM3-Core.html#core-ID-104682815
115 Modern spec URL: https://dom.spec.whatwg.org/#dom-element-tagname
116 Since:
117 */
dom_element_tag_name_read(dom_object * obj,zval * retval)118 zend_result dom_element_tag_name_read(dom_object *obj, zval *retval)
119 {
120 	DOM_PROP_NODE(xmlNodePtr, nodep, obj);
121 
122 	bool uppercase = php_dom_follow_spec_intern(obj) && php_dom_ns_is_html_and_document_is_html(nodep);
123 
124 	zend_string *result = dom_node_get_node_name_attribute_or_element((const xmlNode *) nodep, uppercase);
125 	ZVAL_NEW_STR(retval, result);
126 
127 	return SUCCESS;
128 }
129 
130 /* }}} */
131 
dom_element_reflected_attribute_read(dom_object * obj,zval * retval,const char * name)132 static zend_result dom_element_reflected_attribute_read(dom_object *obj, zval *retval, const char *name)
133 {
134 	DOM_PROP_NODE(xmlNodePtr, nodep, obj);
135 
136 	xmlChar *content = xmlGetNoNsProp(nodep, (const xmlChar *) name);
137 	if (content == NULL) {
138 		ZVAL_EMPTY_STRING(retval);
139 		return SUCCESS;
140 	}
141 
142 	ZVAL_STRING(retval, (const char *) content);
143 	xmlFree(content);
144 
145 	return SUCCESS;
146 }
147 
dom_element_reflected_attribute_write(dom_object * obj,zval * newval,const char * name)148 static xmlAttrPtr dom_element_reflected_attribute_write(dom_object *obj, zval *newval, const char *name)
149 {
150 	xmlNode *nodep = dom_object_get_node(obj);
151 
152 	if (nodep == NULL) {
153 		php_dom_throw_error(INVALID_STATE_ERR, true);
154 		return NULL;
155 	}
156 
157 	/* Typed property, so it is a string already */
158 	ZEND_ASSERT(Z_TYPE_P(newval) == IS_STRING);
159 	return xmlSetNsProp(nodep, NULL, (const xmlChar *) name, (const xmlChar *) Z_STRVAL_P(newval));
160 }
161 
162 /* {{{ className	string
163 URL: https://dom.spec.whatwg.org/#dom-element-classname
164 Since:
165 */
dom_element_class_name_read(dom_object * obj,zval * retval)166 zend_result dom_element_class_name_read(dom_object *obj, zval *retval)
167 {
168 	return dom_element_reflected_attribute_read(obj, retval, "class");
169 }
170 
dom_element_class_name_write(dom_object * obj,zval * newval)171 zend_result dom_element_class_name_write(dom_object *obj, zval *newval)
172 {
173 	if (dom_element_reflected_attribute_write(obj, newval, "class")) {
174 		return SUCCESS;
175 	}
176 	return FAILURE;
177 }
178 /* }}} */
179 
180 /* {{{ classList	TokenList
181 URL: https://dom.spec.whatwg.org/#dom-element-classlist
182 */
dom_element_class_list_read(dom_object * obj,zval * retval)183 zend_result dom_element_class_list_read(dom_object *obj, zval *retval)
184 {
185 	const uint32_t PROP_INDEX = 0;
186 
187 #if ZEND_DEBUG
188 	zend_string *class_list_str = ZSTR_INIT_LITERAL("classList", false);
189 	const zend_property_info *prop_info = zend_get_property_info(dom_modern_element_class_entry, class_list_str, 0);
190 	zend_string_release_ex(class_list_str, false);
191 	ZEND_ASSERT(OBJ_PROP_TO_NUM(prop_info->offset) == PROP_INDEX);
192 #endif
193 
194 	zval *cached_token_list = OBJ_PROP_NUM(&obj->std, PROP_INDEX);
195 	if (Z_ISUNDEF_P(cached_token_list)) {
196 		object_init_ex(cached_token_list, dom_token_list_class_entry);
197 		dom_token_list_object *intern = php_dom_token_list_from_obj(Z_OBJ_P(cached_token_list));
198 		dom_token_list_ctor(intern, obj);
199 	}
200 
201 	ZVAL_OBJ_COPY(retval, Z_OBJ_P(cached_token_list));
202 
203 	return SUCCESS;
204 }
205 /* }}} */
206 
207 /* {{{ id	string
208 URL: https://dom.spec.whatwg.org/#dom-element-id
209 Since:
210 */
dom_element_id_read(dom_object * obj,zval * retval)211 zend_result dom_element_id_read(dom_object *obj, zval *retval)
212 {
213 	return dom_element_reflected_attribute_read(obj, retval, "id");
214 }
215 
216 static void php_set_attribute_id(xmlAttrPtr attrp, bool is_id, php_libxml_ref_obj *document);
217 
dom_element_id_write(dom_object * obj,zval * newval)218 zend_result dom_element_id_write(dom_object *obj, zval *newval)
219 {
220 	xmlAttrPtr attr = dom_element_reflected_attribute_write(obj, newval, "id");
221 	if (!attr) {
222 		return FAILURE;
223 	}
224 	php_set_attribute_id(attr, true, obj->document);
225 	return SUCCESS;
226 }
227 /* }}} */
228 
229 /* {{{ schemaTypeInfo	typeinfo
230 readonly=yes
231 URL: http://www.w3.org/TR/2003/WD-DOM-Level-3-Core-20030226/DOM3-Core.html#Element-schemaTypeInfo
232 Since: DOM Level 3
233 */
dom_element_schema_type_info_read(dom_object * obj,zval * retval)234 zend_result dom_element_schema_type_info_read(dom_object *obj, zval *retval)
235 {
236 	ZVAL_NULL(retval);
237 	return SUCCESS;
238 }
239 
240 /* }}} */
241 
242 /* Note: the object returned is not necessarily a node, but can be an attribute or a namespace declaration. */
dom_get_attribute_or_nsdecl(dom_object * intern,xmlNodePtr elem,const xmlChar * name,size_t name_len)243 static xmlNodePtr dom_get_attribute_or_nsdecl(dom_object *intern, xmlNodePtr elem, const xmlChar *name, size_t name_len) /* {{{ */
244 {
245 	if (!php_dom_follow_spec_intern(intern)) {
246 		int len;
247 		const xmlChar *nqname = xmlSplitQName3(name, &len);
248 
249 		if (nqname != NULL) {
250 			xmlNsPtr ns;
251 			if (strncmp((const char *) name, "xmlns:", len + 1) == 0) {
252 				ns = elem->nsDef;
253 				while (ns) {
254 					if (xmlStrEqual(ns->prefix, nqname)) {
255 						break;
256 					}
257 					ns = ns->next;
258 				}
259 				return (xmlNodePtr)ns;
260 			}
261 			xmlChar *prefix = xmlStrndup(name, len);
262 			ns = xmlSearchNs(elem->doc, elem, prefix);
263 			if (prefix != NULL) {
264 				xmlFree(prefix);
265 			}
266 			if (ns != NULL) {
267 				return (xmlNodePtr)xmlHasNsProp(elem, nqname, ns->href);
268 			}
269 		} else {
270 			if (xmlStrEqual(name, BAD_CAST "xmlns")) {
271 				xmlNsPtr nsPtr = elem->nsDef;
272 				while (nsPtr) {
273 					if (nsPtr->prefix == NULL) {
274 						return (xmlNodePtr)nsPtr;
275 					}
276 					nsPtr = nsPtr->next;
277 				}
278 				return NULL;
279 			}
280 		}
281 		return (xmlNodePtr) xmlHasNsProp(elem, name, NULL);
282 	} else {
283 		return (xmlNodePtr) php_dom_get_attribute_node(elem, name, name_len);
284 	}
285 }
286 /* }}} */
287 
288 /* {{{ URL: http://www.w3.org/TR/2003/WD-DOM-Level-3-Core-20030226/DOM3-Core.html#core-ID-666EE0F9
289 Modern spec URL: https://dom.spec.whatwg.org/#dom-element-getattribute
290 Since:
291 */
PHP_METHOD(DOMElement,getAttribute)292 PHP_METHOD(DOMElement, getAttribute)
293 {
294 	zval *id;
295 	xmlNode *nodep;
296 	char *name;
297 	xmlChar *value = NULL;
298 	dom_object *intern;
299 	xmlNodePtr attr;
300 	size_t name_len;
301 	bool should_free = false;
302 
303 	id = ZEND_THIS;
304 	if (zend_parse_parameters(ZEND_NUM_ARGS(), "s", &name, &name_len) == FAILURE) {
305 		RETURN_THROWS();
306 	}
307 
308 	DOM_GET_OBJ(nodep, id, xmlNodePtr, intern);
309 
310 	attr = dom_get_attribute_or_nsdecl(intern, nodep, BAD_CAST name, name_len);
311 	if (attr) {
312 		switch (attr->type) {
313 			case XML_ATTRIBUTE_NODE:
314 				value = xmlNodeListGetString(attr->doc, attr->children, 1);
315 				should_free = true;
316 				break;
317 			case XML_NAMESPACE_DECL:
318 				value = BAD_CAST ((xmlNsPtr)attr)->href;
319 				should_free = false;
320 				break;
321 			default:
322 				value = BAD_CAST ((xmlAttributePtr)attr)->defaultValue;
323 				should_free = false;
324 		}
325 	}
326 
327 	if (value == NULL) {
328 		if (php_dom_follow_spec_intern(intern)) {
329 			RETURN_NULL();
330 		}
331 		RETURN_EMPTY_STRING();
332 	} else {
333 		RETVAL_STRING((char *)value);
334 		if (should_free) {
335 			xmlFree(value);
336 		}
337 	}
338 }
339 /* }}} end dom_element_get_attribute */
340 
341 /* {{{ URL: https://dom.spec.whatwg.org/#dom-element-getattributenames
342 Since:
343 */
PHP_METHOD(DOMElement,getAttributeNames)344 PHP_METHOD(DOMElement, getAttributeNames)
345 {
346 	zval *id;
347 	xmlNode *nodep;
348 	dom_object *intern;
349 	zval tmp;
350 
351 	if (zend_parse_parameters_none() == FAILURE) {
352 		RETURN_THROWS();
353 	}
354 
355 	DOM_GET_THIS_OBJ(nodep, id, xmlNodePtr, intern);
356 
357 	array_init(return_value);
358 	HashTable *ht = Z_ARRVAL_P(return_value);
359 	zend_hash_real_init_packed(ht);
360 
361 	if (!php_dom_follow_spec_intern(intern)) {
362 		for (xmlNsPtr nsptr = nodep->nsDef; nsptr; nsptr = nsptr->next) {
363 			const char *prefix = (const char *) nsptr->prefix;
364 			if (prefix == NULL) {
365 				ZVAL_STRING(&tmp, "xmlns");
366 			} else {
367 				ZVAL_NEW_STR(&tmp, dom_node_concatenated_name_helper(strlen(prefix), prefix, strlen("xmlns"), (const char *) "xmlns"));
368 			}
369 			zend_hash_next_index_insert(ht, &tmp);
370 		}
371 	}
372 
373 	for (xmlAttrPtr attr = nodep->properties; attr; attr = attr->next) {
374 		ZVAL_NEW_STR(&tmp, dom_node_get_node_name_attribute_or_element((const xmlNode *) attr, false));
375 		zend_hash_next_index_insert(ht, &tmp);
376 	}
377 }
378 /* }}} end DOMElement::getAttributeNames() */
379 
dom_create_attribute(xmlNodePtr nodep,const char * name,const char * value)380 static xmlNodePtr dom_create_attribute(xmlNodePtr nodep, const char *name, const char* value)
381 {
382 	if (xmlStrEqual(BAD_CAST name, BAD_CAST "xmlns")) {
383 		return (xmlNodePtr) xmlNewNs(nodep, BAD_CAST value, NULL);
384 	} else {
385 		return (xmlNodePtr) xmlSetProp(nodep, BAD_CAST name, BAD_CAST value);
386 	}
387 }
388 
dom_check_register_attribute_id(xmlAttrPtr attr,php_libxml_ref_obj * document)389 static void dom_check_register_attribute_id(xmlAttrPtr attr, php_libxml_ref_obj *document)
390 {
391 	dom_mark_ids_modified(document);
392 
393 	if (attr->atype != XML_ATTRIBUTE_ID && attr->doc->type == XML_HTML_DOCUMENT_NODE && attr->ns == NULL && xmlStrEqual(attr->name, BAD_CAST "id")) {
394 		/* To respect XML's ID behaviour, we only do this registration for HTML documents. */
395 		attr->atype = XML_ATTRIBUTE_ID;
396 	}
397 }
398 
399 /* {{{ URL: http://www.w3.org/TR/2003/WD-DOM-Level-3-Core-20030226/DOM3-Core.html#core-ID-F68F082
400 Modern spec URL: https://dom.spec.whatwg.org/#dom-element-setattribute
401 Since:
402 */
PHP_METHOD(DOMElement,setAttribute)403 PHP_METHOD(DOMElement, setAttribute)
404 {
405 	zval *id;
406 	xmlNode *nodep;
407 	int name_valid;
408 	size_t name_len, value_len;
409 	dom_object *intern;
410 	char *name, *value;
411 
412 	id = ZEND_THIS;
413 	if (zend_parse_parameters(ZEND_NUM_ARGS(), "ss", &name, &name_len, &value, &value_len) == FAILURE) {
414 		RETURN_THROWS();
415 	}
416 
417 	if (name_len == 0) {
418 		zend_argument_must_not_be_empty_error(1);
419 		RETURN_THROWS();
420 	}
421 
422 	name_valid = xmlValidateName(BAD_CAST name, 0);
423 	if (name_valid != 0) {
424 		php_dom_throw_error(INVALID_CHARACTER_ERR, true);
425 		RETURN_THROWS();
426 	}
427 
428 	DOM_GET_OBJ(nodep, id, xmlNodePtr, intern);
429 
430 	if (php_dom_follow_spec_intern(intern)) {
431 		xmlChar *name_processed = BAD_CAST name;
432 		if (php_dom_ns_is_html_and_document_is_html(nodep)) {
433 			char *lowercase_copy = zend_str_tolower_dup_ex(name, name_len);
434 			if (lowercase_copy != NULL) {
435 				name_processed = BAD_CAST lowercase_copy;
436 			}
437 		}
438 
439 		/* Can't use xmlSetNsProp unconditionally here because that doesn't take into account the qualified name matching... */
440 		xmlAttrPtr attr = php_dom_get_attribute_node(nodep, BAD_CAST name, name_len);
441 		if (attr != NULL) {
442 			dom_attr_value_will_change(intern, attr);
443 			dom_remove_all_children((xmlNodePtr) attr);
444 			xmlNodePtr node = xmlNewDocText(attr->doc, BAD_CAST value);
445 			xmlAddChild((xmlNodePtr) attr, node);
446 		} else {
447 			attr = xmlSetNsProp(nodep, NULL, name_processed, BAD_CAST value);
448 			if (EXPECTED(attr != NULL)) {
449 				dom_check_register_attribute_id(attr, intern->document);
450 			}
451 		}
452 
453 		if (name_processed != BAD_CAST name) {
454 			efree(name_processed);
455 		}
456 	} else {
457 		xmlNodePtr attr = dom_get_attribute_or_nsdecl(intern, nodep, BAD_CAST name, name_len);
458 		if (attr != NULL) {
459 			switch (attr->type) {
460 				case XML_ATTRIBUTE_NODE:
461 					dom_attr_value_will_change(intern, (xmlAttrPtr) attr);
462 					node_list_unlink(attr->children);
463 					break;
464 				case XML_NAMESPACE_DECL:
465 					RETURN_FALSE;
466 				EMPTY_SWITCH_DEFAULT_CASE();
467 			}
468 		}
469 
470 		attr = dom_create_attribute(nodep, name, value);
471 		if (!attr) {
472 			zend_argument_value_error(1, "must be a valid XML attribute");
473 			RETURN_THROWS();
474 		}
475 		if (attr->type == XML_NAMESPACE_DECL) {
476 			RETURN_TRUE;
477 		}
478 
479 		DOM_RET_OBJ(attr, intern);
480 	}
481 }
482 /* }}} end dom_element_set_attribute */
483 
484 typedef struct dom_deep_ns_redef_item {
485 	xmlNodePtr current_node;
486 	xmlNsPtr defined_ns;
487 } dom_deep_ns_redef_item;
488 
489 /* Reconciliation for a *single* namespace, but reconciles *closest* to the subtree needing it. */
dom_deep_ns_redef(xmlNodePtr node,xmlNsPtr ns_to_redefine)490 static void dom_deep_ns_redef(xmlNodePtr node, xmlNsPtr ns_to_redefine)
491 {
492 	size_t worklist_capacity = 128;
493 	dom_deep_ns_redef_item *worklist = emalloc(sizeof(dom_deep_ns_redef_item) * worklist_capacity);
494 	worklist[0].current_node = node;
495 	worklist[0].defined_ns = NULL;
496 	size_t worklist_size = 1;
497 
498 	while (worklist_size > 0) {
499 		worklist_size--;
500 		dom_deep_ns_redef_item *current_worklist_item = &worklist[worklist_size];
501 		ZEND_ASSERT(current_worklist_item->current_node->type == XML_ELEMENT_NODE);
502 		xmlNsPtr defined_ns = current_worklist_item->defined_ns;
503 
504 		if (current_worklist_item->current_node->ns == ns_to_redefine) {
505 			if (defined_ns == NULL) {
506 				defined_ns = xmlNewNs(current_worklist_item->current_node, ns_to_redefine->href, ns_to_redefine->prefix);
507 			}
508 			current_worklist_item->current_node->ns = defined_ns;
509 		}
510 
511 		for (xmlAttrPtr attr = current_worklist_item->current_node->properties; attr; attr = attr->next) {
512 			if (attr->ns == ns_to_redefine) {
513 				if (defined_ns == NULL) {
514 					defined_ns = xmlNewNs(current_worklist_item->current_node, ns_to_redefine->href, ns_to_redefine->prefix);
515 				}
516 				attr->ns = defined_ns;
517 			}
518 		}
519 
520 		for (xmlNodePtr child = current_worklist_item->current_node->children; child; child = child->next) {
521 			if (child->type != XML_ELEMENT_NODE) {
522 				continue;
523 			}
524 			if (worklist_size == worklist_capacity) {
525 				if (UNEXPECTED(worklist_capacity >= SIZE_MAX / 3 * 2 / sizeof(dom_deep_ns_redef_item))) {
526 					/* Shouldn't be possible to hit, but checked for safety anyway */
527 					goto out;
528 				}
529 				worklist_capacity = worklist_capacity * 3 / 2;
530 				worklist = erealloc(worklist, sizeof(dom_deep_ns_redef_item) * worklist_capacity);
531 			}
532 			worklist[worklist_size].current_node = child;
533 			worklist[worklist_size].defined_ns = defined_ns;
534 			worklist_size++;
535 		}
536 	}
537 
538 out:
539 	efree(worklist);
540 }
541 
dom_remove_attribute(xmlNodePtr thisp,xmlNodePtr attrp)542 static bool dom_remove_attribute(xmlNodePtr thisp, xmlNodePtr attrp)
543 {
544 	ZEND_ASSERT(thisp != NULL);
545 	ZEND_ASSERT(attrp != NULL);
546 
547 	switch (attrp->type) {
548 		case XML_ATTRIBUTE_NODE:
549 			if (php_dom_object_get_data(attrp) == NULL) {
550 				node_list_unlink(attrp->children);
551 				xmlUnlinkNode(attrp);
552 				xmlFreeProp((xmlAttrPtr)attrp);
553 			} else {
554 				xmlUnlinkNode(attrp);
555 			}
556 			break;
557 		case XML_NAMESPACE_DECL: {
558 			/* They will always be removed, but can be re-added.
559 			 *
560 			 * If any reference was left to the namespace, the only effect is that
561 			 * the definition is potentially moved closer to the element using it.
562 			 * If no reference was left, it is actually removed. */
563 			xmlNsPtr ns = (xmlNsPtr) attrp;
564 			if (thisp->nsDef == ns) {
565 				thisp->nsDef = ns->next;
566 			} else if (thisp->nsDef != NULL) {
567 				xmlNsPtr prev = thisp->nsDef;
568 				xmlNsPtr cur = prev->next;
569 				while (cur) {
570 					if (cur == ns) {
571 						prev->next = cur->next;
572 						break;
573 					}
574 					prev = cur;
575 					cur = cur->next;
576 				}
577 			} else {
578 				/* defensive: attrp not defined in thisp ??? */
579 #if ZEND_DEBUG
580 				ZEND_UNREACHABLE();
581 #endif
582 				break; /* defensive */
583 			}
584 
585 			ns->next = NULL;
586 			php_libxml_set_old_ns(thisp->doc, ns); /* note: can't deallocate as it might be referenced by a "fake namespace node" */
587 			/* xmlReconciliateNs() redefines at the top of the tree instead of closest to the child, own reconciliation here.
588 			 * Similarly, the DOM version has other issues too (see dom_libxml_reconcile_ensure_namespaces_are_declared). */
589 			dom_deep_ns_redef(thisp, ns);
590 
591 			break;
592 		}
593 		EMPTY_SWITCH_DEFAULT_CASE();
594 	}
595 	return true;
596 }
597 
598 /* {{{ URL: http://www.w3.org/TR/2003/WD-DOM-Level-3-Core-20030226/DOM3-Core.html#core-ID-6D6AC0F9
599 Modern spec URL: https://dom.spec.whatwg.org/#dom-element-removeattribute
600 Since:
601 */
PHP_METHOD(DOMElement,removeAttribute)602 PHP_METHOD(DOMElement, removeAttribute)
603 {
604 	xmlNodePtr nodep, attrp;
605 	dom_object *intern;
606 	size_t name_len;
607 	char *name;
608 
609 	if (zend_parse_parameters(ZEND_NUM_ARGS(), "s", &name, &name_len) == FAILURE) {
610 		RETURN_THROWS();
611 	}
612 
613 	DOM_GET_OBJ(nodep, ZEND_THIS, xmlNodePtr, intern);
614 
615 	attrp = dom_get_attribute_or_nsdecl(intern, nodep, BAD_CAST name, name_len);
616 	if (attrp == NULL) {
617 		RETURN_FALSE;
618 	}
619 
620 	RETURN_BOOL(dom_remove_attribute(nodep, attrp));
621 }
622 
PHP_METHOD(Dom_Element,removeAttribute)623 PHP_METHOD(Dom_Element, removeAttribute)
624 {
625 	xmlNodePtr nodep, attrp;
626 	dom_object *intern;
627 	size_t name_len;
628 	char *name;
629 
630 	if (zend_parse_parameters(ZEND_NUM_ARGS(), "s", &name, &name_len) == FAILURE) {
631 		RETURN_THROWS();
632 	}
633 
634 	DOM_GET_OBJ(nodep, ZEND_THIS, xmlNodePtr, intern);
635 
636 	attrp = dom_get_attribute_or_nsdecl(intern, nodep, BAD_CAST name, name_len);
637 	if (attrp != NULL) {
638 		dom_remove_attribute(nodep, attrp);
639 	}
640 }
641 /* }}} end dom_element_remove_attribute */
642 
643 /* {{{ URL: http://www.w3.org/TR/2003/WD-DOM-Level-3-Core-20030226/DOM3-Core.html#core-ID-217A91B8
644 Modern spec URL: https://dom.spec.whatwg.org/#dom-element-getattributenode
645 Since:
646 */
PHP_METHOD(DOMElement,getAttributeNode)647 PHP_METHOD(DOMElement, getAttributeNode)
648 {
649 	zval *id;
650 	xmlNodePtr nodep, attrp;
651 	size_t name_len;
652 	dom_object *intern;
653 	char *name;
654 
655 	id = ZEND_THIS;
656 	if (zend_parse_parameters(ZEND_NUM_ARGS(), "s", &name, &name_len) == FAILURE) {
657 		RETURN_THROWS();
658 	}
659 
660 	DOM_GET_OBJ(nodep, id, xmlNodePtr, intern);
661 
662 	attrp = dom_get_attribute_or_nsdecl(intern, nodep, BAD_CAST name, name_len);
663 	if (attrp == NULL) {
664 		if (php_dom_follow_spec_intern(intern)) {
665 			RETURN_NULL();
666 		}
667 		RETURN_FALSE;
668 	}
669 
670 	if (attrp->type == XML_NAMESPACE_DECL) {
671 		xmlNsPtr original = (xmlNsPtr) attrp;
672 		/* Keep parent alive, because we're a fake child. */
673 		GC_ADDREF(&intern->std);
674 		(void) php_dom_create_fake_namespace_decl(nodep, original, return_value, intern);
675 	} else {
676 		DOM_RET_OBJ((xmlNodePtr) attrp, intern);
677 	}
678 }
679 /* }}} end dom_element_get_attribute_node */
680 
dom_element_set_attribute_node_common(INTERNAL_FUNCTION_PARAMETERS,bool use_ns,bool modern)681 static void dom_element_set_attribute_node_common(INTERNAL_FUNCTION_PARAMETERS, bool use_ns, bool modern)
682 {
683 	zval *id, *node;
684 	xmlNode *nodep;
685 	xmlNs *nsp;
686 	xmlAttr *attrp, *existattrp = NULL;
687 	dom_object *intern, *attrobj, *oldobj;
688 
689 	id = ZEND_THIS;
690 	if (zend_parse_parameters(ZEND_NUM_ARGS(), "O", &node, dom_get_attr_ce(modern)) == FAILURE) {
691 		RETURN_THROWS();
692 	}
693 
694 	DOM_GET_OBJ(nodep, id, xmlNodePtr, intern);
695 	DOM_GET_OBJ(attrp, node, xmlAttrPtr, attrobj);
696 
697 	/* ZPP Guarantees that a DOMAttr class is given, as it is converted to a xmlAttr
698 	 * to pass to libxml (see http://www.xmlsoft.org/html/libxml-tree.html#xmlAttr)
699 	 * if it is not of type XML_ATTRIBUTE_NODE it indicates a bug somewhere */
700 	ZEND_ASSERT(attrp->type == XML_ATTRIBUTE_NODE);
701 
702 	if (modern) {
703 		if (attrp->parent != NULL && attrp->parent != nodep) {
704 			php_dom_throw_error(INUSE_ATTRIBUTE_ERR, /* strict */ true);
705 			RETURN_THROWS();
706 		}
707 		if (attrp->doc != NULL && attrp->doc != nodep->doc) {
708 			php_dom_adopt_node((xmlNodePtr) attrp, intern, nodep->doc);
709 		}
710 	} else {
711 		if (!(attrp->doc == NULL || attrp->doc == nodep->doc)) {
712 			php_dom_throw_error(WRONG_DOCUMENT_ERR, dom_get_strict_error(intern->document));
713 			RETURN_FALSE;
714 		}
715 	}
716 
717 	nsp = attrp->ns;
718 	if (use_ns && nsp != NULL) {
719 		existattrp = xmlHasNsProp(nodep, attrp->name, nsp->href);
720 	} else {
721 		existattrp = xmlHasProp(nodep, attrp->name);
722 	}
723 
724 	if (existattrp != NULL && existattrp->type != XML_ATTRIBUTE_DECL) {
725 		if ((oldobj = php_dom_object_get_data((xmlNodePtr) existattrp)) != NULL &&
726 			((php_libxml_node_ptr *)oldobj->ptr)->node == (xmlNodePtr) attrp)
727 		{
728 			RETURN_NULL();
729 		}
730 		xmlUnlinkNode((xmlNodePtr) existattrp);
731 	}
732 
733 	if (attrp->parent != NULL) {
734 		xmlUnlinkNode((xmlNodePtr) attrp);
735 	}
736 
737 	if (attrp->doc == NULL && nodep->doc != NULL) {
738 		attrobj->document = intern->document;
739 		php_libxml_increment_doc_ref((php_libxml_node_object *)attrobj, NULL);
740 	}
741 
742 	xmlAddChild(nodep, (xmlNodePtr) attrp);
743 	if (!modern) {
744 		dom_mark_ids_modified(intern->document);
745 		php_dom_reconcile_attribute_namespace_after_insertion(attrp);
746 	} else {
747 		dom_check_register_attribute_id(attrp, intern->document);
748 	}
749 
750 	/* Returns old property if removed otherwise NULL */
751 	if (existattrp != NULL) {
752 		DOM_RET_OBJ((xmlNodePtr) existattrp, intern);
753 	} else {
754 		RETURN_NULL();
755 	}
756 }
757 
758 /* {{{ URL: http://www.w3.org/TR/2003/WD-DOM-Level-3-Core-20030226/DOM3-Core.html#core-ID-887236154
759 Modern spec URL: https://dom.spec.whatwg.org/#dom-element-setattributenode
760 Since:
761 */
PHP_METHOD(DOMElement,setAttributeNode)762 PHP_METHOD(DOMElement, setAttributeNode)
763 {
764 	dom_element_set_attribute_node_common(INTERNAL_FUNCTION_PARAM_PASSTHRU, /* use_ns */ false, /* modern */ false);
765 }
766 /* }}} end dom_element_set_attribute_node */
767 
768 /* {{{ URL: http://www.w3.org/TR/2003/WD-DOM-Level-3-Core-20030226/DOM3-Core.html#core-ID-D589198
769 Since:
770 */
dom_element_remove_attribute_node(INTERNAL_FUNCTION_PARAMETERS,zend_class_entry * node_ce)771 static void dom_element_remove_attribute_node(INTERNAL_FUNCTION_PARAMETERS, zend_class_entry *node_ce)
772 {
773 	zval *node;
774 	xmlNode *nodep;
775 	xmlAttr *attrp;
776 	dom_object *intern, *attrobj;
777 
778 	if (zend_parse_parameters(ZEND_NUM_ARGS(), "O", &node, node_ce) == FAILURE) {
779 		RETURN_THROWS();
780 	}
781 
782 	DOM_GET_OBJ(nodep, ZEND_THIS, xmlNodePtr, intern);
783 
784 	DOM_GET_OBJ(attrp, node, xmlAttrPtr, attrobj);
785 
786 	ZEND_ASSERT(attrp->type == XML_ATTRIBUTE_NODE);
787 
788 	if (attrp->parent != nodep) {
789 		php_dom_throw_error(NOT_FOUND_ERR, dom_get_strict_error(intern->document));
790 		RETURN_FALSE;
791 	}
792 
793 	xmlUnlinkNode((xmlNodePtr) attrp);
794 
795 	DOM_RET_OBJ((xmlNodePtr) attrp, intern);
796 }
797 
PHP_METHOD(DOMElement,removeAttributeNode)798 PHP_METHOD(DOMElement, removeAttributeNode)
799 {
800 	dom_element_remove_attribute_node(INTERNAL_FUNCTION_PARAM_PASSTHRU, dom_attr_class_entry);
801 }
802 
PHP_METHOD(Dom_Element,removeAttributeNode)803 PHP_METHOD(Dom_Element, removeAttributeNode)
804 {
805 	dom_element_remove_attribute_node(INTERNAL_FUNCTION_PARAM_PASSTHRU, dom_modern_attr_class_entry);
806 }
807 /* }}} end dom_element_remove_attribute_node */
808 
809 /* {{{ URL: http://www.w3.org/TR/2003/WD-DOM-Level-3-Core-20030226/DOM3-Core.html#core-ID-1938918D
810 Modern spec URL: https://dom.spec.whatwg.org/#concept-getelementsbytagname
811 Since:
812 */
dom_element_get_elements_by_tag_name(INTERNAL_FUNCTION_PARAMETERS,bool modern)813 static void dom_element_get_elements_by_tag_name(INTERNAL_FUNCTION_PARAMETERS, bool modern)
814 {
815 	size_t name_len;
816 	dom_object *intern, *namednode;
817 	char *name;
818 
819 	if (zend_parse_parameters(ZEND_NUM_ARGS(), "s", &name, &name_len) == FAILURE) {
820 		RETURN_THROWS();
821 	}
822 
823 	DOM_GET_THIS_INTERN(intern);
824 
825 	if (modern) {
826 		php_dom_create_iterator(return_value, DOM_HTMLCOLLECTION, true);
827 	} else {
828 		php_dom_create_iterator(return_value, DOM_NODELIST, false);
829 	}
830 	namednode = Z_DOMOBJ_P(return_value);
831 	dom_namednode_iter(intern, 0, namednode, NULL, name, name_len, NULL, 0);
832 }
833 
PHP_METHOD(DOMElement,getElementsByTagName)834 PHP_METHOD(DOMElement, getElementsByTagName)
835 {
836 	dom_element_get_elements_by_tag_name(INTERNAL_FUNCTION_PARAM_PASSTHRU, false);
837 }
838 
PHP_METHOD(Dom_Element,getElementsByTagName)839 PHP_METHOD(Dom_Element, getElementsByTagName)
840 {
841 	dom_element_get_elements_by_tag_name(INTERNAL_FUNCTION_PARAM_PASSTHRU, true);
842 }
843 /* }}} end dom_element_get_elements_by_tag_name */
844 
845 /* should_free_result must be initialized to false */
dom_get_attribute_ns(dom_object * intern,xmlNodePtr elemp,const char * uri,size_t uri_len,const char * name,bool * should_free_result)846 static const xmlChar *dom_get_attribute_ns(dom_object *intern, xmlNodePtr elemp, const char *uri, size_t uri_len, const char *name, bool *should_free_result)
847 {
848 	bool follow_spec = php_dom_follow_spec_intern(intern);
849 	if (follow_spec && uri_len == 0) {
850 		uri = NULL;
851 	}
852 
853 	xmlChar *strattr = xmlGetNsProp(elemp, BAD_CAST name, BAD_CAST uri);
854 
855 	if (strattr != NULL) {
856 		*should_free_result = true;
857 		return strattr;
858 	} else {
859 		if (!follow_spec && xmlStrEqual(BAD_CAST uri, BAD_CAST DOM_XMLNS_NS_URI)) {
860 			xmlNsPtr nsptr = dom_get_nsdecl(elemp, BAD_CAST name);
861 			if (nsptr != NULL) {
862 				return nsptr->href;
863 			} else {
864 				return NULL;
865 			}
866 		} else {
867 			return NULL;
868 		}
869 	}
870 }
871 
872 /* {{{ URL: http://www.w3.org/TR/2003/WD-DOM-Level-3-Core-20030226/DOM3-Core.html#core-ID-ElGetAttrNS
873 Modern spec URL: https://dom.spec.whatwg.org/#dom-element-getattributens
874 Since: DOM Level 2
875 */
PHP_METHOD(DOMElement,getAttributeNS)876 PHP_METHOD(DOMElement, getAttributeNS)
877 {
878 	zval *id;
879 	xmlNodePtr elemp;
880 	dom_object *intern;
881 	size_t uri_len = 0, name_len = 0;
882 	char *uri, *name;
883 
884 	id = ZEND_THIS;
885 	if (zend_parse_parameters(ZEND_NUM_ARGS(), "s!s", &uri, &uri_len, &name, &name_len) == FAILURE) {
886 		RETURN_THROWS();
887 	}
888 
889 	DOM_GET_OBJ(elemp, id, xmlNodePtr, intern);
890 
891 	bool should_free_result = false;
892 	const xmlChar *result = dom_get_attribute_ns(intern, elemp, uri, uri_len, name, &should_free_result);
893 	if (result == NULL) {
894 		if (php_dom_follow_spec_intern(intern)) {
895 			RETURN_NULL();
896 		}
897 		RETURN_EMPTY_STRING();
898 	} else {
899 		RETVAL_STRING((const char *) result);
900 		if (should_free_result) {
901 			xmlFree(BAD_CAST result);
902 		}
903 	}
904 }
905 /* }}} end dom_element_get_attribute_ns */
906 
dom_set_attribute_ns_legacy(dom_object * intern,xmlNodePtr elemp,char * uri,size_t uri_len,char * name,size_t name_len,const char * value)907 static void dom_set_attribute_ns_legacy(dom_object *intern, xmlNodePtr elemp, char *uri, size_t uri_len, char *name, size_t name_len, const char *value)
908 {
909 	if (name_len == 0) {
910 		zend_argument_must_not_be_empty_error(2);
911 		return;
912 	}
913 
914 	xmlNodePtr nodep = NULL;
915 	xmlNsPtr nsptr;
916 	xmlAttr *attr;
917 	char *localname = NULL, *prefix = NULL;
918 	int is_xmlns = 0, name_valid;
919 	bool stricterror = dom_get_strict_error(intern->document);
920 
921 	int errorcode = dom_check_qname(name, &localname, &prefix, uri_len, name_len);
922 
923 	if (errorcode == 0) {
924 		dom_mark_ids_modified(intern->document);
925 
926 		if (uri_len > 0) {
927 			nodep = (xmlNodePtr) xmlHasNsProp(elemp, BAD_CAST localname, BAD_CAST uri);
928 			if (nodep != NULL && nodep->type != XML_ATTRIBUTE_DECL) {
929 				node_list_unlink(nodep->children);
930 			}
931 
932 			if ((xmlStrEqual(BAD_CAST prefix, BAD_CAST "xmlns") ||
933 				(prefix == NULL && xmlStrEqual(BAD_CAST localname, BAD_CAST "xmlns"))) &&
934 				xmlStrEqual(BAD_CAST uri, BAD_CAST DOM_XMLNS_NS_URI)) {
935 				is_xmlns = 1;
936 				if (prefix == NULL) {
937 					nsptr = dom_get_nsdecl(elemp, NULL);
938 				} else {
939 					nsptr = dom_get_nsdecl(elemp, BAD_CAST localname);
940 				}
941 			} else {
942 				nsptr = xmlSearchNsByHref(elemp->doc, elemp, BAD_CAST uri);
943 				if (nsptr && nsptr->prefix == NULL) {
944 					xmlNsPtr tmpnsptr;
945 
946 					tmpnsptr = nsptr->next;
947 					while (tmpnsptr) {
948 						if ((tmpnsptr->prefix != NULL) && (tmpnsptr->href != NULL) &&
949 							(xmlStrEqual(tmpnsptr->href, BAD_CAST uri))) {
950 							nsptr = tmpnsptr;
951 							break;
952 						}
953 						tmpnsptr = tmpnsptr->next;
954 					}
955 					if (tmpnsptr == NULL) {
956 						nsptr = dom_get_ns_resolve_prefix_conflict(elemp, (const char *) nsptr->href);
957 					}
958 				}
959 			}
960 
961 			if (nsptr == NULL) {
962 				if (is_xmlns == 1) {
963 					xmlNewNs(elemp, BAD_CAST value, prefix == NULL ? NULL : BAD_CAST localname);
964 				} else {
965 					nsptr = dom_get_ns(elemp, uri, &errorcode, prefix);
966 				}
967 				xmlReconciliateNs(elemp->doc, elemp);
968 			} else {
969 				if (is_xmlns == 1) {
970 					if (nsptr->href) {
971 						xmlFree(BAD_CAST nsptr->href);
972 					}
973 					nsptr->href = xmlStrdup(BAD_CAST value);
974 				}
975 			}
976 
977 			if (errorcode == 0 && is_xmlns == 0) {
978 				xmlSetNsProp(elemp, nsptr, BAD_CAST localname, BAD_CAST value);
979 			}
980 		} else {
981 			name_valid = xmlValidateName(BAD_CAST localname, 0);
982 			if (name_valid != 0) {
983 				errorcode = INVALID_CHARACTER_ERR;
984 				stricterror = 1;
985 			} else {
986 				attr = xmlHasProp(elemp, BAD_CAST localname);
987 				if (attr != NULL && attr->type != XML_ATTRIBUTE_DECL) {
988 					node_list_unlink(attr->children);
989 				}
990 				xmlSetProp(elemp, BAD_CAST localname, BAD_CAST value);
991 			}
992 		}
993 	}
994 
995 	xmlFree(localname);
996 	if (prefix != NULL) {
997 		xmlFree(prefix);
998 	}
999 
1000 	if (errorcode != 0) {
1001 		php_dom_throw_error(errorcode, stricterror);
1002 	}
1003 }
1004 
1005 /* https://dom.spec.whatwg.org/#dom-element-setattributens */
dom_set_attribute_ns_modern(dom_object * intern,xmlNodePtr elemp,zend_string * uri,const zend_string * name,const char * value)1006 static void dom_set_attribute_ns_modern(dom_object *intern, xmlNodePtr elemp, zend_string *uri, const zend_string *name, const char *value)
1007 {
1008 	xmlChar *localname = NULL, *prefix = NULL;
1009 	int errorcode = dom_validate_and_extract(uri, name, &localname, &prefix);
1010 
1011 	if (errorcode == 0) {
1012 		php_dom_libxml_ns_mapper *ns_mapper = php_dom_get_ns_mapper(intern);
1013 		xmlNsPtr ns = php_dom_libxml_ns_mapper_get_ns_raw_prefix_string(ns_mapper, prefix, xmlStrlen(prefix), uri);
1014 		xmlAttrPtr attr = xmlSetNsProp(elemp, ns, localname, BAD_CAST value);
1015 		if (UNEXPECTED(attr == NULL)) {
1016 			php_dom_throw_error(INVALID_STATE_ERR, /* strict */ true);
1017 		} else {
1018 			dom_check_register_attribute_id(attr, intern->document);
1019 		}
1020 	} else {
1021 		php_dom_throw_error(errorcode, /* strict */ true);
1022 	}
1023 
1024 	xmlFree(localname);
1025 	xmlFree(prefix);
1026 }
1027 
1028 /* {{{ URL: http://www.w3.org/TR/2003/WD-DOM-Level-3-Core-20030226/DOM3-Core.html#core-ID-ElSetAttrNS
1029 Modern spec URL: https://dom.spec.whatwg.org/#dom-element-setattributens
1030 Since: DOM Level 2
1031 */
PHP_METHOD(DOMElement,setAttributeNS)1032 PHP_METHOD(DOMElement, setAttributeNS)
1033 {
1034 	zval *id;
1035 	xmlNodePtr elemp;
1036 	size_t value_len = 0;
1037 	char *value;
1038 	zend_string *uri;
1039 	zend_string *name = NULL;
1040 	dom_object *intern;
1041 
1042 	id = ZEND_THIS;
1043 	if (zend_parse_parameters(ZEND_NUM_ARGS(), "S!Ss", &uri, &name, &value, &value_len) == FAILURE) {
1044 		RETURN_THROWS();
1045 	}
1046 
1047 	DOM_GET_OBJ(elemp, id, xmlNodePtr, intern);
1048 
1049 	if (php_dom_follow_spec_intern(intern)) {
1050 		dom_set_attribute_ns_modern(intern, elemp, uri, name, value);
1051 	} else {
1052 		dom_set_attribute_ns_legacy(intern, elemp, uri ? ZSTR_VAL(uri) : NULL, uri ? ZSTR_LEN(uri) : 0, ZSTR_VAL(name), ZSTR_LEN(name), value);
1053 	}
1054 }
1055 /* }}} end dom_element_set_attribute_ns */
1056 
dom_remove_eliminated_ns_single_element(xmlNodePtr node,xmlNsPtr eliminatedNs)1057 static void dom_remove_eliminated_ns_single_element(xmlNodePtr node, xmlNsPtr eliminatedNs)
1058 {
1059 	ZEND_ASSERT(node->type == XML_ELEMENT_NODE);
1060 	if (node->ns == eliminatedNs) {
1061 		node->ns = NULL;
1062 	}
1063 
1064 	for (xmlAttrPtr attr = node->properties; attr != NULL; attr = attr->next) {
1065 		if (attr->ns == eliminatedNs) {
1066 			attr->ns = NULL;
1067 		}
1068 	}
1069 }
1070 
dom_remove_eliminated_ns(xmlNodePtr node,xmlNsPtr eliminatedNs)1071 static void dom_remove_eliminated_ns(xmlNodePtr node, xmlNsPtr eliminatedNs)
1072 {
1073 	dom_remove_eliminated_ns_single_element(node, eliminatedNs);
1074 
1075 	xmlNodePtr base = node;
1076 	node = node->children;
1077 	while (node != NULL) {
1078 		ZEND_ASSERT(node != base);
1079 
1080 		if (node->type == XML_ELEMENT_NODE) {
1081 			dom_remove_eliminated_ns_single_element(node, eliminatedNs);
1082 		}
1083 
1084 		node = php_dom_next_in_tree_order(node, base);
1085 	}
1086 }
1087 
dom_eliminate_ns(xmlNodePtr nodep,xmlNsPtr nsptr)1088 static void dom_eliminate_ns(xmlNodePtr nodep, xmlNsPtr nsptr)
1089 {
1090 	if (nsptr->href != NULL) {
1091 		xmlFree((char *) nsptr->href);
1092 		nsptr->href = NULL;
1093 	}
1094 	if (nsptr->prefix != NULL) {
1095 		xmlFree((char *) nsptr->prefix);
1096 		nsptr->prefix = NULL;
1097 	}
1098 
1099 	/* Remove it from the list and move it to the old ns list */
1100 	xmlNsPtr current_ns = nodep->nsDef;
1101 	if (current_ns == nsptr) {
1102 		nodep->nsDef = nsptr->next;
1103 	} else {
1104 		do {
1105 			if (current_ns->next == nsptr) {
1106 				current_ns->next = nsptr->next;
1107 				break;
1108 			}
1109 			current_ns = current_ns->next;
1110 		} while (current_ns != NULL);
1111 	}
1112 	nsptr->next = NULL;
1113 	php_libxml_set_old_ns(nodep->doc, nsptr);
1114 
1115 	dom_remove_eliminated_ns(nodep, nsptr);
1116 }
1117 
1118 /* {{{ URL: http://www.w3.org/TR/2003/WD-DOM-Level-3-Core-20030226/DOM3-Core.html#core-ID-ElRemAtNS
1119 Modern spec URL: https://dom.spec.whatwg.org/#dom-element-removeattributens
1120 Since: DOM Level 2
1121 */
PHP_METHOD(DOMElement,removeAttributeNS)1122 PHP_METHOD(DOMElement, removeAttributeNS)
1123 {
1124 	zval *id;
1125 	xmlNode *nodep;
1126 	xmlAttr *attrp;
1127 	xmlNsPtr nsptr;
1128 	dom_object *intern;
1129 	size_t name_len, uri_len;
1130 	char *name, *uri;
1131 
1132 	id = ZEND_THIS;
1133 	if (zend_parse_parameters(ZEND_NUM_ARGS(), "s!s", &uri, &uri_len, &name, &name_len) == FAILURE) {
1134 		RETURN_THROWS();
1135 	}
1136 
1137 	DOM_GET_OBJ(nodep, id, xmlNodePtr, intern);
1138 
1139 	bool follow_spec = php_dom_follow_spec_intern(intern);
1140 	if (follow_spec && uri_len == 0) {
1141 		uri = NULL;
1142 	}
1143 
1144 	attrp = xmlHasNsProp(nodep, BAD_CAST name, BAD_CAST uri);
1145 
1146 	if (!follow_spec) {
1147 		nsptr = dom_get_nsdecl(nodep, BAD_CAST name);
1148 		if (nsptr != NULL) {
1149 			if (xmlStrEqual(BAD_CAST uri, nsptr->href)) {
1150 				dom_eliminate_ns(nodep, nsptr);
1151 			} else {
1152 				return;
1153 			}
1154 		}
1155 	}
1156 
1157 	if (attrp && attrp->type != XML_ATTRIBUTE_DECL) {
1158 		if (php_dom_object_get_data((xmlNodePtr) attrp) == NULL) {
1159 			node_list_unlink(attrp->children);
1160 			xmlUnlinkNode((xmlNodePtr) attrp);
1161 			xmlFreeProp(attrp);
1162 		} else {
1163 			xmlUnlinkNode((xmlNodePtr) attrp);
1164 		}
1165 	}
1166 }
1167 /* }}} end dom_element_remove_attribute_ns */
1168 
1169 /* {{{ URL: http://www.w3.org/TR/2003/WD-DOM-Level-3-Core-20030226/DOM3-Core.html#core-ID-ElGetAtNodeNS
1170 Modern spec URL: https://dom.spec.whatwg.org/#dom-element-getattributenodens
1171 Since: DOM Level 2
1172 */
PHP_METHOD(DOMElement,getAttributeNodeNS)1173 PHP_METHOD(DOMElement, getAttributeNodeNS)
1174 {
1175 	zval *id;
1176 	xmlNodePtr elemp;
1177 	xmlAttrPtr attrp;
1178 	dom_object *intern;
1179 	size_t uri_len, name_len;
1180 	char *uri, *name;
1181 
1182 	id = ZEND_THIS;
1183 	if (zend_parse_parameters(ZEND_NUM_ARGS(), "s!s", &uri, &uri_len, &name, &name_len) == FAILURE) {
1184 		RETURN_THROWS();
1185 	}
1186 
1187 	DOM_GET_OBJ(elemp, id, xmlNodePtr, intern);
1188 
1189 	bool follow_spec = php_dom_follow_spec_intern(intern);
1190 	if (follow_spec && uri_len == 0) {
1191 		uri = NULL;
1192 	}
1193 
1194 	attrp = xmlHasNsProp(elemp, BAD_CAST name, BAD_CAST uri);
1195 
1196 	if (attrp == NULL) {
1197 		if (!follow_spec && xmlStrEqual(BAD_CAST uri, BAD_CAST DOM_XMLNS_NS_URI)) {
1198 			xmlNsPtr nsptr;
1199 			nsptr = dom_get_nsdecl(elemp, BAD_CAST name);
1200 			if (nsptr != NULL) {
1201 				/* Keep parent alive, because we're a fake child. */
1202 				GC_ADDREF(&intern->std);
1203 				(void) php_dom_create_fake_namespace_decl(elemp, nsptr, return_value, intern);
1204 			} else {
1205 				RETURN_NULL();
1206 			}
1207 		} else {
1208 			RETURN_NULL();
1209 		}
1210 	} else {
1211 		DOM_RET_OBJ((xmlNodePtr) attrp, intern);
1212 	}
1213 
1214 }
1215 /* }}} end dom_element_get_attribute_node_ns */
1216 
1217 /* {{{ URL: http://www.w3.org/TR/2003/WD-DOM-Level-3-Core-20030226/DOM3-Core.html#core-ID-ElSetAtNodeNS
1218 Modern spec URL: https://dom.spec.whatwg.org/#dom-element-setattributenodens
1219 Since: DOM Level 2
1220 */
PHP_METHOD(DOMElement,setAttributeNodeNS)1221 PHP_METHOD(DOMElement, setAttributeNodeNS)
1222 {
1223 	dom_element_set_attribute_node_common(INTERNAL_FUNCTION_PARAM_PASSTHRU, /* use_ns */ true, /* modern */ false);
1224 }
1225 
PHP_METHOD(Dom_Element,setAttributeNodeNS)1226 PHP_METHOD(Dom_Element, setAttributeNodeNS)
1227 {
1228 	dom_element_set_attribute_node_common(INTERNAL_FUNCTION_PARAM_PASSTHRU, /* use_ns */ true, /* modern */ true);
1229 }
1230 /* }}} end dom_element_set_attribute_node_ns */
1231 
1232 /* {{{ URL: http://www.w3.org/TR/2003/WD-DOM-Level-3-Core-20030226/DOM3-Core.html#core-ID-A6C90942
1233 Modern spec URL: https://dom.spec.whatwg.org/#concept-getelementsbytagnamens
1234 Since: DOM Level 2
1235 */
dom_element_get_elements_by_tag_name_ns(INTERNAL_FUNCTION_PARAMETERS,bool modern)1236 static void dom_element_get_elements_by_tag_name_ns(INTERNAL_FUNCTION_PARAMETERS, bool modern)
1237 {
1238 	size_t uri_len, name_len;
1239 	dom_object *intern, *namednode;
1240 	char *uri, *name;
1241 
1242 	if (zend_parse_parameters(ZEND_NUM_ARGS(), "s!s", &uri, &uri_len, &name, &name_len) == FAILURE) {
1243 		RETURN_THROWS();
1244 	}
1245 
1246 	DOM_GET_THIS_INTERN(intern);
1247 
1248 	if (modern) {
1249 		php_dom_create_iterator(return_value, DOM_HTMLCOLLECTION, true);
1250 	} else {
1251 		php_dom_create_iterator(return_value, DOM_NODELIST, false);
1252 	}
1253 	namednode = Z_DOMOBJ_P(return_value);
1254 	dom_namednode_iter(intern, 0, namednode, NULL, name, name_len, uri ? uri : "", uri_len);
1255 }
1256 
PHP_METHOD(DOMElement,getElementsByTagNameNS)1257 PHP_METHOD(DOMElement, getElementsByTagNameNS)
1258 {
1259 	dom_element_get_elements_by_tag_name_ns(INTERNAL_FUNCTION_PARAM_PASSTHRU, false);
1260 }
1261 
PHP_METHOD(Dom_Element,getElementsByTagNameNS)1262 PHP_METHOD(Dom_Element, getElementsByTagNameNS)
1263 {
1264 	dom_element_get_elements_by_tag_name_ns(INTERNAL_FUNCTION_PARAM_PASSTHRU, true);
1265 }
1266 /* }}} end dom_element_get_elements_by_tag_name_ns */
1267 
1268 /* {{{ URL: http://www.w3.org/TR/2003/WD-DOM-Level-3-Core-20030226/DOM3-Core.html#core-ID-ElHasAttr
1269 Modern spec URL: https://dom.spec.whatwg.org/#dom-element-hasattribute
1270 Since: DOM Level 2
1271 */
PHP_METHOD(DOMElement,hasAttribute)1272 PHP_METHOD(DOMElement, hasAttribute)
1273 {
1274 	zval *id;
1275 	xmlNode *nodep;
1276 	dom_object *intern;
1277 	char *name;
1278 	size_t name_len;
1279 	xmlNodePtr attr;
1280 
1281 	id = ZEND_THIS;
1282 	if (zend_parse_parameters(ZEND_NUM_ARGS(), "s", &name, &name_len) == FAILURE) {
1283 		RETURN_THROWS();
1284 	}
1285 
1286 	DOM_GET_OBJ(nodep, id, xmlNodePtr, intern);
1287 
1288 	attr = dom_get_attribute_or_nsdecl(intern, nodep, BAD_CAST name, name_len);
1289 	if (attr == NULL) {
1290 		RETURN_FALSE;
1291 	} else {
1292 		RETURN_TRUE;
1293 	}
1294 }
1295 /* }}} end dom_element_has_attribute */
1296 
1297 /* {{{ URL: http://www.w3.org/TR/2003/WD-DOM-Level-3-Core-20030226/DOM3-Core.html#core-ID-ElHasAttrNS
1298 Modern spec URL: https://dom.spec.whatwg.org/#dom-element-hasattributens
1299 Since: DOM Level 2
1300 */
PHP_METHOD(DOMElement,hasAttributeNS)1301 PHP_METHOD(DOMElement, hasAttributeNS)
1302 {
1303 	zval *id;
1304 	xmlNodePtr elemp;
1305 	dom_object *intern;
1306 	size_t uri_len, name_len;
1307 	char *uri, *name;
1308 
1309 	id = ZEND_THIS;
1310 	if (zend_parse_parameters(ZEND_NUM_ARGS(), "s!s", &uri, &uri_len, &name, &name_len) == FAILURE) {
1311 		RETURN_THROWS();
1312 	}
1313 
1314 	DOM_GET_OBJ(elemp, id, xmlNodePtr, intern);
1315 
1316 	bool should_free_result = false;
1317 	const xmlChar *result = dom_get_attribute_ns(intern, elemp, uri, uri_len, name, &should_free_result);
1318 	if (result == NULL) {
1319 		RETURN_FALSE;
1320 	} else {
1321 		if (should_free_result) {
1322 			xmlFree(BAD_CAST result);
1323 		}
1324 		RETURN_TRUE;
1325 	}
1326 }
1327 /* }}} end dom_element_has_attribute_ns */
1328 
php_set_attribute_id(xmlAttrPtr attrp,bool is_id,php_libxml_ref_obj * document)1329 static void php_set_attribute_id(xmlAttrPtr attrp, bool is_id, php_libxml_ref_obj *document) /* {{{ */
1330 {
1331 	if (is_id && attrp->atype != XML_ATTRIBUTE_ID) {
1332 		attrp->atype = XML_ATTRIBUTE_ID;
1333 	} else if (!is_id && attrp->atype == XML_ATTRIBUTE_ID) {
1334 		xmlRemoveID(attrp->doc, attrp);
1335 		attrp->atype = 0;
1336 	}
1337 
1338 	dom_mark_ids_modified(document);
1339 }
1340 /* }}} */
1341 
1342 /* {{{ URL: http://www.w3.org/TR/2003/WD-DOM-Level-3-Core-20030226/DOM3-Core.html#core-ID-ElSetIdAttr
1343 Since: DOM Level 3
1344 */
PHP_METHOD(DOMElement,setIdAttribute)1345 PHP_METHOD(DOMElement, setIdAttribute)
1346 {
1347 	zval *id;
1348 	xmlNode *nodep;
1349 	xmlAttrPtr attrp;
1350 	dom_object *intern;
1351 	char *name;
1352 	size_t name_len;
1353 	bool is_id;
1354 
1355 	id = ZEND_THIS;
1356 	if (zend_parse_parameters(ZEND_NUM_ARGS(), "sb", &name, &name_len, &is_id) == FAILURE) {
1357 		RETURN_THROWS();
1358 	}
1359 
1360 	DOM_GET_OBJ(nodep, id, xmlNodePtr, intern);
1361 
1362 	attrp = xmlHasNsProp(nodep, BAD_CAST name, NULL);
1363 	if (attrp == NULL || attrp->type == XML_ATTRIBUTE_DECL) {
1364 		php_dom_throw_error(NOT_FOUND_ERR, dom_get_strict_error(intern->document));
1365 	} else {
1366 		php_set_attribute_id(attrp, is_id, intern->document);
1367 	}
1368 }
1369 /* }}} end dom_element_set_id_attribute */
1370 
1371 /* {{{ URL: http://www.w3.org/TR/2003/WD-DOM-Level-3-Core-20030226/DOM3-Core.html#core-ID-ElSetIdAttrNS
1372 Since: DOM Level 3
1373 */
PHP_METHOD(DOMElement,setIdAttributeNS)1374 PHP_METHOD(DOMElement, setIdAttributeNS)
1375 {
1376 	zval *id;
1377 	xmlNodePtr elemp;
1378 	xmlAttrPtr attrp;
1379 	dom_object *intern;
1380 	size_t uri_len, name_len;
1381 	char *uri, *name;
1382 	bool is_id;
1383 
1384 	id = ZEND_THIS;
1385 	if (zend_parse_parameters(ZEND_NUM_ARGS(), "ssb", &uri, &uri_len, &name, &name_len, &is_id) == FAILURE) {
1386 		RETURN_THROWS();
1387 	}
1388 
1389 	DOM_GET_OBJ(elemp, id, xmlNodePtr, intern);
1390 
1391 	attrp = xmlHasNsProp(elemp, BAD_CAST name, BAD_CAST uri);
1392 	if (attrp == NULL || attrp->type == XML_ATTRIBUTE_DECL) {
1393 		php_dom_throw_error(NOT_FOUND_ERR, dom_get_strict_error(intern->document));
1394 	} else {
1395 		php_set_attribute_id(attrp, is_id, intern->document);
1396 	}
1397 }
1398 /* }}} end dom_element_set_id_attribute_ns */
1399 
1400 /* {{{ URL: http://www.w3.org/TR/2003/WD-DOM-Level-3-Core-20030226/DOM3-Core.html#core-ID-ElSetIdAttrNode
1401 Since: DOM Level 3
1402 */
dom_element_set_id_attribute_node(INTERNAL_FUNCTION_PARAMETERS,zend_class_entry * attr_ce)1403 static void dom_element_set_id_attribute_node(INTERNAL_FUNCTION_PARAMETERS, zend_class_entry *attr_ce)
1404 {
1405 	zval *id, *node;
1406 	xmlNode *nodep;
1407 	xmlAttrPtr attrp;
1408 	dom_object *intern, *attrobj;
1409 	bool is_id;
1410 
1411 	id = ZEND_THIS;
1412 	if (zend_parse_parameters(ZEND_NUM_ARGS(), "Ob", &node, attr_ce, &is_id) != SUCCESS) {
1413 		RETURN_THROWS();
1414 	}
1415 
1416 	DOM_GET_OBJ(nodep, id, xmlNodePtr, intern);
1417 	DOM_GET_OBJ(attrp, node, xmlAttrPtr, attrobj);
1418 
1419 	if (attrp->parent != nodep) {
1420 		php_dom_throw_error(NOT_FOUND_ERR, dom_get_strict_error(intern->document));
1421 	} else {
1422 		php_set_attribute_id(attrp, is_id, intern->document);
1423 	}
1424 }
1425 
PHP_METHOD(DOMElement,setIdAttributeNode)1426 PHP_METHOD(DOMElement, setIdAttributeNode)
1427 {
1428 	dom_element_set_id_attribute_node(INTERNAL_FUNCTION_PARAM_PASSTHRU, dom_attr_class_entry);
1429 }
1430 
PHP_METHOD(Dom_Element,setIdAttributeNode)1431 PHP_METHOD(Dom_Element, setIdAttributeNode)
1432 {
1433 	dom_element_set_id_attribute_node(INTERNAL_FUNCTION_PARAM_PASSTHRU, dom_modern_attr_class_entry);
1434 }
1435 /* }}} end dom_element_set_id_attribute_node */
1436 
1437 /* {{{ URL:
1438 Since:
1439 */
PHP_METHOD(DOMElement,remove)1440 PHP_METHOD(DOMElement, remove)
1441 {
1442 	dom_object *intern;
1443 
1444 	if (zend_parse_parameters_none() == FAILURE) {
1445 		RETURN_THROWS();
1446 	}
1447 
1448 	DOM_GET_THIS_INTERN(intern);
1449 
1450 	dom_child_node_remove(intern);
1451 }
1452 /* }}} end DOMElement::remove */
1453 
PHP_METHOD(DOMElement,after)1454 PHP_METHOD(DOMElement, after)
1455 {
1456 	uint32_t argc = 0;
1457 	zval *args;
1458 	dom_object *intern;
1459 
1460 	ZEND_PARSE_PARAMETERS_START(0, -1)
1461 		Z_PARAM_VARIADIC('*', args, argc)
1462 	ZEND_PARSE_PARAMETERS_END();
1463 
1464 	DOM_GET_THIS_INTERN(intern);
1465 
1466 	dom_parent_node_after(intern, args, argc);
1467 }
1468 
PHP_METHOD(DOMElement,before)1469 PHP_METHOD(DOMElement, before)
1470 {
1471 	uint32_t argc = 0;
1472 	zval *args;
1473 	dom_object *intern;
1474 
1475 	ZEND_PARSE_PARAMETERS_START(0, -1)
1476 		Z_PARAM_VARIADIC('*', args, argc)
1477 	ZEND_PARSE_PARAMETERS_END();
1478 
1479 	DOM_GET_THIS_INTERN(intern);
1480 
1481 	dom_parent_node_before(intern, args, argc);
1482 }
1483 
1484 /* {{{ URL: https://dom.spec.whatwg.org/#dom-parentnode-append
1485 Since: DOM Living Standard (DOM4)
1486 */
PHP_METHOD(DOMElement,append)1487 PHP_METHOD(DOMElement, append)
1488 {
1489 	uint32_t argc = 0;
1490 	zval *args;
1491 	dom_object *intern;
1492 
1493 	ZEND_PARSE_PARAMETERS_START(0, -1)
1494 		Z_PARAM_VARIADIC('*', args, argc)
1495 	ZEND_PARSE_PARAMETERS_END();
1496 
1497 	DOM_GET_THIS_INTERN(intern);
1498 
1499 	dom_parent_node_append(intern, args, argc);
1500 }
1501 /* }}} end DOMElement::append */
1502 
1503 /* {{{ URL: https://dom.spec.whatwg.org/#dom-parentnode-prepend
1504 Since: DOM Living Standard (DOM4)
1505 */
PHP_METHOD(DOMElement,prepend)1506 PHP_METHOD(DOMElement, prepend)
1507 {
1508 	uint32_t argc = 0;
1509 	zval *args;
1510 	dom_object *intern;
1511 
1512 	ZEND_PARSE_PARAMETERS_START(0, -1)
1513 		Z_PARAM_VARIADIC('*', args, argc)
1514 	ZEND_PARSE_PARAMETERS_END();
1515 
1516 	DOM_GET_THIS_INTERN(intern);
1517 
1518 	dom_parent_node_prepend(intern, args, argc);
1519 }
1520 /* }}} end DOMElement::prepend */
1521 
1522 /* {{{ URL: https://dom.spec.whatwg.org/#dom-parentnode-replacechildren
1523 Since: DOM Living Standard (DOM4)
1524 */
PHP_METHOD(DOMElement,replaceWith)1525 PHP_METHOD(DOMElement, replaceWith)
1526 {
1527 	uint32_t argc = 0;
1528 	zval *args;
1529 	dom_object *intern;
1530 
1531 	ZEND_PARSE_PARAMETERS_START(0, -1)
1532 		Z_PARAM_VARIADIC('*', args, argc)
1533 	ZEND_PARSE_PARAMETERS_END();
1534 
1535 	DOM_GET_THIS_INTERN(intern);
1536 
1537 	dom_child_replace_with(intern, args, argc);
1538 }
1539 /* }}} end DOMElement::prepend */
1540 
1541 /* {{{ URL: https://dom.spec.whatwg.org/#dom-parentnode-replacechildren
1542 Since:
1543 */
PHP_METHOD(DOMElement,replaceChildren)1544 PHP_METHOD(DOMElement, replaceChildren)
1545 {
1546 	uint32_t argc = 0;
1547 	zval *args;
1548 	dom_object *intern;
1549 
1550 	ZEND_PARSE_PARAMETERS_START(0, -1)
1551 		Z_PARAM_VARIADIC('*', args, argc)
1552 	ZEND_PARSE_PARAMETERS_END();
1553 
1554 	DOM_GET_THIS_INTERN(intern);
1555 
1556 	dom_parent_node_replace_children(intern, args, argc);
1557 }
1558 /* }}} */
1559 
1560 #define INSERT_ADJACENT_RES_ADOPT_FAILED ((void*) -1)
1561 #define INSERT_ADJACENT_RES_SYNTAX_FAILED INSERT_ADJACENT_RES_ADOPT_FAILED
1562 #define INSERT_ADJACENT_RES_PRE_INSERT_FAILED ((void*) -2)
1563 
dom_insert_adjacent(const zend_string * where,xmlNodePtr thisp,dom_object * this_intern,xmlNodePtr otherp)1564 static xmlNodePtr dom_insert_adjacent(const zend_string *where, xmlNodePtr thisp, dom_object *this_intern, xmlNodePtr otherp)
1565 {
1566 	if (zend_string_equals_literal_ci(where, "beforebegin")) {
1567 		if (thisp->parent == NULL) {
1568 			return NULL;
1569 		}
1570 		if (!php_dom_adopt_node(otherp, this_intern, thisp->doc)) {
1571 			return INSERT_ADJACENT_RES_ADOPT_FAILED;
1572 		}
1573 		if (!php_dom_pre_insert(this_intern->document, otherp, thisp->parent, thisp)) {
1574 			return INSERT_ADJACENT_RES_PRE_INSERT_FAILED;
1575 		}
1576 	} else if (zend_string_equals_literal_ci(where, "afterbegin")) {
1577 		if (!php_dom_adopt_node(otherp, this_intern, thisp->doc)) {
1578 			return INSERT_ADJACENT_RES_ADOPT_FAILED;
1579 		}
1580 		if (!php_dom_pre_insert(this_intern->document, otherp, thisp, thisp->children)) {
1581 			return INSERT_ADJACENT_RES_PRE_INSERT_FAILED;
1582 		}
1583 	} else if (zend_string_equals_literal_ci(where, "beforeend")) {
1584 		if (!php_dom_adopt_node(otherp, this_intern, thisp->doc)) {
1585 			return INSERT_ADJACENT_RES_ADOPT_FAILED;
1586 		}
1587 		if (!php_dom_pre_insert(this_intern->document, otherp, thisp, NULL)) {
1588 			return INSERT_ADJACENT_RES_PRE_INSERT_FAILED;
1589 		}
1590 	} else if (zend_string_equals_literal_ci(where, "afterend")) {
1591 		if (thisp->parent == NULL) {
1592 			return NULL;
1593 		}
1594 		if (!php_dom_adopt_node(otherp, this_intern, thisp->doc)) {
1595 			return INSERT_ADJACENT_RES_ADOPT_FAILED;
1596 		}
1597 		if (!php_dom_pre_insert(this_intern->document, otherp, thisp->parent, thisp->next))  {
1598 			return INSERT_ADJACENT_RES_PRE_INSERT_FAILED;
1599 		}
1600 	} else {
1601 		php_dom_throw_error(SYNTAX_ERR, dom_get_strict_error(this_intern->document));
1602 		return INSERT_ADJACENT_RES_SYNTAX_FAILED;
1603 	}
1604 	return otherp;
1605 }
1606 
1607 /* {{{ URL: https://dom.spec.whatwg.org/#dom-element-insertadjacentelement
1608 Since:
1609 */
dom_element_insert_adjacent_element(INTERNAL_FUNCTION_PARAMETERS,const zend_string * where,zval * element_zval)1610 static void dom_element_insert_adjacent_element(INTERNAL_FUNCTION_PARAMETERS, const zend_string *where, zval *element_zval)
1611 {
1612 	zval *id;
1613 	xmlNodePtr thisp, otherp;
1614 	dom_object *this_intern, *other_intern;
1615 
1616 	DOM_GET_THIS_OBJ(thisp, id, xmlNodePtr, this_intern);
1617 	DOM_GET_OBJ(otherp, element_zval, xmlNodePtr, other_intern);
1618 
1619 	xmlNodePtr result = dom_insert_adjacent(where, thisp, this_intern, otherp);
1620 	if (result == NULL) {
1621 		RETURN_NULL();
1622 	} else if (result != INSERT_ADJACENT_RES_ADOPT_FAILED && result != INSERT_ADJACENT_RES_PRE_INSERT_FAILED) {
1623 		DOM_RET_OBJ(otherp, other_intern);
1624 	} else {
1625 		RETURN_THROWS();
1626 	}
1627 }
1628 
PHP_METHOD(DOMElement,insertAdjacentElement)1629 PHP_METHOD(DOMElement, insertAdjacentElement)
1630 {
1631 	zend_string *where;
1632 	zval *element_zval;
1633 
1634 	if (zend_parse_parameters(ZEND_NUM_ARGS(), "SO", &where, &element_zval, dom_element_class_entry) != SUCCESS) {
1635 		RETURN_THROWS();
1636 	}
1637 
1638 	dom_element_insert_adjacent_element(INTERNAL_FUNCTION_PARAM_PASSTHRU, where, element_zval);
1639 }
1640 
PHP_METHOD(Dom_Element,insertAdjacentElement)1641 PHP_METHOD(Dom_Element, insertAdjacentElement)
1642 {
1643 	zval *element_zval, *where_zv;
1644 
1645 	ZEND_PARSE_PARAMETERS_START(2, 2)
1646 		Z_PARAM_OBJECT_OF_CLASS(where_zv, dom_adjacent_position_class_entry)
1647 		Z_PARAM_OBJECT_OF_CLASS(element_zval, dom_modern_element_class_entry)
1648 	ZEND_PARSE_PARAMETERS_END();
1649 
1650 	const zend_string *where = Z_STR_P(zend_enum_fetch_case_name(Z_OBJ_P(where_zv)));
1651 	dom_element_insert_adjacent_element(INTERNAL_FUNCTION_PARAM_PASSTHRU, where, element_zval);
1652 }
1653 /* }}} end DOMElement::insertAdjacentElement */
1654 
1655 /* {{{ URL: https://dom.spec.whatwg.org/#dom-element-insertadjacenttext
1656 Since:
1657 */
dom_element_insert_adjacent_text(INTERNAL_FUNCTION_PARAMETERS,const zend_string * where,const zend_string * data)1658 static void dom_element_insert_adjacent_text(INTERNAL_FUNCTION_PARAMETERS, const zend_string *where, const zend_string *data)
1659 {
1660 	dom_object *this_intern;
1661 	zval *id;
1662 	xmlNodePtr thisp;
1663 
1664 	DOM_GET_THIS_OBJ(thisp, id, xmlNodePtr, this_intern);
1665 
1666 	if (UNEXPECTED(ZEND_SIZE_T_INT_OVFL(ZSTR_LEN(data)))) {
1667 		zend_argument_value_error(2, "is too long");
1668 		RETURN_THROWS();
1669 	}
1670 
1671 	xmlNodePtr otherp = xmlNewDocTextLen(thisp->doc, (const xmlChar *) ZSTR_VAL(data), ZSTR_LEN(data));
1672 	xmlNodePtr result = dom_insert_adjacent(where, thisp, this_intern, otherp);
1673 	if (result == NULL || result == INSERT_ADJACENT_RES_ADOPT_FAILED) {
1674 		xmlFreeNode(otherp);
1675 	}
1676 }
1677 
PHP_METHOD(DOMElement,insertAdjacentText)1678 PHP_METHOD(DOMElement, insertAdjacentText)
1679 {
1680 	zend_string *where, *data;
1681 
1682 	if (zend_parse_parameters(ZEND_NUM_ARGS(), "SS", &where, &data) == FAILURE) {
1683 		RETURN_THROWS();
1684 	}
1685 
1686 	dom_element_insert_adjacent_text(INTERNAL_FUNCTION_PARAM_PASSTHRU, where, data);
1687 }
1688 
PHP_METHOD(Dom_Element,insertAdjacentText)1689 PHP_METHOD(Dom_Element, insertAdjacentText)
1690 {
1691 	zval *where_zv;
1692 	zend_string *data;
1693 
1694 	ZEND_PARSE_PARAMETERS_START(2, 2)
1695 		Z_PARAM_OBJECT_OF_CLASS(where_zv, dom_adjacent_position_class_entry)
1696 		Z_PARAM_STR(data)
1697 	ZEND_PARSE_PARAMETERS_END();
1698 
1699 	const zend_string *where = Z_STR_P(zend_enum_fetch_case_name(Z_OBJ_P(where_zv)));
1700 	dom_element_insert_adjacent_text(INTERNAL_FUNCTION_PARAM_PASSTHRU, where, data);
1701 }
1702 /* }}} end DOMElement::insertAdjacentText */
1703 
1704 /* {{{ URL: https://dom.spec.whatwg.org/#dom-element-toggleattribute
1705 Since:
1706 */
PHP_METHOD(DOMElement,toggleAttribute)1707 PHP_METHOD(DOMElement, toggleAttribute)
1708 {
1709 	char *qname, *qname_tmp = NULL;
1710 	size_t qname_length;
1711 	bool force, force_is_null = true;
1712 	xmlNodePtr thisp;
1713 	zval *id;
1714 	dom_object *intern;
1715 	bool retval;
1716 
1717 	if (zend_parse_parameters(ZEND_NUM_ARGS(), "s|b!", &qname, &qname_length, &force, &force_is_null) == FAILURE) {
1718 		RETURN_THROWS();
1719 	}
1720 
1721 	DOM_GET_THIS_OBJ(thisp, id, xmlNodePtr, intern);
1722 
1723 	/* Step 1 */
1724 	if (xmlValidateName(BAD_CAST qname, 0) != 0) {
1725 		php_dom_throw_error(INVALID_CHARACTER_ERR, true);
1726 		RETURN_THROWS();
1727 	}
1728 
1729 	bool follow_spec = php_dom_follow_spec_intern(intern);
1730 
1731 	/* Step 2 */
1732 	if (thisp->doc != NULL && thisp->doc->type == XML_HTML_DOCUMENT_NODE
1733 		&& ((!follow_spec && thisp->ns == NULL) || (thisp->ns != NULL && xmlStrEqual(thisp->ns->href, BAD_CAST DOM_XHTML_NS_URI)))) {
1734 		qname_tmp = zend_str_tolower_dup_ex(qname, qname_length);
1735 		if (qname_tmp != NULL) {
1736 			qname = qname_tmp;
1737 		}
1738 	}
1739 
1740 	/* Step 3 */
1741 	xmlNodePtr attribute = dom_get_attribute_or_nsdecl(intern, thisp, BAD_CAST qname, qname_length);
1742 
1743 	/* Step 4 */
1744 	if (attribute == NULL) {
1745 		/* Step 4.1 */
1746 		if (force_is_null || force) {
1747 			if (follow_spec) {
1748 				xmlSetNsProp(thisp, NULL, BAD_CAST qname, NULL);
1749 			} else {
1750 				/* The behaviour for namespaces isn't defined by spec, but this is based on observing browers behaviour.
1751 				* It follows the same rules when you'd manually add an attribute using the other APIs. */
1752 				int len;
1753 				const xmlChar *split = xmlSplitQName3((const xmlChar *) qname, &len);
1754 				if (split == NULL || strncmp(qname, "xmlns:", len + 1 /* +1 for matching ':' too */) != 0) {
1755 					/* unqualified name, or qualified name with no xml namespace declaration */
1756 					dom_create_attribute(thisp, qname, "");
1757 				} else {
1758 					/* qualified name with xml namespace declaration */
1759 					xmlNewNs(thisp, (const xmlChar *) "", (const xmlChar *) (qname + len + 1));
1760 				}
1761 			}
1762 			retval = true;
1763 			goto out;
1764 		}
1765 		/* Step 4.2 */
1766 		retval = false;
1767 		goto out;
1768 	}
1769 
1770 	/* Step 5 */
1771 	if (force_is_null || !force) {
1772 		dom_remove_attribute(thisp, attribute);
1773 		retval = false;
1774 		goto out;
1775 	}
1776 
1777 	/* Step 6 */
1778 	retval = true;
1779 
1780 out:
1781 	if (qname_tmp) {
1782 		efree(qname_tmp);
1783 	}
1784 	RETURN_BOOL(retval);
1785 }
1786 /* }}} end DOMElement::prepend */
1787 
php_dom_dispatch_query_selector(INTERNAL_FUNCTION_PARAMETERS,bool all)1788 static void php_dom_dispatch_query_selector(INTERNAL_FUNCTION_PARAMETERS, bool all)
1789 {
1790 	zend_string *selectors_str;
1791 
1792 	ZEND_PARSE_PARAMETERS_START(1, 1)
1793 		Z_PARAM_STR(selectors_str)
1794 	ZEND_PARSE_PARAMETERS_END();
1795 
1796 	xmlNodePtr thisp;
1797 	dom_object *intern;
1798 	zval *id;
1799 	DOM_GET_THIS_OBJ(thisp, id, xmlNodePtr, intern);
1800 
1801 	if (all) {
1802 		dom_parent_node_query_selector_all(thisp, intern, return_value, selectors_str);
1803 	} else {
1804 		dom_parent_node_query_selector(thisp, intern, return_value, selectors_str);
1805 	}
1806 }
1807 
PHP_METHOD(Dom_Element,querySelector)1808 PHP_METHOD(Dom_Element, querySelector)
1809 {
1810 	php_dom_dispatch_query_selector(INTERNAL_FUNCTION_PARAM_PASSTHRU, false);
1811 }
1812 
PHP_METHOD(Dom_Element,querySelectorAll)1813 PHP_METHOD(Dom_Element, querySelectorAll)
1814 {
1815 	php_dom_dispatch_query_selector(INTERNAL_FUNCTION_PARAM_PASSTHRU, true);
1816 }
1817 
PHP_METHOD(Dom_Element,matches)1818 PHP_METHOD(Dom_Element, matches)
1819 {
1820 	zend_string *selectors_str;
1821 
1822 	ZEND_PARSE_PARAMETERS_START(1, 1)
1823 		Z_PARAM_STR(selectors_str)
1824 	ZEND_PARSE_PARAMETERS_END();
1825 
1826 	xmlNodePtr thisp;
1827 	dom_object *intern;
1828 	zval *id;
1829 	DOM_GET_THIS_OBJ(thisp, id, xmlNodePtr, intern);
1830 
1831 	dom_element_matches(thisp, intern, return_value, selectors_str);
1832 }
1833 
PHP_METHOD(Dom_Element,closest)1834 PHP_METHOD(Dom_Element, closest)
1835 {
1836 	zend_string *selectors_str;
1837 
1838 	ZEND_PARSE_PARAMETERS_START(1, 1)
1839 		Z_PARAM_STR(selectors_str)
1840 	ZEND_PARSE_PARAMETERS_END();
1841 
1842 	xmlNodePtr thisp;
1843 	dom_object *intern;
1844 	zval *id;
1845 	DOM_GET_THIS_OBJ(thisp, id, xmlNodePtr, intern);
1846 
1847 	dom_element_closest(thisp, intern, return_value, selectors_str);
1848 }
1849 
dom_modern_element_substituted_node_value_read(dom_object * obj,zval * retval)1850 zend_result dom_modern_element_substituted_node_value_read(dom_object *obj, zval *retval)
1851 {
1852 	DOM_PROP_NODE(xmlNodePtr, nodep, obj);
1853 
1854 	xmlChar *content = xmlNodeGetContent(nodep);
1855 
1856 	if (UNEXPECTED(content == NULL)) {
1857 		php_dom_throw_error(INVALID_STATE_ERR, true);
1858 		return FAILURE;
1859 	} else {
1860 		ZVAL_STRING(retval, (const char *) content);
1861 		xmlFree(content);
1862 	}
1863 
1864 	return SUCCESS;
1865 }
1866 
dom_modern_element_substituted_node_value_write(dom_object * obj,zval * newval)1867 zend_result dom_modern_element_substituted_node_value_write(dom_object *obj, zval *newval)
1868 {
1869 	DOM_PROP_NODE(xmlNodePtr, nodep, obj);
1870 
1871 	php_libxml_invalidate_node_list_cache(obj->document);
1872 	dom_remove_all_children(nodep);
1873 	xmlNodeSetContentLen(nodep, (xmlChar *) Z_STRVAL_P(newval), Z_STRLEN_P(newval));
1874 
1875 	return SUCCESS;
1876 }
1877 
dom_element_get_in_scope_namespace_info(php_dom_libxml_ns_mapper * ns_mapper,HashTable * result,xmlNodePtr nodep,dom_object * intern)1878 static void dom_element_get_in_scope_namespace_info(php_dom_libxml_ns_mapper *ns_mapper, HashTable *result, xmlNodePtr nodep, dom_object *intern)
1879 {
1880 	HashTable prefix_to_ns_table;
1881 	zend_hash_init(&prefix_to_ns_table, 0, NULL, NULL, false);
1882 	zend_hash_real_init_mixed(&prefix_to_ns_table);
1883 
1884 	/* https://www.w3.org/TR/1999/REC-xpath-19991116/#namespace-nodes */
1885 	for (const xmlNode *cur = nodep; cur != NULL; cur = cur->parent) {
1886 		if (cur->type == XML_ELEMENT_NODE) {
1887 			/* Find the last attribute */
1888 			const xmlAttr *last = NULL;
1889 			for (const xmlAttr *attr = cur->properties; attr != NULL; attr = attr->next) {
1890 				last = attr;
1891 			}
1892 
1893 			/* Reversed loop because the parent traversal is reversed as well,
1894 			 * this will keep the ordering consistent. */
1895 			for (const xmlAttr *attr = last; attr != NULL; attr = attr->prev) {
1896 				if (attr->ns != NULL && php_dom_ns_is_fast_ex(attr->ns, php_dom_ns_is_xmlns_magic_token)
1897 					&& attr->children != NULL && attr->children->content != NULL) {
1898 					const char *prefix = attr->ns->prefix == NULL ? NULL : (const char *) attr->name;
1899 					const char *key = prefix == NULL ? "" : prefix;
1900 					xmlNsPtr ns = php_dom_libxml_ns_mapper_get_ns_raw_strings_nullsafe(ns_mapper, prefix, (const char *) attr->children->content);
1901 					/* NULL is a valid value for the sentinel */
1902 					zval zv;
1903 					ZVAL_PTR(&zv, ns);
1904 					zend_hash_str_add(&prefix_to_ns_table, key, strlen(key), &zv);
1905 				}
1906 			}
1907 		}
1908 	}
1909 
1910 	xmlNsPtr ns;
1911 	zend_string *prefix;
1912 	ZEND_HASH_MAP_REVERSE_FOREACH_STR_KEY_PTR(&prefix_to_ns_table, prefix, ns) {
1913 		if (ZSTR_LEN(prefix) == 0 && (ns == NULL || ns->href == NULL || *ns->href == '\0')) {
1914 			/* Exception: "the value of the xmlns attribute for the nearest such element is non-empty" */
1915 			continue;
1916 		}
1917 
1918 		zval zv;
1919 		object_init_ex(&zv, dom_namespace_info_class_entry);
1920 		zend_object *obj = Z_OBJ(zv);
1921 
1922 		if (ZSTR_LEN(prefix) != 0) {
1923 			ZVAL_STR_COPY(OBJ_PROP_NUM(obj, 0), prefix);
1924 		} else {
1925 			ZVAL_NULL(OBJ_PROP_NUM(obj, 0));
1926 		}
1927 
1928 		if (ns != NULL && ns->href != NULL && *ns->href != '\0') {
1929 			ZVAL_STRING(OBJ_PROP_NUM(obj, 1), (const char *) ns->href);
1930 		} else {
1931 			ZVAL_NULL(OBJ_PROP_NUM(obj, 1));
1932 		}
1933 
1934 		php_dom_create_object(nodep, OBJ_PROP_NUM(obj, 2), intern);
1935 
1936 		zend_hash_next_index_insert_new(result, &zv);
1937 	} ZEND_HASH_FOREACH_END();
1938 
1939 	zend_hash_destroy(&prefix_to_ns_table);
1940 }
1941 
PHP_METHOD(Dom_Element,getInScopeNamespaces)1942 PHP_METHOD(Dom_Element, getInScopeNamespaces)
1943 {
1944 	zval *id;
1945 	xmlNode *nodep;
1946 	dom_object *intern;
1947 
1948 	ZEND_PARSE_PARAMETERS_NONE();
1949 
1950 	DOM_GET_THIS_OBJ(nodep, id, xmlNodePtr, intern);
1951 
1952 	php_dom_libxml_ns_mapper *ns_mapper = php_dom_get_ns_mapper(intern);
1953 
1954 	array_init(return_value);
1955 	HashTable *result = Z_ARRVAL_P(return_value);
1956 
1957 	dom_element_get_in_scope_namespace_info(ns_mapper, result, nodep, intern);
1958 }
1959 
PHP_METHOD(Dom_Element,getDescendantNamespaces)1960 PHP_METHOD(Dom_Element, getDescendantNamespaces)
1961 {
1962 	zval *id;
1963 	xmlNode *nodep;
1964 	dom_object *intern;
1965 
1966 	ZEND_PARSE_PARAMETERS_NONE();
1967 
1968 	DOM_GET_THIS_OBJ(nodep, id, xmlNodePtr, intern);
1969 
1970 	php_dom_libxml_ns_mapper *ns_mapper = php_dom_get_ns_mapper(intern);
1971 
1972 	array_init(return_value);
1973 	HashTable *result = Z_ARRVAL_P(return_value);
1974 
1975 	dom_element_get_in_scope_namespace_info(ns_mapper, result, nodep, intern);
1976 
1977 	xmlNodePtr cur = nodep->children;
1978 	while (cur != NULL) {
1979 		if (cur->type == XML_ELEMENT_NODE) {
1980 			/* TODO: this could be more optimized by updating the same HashTable repeatedly
1981 			 * instead of recreating it on every node. */
1982 			dom_element_get_in_scope_namespace_info(ns_mapper, result, cur, intern);
1983 		}
1984 
1985 		cur = php_dom_next_in_tree_order(cur, nodep);
1986 	}
1987 }
1988 
PHP_METHOD(Dom_Element,rename)1989 PHP_METHOD(Dom_Element, rename)
1990 {
1991 	zend_string *namespace_uri, *qualified_name;
1992 	ZEND_PARSE_PARAMETERS_START(2, 2)
1993 		Z_PARAM_STR_OR_NULL(namespace_uri)
1994 		Z_PARAM_STR(qualified_name)
1995 	ZEND_PARSE_PARAMETERS_END();
1996 
1997 	zval *id;
1998 	dom_object *intern;
1999 	xmlNodePtr nodep;
2000 	DOM_GET_THIS_OBJ(nodep, id, xmlNodePtr, intern);
2001 
2002 	xmlChar *localname = NULL, *prefix = NULL;
2003 	int errorcode = dom_validate_and_extract(namespace_uri, qualified_name, &localname, &prefix);
2004 	if (UNEXPECTED(errorcode != 0)) {
2005 		php_dom_throw_error(errorcode, /* strict */ true);
2006 		goto cleanup;
2007 	}
2008 
2009 	if (nodep->type == XML_ATTRIBUTE_NODE) {
2010 		/* Check for duplicate attributes. */
2011 		xmlAttrPtr existing = xmlHasNsProp(nodep->parent, localname, namespace_uri && ZSTR_VAL(namespace_uri)[0] != '\0' ? BAD_CAST ZSTR_VAL(namespace_uri) : NULL);
2012 		if (existing != NULL && existing != (xmlAttrPtr) nodep) {
2013 			php_dom_throw_error_with_message(INVALID_MODIFICATION_ERR, "An attribute with the given name in the given namespace already exists", /* strict */ true);
2014 			goto cleanup;
2015 		}
2016 	} else {
2017 		ZEND_ASSERT(nodep->type == XML_ELEMENT_NODE);
2018 
2019 		/* Check for moving to or away from the HTML namespace. */
2020 		bool is_currently_html_ns = php_dom_ns_is_fast(nodep, php_dom_ns_is_html_magic_token);
2021 		bool will_be_html_ns = namespace_uri != NULL && zend_string_equals_literal(namespace_uri, DOM_XHTML_NS_URI);
2022 		if (is_currently_html_ns != will_be_html_ns) {
2023 			if (is_currently_html_ns) {
2024 				php_dom_throw_error_with_message(
2025 					INVALID_MODIFICATION_ERR,
2026 					"It is not possible to move an element out of the HTML namespace because the HTML namespace is tied to the HTMLElement class",
2027 					/* strict */ true
2028 				);
2029 			} else {
2030 				php_dom_throw_error_with_message(
2031 					INVALID_MODIFICATION_ERR,
2032 					"It is not possible to move an element into the HTML namespace because the HTML namespace is tied to the HTMLElement class",
2033 					/* strict */ true
2034 				);
2035 			}
2036 			goto cleanup;
2037 		}
2038 
2039 		/* If we currently have a template but the new element type won't be a template, then throw away the templated content. */
2040 		if (is_currently_html_ns && xmlStrEqual(nodep->name, BAD_CAST "template") && !xmlStrEqual(localname, BAD_CAST "template")) {
2041 			php_dom_throw_error_with_message(
2042 				INVALID_MODIFICATION_ERR,
2043 				"It is not possible to rename the template element because it hosts a document fragment",
2044 				/* strict */ true
2045 			);
2046 			goto cleanup;
2047 		}
2048 	}
2049 
2050 	php_libxml_invalidate_node_list_cache(intern->document);
2051 
2052 	php_dom_libxml_ns_mapper *ns_mapper = php_dom_get_ns_mapper(intern);
2053 
2054 	/* Update namespace uri + prefix by querying the namespace mapper */
2055 	/* prefix can be NULL here, but that is taken care of by the called APIs. */
2056 	nodep->ns = php_dom_libxml_ns_mapper_get_ns_raw_prefix_string(ns_mapper, prefix, xmlStrlen(prefix), namespace_uri);
2057 
2058 	/* Change the local name */
2059 	if (xmlDictOwns(nodep->doc->dict, nodep->name) != 1) {
2060 		xmlFree((xmlChar *) nodep->name);
2061 	}
2062 	const xmlChar *copy = xmlDictLookup(nodep->doc->dict, localname, -1);
2063 	if (copy != NULL) {
2064 		nodep->name = copy;
2065 	} else {
2066 		nodep->name = localname;
2067 		localname = NULL;
2068 	}
2069 
2070 cleanup:
2071 	xmlFree(localname);
2072 	xmlFree(prefix);
2073 }
2074 
2075 #endif
2076