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 #include "namespace_compat.h"
26 #include "private_data.h"
27 #include "internal_helpers.h"
28 #include "dom_properties.h"
29
30 /*
31 * class DOMNode
32 *
33 * URL: https://www.w3.org/TR/2003/WD-DOM-Level-3-Core-20030226/DOM3-Core.html#core-ID-1950641247
34 * Since:
35 */
36
dom_node_concatenated_name_helper(size_t name_len,const char * name,size_t prefix_len,const char * prefix)37 zend_string *dom_node_concatenated_name_helper(size_t name_len, const char *name, size_t prefix_len, const char *prefix)
38 {
39 /* prefix_len can't overflow because it would need to occupy the entire address space */
40 zend_string *str = zend_string_safe_alloc(1, name_len, prefix_len + 1, false);
41 memcpy(ZSTR_VAL(str), prefix, prefix_len);
42 ZSTR_VAL(str)[prefix_len] = ':';
43 memcpy(ZSTR_VAL(str) + prefix_len + 1, name, name_len + 1 /* include \0 */);
44 return str;
45 }
46
dom_node_get_node_name_attribute_or_element(const xmlNode * nodep,bool uppercase)47 zend_string *dom_node_get_node_name_attribute_or_element(const xmlNode *nodep, bool uppercase)
48 {
49 zend_string *ret;
50 size_t name_len = strlen((const char *) nodep->name);
51 if (nodep->ns != NULL && nodep->ns->prefix != NULL) {
52 ret = dom_node_concatenated_name_helper(name_len, (const char *) nodep->name, strlen((const char *) nodep->ns->prefix), (const char *) nodep->ns->prefix);
53 } else {
54 ret = zend_string_init((const char *) nodep->name, name_len, false);
55 }
56 if (uppercase) {
57 zend_str_toupper(ZSTR_VAL(ret), ZSTR_LEN(ret));
58 }
59 return ret;
60 }
61
php_dom_is_node_connected(const xmlNode * node)62 bool php_dom_is_node_connected(const xmlNode *node)
63 {
64 ZEND_ASSERT(node != NULL);
65 do {
66 if (node->type == XML_DOCUMENT_NODE || node->type == XML_HTML_DOCUMENT_NODE) {
67 return true;
68 }
69 node = node->parent;
70 } while (node != NULL);
71 return false;
72 }
73
74 /* {{{ nodeName string
75 readonly=yes
76 URL: http://www.w3.org/TR/2003/WD-DOM-Level-3-Core-20030226/DOM3-Core.html#core-ID-F68D095
77 Modern spec URL: https://dom.spec.whatwg.org/#dom-node-nodename
78 Since:
79 */
dom_node_node_name_read(dom_object * obj,zval * retval)80 zend_result dom_node_node_name_read(dom_object *obj, zval *retval)
81 {
82 DOM_PROP_NODE(xmlNodePtr, nodep, obj);
83
84 bool uppercase = false;
85
86 switch (nodep->type) {
87 case XML_ELEMENT_NODE:
88 uppercase = php_dom_follow_spec_intern(obj) && php_dom_ns_is_html_and_document_is_html(nodep);
89 ZEND_FALLTHROUGH;
90 case XML_ATTRIBUTE_NODE:
91 ZVAL_NEW_STR(retval, dom_node_get_node_name_attribute_or_element(nodep, uppercase));
92 break;
93 case XML_NAMESPACE_DECL: {
94 xmlNsPtr ns = nodep->ns;
95 if (ns != NULL && ns->prefix) {
96 zend_string *str = dom_node_concatenated_name_helper(strlen((const char *) ns->prefix), (const char *) ns->prefix, strlen("xmlns"), "xmlns");
97 ZVAL_NEW_STR(retval, str);
98 } else {
99 ZVAL_STRING(retval, (const char *) nodep->name);
100 }
101 break;
102 }
103 case XML_DOCUMENT_TYPE_NODE:
104 case XML_DTD_NODE:
105 case XML_PI_NODE:
106 case XML_ENTITY_DECL:
107 case XML_ENTITY_REF_NODE:
108 case XML_NOTATION_NODE:
109 ZVAL_STRING(retval, (char *) nodep->name);
110 break;
111 case XML_CDATA_SECTION_NODE:
112 ZVAL_STRING(retval, "#cdata-section");
113 break;
114 case XML_COMMENT_NODE:
115 ZVAL_STRING(retval, "#comment");
116 break;
117 case XML_HTML_DOCUMENT_NODE:
118 case XML_DOCUMENT_NODE:
119 ZVAL_STRING(retval, "#document");
120 break;
121 case XML_DOCUMENT_FRAG_NODE:
122 ZVAL_STRING(retval, "#document-fragment");
123 break;
124 case XML_TEXT_NODE:
125 ZVAL_STRING(retval, "#text");
126 break;
127 EMPTY_SWITCH_DEFAULT_CASE();
128 }
129
130 return SUCCESS;
131 }
132
133 /* }}} */
134
135 /* {{{ nodeValue string
136 URL: http://www.w3.org/TR/2003/WD-DOM-Level-3-Core-20030226/DOM3-Core.html#core-ID-F68D080
137 Modern spec URL: https://dom.spec.whatwg.org/#dom-node-nodevalue
138 Since:
139 */
dom_node_node_value_read(dom_object * obj,zval * retval)140 zend_result dom_node_node_value_read(dom_object *obj, zval *retval)
141 {
142 DOM_PROP_NODE(xmlNodePtr, nodep, obj);
143
144 /* Access to Element node is implemented as a convenience method */
145 switch (nodep->type) {
146 case XML_ELEMENT_NODE: {
147 if (php_dom_follow_spec_intern(obj)) {
148 ZVAL_NULL(retval);
149 break;
150 }
151 ZEND_FALLTHROUGH;
152 }
153 case XML_ATTRIBUTE_NODE:
154 case XML_TEXT_NODE:
155 case XML_COMMENT_NODE:
156 case XML_CDATA_SECTION_NODE:
157 case XML_PI_NODE:
158 php_dom_get_content_into_zval(nodep, retval, true);
159 break;
160 case XML_NAMESPACE_DECL: {
161 char *str = (char *) xmlNodeGetContent(nodep->children);
162 if (str != NULL) {
163 ZVAL_STRING(retval, str);
164 xmlFree(str);
165 } else {
166 ZVAL_NULL(retval);
167 }
168 break;
169 }
170 default:
171 ZVAL_NULL(retval);
172 break;
173 }
174
175 return SUCCESS;
176 }
177
dom_node_node_value_write(dom_object * obj,zval * newval)178 zend_result dom_node_node_value_write(dom_object *obj, zval *newval)
179 {
180 DOM_PROP_NODE(xmlNodePtr, nodep, obj);
181
182 /* Cannot fail because the type is either null or a string. */
183 zend_string *str = zval_get_string(newval);
184
185 /* Access to Element node is implemented as a convenience method */
186 switch (nodep->type) {
187 case XML_ATTRIBUTE_NODE:
188 dom_attr_value_will_change(obj, (xmlAttrPtr) nodep);
189 if (php_dom_follow_spec_intern(obj)) {
190 dom_remove_all_children(nodep);
191 xmlAddChild(nodep, xmlNewTextLen(BAD_CAST ZSTR_VAL(str), ZSTR_LEN(str)));
192 break;
193 }
194 ZEND_FALLTHROUGH;
195 case XML_ELEMENT_NODE:
196 dom_remove_all_children(nodep);
197 ZEND_FALLTHROUGH;
198 case XML_TEXT_NODE:
199 case XML_COMMENT_NODE:
200 case XML_CDATA_SECTION_NODE:
201 case XML_PI_NODE:
202 xmlNodeSetContentLen(nodep, BAD_CAST ZSTR_VAL(str), ZSTR_LEN(str));
203 break;
204 default:
205 break;
206 }
207
208 php_libxml_invalidate_node_list_cache(obj->document);
209
210 zend_string_release_ex(str, 0);
211 return SUCCESS;
212 }
213
214 /* }}} */
215
216 /* {{{ nodeType int
217 readonly=yes
218 URL: http://www.w3.org/TR/2003/WD-DOM-Level-3-Core-20030226/DOM3-Core.html#core-ID-111237558
219 Since:
220 */
dom_node_node_type_read(dom_object * obj,zval * retval)221 zend_result dom_node_node_type_read(dom_object *obj, zval *retval)
222 {
223 DOM_PROP_NODE(xmlNodePtr, nodep, obj);
224
225 /* Specs dictate that they are both type XML_DOCUMENT_TYPE_NODE */
226 if (nodep->type == XML_DTD_NODE) {
227 ZVAL_LONG(retval, XML_DOCUMENT_TYPE_NODE);
228 } else {
229 ZVAL_LONG(retval, nodep->type);
230 }
231
232 return SUCCESS;
233 }
234
235 /* }}} */
236
dom_node_parent_get(dom_object * obj,zval * retval,bool only_element)237 static zend_result dom_node_parent_get(dom_object *obj, zval *retval, bool only_element)
238 {
239 DOM_PROP_NODE(xmlNodePtr, nodep, obj);
240
241 xmlNodePtr nodeparent = nodep->parent;
242 if (!nodeparent || (only_element && nodeparent->type != XML_ELEMENT_NODE)) {
243 ZVAL_NULL(retval);
244 return SUCCESS;
245 }
246
247 php_dom_create_object(nodeparent, retval, obj);
248 return SUCCESS;
249 }
250
251 /* {{{ parentNode ?DomNode
252 readonly=yes
253 URL: http://www.w3.org/TR/2003/WD-DOM-Level-3-Core-20030226/DOM3-Core.html#core-ID-1060184317
254 Since:
255 */
dom_node_parent_node_read(dom_object * obj,zval * retval)256 zend_result dom_node_parent_node_read(dom_object *obj, zval *retval)
257 {
258 return dom_node_parent_get(obj, retval, false);
259 }
260
261 /* }}} */
262
263 /* {{{ parentElement ?DomElement
264 readonly=yes
265 URL: https://dom.spec.whatwg.org/#parent-element
266 Since:
267 */
dom_node_parent_element_read(dom_object * obj,zval * retval)268 zend_result dom_node_parent_element_read(dom_object *obj, zval *retval)
269 {
270 return dom_node_parent_get(obj, retval, true);
271 }
272
273 /* }}} */
274
275 /* {{{ childNodes DomNodeList
276 readonly=yes
277 URL: http://www.w3.org/TR/2003/WD-DOM-Level-3-Core-20030226/DOM3-Core.html#core-ID-1451460987
278 Since:
279 */
dom_node_child_nodes_read(dom_object * obj,zval * retval)280 zend_result dom_node_child_nodes_read(dom_object *obj, zval *retval)
281 {
282 DOM_PROP_NODE(xmlNodePtr, nodep, obj);
283
284 php_dom_create_iterator(retval, DOM_NODELIST, php_dom_follow_spec_intern(obj));
285 dom_object *intern = Z_DOMOBJ_P(retval);
286 dom_namednode_iter(obj, XML_ELEMENT_NODE, intern, NULL, NULL, 0, NULL, 0);
287
288 return SUCCESS;
289 }
290 /* }}} */
291
292 /* {{{ firstChild DomNode
293 readonly=yes
294 URL: http://www.w3.org/TR/2003/WD-DOM-Level-3-Core-20030226/DOM3-Core.html#core-ID-169727388
295 Since:
296 */
dom_node_first_child_read(dom_object * obj,zval * retval)297 zend_result dom_node_first_child_read(dom_object *obj, zval *retval)
298 {
299 DOM_PROP_NODE(xmlNodePtr, nodep, obj);
300
301 xmlNodePtr first = NULL;
302 if (dom_node_children_valid(nodep)) {
303 first = nodep->children;
304 }
305
306 php_dom_create_nullable_object(first, retval, obj);
307 return SUCCESS;
308 }
309
310 /* }}} */
311
312 /* {{{ lastChild DomNode
313 readonly=yes
314 URL: http://www.w3.org/TR/2003/WD-DOM-Level-3-Core-20030226/DOM3-Core.html#core-ID-61AD09FB
315 Since:
316 */
dom_node_last_child_read(dom_object * obj,zval * retval)317 zend_result dom_node_last_child_read(dom_object *obj, zval *retval)
318 {
319 DOM_PROP_NODE(xmlNodePtr, nodep, obj);
320
321 xmlNodePtr last = NULL;
322 if (dom_node_children_valid(nodep)) {
323 last = nodep->last;
324 }
325
326 php_dom_create_nullable_object(last, retval, obj);
327 return SUCCESS;
328 }
329
330 /* }}} */
331
332 /* {{{ previousSibling DomNode
333 readonly=yes
334 URL: http://www.w3.org/TR/2003/WD-DOM-Level-3-Core-20030226/DOM3-Core.html#core-ID-640FB3C8
335 Since:
336 */
dom_node_previous_sibling_read(dom_object * obj,zval * retval)337 zend_result dom_node_previous_sibling_read(dom_object *obj, zval *retval)
338 {
339 DOM_PROP_NODE(xmlNodePtr, nodep, obj);
340
341 xmlNodePtr prevsib = nodep->prev;
342
343 php_dom_create_nullable_object(prevsib, retval, obj);
344 return SUCCESS;
345 }
346
347 /* }}} */
348
349 /* {{{ nextSibling DomNode
350 readonly=yes
351 URL: http://www.w3.org/TR/2003/WD-DOM-Level-3-Core-20030226/DOM3-Core.html#core-ID-6AC54C2F
352 Since:
353 */
dom_node_next_sibling_read(dom_object * obj,zval * retval)354 zend_result dom_node_next_sibling_read(dom_object *obj, zval *retval)
355 {
356 DOM_PROP_NODE(xmlNodePtr, nodep, obj);
357
358 xmlNodePtr nextsib = nodep->next;
359
360 php_dom_create_nullable_object(nextsib, retval, obj);
361 return SUCCESS;
362 }
363
364 /* }}} */
365
366 /* {{{ previousElementSibling DomNode
367 readonly=yes
368 URL: http://www.w3.org/TR/2003/WD-DOM-Level-3-Core-20030226/DOM3-Core.html#core-ID-640FB3C8
369 Since:
370 */
dom_node_previous_element_sibling_read(dom_object * obj,zval * retval)371 zend_result dom_node_previous_element_sibling_read(dom_object *obj, zval *retval)
372 {
373 DOM_PROP_NODE(xmlNodePtr, nodep, obj);
374
375 xmlNodePtr prevsib = nodep->prev;
376
377 while (prevsib && prevsib->type != XML_ELEMENT_NODE) {
378 prevsib = prevsib->prev;
379 }
380
381 php_dom_create_nullable_object(prevsib, retval, obj);
382 return SUCCESS;
383 }
384
385 /* }}} */
386
387 /* {{{ nextElementSibling DomNode
388 readonly=yes
389 URL: http://www.w3.org/TR/2003/WD-DOM-Level-3-Core-20030226/DOM3-Core.html#core-ID-6AC54C2F
390 Since:
391 */
dom_node_next_element_sibling_read(dom_object * obj,zval * retval)392 zend_result dom_node_next_element_sibling_read(dom_object *obj, zval *retval)
393 {
394 DOM_PROP_NODE(xmlNodePtr, nodep, obj);
395
396 xmlNodePtr nextsib = nodep->next;
397
398 while (nextsib != NULL && nextsib->type != XML_ELEMENT_NODE) {
399 nextsib = nextsib->next;
400 }
401
402 php_dom_create_nullable_object(nextsib, retval, obj);
403 return SUCCESS;
404 }
405
406 /* }}} */
407
408 /* {{{ attributes DomNamedNodeMap
409 readonly=yes
410 URL: http://www.w3.org/TR/2003/WD-DOM-Level-3-Core-20030226/DOM3-Core.html#core-ID-84CF096
411 Since:
412 */
dom_node_attributes_read(dom_object * obj,zval * retval)413 zend_result dom_node_attributes_read(dom_object *obj, zval *retval)
414 {
415 DOM_PROP_NODE(xmlNodePtr, nodep, obj);
416
417 if (nodep->type == XML_ELEMENT_NODE) {
418 php_dom_create_iterator(retval, DOM_NAMEDNODEMAP, php_dom_follow_spec_intern(obj));
419 dom_object *intern = Z_DOMOBJ_P(retval);
420 dom_namednode_iter(obj, XML_ATTRIBUTE_NODE, intern, NULL, NULL, 0, NULL, 0);
421 } else {
422 ZVAL_NULL(retval);
423 }
424
425 return SUCCESS;
426 }
427
428 /* }}} */
429
430 /* {{{ isConnected boolean
431 readonly=yes
432 URL: https://dom.spec.whatwg.org/#dom-node-isconnected
433 Since:
434 */
dom_node_is_connected_read(dom_object * obj,zval * retval)435 zend_result dom_node_is_connected_read(dom_object *obj, zval *retval)
436 {
437 DOM_PROP_NODE(xmlNodePtr, nodep, obj);
438 ZVAL_BOOL(retval, php_dom_is_node_connected(nodep));
439 return SUCCESS;
440 }
441 /* }}} */
442
443 /* {{{ ownerDocument DomDocument
444 readonly=yes
445 URL: http://www.w3.org/TR/2003/WD-DOM-Level-3-Core-20030226/DOM3-Core.html#core-node-ownerDoc
446 Since:
447 */
dom_node_owner_document_read(dom_object * obj,zval * retval)448 zend_result dom_node_owner_document_read(dom_object *obj, zval *retval)
449 {
450 DOM_PROP_NODE(xmlNodePtr, nodep, obj);
451
452 if (nodep->type == XML_DOCUMENT_NODE || nodep->type == XML_HTML_DOCUMENT_NODE) {
453 ZVAL_NULL(retval);
454 return SUCCESS;
455 }
456
457 xmlDocPtr docp = nodep->doc;
458 if (!docp) {
459 return FAILURE;
460 }
461
462 php_dom_create_object((xmlNodePtr) docp, retval, obj);
463 return SUCCESS;
464 }
465
466 /* }}} */
467
468 /* {{{ namespaceUri string
469 readonly=yes
470 URL: http://www.w3.org/TR/2003/WD-DOM-Level-3-Core-20030226/DOM3-Core.html#core-ID-NodeNSname
471 Since: DOM Level 2
472 */
dom_node_namespace_uri_read(dom_object * obj,zval * retval)473 zend_result dom_node_namespace_uri_read(dom_object *obj, zval *retval)
474 {
475 DOM_PROP_NODE(xmlNodePtr, nodep, obj);
476
477 const char *str = NULL;
478 switch (nodep->type) {
479 case XML_ELEMENT_NODE:
480 case XML_ATTRIBUTE_NODE:
481 case XML_NAMESPACE_DECL:
482 if (nodep->ns != NULL) {
483 str = (const char *) nodep->ns->href;
484 }
485 break;
486 default:
487 str = NULL;
488 break;
489 }
490
491 if (str != NULL) {
492 ZVAL_STRING(retval, str);
493 } else {
494 ZVAL_NULL(retval);
495 }
496
497 return SUCCESS;
498 }
499
500 /* }}} */
501
502 /* {{{ prefix string
503 readonly=no
504 URL: http://www.w3.org/TR/2003/WD-DOM-Level-3-Core-20030226/DOM3-Core.html#core-ID-NodeNSPrefix
505 Modern spec URL: https://dom.spec.whatwg.org/#concept-element-namespace-prefix
506 Since: DOM Level 2
507 */
dom_node_prefix_read(dom_object * obj,zval * retval)508 zend_result dom_node_prefix_read(dom_object *obj, zval *retval)
509 {
510 DOM_PROP_NODE(xmlNodePtr, nodep, obj);
511
512 const char *str = NULL;
513 switch (nodep->type) {
514 case XML_ELEMENT_NODE:
515 case XML_ATTRIBUTE_NODE:
516 case XML_NAMESPACE_DECL: {
517 xmlNsPtr ns = nodep->ns;
518 if (ns != NULL && ns->prefix) {
519 str = (char *) ns->prefix;
520 }
521 break;
522 }
523 default:
524 str = NULL;
525 break;
526 }
527
528 if (str == NULL) {
529 ZVAL_EMPTY_STRING(retval);
530 } else {
531 ZVAL_STRING(retval, str);
532 }
533 return SUCCESS;
534 }
535
dom_modern_node_prefix_read(dom_object * obj,zval * retval)536 zend_result dom_modern_node_prefix_read(dom_object *obj, zval *retval)
537 {
538 DOM_PROP_NODE(xmlNodePtr, nodep, obj);
539
540 xmlNsPtr ns = nodep->ns;
541 if (ns != NULL && ns->prefix != NULL) {
542 ZVAL_STRING(retval, (const char *) ns->prefix);
543 } else {
544 ZVAL_NULL(retval);
545 }
546 return SUCCESS;
547 }
548
dom_node_prefix_write(dom_object * obj,zval * newval)549 zend_result dom_node_prefix_write(dom_object *obj, zval *newval)
550 {
551 zend_string *prefix_str;
552 xmlNode *nsnode = NULL;
553 xmlNsPtr ns = NULL, curns;
554 char *strURI;
555 char *prefix;
556
557 DOM_PROP_NODE(xmlNodePtr, nodep, obj);
558
559 switch (nodep->type) {
560 case XML_ELEMENT_NODE:
561 nsnode = nodep;
562 ZEND_FALLTHROUGH;
563 case XML_ATTRIBUTE_NODE:
564 if (nsnode == NULL) {
565 nsnode = nodep->parent;
566 if (nsnode == NULL) {
567 nsnode = xmlDocGetRootElement(nodep->doc);
568 }
569 }
570 /* Typed property, this is already a string */
571 ZEND_ASSERT(Z_TYPE_P(newval) == IS_STRING);
572 prefix_str = Z_STR_P(newval);
573
574 prefix = ZSTR_VAL(prefix_str);
575 if (*prefix == '\0') {
576 /* The empty string namespace prefix does not exist.
577 * We should fall back to the default namespace in this case. */
578 prefix = NULL;
579 }
580 if (nsnode && nodep->ns != NULL && !xmlStrEqual(nodep->ns->prefix, BAD_CAST prefix)) {
581 strURI = (char *) nodep->ns->href;
582 /* Validate namespace naming constraints */
583 if (strURI == NULL ||
584 (zend_string_equals_literal(prefix_str, "xml") && strcmp(strURI, (char *) XML_XML_NAMESPACE)) ||
585 (nodep->type == XML_ATTRIBUTE_NODE && zend_string_equals_literal(prefix_str, "xmlns") &&
586 strcmp(strURI, DOM_XMLNS_NS_URI)) ||
587 (nodep->type == XML_ATTRIBUTE_NODE && !strcmp((char *) nodep->name, "xmlns"))) {
588 php_dom_throw_error(NAMESPACE_ERR, dom_get_strict_error(obj->document));
589 return FAILURE;
590 } else {
591 curns = nsnode->nsDef;
592 while (curns != NULL) {
593 if (xmlStrEqual(BAD_CAST prefix, curns->prefix) && xmlStrEqual(nodep->ns->href, curns->href)) {
594 ns = curns;
595 break;
596 }
597 curns = curns->next;
598 }
599 if (ns == NULL) {
600 ns = xmlNewNs(nsnode, nodep->ns->href, BAD_CAST prefix);
601 /* Sadly, we cannot distinguish between OOM and namespace conflict.
602 * But OOM will almost never happen. */
603 if (UNEXPECTED(ns == NULL)) {
604 php_dom_throw_error(NAMESPACE_ERR, /* strict */ true);
605 return FAILURE;
606 }
607 }
608 }
609
610 xmlSetNs(nodep, ns);
611 }
612 break;
613 default:
614 break;
615 }
616
617 return SUCCESS;
618 }
619
620 /* }}} */
621
622 /* {{{ localName string
623 readonly=yes
624 URL: http://www.w3.org/TR/2003/WD-DOM-Level-3-Core-20030226/DOM3-Core.html#core-ID-NodeNSLocalN
625 Since: DOM Level 2
626 */
dom_node_local_name_read(dom_object * obj,zval * retval)627 zend_result dom_node_local_name_read(dom_object *obj, zval *retval)
628 {
629 DOM_PROP_NODE(xmlNodePtr, nodep, obj);
630
631 if (nodep->type == XML_ELEMENT_NODE || nodep->type == XML_ATTRIBUTE_NODE || nodep->type == XML_NAMESPACE_DECL) {
632 ZVAL_STRING(retval, (char *) (nodep->name));
633 } else {
634 ZVAL_NULL(retval);
635 }
636
637 return SUCCESS;
638 }
639
640 /* }}} */
641
642 /* {{{ baseURI string
643 readonly=yes
644 URL: http://www.w3.org/TR/2003/WD-DOM-Level-3-Core-20030226/DOM3-Core.html#Node3-baseURI
645 Since: DOM Level 3
646 */
dom_node_base_uri_read(dom_object * obj,zval * retval)647 zend_result dom_node_base_uri_read(dom_object *obj, zval *retval)
648 {
649 DOM_PROP_NODE(xmlNodePtr, nodep, obj);
650
651 xmlChar *baseuri = xmlNodeGetBase(nodep->doc, nodep);
652 if (baseuri) {
653 ZVAL_STRING(retval, (const char *) baseuri);
654 xmlFree(baseuri);
655 } else {
656 if (php_dom_follow_spec_intern(obj)) {
657 if (nodep->doc->URL) {
658 ZVAL_STRING(retval, (const char *) nodep->doc->URL);
659 } else {
660 ZVAL_STRING(retval, "about:blank");
661 }
662 } else {
663 ZVAL_NULL(retval);
664 }
665 }
666
667 return SUCCESS;
668 }
669
670 /* }}} */
671
672 /* {{{ textContent string
673 URL: http://www.w3.org/TR/2003/WD-DOM-Level-3-Core-20030226/DOM3-Core.html#Node3-textContent
674 Modern spec URL: https://dom.spec.whatwg.org/#dom-node-textcontent
675 Since: DOM Level 3
676 */
677 /* Determines when the operation is a no-op. */
dom_skip_text_content(dom_object * obj,xmlNodePtr nodep)678 static bool dom_skip_text_content(dom_object *obj, xmlNodePtr nodep)
679 {
680 if (php_dom_follow_spec_intern(obj)) {
681 int type = nodep->type;
682 if (type != XML_DOCUMENT_FRAG_NODE && type != XML_ELEMENT_NODE && type != XML_ATTRIBUTE_NODE
683 && type != XML_TEXT_NODE && type != XML_CDATA_SECTION_NODE && type != XML_COMMENT_NODE && type != XML_PI_NODE) {
684 /* Yes, success... It's a no-op for these cases. */
685 return true;
686 }
687 }
688 return false;
689 }
690
dom_node_text_content_read(dom_object * obj,zval * retval)691 zend_result dom_node_text_content_read(dom_object *obj, zval *retval)
692 {
693 DOM_PROP_NODE(xmlNodePtr, nodep, obj);
694
695 if (dom_skip_text_content(obj, nodep)) {
696 ZVAL_NULL(retval);
697 } else {
698 php_dom_get_content_into_zval(nodep, retval, false);
699 }
700
701 return SUCCESS;
702 }
703
dom_node_text_content_write(dom_object * obj,zval * newval)704 zend_result dom_node_text_content_write(dom_object *obj, zval *newval)
705 {
706 DOM_PROP_NODE(xmlNodePtr, nodep, obj);
707
708 php_libxml_invalidate_node_list_cache(obj->document);
709
710 /* Typed property, this is already a string */
711 ZEND_ASSERT(Z_TYPE_P(newval) == IS_STRING || Z_TYPE_P(newval) == IS_NULL);
712 const xmlChar *xmlChars;
713 size_t len;
714 if (Z_TYPE_P(newval) == IS_NULL) {
715 xmlChars = (const xmlChar *) "";
716 len = 0;
717 } else {
718 xmlChars = (const xmlChar *) Z_STRVAL_P(newval);
719 len = Z_STRLEN_P(newval);
720 }
721
722 int type = nodep->type;
723
724 /* We can't directly call xmlNodeSetContent, because it might encode the string through
725 * xmlStringLenGetNodeList for types XML_DOCUMENT_FRAG_NODE, XML_ELEMENT_NODE, XML_ATTRIBUTE_NODE.
726 * See tree.c:xmlNodeSetContent in libxml.
727 * In these cases we need to use a text node to avoid the encoding.
728 * For the other cases, we *can* rely on xmlNodeSetContent because it is either a no-op, or handles
729 * the content without encoding. */
730 if (type == XML_DOCUMENT_FRAG_NODE || type == XML_ELEMENT_NODE || type == XML_ATTRIBUTE_NODE) {
731 dom_remove_all_children(nodep);
732 xmlNode *textNode = xmlNewDocTextLen(nodep->doc, xmlChars, len);
733 xmlAddChild(nodep, textNode);
734 } else {
735 xmlNodeSetContent(nodep, xmlChars);
736 }
737
738 return SUCCESS;
739 }
740
741 /* }}} */
742
dom_insert_fragment(xmlNodePtr nodep,xmlNodePtr prevsib,xmlNodePtr nextsib,xmlNodePtr fragment,dom_object * intern)743 static xmlNodePtr dom_insert_fragment(xmlNodePtr nodep, xmlNodePtr prevsib, xmlNodePtr nextsib, xmlNodePtr fragment, dom_object *intern) /* {{{ */
744 {
745 xmlNodePtr newchild, node;
746
747 newchild = fragment->children;
748
749 if (newchild) {
750 if (prevsib == NULL) {
751 nodep->children = newchild;
752 } else {
753 prevsib->next = newchild;
754 }
755 newchild->prev = prevsib;
756 if (nextsib == NULL) {
757 nodep->last = fragment->last;
758 } else {
759 fragment->last->next = nextsib;
760 nextsib->prev = fragment->last;
761 }
762
763 node = newchild;
764 while (node != NULL) {
765 node->parent = nodep;
766 if (node->doc != nodep->doc) {
767 xmlSetTreeDoc(node, nodep->doc);
768 dom_object *childobj = node->_private;
769 if (childobj != NULL) {
770 childobj->document = intern->document;
771 php_libxml_increment_doc_ref((php_libxml_node_object *)childobj, NULL);
772 }
773 }
774 if (node == fragment->last) {
775 break;
776 }
777 node = node->next;
778 }
779
780 fragment->children = NULL;
781 fragment->last = NULL;
782 }
783
784 return newchild;
785 }
786 /* }}} */
787
788 /* {{{ URL: http://www.w3.org/TR/2003/WD-DOM-Level-3-Core-20030226/DOM3-Core.html#core-ID-952280727
789 Since:
790 */
dom_node_insert_before_legacy(zval * return_value,zval * ref,dom_object * intern,dom_object * childobj,xmlNodePtr parentp,xmlNodePtr child)791 static void dom_node_insert_before_legacy(zval *return_value, zval *ref, dom_object *intern, dom_object *childobj, xmlNodePtr parentp, xmlNodePtr child)
792 {
793 if (!dom_node_children_valid(parentp)) {
794 RETURN_FALSE;
795 }
796
797 xmlNodePtr new_child = NULL;
798 bool stricterror = dom_get_strict_error(intern->document);
799
800 if (dom_node_is_read_only(parentp) == SUCCESS ||
801 (child->parent != NULL && dom_node_is_read_only(child->parent) == SUCCESS)) {
802 php_dom_throw_error(NO_MODIFICATION_ALLOWED_ERR, stricterror);
803 RETURN_FALSE;
804 }
805
806 if (dom_hierarchy(parentp, child) == FAILURE) {
807 php_dom_throw_error(HIERARCHY_REQUEST_ERR, stricterror);
808 RETURN_FALSE;
809 }
810
811 if (child->doc != parentp->doc && child->doc != NULL) {
812 php_dom_throw_error(WRONG_DOCUMENT_ERR, stricterror);
813 RETURN_FALSE;
814 }
815
816 if (child->type == XML_DOCUMENT_FRAG_NODE && child->children == NULL) {
817 /* TODO Drop Warning? */
818 php_error_docref(NULL, E_WARNING, "Document Fragment is empty");
819 RETURN_FALSE;
820 }
821
822 if (child->doc == NULL && parentp->doc != NULL) {
823 childobj->document = intern->document;
824 php_libxml_increment_doc_ref((php_libxml_node_object *)childobj, NULL);
825 }
826
827 php_libxml_invalidate_node_list_cache(intern->document);
828
829 if (ref != NULL) {
830 xmlNodePtr refp;
831 dom_object *refpobj;
832 DOM_GET_OBJ(refp, ref, xmlNodePtr, refpobj);
833 if (refp->parent != parentp) {
834 php_dom_throw_error(NOT_FOUND_ERR, stricterror);
835 RETURN_FALSE;
836 }
837
838 if (child->parent != NULL) {
839 xmlUnlinkNode(child);
840 }
841
842 if (child->type == XML_TEXT_NODE && (refp->type == XML_TEXT_NODE ||
843 (refp->prev != NULL && refp->prev->type == XML_TEXT_NODE))) {
844 if (child->doc == NULL) {
845 xmlSetTreeDoc(child, parentp->doc);
846 }
847 new_child = child;
848 new_child->parent = refp->parent;
849 new_child->next = refp;
850 new_child->prev = refp->prev;
851 refp->prev = new_child;
852 if (new_child->prev != NULL) {
853 new_child->prev->next = new_child;
854 }
855 if (new_child->parent != NULL) {
856 if (new_child->parent->children == refp) {
857 new_child->parent->children = new_child;
858 }
859 }
860
861 } else if (child->type == XML_ATTRIBUTE_NODE) {
862 xmlAttrPtr lastattr;
863
864 if (child->ns == NULL)
865 lastattr = xmlHasProp(refp->parent, child->name);
866 else
867 lastattr = xmlHasNsProp(refp->parent, child->name, child->ns->href);
868 if (lastattr != NULL && lastattr->type != XML_ATTRIBUTE_DECL) {
869 if (lastattr != (xmlAttrPtr) child) {
870 xmlUnlinkNode((xmlNodePtr) lastattr);
871 php_libxml_node_free_resource((xmlNodePtr) lastattr);
872 } else {
873 DOM_RET_OBJ(child, intern);
874 return;
875 }
876 }
877 new_child = xmlAddPrevSibling(refp, child);
878 if (UNEXPECTED(NULL == new_child)) {
879 goto cannot_add;
880 }
881 } else if (child->type == XML_DOCUMENT_FRAG_NODE) {
882 xmlNodePtr last = child->last;
883 new_child = dom_insert_fragment(parentp, refp->prev, refp, child, intern);
884 dom_reconcile_ns_list(parentp->doc, new_child, last);
885 } else {
886 new_child = xmlAddPrevSibling(refp, child);
887 if (UNEXPECTED(NULL == new_child)) {
888 goto cannot_add;
889 }
890 dom_reconcile_ns(parentp->doc, new_child);
891 }
892 } else {
893 if (child->parent != NULL){
894 xmlUnlinkNode(child);
895 }
896 if (child->type == XML_TEXT_NODE && parentp->last != NULL && parentp->last->type == XML_TEXT_NODE) {
897 child->parent = parentp;
898 if (child->doc == NULL) {
899 xmlSetTreeDoc(child, parentp->doc);
900 }
901 new_child = child;
902 if (parentp->children == NULL) {
903 parentp->children = child;
904 parentp->last = child;
905 } else {
906 child = parentp->last;
907 child->next = new_child;
908 new_child->prev = child;
909 parentp->last = new_child;
910 }
911 } else if (child->type == XML_ATTRIBUTE_NODE) {
912 xmlAttrPtr lastattr;
913
914 if (child->ns == NULL)
915 lastattr = xmlHasProp(parentp, child->name);
916 else
917 lastattr = xmlHasNsProp(parentp, child->name, child->ns->href);
918 if (lastattr != NULL && lastattr->type != XML_ATTRIBUTE_DECL) {
919 if (lastattr != (xmlAttrPtr) child) {
920 xmlUnlinkNode((xmlNodePtr) lastattr);
921 php_libxml_node_free_resource((xmlNodePtr) lastattr);
922 } else {
923 DOM_RET_OBJ(child, intern);
924 return;
925 }
926 }
927 new_child = xmlAddChild(parentp, child);
928 if (UNEXPECTED(NULL == new_child)) {
929 goto cannot_add;
930 }
931 } else if (child->type == XML_DOCUMENT_FRAG_NODE) {
932 xmlNodePtr last = child->last;
933 new_child = dom_insert_fragment(parentp, parentp->last, NULL, child, intern);
934 dom_reconcile_ns_list(parentp->doc, new_child, last);
935 } else {
936 new_child = xmlAddChild(parentp, child);
937 if (UNEXPECTED(NULL == new_child)) {
938 goto cannot_add;
939 }
940 dom_reconcile_ns(parentp->doc, new_child);
941 }
942 }
943
944 DOM_RET_OBJ(new_child, intern);
945 return;
946 cannot_add:
947 zend_throw_error(NULL, "Cannot add newnode as the previous sibling of refnode");
948 RETURN_THROWS();
949 }
950 /* }}} end dom_node_insert_before */
951
952 /* https://dom.spec.whatwg.org/#dom-node-insertbefore */
dom_node_insert_before_modern(zval * return_value,zval * ref,dom_object * intern,xmlNodePtr parentp,xmlNodePtr child)953 static void dom_node_insert_before_modern(zval *return_value, zval *ref, dom_object *intern, xmlNodePtr parentp, xmlNodePtr child)
954 {
955 xmlNodePtr refp = NULL;
956 dom_object *refobjp;
957 if (php_dom_pre_insert_is_parent_invalid(parentp)) {
958 php_dom_throw_error(HIERARCHY_REQUEST_ERR, /* strict */ true);
959 RETURN_THROWS();
960 }
961 if (ref != NULL) {
962 DOM_GET_OBJ(refp, ref, xmlNodePtr, refobjp);
963 }
964 php_libxml_invalidate_node_list_cache(intern->document);
965 php_dom_pre_insert(intern->document, child, parentp, refp);
966 DOM_RET_OBJ(child, intern);
967 }
968
dom_node_insert_before(INTERNAL_FUNCTION_PARAMETERS,bool modern)969 static void dom_node_insert_before(INTERNAL_FUNCTION_PARAMETERS, bool modern)
970 {
971 zval *id, *node, *ref = NULL;
972 xmlNodePtr child, parentp;
973 dom_object *intern, *childobj;
974
975 id = ZEND_THIS;
976 if (zend_parse_parameters(ZEND_NUM_ARGS(), "O|O!", &node, dom_get_node_ce(modern), &ref, dom_get_node_ce(modern)) == FAILURE) {
977 RETURN_THROWS();
978 }
979
980 DOM_GET_OBJ(parentp, id, xmlNodePtr, intern);
981
982 DOM_GET_OBJ(child, node, xmlNodePtr, childobj);
983
984 if (modern) {
985 dom_node_insert_before_modern(return_value, ref, intern, parentp, child);
986 } else {
987 dom_node_insert_before_legacy(return_value, ref, intern, childobj, parentp, child);
988 }
989 }
990
PHP_METHOD(DOMNode,insertBefore)991 PHP_METHOD(DOMNode, insertBefore)
992 {
993 dom_node_insert_before(INTERNAL_FUNCTION_PARAM_PASSTHRU, false);
994 }
995
PHP_METHOD(Dom_Node,insertBefore)996 PHP_METHOD(Dom_Node, insertBefore)
997 {
998 dom_node_insert_before(INTERNAL_FUNCTION_PARAM_PASSTHRU, true);
999 }
1000
1001 /* https://dom.spec.whatwg.org/#concept-node-replace */
dom_replace_node_validity_checks(xmlNodePtr parent,xmlNodePtr node,xmlNodePtr child)1002 static zend_result dom_replace_node_validity_checks(xmlNodePtr parent, xmlNodePtr node, xmlNodePtr child)
1003 {
1004 /* 1. If parent is not a Document, DocumentFragment, or Element node, then throw a "HierarchyRequestError" DOMException. */
1005 if (php_dom_pre_insert_is_parent_invalid(parent)) {
1006 php_dom_throw_error(HIERARCHY_REQUEST_ERR, /* strict */ true);
1007 return FAILURE;
1008 }
1009
1010 /* 2. If node is a host-including inclusive ancestor of parent, then throw a "HierarchyRequestError" DOMException. */
1011 if (dom_hierarchy(parent, node) != SUCCESS) {
1012 php_dom_throw_error(HIERARCHY_REQUEST_ERR, /* strict */ true);
1013 return FAILURE;
1014 }
1015
1016 /* 3. If child’s parent is not parent, then throw a "NotFoundError" DOMException. */
1017 if (child->parent != parent) {
1018 php_dom_throw_error(NOT_FOUND_ERR, /* strict */ true);
1019 return FAILURE;
1020 }
1021
1022 /* 4. If node is not a DocumentFragment, DocumentType, Element, or CharacterData node, then throw a "HierarchyRequestError" DOMException. */
1023 if (node->type != XML_DOCUMENT_FRAG_NODE
1024 && node->type != XML_DTD_NODE
1025 && node->type != XML_ELEMENT_NODE
1026 && node->type != XML_TEXT_NODE
1027 && node->type != XML_CDATA_SECTION_NODE
1028 && node->type != XML_COMMENT_NODE
1029 && node->type != XML_PI_NODE) {
1030 php_dom_throw_error(HIERARCHY_REQUEST_ERR, /* strict */ true);
1031 return FAILURE;
1032 }
1033
1034 /* 5. If either node is a Text node and parent is a document, or node is a doctype and parent is not a document,
1035 * then throw a "HierarchyRequestError" DOMException. */
1036 bool parent_is_document = parent->type == XML_DOCUMENT_NODE || parent->type == XML_HTML_DOCUMENT_NODE;
1037 if (parent_is_document && (node->type == XML_TEXT_NODE || node->type == XML_CDATA_SECTION_NODE)) {
1038 php_dom_throw_error_with_message(HIERARCHY_REQUEST_ERR, "Cannot insert text as a child of a document", /* strict */ true);
1039 return FAILURE;
1040 }
1041 if (!parent_is_document && node->type == XML_DTD_NODE) {
1042 php_dom_throw_error_with_message(HIERARCHY_REQUEST_ERR, "Cannot insert a document type into anything other than a document", /* strict */ true);
1043 return FAILURE;
1044 }
1045
1046 /* 6. If parent is a document, and any of the statements below, switched on the interface node implements, are true,
1047 * then throw a "HierarchyRequestError" DOMException.
1048 * Spec note: These statements _slightly_ differ from the pre-insert algorithm. */
1049 if (parent_is_document) {
1050 /* DocumentFragment */
1051 if (node->type == XML_DOCUMENT_FRAG_NODE) {
1052 if (!php_dom_fragment_insertion_hierarchy_check_replace(parent, node, child)) {
1053 return FAILURE;
1054 }
1055 }
1056 /* Element */
1057 else if (node->type == XML_ELEMENT_NODE) {
1058 /* parent has an element child that is not child ... */
1059 if (xmlDocGetRootElement((xmlDocPtr) parent) != child) {
1060 php_dom_throw_error_with_message(HIERARCHY_REQUEST_ERR, "Cannot have more than one element child in a document", /* strict */ true);
1061 return FAILURE;
1062 }
1063 /* ... or a doctype is following child. */
1064 if (php_dom_has_sibling_following_node(child, XML_DTD_NODE)) {
1065 php_dom_throw_error_with_message(HIERARCHY_REQUEST_ERR, "Document types must be the first child in a document", /* strict */ true);
1066 return FAILURE;
1067 }
1068 }
1069 /* DocumentType */
1070 else if (node->type == XML_DTD_NODE) {
1071 /* parent has a doctype child that is not child, or an element is preceding child. */
1072 xmlDocPtr doc = (xmlDocPtr) parent;
1073 if (doc->intSubset != (xmlDtdPtr) child || php_dom_has_sibling_preceding_node(child, XML_ELEMENT_NODE)) {
1074 php_dom_throw_error_with_message(HIERARCHY_REQUEST_ERR, "Document types must be the first child in a document", /* strict */ true);
1075 return FAILURE;
1076 }
1077 }
1078 }
1079
1080 /* Steps 7 and onward perform the removal and insertion, and also track changes for mutation records.
1081 * We don't implement mutation records so we can just skip straight to the replace part. */
1082
1083 return SUCCESS;
1084 }
1085
1086 /* {{{ URL: http://www.w3.org/TR/2003/WD-DOM-Level-3-Core-20030226/DOM3-Core.html#core-ID-785887307
1087 Modern spec URL: https://dom.spec.whatwg.org/#dom-node-replacechild
1088 Since:
1089 */
dom_node_replace_child(INTERNAL_FUNCTION_PARAMETERS,bool modern)1090 static void dom_node_replace_child(INTERNAL_FUNCTION_PARAMETERS, bool modern)
1091 {
1092 zval *id, *newnode, *oldnode;
1093 xmlNodePtr newchild, oldchild, nodep;
1094 dom_object *intern, *newchildobj, *oldchildobj;
1095
1096 id = ZEND_THIS;
1097 if (zend_parse_parameters(ZEND_NUM_ARGS(), "OO", &newnode, dom_get_node_ce(modern), &oldnode, dom_get_node_ce(modern)) == FAILURE) {
1098 RETURN_THROWS();
1099 }
1100
1101 DOM_GET_OBJ(nodep, id, xmlNodePtr, intern);
1102
1103 DOM_GET_OBJ(newchild, newnode, xmlNodePtr, newchildobj);
1104 DOM_GET_OBJ(oldchild, oldnode, xmlNodePtr, oldchildobj);
1105
1106 bool stricterror = dom_get_strict_error(intern->document);
1107
1108 if (newchild->doc != nodep->doc && newchild->doc != NULL) {
1109 php_dom_throw_error(WRONG_DOCUMENT_ERR, stricterror);
1110 RETURN_FALSE;
1111 }
1112
1113 if (modern) {
1114 if (dom_replace_node_validity_checks(nodep, newchild, oldchild) != SUCCESS) {
1115 RETURN_THROWS();
1116 }
1117 } else {
1118 if (!dom_node_children_valid(nodep)) {
1119 RETURN_FALSE;
1120 }
1121
1122 if (!nodep->children) {
1123 RETURN_FALSE;
1124 }
1125
1126 if (dom_node_is_read_only(nodep) == SUCCESS ||
1127 (newchild->parent != NULL && dom_node_is_read_only(newchild->parent) == SUCCESS)) {
1128 php_dom_throw_error(NO_MODIFICATION_ALLOWED_ERR, stricterror);
1129 RETURN_FALSE;
1130 }
1131
1132 if (dom_hierarchy(nodep, newchild) == FAILURE) {
1133 php_dom_throw_error(HIERARCHY_REQUEST_ERR, stricterror);
1134 RETURN_FALSE;
1135 }
1136
1137 if (oldchild->parent != nodep) {
1138 php_dom_throw_error(NOT_FOUND_ERR, stricterror);
1139 RETURN_FALSE;
1140 }
1141 }
1142
1143 if (newchild->type == XML_DOCUMENT_FRAG_NODE) {
1144 xmlNodePtr prevsib, nextsib;
1145 prevsib = oldchild->prev;
1146 nextsib = oldchild->next;
1147
1148 xmlUnlinkNode(oldchild);
1149
1150 xmlNodePtr last = newchild->last;
1151 newchild = dom_insert_fragment(nodep, prevsib, nextsib, newchild, intern);
1152 if (newchild && !modern) {
1153 dom_reconcile_ns_list(nodep->doc, newchild, last);
1154 }
1155 } else if (oldchild != newchild) {
1156 xmlDtdPtr intSubset = xmlGetIntSubset(nodep->doc);
1157 bool replacedoctype = (intSubset == (xmlDtd *) oldchild);
1158
1159 if (newchild->doc == NULL && nodep->doc != NULL) {
1160 xmlSetTreeDoc(newchild, nodep->doc);
1161 newchildobj->document = intern->document;
1162 php_libxml_increment_doc_ref((php_libxml_node_object *)newchildobj, NULL);
1163 }
1164 xmlReplaceNode(oldchild, newchild);
1165 if (!modern) {
1166 dom_reconcile_ns(nodep->doc, newchild);
1167 }
1168
1169 if (replacedoctype) {
1170 nodep->doc->intSubset = (xmlDtd *) newchild;
1171 }
1172 }
1173 php_libxml_invalidate_node_list_cache(intern->document);
1174 DOM_RET_OBJ(oldchild, intern);
1175 }
1176
PHP_METHOD(DOMNode,replaceChild)1177 PHP_METHOD(DOMNode, replaceChild)
1178 {
1179 dom_node_replace_child(INTERNAL_FUNCTION_PARAM_PASSTHRU, false);
1180 }
1181
PHP_METHOD(Dom_Node,replaceChild)1182 PHP_METHOD(Dom_Node, replaceChild)
1183 {
1184 dom_node_replace_child(INTERNAL_FUNCTION_PARAM_PASSTHRU, true);
1185 }
1186 /* }}} end dom_node_replace_child */
1187
1188 /* {{{ URL: http://www.w3.org/TR/2003/WD-DOM-Level-3-Core-20030226/DOM3-Core.html#core-ID-1734834066
1189 Since:
1190 */
dom_node_remove_child(INTERNAL_FUNCTION_PARAMETERS,zend_class_entry * node_ce)1191 static void dom_node_remove_child(INTERNAL_FUNCTION_PARAMETERS, zend_class_entry *node_ce)
1192 {
1193 zval *node;
1194 xmlNodePtr child, nodep;
1195 dom_object *intern, *childobj;
1196
1197 ZEND_PARSE_PARAMETERS_START(1, 1)
1198 Z_PARAM_OBJECT_OF_CLASS(node, node_ce)
1199 ZEND_PARSE_PARAMETERS_END();
1200
1201 DOM_GET_OBJ(nodep, ZEND_THIS, xmlNodePtr, intern);
1202
1203 DOM_GET_OBJ(child, node, xmlNodePtr, childobj);
1204
1205 bool stricterror = dom_get_strict_error(intern->document);
1206
1207 if (!nodep->children || child->parent != nodep) {
1208 php_dom_throw_error(NOT_FOUND_ERR, stricterror);
1209 RETURN_FALSE;
1210 }
1211
1212 if (dom_node_is_read_only(nodep) == SUCCESS ||
1213 (child->parent != NULL && dom_node_is_read_only(child->parent) == SUCCESS)) {
1214 php_dom_throw_error(NO_MODIFICATION_ALLOWED_ERR, stricterror);
1215 RETURN_FALSE;
1216 }
1217
1218 xmlUnlinkNode(child);
1219 php_libxml_invalidate_node_list_cache(intern->document);
1220 DOM_RET_OBJ(child, intern);
1221 }
1222
PHP_METHOD(DOMNode,removeChild)1223 PHP_METHOD(DOMNode, removeChild)
1224 {
1225 dom_node_remove_child(INTERNAL_FUNCTION_PARAM_PASSTHRU, dom_node_class_entry);
1226 }
1227
PHP_METHOD(Dom_Node,removeChild)1228 PHP_METHOD(Dom_Node, removeChild)
1229 {
1230 dom_node_remove_child(INTERNAL_FUNCTION_PARAM_PASSTHRU, dom_modern_node_class_entry);
1231 }
1232 /* }}} end dom_node_remove_child */
1233
1234 /* {{{ URL: http://www.w3.org/TR/2003/WD-DOM-Level-3-Core-20030226/DOM3-Core.html#core-ID-184E7107
1235 Modern spec URL: https://dom.spec.whatwg.org/#dom-node-appendchild
1236 Since:
1237 */
dom_node_append_child_legacy(zval * return_value,dom_object * intern,dom_object * childobj,xmlNodePtr nodep,xmlNodePtr child)1238 static void dom_node_append_child_legacy(zval *return_value, dom_object *intern, dom_object *childobj, xmlNodePtr nodep, xmlNodePtr child)
1239 {
1240 xmlNodePtr new_child = NULL;
1241
1242 if (!dom_node_children_valid(nodep)) {
1243 RETURN_FALSE;
1244 }
1245
1246 bool stricterror = dom_get_strict_error(intern->document);
1247
1248 if (dom_node_is_read_only(nodep) == SUCCESS ||
1249 (child->parent != NULL && dom_node_is_read_only(child->parent) == SUCCESS)) {
1250 php_dom_throw_error(NO_MODIFICATION_ALLOWED_ERR, stricterror);
1251 RETURN_FALSE;
1252 }
1253
1254 if (dom_hierarchy(nodep, child) == FAILURE) {
1255 php_dom_throw_error(HIERARCHY_REQUEST_ERR, stricterror);
1256 RETURN_FALSE;
1257 }
1258
1259 if (!(child->doc == NULL || child->doc == nodep->doc)) {
1260 php_dom_throw_error(WRONG_DOCUMENT_ERR, stricterror);
1261 RETURN_FALSE;
1262 }
1263
1264 if (child->type == XML_DOCUMENT_FRAG_NODE && child->children == NULL) {
1265 /* TODO Drop Warning? */
1266 php_error_docref(NULL, E_WARNING, "Document Fragment is empty");
1267 RETURN_FALSE;
1268 }
1269
1270 if (child->doc == NULL && nodep->doc != NULL) {
1271 childobj->document = intern->document;
1272 php_libxml_increment_doc_ref((php_libxml_node_object *)childobj, NULL);
1273 }
1274
1275 if (child->parent != NULL){
1276 xmlUnlinkNode(child);
1277 }
1278
1279 if (child->type == XML_TEXT_NODE && nodep->last != NULL && nodep->last->type == XML_TEXT_NODE) {
1280 child->parent = nodep;
1281 if (child->doc == NULL) {
1282 xmlSetTreeDoc(child, nodep->doc);
1283 }
1284 new_child = child;
1285 if (nodep->children == NULL) {
1286 nodep->children = child;
1287 nodep->last = child;
1288 } else {
1289 child = nodep->last;
1290 child->next = new_child;
1291 new_child->prev = child;
1292 nodep->last = new_child;
1293 }
1294 } else if (child->type == XML_ATTRIBUTE_NODE) {
1295 xmlAttrPtr lastattr;
1296
1297 if (child->ns == NULL)
1298 lastattr = xmlHasProp(nodep, child->name);
1299 else
1300 lastattr = xmlHasNsProp(nodep, child->name, child->ns->href);
1301 if (lastattr != NULL && lastattr->type != XML_ATTRIBUTE_DECL) {
1302 if (lastattr != (xmlAttrPtr) child) {
1303 xmlUnlinkNode((xmlNodePtr) lastattr);
1304 php_libxml_node_free_resource((xmlNodePtr) lastattr);
1305 }
1306 }
1307 new_child = xmlAddChild(nodep, child);
1308 if (UNEXPECTED(new_child == NULL)) {
1309 goto cannot_add;
1310 }
1311 php_dom_reconcile_attribute_namespace_after_insertion((xmlAttrPtr) new_child);
1312 } else if (child->type == XML_DOCUMENT_FRAG_NODE) {
1313 xmlNodePtr last = child->last;
1314 new_child = dom_insert_fragment(nodep, nodep->last, NULL, child, intern);
1315 dom_reconcile_ns_list(nodep->doc, new_child, last);
1316 } else if (child->type == XML_DTD_NODE) {
1317 if (nodep->doc->intSubset != NULL) {
1318 php_dom_throw_error_with_message(HIERARCHY_REQUEST_ERR, "A document may only contain one document type", stricterror);
1319 RETURN_FALSE;
1320 }
1321 new_child = xmlAddChild(nodep, child);
1322 if (UNEXPECTED(new_child == NULL)) {
1323 goto cannot_add;
1324 }
1325 nodep->doc->intSubset = (xmlDtdPtr) new_child;
1326 } else {
1327 new_child = xmlAddChild(nodep, child);
1328 if (UNEXPECTED(new_child == NULL)) {
1329 goto cannot_add;
1330 }
1331 dom_reconcile_ns(nodep->doc, new_child);
1332 }
1333
1334 php_libxml_invalidate_node_list_cache(intern->document);
1335
1336 DOM_RET_OBJ(new_child, intern);
1337 return;
1338 cannot_add:
1339 php_dom_throw_error(INVALID_STATE_ERR, stricterror);
1340 RETURN_FALSE;
1341 }
1342 /* }}} end dom_node_append_child */
1343
PHP_METHOD(DOMNode,appendChild)1344 PHP_METHOD(DOMNode, appendChild)
1345 {
1346 zval *node;
1347 xmlNodePtr nodep, child;
1348 dom_object *intern, *childobj;
1349
1350 ZEND_PARSE_PARAMETERS_START(1, 1)
1351 Z_PARAM_OBJECT_OF_CLASS(node, dom_node_class_entry)
1352 ZEND_PARSE_PARAMETERS_END();
1353
1354 DOM_GET_OBJ(nodep, ZEND_THIS, xmlNodePtr, intern);
1355 DOM_GET_OBJ(child, node, xmlNodePtr, childobj);
1356
1357 dom_node_append_child_legacy(return_value, intern, childobj, nodep, child);
1358 }
1359
PHP_METHOD(Dom_Node,appendChild)1360 PHP_METHOD(Dom_Node, appendChild)
1361 {
1362 zval *node;
1363 xmlNodePtr nodep, child;
1364 dom_object *intern, *childobj;
1365
1366 ZEND_PARSE_PARAMETERS_START(1, 1)
1367 Z_PARAM_OBJECT_OF_CLASS(node, dom_modern_node_class_entry)
1368 ZEND_PARSE_PARAMETERS_END();
1369
1370 DOM_GET_OBJ(nodep, ZEND_THIS, xmlNodePtr, intern);
1371 DOM_GET_OBJ(child, node, xmlNodePtr, childobj);
1372
1373 /* Parent check from pre-insertion validation done here:
1374 * If parent is not a Document, DocumentFragment, or Element node, then throw a "HierarchyRequestError" DOMException. */
1375 if (php_dom_pre_insert_is_parent_invalid(nodep)) {
1376 php_dom_throw_error(HIERARCHY_REQUEST_ERR, /* strict */ true);
1377 RETURN_THROWS();
1378 }
1379 /* Append, this doesn't do the parent check so we do it here. */
1380 php_libxml_invalidate_node_list_cache(intern->document);
1381 php_dom_node_append(intern->document, child, nodep);
1382 DOM_RET_OBJ(child, intern);
1383 }
1384
1385 /* {{{ URL: http://www.w3.org/TR/2003/WD-DOM-Level-3-Core-20030226/DOM3-Core.html#core-ID-810594187
1386 Since:
1387 */
PHP_METHOD(DOMNode,hasChildNodes)1388 PHP_METHOD(DOMNode, hasChildNodes)
1389 {
1390 xmlNode *nodep;
1391 dom_object *intern;
1392
1393 ZEND_PARSE_PARAMETERS_NONE();
1394
1395 DOM_GET_OBJ(nodep, ZEND_THIS, xmlNodePtr, intern);
1396
1397 RETURN_BOOL(dom_node_children_valid(nodep) && nodep->children != NULL);
1398 }
1399 /* }}} end dom_node_has_child_nodes */
1400
1401 /* {{{ URL: http://www.w3.org/TR/2003/WD-DOM-Level-3-Core-20030226/DOM3-Core.html#core-ID-3A0ED0A4
1402 Since:
1403 */
PHP_METHOD(DOMNode,cloneNode)1404 PHP_METHOD(DOMNode, cloneNode)
1405 {
1406 zval *id;
1407 xmlNode *n, *node;
1408 dom_object *intern;
1409 bool recursive = 0;
1410
1411 id = ZEND_THIS;
1412 if (zend_parse_parameters(ZEND_NUM_ARGS(), "|b", &recursive) == FAILURE) {
1413 RETURN_THROWS();
1414 }
1415
1416 DOM_GET_OBJ(n, id, xmlNodePtr, intern);
1417
1418 php_dom_private_data *private_data = NULL;
1419 bool clone_document = n->type == XML_DOCUMENT_NODE || n->type == XML_HTML_DOCUMENT_NODE;
1420 if (php_dom_follow_spec_intern(intern)) {
1421 if (clone_document) {
1422 private_data = php_dom_private_data_create();
1423 } else {
1424 private_data = php_dom_get_private_data(intern);
1425 }
1426 }
1427
1428 node = dom_clone_node(php_dom_ns_mapper_from_private(private_data), n, n->doc, recursive);
1429
1430 if (!node) {
1431 if (clone_document && private_data != NULL) {
1432 php_dom_private_data_destroy(private_data);
1433 }
1434 RETURN_FALSE;
1435 }
1436
1437 /* If document cloned we want a new document proxy */
1438 if (clone_document) {
1439 dom_object *new_intern;
1440 if (private_data) {
1441 /* We have the issue here that we can't create a modern node without an intern.
1442 * Fortunately, it's impossible to have a custom document class for the modern DOM (final base class),
1443 * so we can solve this by invoking the instantiation helper directly. */
1444 zend_class_entry *ce = n->type == XML_DOCUMENT_NODE ? dom_xml_document_class_entry : dom_html_document_class_entry;
1445 new_intern = php_dom_instantiate_object_helper(return_value, ce, node, NULL);
1446 } else {
1447 DOM_RET_OBJ(node, NULL);
1448 new_intern = Z_DOMOBJ_P(return_value);
1449 }
1450 php_dom_update_document_after_clone(intern, n, new_intern, node);
1451 ZEND_ASSERT(new_intern->document->private_data == NULL);
1452 new_intern->document->private_data = php_dom_libxml_private_data_header(private_data);
1453 } else {
1454 if (node->type == XML_ATTRIBUTE_NODE && n->ns != NULL && node->ns == NULL) {
1455 /* Let reconciliation deal with this. The lifetime of the namespace poses no problem
1456 * because we're increasing the refcount of the document proxy at the return.
1457 * libxml2 doesn't set the ns because it can't know that this is safe. */
1458 node->ns = n->ns;
1459 }
1460
1461 DOM_RET_OBJ(node, intern);
1462 }
1463 }
1464 /* }}} end dom_node_clone_node */
1465
1466 /* {{{ URL: http://www.w3.org/TR/2003/WD-DOM-Level-3-Core-20030226/DOM3-Core.html#core-ID-normalize
1467 Since:
1468 */
PHP_METHOD(DOMNode,normalize)1469 PHP_METHOD(DOMNode, normalize)
1470 {
1471 zval *id;
1472 xmlNode *nodep;
1473 dom_object *intern;
1474
1475 id = ZEND_THIS;
1476 ZEND_PARSE_PARAMETERS_NONE();
1477
1478 DOM_GET_OBJ(nodep, id, xmlNodePtr, intern);
1479
1480 if (php_dom_follow_spec_intern(intern)) {
1481 php_dom_normalize_modern(nodep);
1482 } else {
1483 php_dom_normalize_legacy(nodep);
1484 }
1485 }
1486 /* }}} end dom_node_normalize */
1487
1488 /* {{{ URL: http://www.w3.org/TR/2003/WD-DOM-Level-3-Core-20030226/DOM3-Core.html#core-Level-2-Core-Node-supports
1489 Since: DOM Level 2
1490 */
PHP_METHOD(DOMNode,isSupported)1491 PHP_METHOD(DOMNode, isSupported)
1492 {
1493 zend_string *feature, *version;
1494
1495 if (zend_parse_parameters(ZEND_NUM_ARGS(), "SS", &feature, &version) == FAILURE) {
1496 RETURN_THROWS();
1497 }
1498
1499 RETURN_BOOL(dom_has_feature(feature, version));
1500 }
1501 /* }}} end dom_node_is_supported */
1502
1503 /* {{{ URL: http://www.w3.org/TR/2003/WD-DOM-Level-3-Core-20030226/DOM3-Core.html#core-ID-NodeHasAttrs
1504 Since: DOM Level 2
1505 */
PHP_METHOD(DOMNode,hasAttributes)1506 PHP_METHOD(DOMNode, hasAttributes)
1507 {
1508 xmlNode *nodep;
1509 dom_object *intern;
1510
1511 ZEND_PARSE_PARAMETERS_NONE();
1512
1513 DOM_GET_OBJ(nodep, ZEND_THIS, xmlNodePtr, intern);
1514
1515 RETURN_BOOL(nodep->type == XML_ELEMENT_NODE && nodep->properties != NULL);
1516 }
1517 /* }}} end dom_node_has_attributes */
1518
1519 /* {{{ URL: http://www.w3.org/TR/2003/WD-DOM-Level-3-Core-20030226/DOM3-Core.html#Node3-isSameNode
1520 Since: DOM Level 3
1521 */
dom_node_is_same_node(INTERNAL_FUNCTION_PARAMETERS,zval * node)1522 static void dom_node_is_same_node(INTERNAL_FUNCTION_PARAMETERS, zval *node)
1523 {
1524 zval *id;
1525 xmlNodePtr nodeotherp, nodep;
1526 dom_object *intern, *nodeotherobj;
1527
1528 DOM_GET_THIS_OBJ(nodep, id, xmlNodePtr, intern);
1529
1530 DOM_GET_OBJ(nodeotherp, node, xmlNodePtr, nodeotherobj);
1531
1532 if (nodep == nodeotherp) {
1533 RETURN_TRUE;
1534 } else {
1535 RETURN_FALSE;
1536 }
1537 }
1538
PHP_METHOD(DOMNode,isSameNode)1539 PHP_METHOD(DOMNode, isSameNode)
1540 {
1541 zval *node;
1542 ZEND_PARSE_PARAMETERS_START(1, 1)
1543 Z_PARAM_OBJECT_OF_CLASS(node, dom_node_class_entry)
1544 ZEND_PARSE_PARAMETERS_END();
1545
1546 dom_node_is_same_node(INTERNAL_FUNCTION_PARAM_PASSTHRU, node);
1547 }
1548
PHP_METHOD(Dom_Node,isSameNode)1549 PHP_METHOD(Dom_Node, isSameNode)
1550 {
1551 zval *node;
1552 ZEND_PARSE_PARAMETERS_START(1, 1)
1553 Z_PARAM_OBJECT_OF_CLASS_OR_NULL(node, dom_modern_node_class_entry)
1554 ZEND_PARSE_PARAMETERS_END();
1555
1556 if (node == NULL) {
1557 RETURN_FALSE;
1558 }
1559
1560 dom_node_is_same_node(INTERNAL_FUNCTION_PARAM_PASSTHRU, node);
1561 }
1562 /* }}} end dom_node_is_same_node */
1563
php_dom_node_is_content_equal(const xmlNode * this,const xmlNode * other)1564 static bool php_dom_node_is_content_equal(const xmlNode *this, const xmlNode *other)
1565 {
1566 xmlChar *this_content = xmlNodeGetContent(this);
1567 xmlChar *other_content = xmlNodeGetContent(other);
1568 bool result = xmlStrEqual(this_content, other_content);
1569 xmlFree(this_content);
1570 xmlFree(other_content);
1571 return result;
1572 }
1573
php_dom_node_is_ns_uri_equal(const xmlNode * this,const xmlNode * other)1574 static bool php_dom_node_is_ns_uri_equal(const xmlNode *this, const xmlNode *other)
1575 {
1576 const xmlChar *this_ns = this->ns ? this->ns->href : NULL;
1577 const xmlChar *other_ns = other->ns ? other->ns->href : NULL;
1578 return xmlStrEqual(this_ns, other_ns);
1579 }
1580
php_dom_node_is_ns_prefix_equal(const xmlNode * this,const xmlNode * other)1581 static bool php_dom_node_is_ns_prefix_equal(const xmlNode *this, const xmlNode *other)
1582 {
1583 const xmlChar *this_ns = this->ns ? this->ns->prefix : NULL;
1584 const xmlChar *other_ns = other->ns ? other->ns->prefix : NULL;
1585 return xmlStrEqual(this_ns, other_ns);
1586 }
1587
1588 static bool php_dom_node_is_equal_node(const xmlNode *this, const xmlNode *other, bool spec_compliant);
1589
1590 #define PHP_DOM_FUNC_CAT(prefix, suffix) prefix##_##suffix
1591 /* xmlNode and xmlNs have incompatible struct layouts, i.e. the next field is in a different offset */
1592 #define PHP_DOM_DEFINE_LIST_COUNTER_HELPER(type) \
1593 static size_t PHP_DOM_FUNC_CAT(php_dom_node_count_list_size, type)(const type *node) \
1594 { \
1595 size_t counter = 0; \
1596 while (node) { \
1597 counter++; \
1598 node = node->next; \
1599 } \
1600 return counter; \
1601 }
1602 #define PHP_DOM_DEFINE_LIST_EQUALITY_ORDERED_HELPER(type) \
1603 static bool PHP_DOM_FUNC_CAT(php_dom_node_list_equality_check_ordered, type)(const type *list1, const type *list2, bool spec_compliant) \
1604 { \
1605 size_t count = PHP_DOM_FUNC_CAT(php_dom_node_count_list_size, type)(list1); \
1606 if (count != PHP_DOM_FUNC_CAT(php_dom_node_count_list_size, type)(list2)) { \
1607 return false; \
1608 } \
1609 for (size_t i = 0; i < count; i++) { \
1610 if (!php_dom_node_is_equal_node((const xmlNode *) list1, (const xmlNode *) list2, spec_compliant)) { \
1611 return false; \
1612 } \
1613 list1 = list1->next; \
1614 list2 = list2->next; \
1615 } \
1616 return true; \
1617 }
1618 #define PHP_DOM_DEFINE_LIST_EQUALITY_UNORDERED_HELPER(type) \
1619 static bool PHP_DOM_FUNC_CAT(php_dom_node_list_equality_check_unordered, type)(const type *list1, const type *list2, bool spec_compliant)\
1620 { \
1621 size_t count = PHP_DOM_FUNC_CAT(php_dom_node_count_list_size, type)(list1); \
1622 if (count != PHP_DOM_FUNC_CAT(php_dom_node_count_list_size, type)(list2)) { \
1623 return false; \
1624 } \
1625 for (const type *n1 = list1; n1 != NULL; n1 = n1->next) { \
1626 bool found = false; \
1627 for (const type *n2 = list2; n2 != NULL && !found; n2 = n2->next) { \
1628 if (php_dom_node_is_equal_node((const xmlNode *) n1, (const xmlNode *) n2, spec_compliant)) { \
1629 found = true; \
1630 } \
1631 } \
1632 if (!found) { \
1633 return false; \
1634 } \
1635 } \
1636 return true; \
1637 }
1638
1639 PHP_DOM_DEFINE_LIST_COUNTER_HELPER(xmlNode)
PHP_DOM_DEFINE_LIST_COUNTER_HELPER(xmlNs)1640 PHP_DOM_DEFINE_LIST_COUNTER_HELPER(xmlNs)
1641 PHP_DOM_DEFINE_LIST_EQUALITY_ORDERED_HELPER(xmlNode)
1642 PHP_DOM_DEFINE_LIST_EQUALITY_UNORDERED_HELPER(xmlNode)
1643 PHP_DOM_DEFINE_LIST_EQUALITY_UNORDERED_HELPER(xmlNs)
1644
1645 static bool php_dom_is_equal_attr(const xmlAttr *this_attr, const xmlAttr *other_attr)
1646 {
1647 ZEND_ASSERT(this_attr != NULL);
1648 ZEND_ASSERT(other_attr != NULL);
1649 return xmlStrEqual(this_attr->name, other_attr->name)
1650 && php_dom_node_is_ns_uri_equal((const xmlNode *) this_attr, (const xmlNode *) other_attr)
1651 && php_dom_node_is_content_equal((const xmlNode *) this_attr, (const xmlNode *) other_attr);
1652 }
1653
php_dom_node_is_equal_node(const xmlNode * this,const xmlNode * other,bool spec_compliant)1654 static bool php_dom_node_is_equal_node(const xmlNode *this, const xmlNode *other, bool spec_compliant)
1655 {
1656 ZEND_ASSERT(this != NULL);
1657 ZEND_ASSERT(other != NULL);
1658
1659 if (this->type != other->type) {
1660 return false;
1661 }
1662
1663 /* Notes:
1664 * - XML_DOCUMENT_TYPE_NODE is no longer created by libxml2, we only have to support XML_DTD_NODE.
1665 * - element and attribute declarations are not exposed as nodes in DOM, so no comparison is needed for those. */
1666 if (this->type == XML_ELEMENT_NODE) {
1667 return xmlStrEqual(this->name, other->name)
1668 && php_dom_node_is_ns_prefix_equal(this, other)
1669 && php_dom_node_is_ns_uri_equal(this, other)
1670 /* Check attributes first, then namespace declarations, then children */
1671 && php_dom_node_list_equality_check_unordered_xmlNode((const xmlNode *) this->properties, (const xmlNode *) other->properties, spec_compliant)
1672 && (spec_compliant || php_dom_node_list_equality_check_unordered_xmlNs(this->nsDef, other->nsDef, false))
1673 && php_dom_node_list_equality_check_ordered_xmlNode(this->children, other->children, spec_compliant);
1674 } else if (this->type == XML_DTD_NODE) {
1675 /* Note: in the living spec entity declarations and notations are no longer compared because they're considered obsolete. */
1676 const xmlDtd *this_dtd = (const xmlDtd *) this;
1677 const xmlDtd *other_dtd = (const xmlDtd *) other;
1678 return xmlStrEqual(this_dtd->name, other_dtd->name)
1679 && xmlStrEqual(this_dtd->ExternalID, other_dtd->ExternalID)
1680 && xmlStrEqual(this_dtd->SystemID, other_dtd->SystemID);
1681 } else if (this->type == XML_PI_NODE) {
1682 return xmlStrEqual(this->name, other->name) && xmlStrEqual(this->content, other->content);
1683 } else if (this->type == XML_TEXT_NODE || this->type == XML_COMMENT_NODE || this->type == XML_CDATA_SECTION_NODE) {
1684 return xmlStrEqual(this->content, other->content);
1685 } else if (this->type == XML_ATTRIBUTE_NODE) {
1686 const xmlAttr *this_attr = (const xmlAttr *) this;
1687 const xmlAttr *other_attr = (const xmlAttr *) other;
1688 return php_dom_is_equal_attr(this_attr, other_attr);
1689 } else if (this->type == XML_ENTITY_REF_NODE) {
1690 return xmlStrEqual(this->name, other->name);
1691 } else if (this->type == XML_ENTITY_DECL || this->type == XML_NOTATION_NODE || this->type == XML_ENTITY_NODE) {
1692 const xmlEntity *this_entity = (const xmlEntity *) this;
1693 const xmlEntity *other_entity = (const xmlEntity *) other;
1694 return this_entity->etype == other_entity->etype
1695 && xmlStrEqual(this_entity->name, other_entity->name)
1696 && xmlStrEqual(this_entity->ExternalID, other_entity->ExternalID)
1697 && xmlStrEqual(this_entity->SystemID, other_entity->SystemID)
1698 && php_dom_node_is_content_equal(this, other);
1699 } else if (this->type == XML_NAMESPACE_DECL) {
1700 const xmlNs *this_ns = (const xmlNs *) this;
1701 const xmlNs *other_ns = (const xmlNs *) other;
1702 return xmlStrEqual(this_ns->prefix, other_ns->prefix) && xmlStrEqual(this_ns->href, other_ns->href);
1703 } else if (this->type == XML_DOCUMENT_FRAG_NODE || this->type == XML_HTML_DOCUMENT_NODE || this->type == XML_DOCUMENT_NODE) {
1704 return php_dom_node_list_equality_check_ordered_xmlNode(this->children, other->children, spec_compliant);
1705 }
1706
1707 return false;
1708 }
1709
1710 /* {{{ URL: https://dom.spec.whatwg.org/#dom-node-isequalnode (for everything still in the living spec)
1711 * URL: https://www.w3.org/TR/2004/REC-DOM-Level-3-Core-20040407/DOM3-Core.html#core-Node3-isEqualNode (for old nodes removed from the living spec)
1712 Since: DOM Level 3
1713 */
dom_node_is_equal_node_common(INTERNAL_FUNCTION_PARAMETERS,bool modern)1714 static void dom_node_is_equal_node_common(INTERNAL_FUNCTION_PARAMETERS, bool modern)
1715 {
1716 zval *id, *node;
1717 xmlNodePtr otherp, nodep;
1718 dom_object *intern;
1719
1720 id = ZEND_THIS;
1721 ZEND_PARSE_PARAMETERS_START(1, 1)
1722 Z_PARAM_OBJECT_OF_CLASS_OR_NULL(node, dom_get_node_ce(modern))
1723 ZEND_PARSE_PARAMETERS_END();
1724
1725 if (node == NULL) {
1726 RETURN_FALSE;
1727 }
1728
1729 DOM_GET_OBJ(otherp, node, xmlNodePtr, intern);
1730 DOM_GET_THIS_OBJ(nodep, id, xmlNodePtr, intern);
1731
1732 if (nodep == otherp) {
1733 RETURN_TRUE;
1734 }
1735
1736 /* Empty fragments/documents only match if they're both empty */
1737 if (nodep == NULL || otherp == NULL) {
1738 RETURN_BOOL(nodep == NULL && otherp == NULL);
1739 }
1740
1741 RETURN_BOOL(php_dom_node_is_equal_node(nodep, otherp, modern));
1742 }
1743
PHP_METHOD(DOMNode,isEqualNode)1744 PHP_METHOD(DOMNode, isEqualNode)
1745 {
1746 dom_node_is_equal_node_common(INTERNAL_FUNCTION_PARAM_PASSTHRU, false);
1747 }
1748
PHP_METHOD(Dom_Node,isEqualNode)1749 PHP_METHOD(Dom_Node, isEqualNode)
1750 {
1751 dom_node_is_equal_node_common(INTERNAL_FUNCTION_PARAM_PASSTHRU, true);
1752 }
1753 /* }}} end DOMNode::isEqualNode */
1754
1755 /* https://dom.spec.whatwg.org/#locate-a-namespace-prefix */
dom_locate_a_namespace_prefix(xmlNodePtr elem,const char * uri)1756 static const xmlChar *dom_locate_a_namespace_prefix(xmlNodePtr elem, const char *uri)
1757 {
1758 do {
1759 /* 1. If element’s namespace is namespace and its namespace prefix is non-null, then return its namespace prefix. */
1760 if (elem->ns != NULL && elem->ns->prefix != NULL && xmlStrEqual(elem->ns->href, BAD_CAST uri)) {
1761 return elem->ns->prefix;
1762 }
1763
1764 /* 2. If element has an attribute whose namespace prefix is "xmlns" and value is namespace,
1765 * then return element’s first such attribute’s local name. */
1766 for (xmlAttrPtr attr = elem->properties; attr != NULL; attr = attr->next) {
1767 if (attr->ns != NULL && attr->children != NULL
1768 && xmlStrEqual(attr->ns->prefix, BAD_CAST "xmlns") && xmlStrEqual(attr->children->content, BAD_CAST uri)) {
1769 return attr->name;
1770 }
1771 }
1772
1773 /* 3. If element’s parent element is not null, then return the result of running locate a namespace prefix on that element using namespace. */
1774 elem = elem->parent;
1775 } while (elem != NULL && elem->type == XML_ELEMENT_NODE);
1776
1777 /* 4. Return null. */
1778 return NULL;
1779 }
1780
1781 /* {{{ URL: http://www.w3.org/TR/2003/WD-DOM-Level-3-Core-20030226/DOM3-Core.html#Node3-lookupNamespacePrefix
1782 Modern spec URL: https://dom.spec.whatwg.org/#dom-node-lookupprefix
1783 Since: DOM Level 3
1784 */
dom_node_lookup_prefix(INTERNAL_FUNCTION_PARAMETERS,bool modern)1785 static void dom_node_lookup_prefix(INTERNAL_FUNCTION_PARAMETERS, bool modern)
1786 {
1787 zval *id;
1788 xmlNodePtr nodep, lookupp = NULL;
1789 dom_object *intern;
1790 xmlNsPtr nsptr;
1791 size_t uri_len = 0;
1792 char *uri;
1793
1794 id = ZEND_THIS;
1795 if (zend_parse_parameters(ZEND_NUM_ARGS(), modern ? "s!" : "s", &uri, &uri_len) == FAILURE) {
1796 RETURN_THROWS();
1797 }
1798
1799 DOM_GET_OBJ(nodep, id, xmlNodePtr, intern);
1800
1801 /* 1. If namespace is null or the empty string, then return null. */
1802 if (uri_len > 0) {
1803 /* 2. Switch on the interface this implements: */
1804 switch (nodep->type) {
1805 case XML_ELEMENT_NODE:
1806 lookupp = nodep;
1807 break;
1808 case XML_DOCUMENT_NODE:
1809 case XML_HTML_DOCUMENT_NODE:
1810 lookupp = xmlDocGetRootElement((xmlDocPtr) nodep);
1811 break;
1812 case XML_ENTITY_NODE :
1813 case XML_NOTATION_NODE:
1814 case XML_DOCUMENT_FRAG_NODE:
1815 case XML_DOCUMENT_TYPE_NODE:
1816 case XML_DTD_NODE:
1817 RETURN_NULL();
1818 break;
1819 default:
1820 lookupp = nodep->parent;
1821 }
1822
1823 if (lookupp != NULL) {
1824 if (modern) {
1825 const char * result = (const char *) dom_locate_a_namespace_prefix(lookupp, uri);
1826 if (result != NULL) {
1827 RETURN_STRING(result);
1828 }
1829 } else {
1830 nsptr = xmlSearchNsByHref(lookupp->doc, lookupp, BAD_CAST uri);
1831 if (nsptr && nsptr->prefix != NULL) {
1832 RETURN_STRING((const char *) nsptr->prefix);
1833 }
1834 }
1835 }
1836 }
1837
1838 RETURN_NULL();
1839 }
1840
PHP_METHOD(DOMNode,lookupPrefix)1841 PHP_METHOD(DOMNode, lookupPrefix)
1842 {
1843 dom_node_lookup_prefix(INTERNAL_FUNCTION_PARAM_PASSTHRU, false);
1844 }
1845
PHP_METHOD(Dom_Node,lookupPrefix)1846 PHP_METHOD(Dom_Node, lookupPrefix)
1847 {
1848 dom_node_lookup_prefix(INTERNAL_FUNCTION_PARAM_PASSTHRU, true);
1849 }
1850 /* }}} end dom_node_lookup_prefix */
1851
1852 /* https://dom.spec.whatwg.org/#locate-a-namespace */
dom_locate_a_namespace(const xmlNode * node,const zend_string * prefix)1853 const char *dom_locate_a_namespace(const xmlNode *node, const zend_string *prefix)
1854 {
1855 /* switch on the interface node implements: */
1856 if (node->type == XML_ELEMENT_NODE) {
1857 if (prefix != NULL) {
1858 /* 1. If prefix is "xml", then return the XML namespace. */
1859 if (zend_string_equals_literal_ci(prefix, "xml")) {
1860 return DOM_XML_NS_URI;
1861 }
1862
1863 /* 2. If prefix is "xmlns", then return the XMLNS namespace. */
1864 if (zend_string_equals_literal_ci(prefix, "xmlns")) {
1865 return DOM_XMLNS_NS_URI;
1866 }
1867 }
1868
1869 do {
1870 /* 3. If its namespace is non-null and its namespace prefix is prefix, then return namespace. */
1871 if (node->ns != NULL && xmlStrEqual(node->ns->prefix, BAD_CAST (prefix ? ZSTR_VAL(prefix) : NULL))) {
1872 return (const char *) node->ns->href;
1873 }
1874
1875 /* 4. If it has an attribute whose namespace is the XMLNS namespace, namespace prefix is "xmlns", and local name is prefix,
1876 * or if prefix is null and it has an attribute whose namespace is the XMLNS namespace, namespace prefix is null, and local name is "xmlns",
1877 * then return its value if it is not the empty string, and null otherwise. */
1878 for (xmlAttrPtr attr = node->properties; attr != NULL; attr = attr->next) {
1879 if (attr->ns == NULL || !php_dom_ns_is_fast_ex(attr->ns, php_dom_ns_is_xmlns_magic_token)) {
1880 continue;
1881 }
1882 if ((prefix != NULL && xmlStrEqual(attr->ns->prefix, BAD_CAST "xmlns") && xmlStrEqual(attr->name, BAD_CAST ZSTR_VAL(prefix)))
1883 || (prefix == NULL && attr->ns->prefix == NULL && xmlStrEqual(attr->name, BAD_CAST "xmlns"))) {
1884 if (attr->children != NULL && attr->children->content[0] != '\0') {
1885 return (const char *) attr->children->content;
1886 } else {
1887 return NULL;
1888 }
1889 }
1890 }
1891
1892 /* 5. If its parent element is null, then return null. */
1893 if (node->parent == NULL || node->parent->type != XML_ELEMENT_NODE) {
1894 return NULL;
1895 }
1896
1897 /* 6. Return the result of running locate a namespace on its parent element using prefix. */
1898 node = node->parent;
1899 } while (true);
1900 } else if (node->type == XML_DOCUMENT_NODE || node->type == XML_HTML_DOCUMENT_NODE) {
1901 /* 1. If its document element is null, then return null. */
1902 node = xmlDocGetRootElement((xmlDocPtr) node);
1903 if (UNEXPECTED(node == NULL)) {
1904 return NULL;
1905 }
1906
1907 /* 2. Return the result of running locate a namespace on its document element using prefix. */
1908 return dom_locate_a_namespace(node, prefix);
1909 } else if (node->type == XML_DTD_NODE || node->type == XML_DOCUMENT_FRAG_NODE) {
1910 return NULL;
1911 } else {
1912 /* 1. If its element is null, then return null / If its parent element is null, then return null. */
1913 if (node->parent == NULL || node->parent->type != XML_ELEMENT_NODE) {
1914 return NULL;
1915 }
1916
1917 /* 2. Return the result of running locate a namespace on its element using prefix. */
1918 return dom_locate_a_namespace(node->parent, prefix);
1919 }
1920 }
1921
1922 /* {{{ URL: http://www.w3.org/TR/DOM-Level-3-Core/core.html#Node3-isDefaultNamespace
1923 Modern spec URL: https://dom.spec.whatwg.org/#dom-node-isdefaultnamespace
1924 Since: DOM Level 3
1925 */
PHP_METHOD(DOMNode,isDefaultNamespace)1926 PHP_METHOD(DOMNode, isDefaultNamespace)
1927 {
1928 zval *id;
1929 xmlNodePtr nodep;
1930 dom_object *intern;
1931 xmlNsPtr nsptr;
1932 size_t uri_len = 0;
1933 char *uri;
1934
1935 ZEND_PARSE_PARAMETERS_START(1, 1)
1936 Z_PARAM_STRING(uri, uri_len)
1937 ZEND_PARSE_PARAMETERS_END();
1938
1939 DOM_GET_THIS_OBJ(nodep, id, xmlNodePtr, intern);
1940
1941 if (uri_len > 0) {
1942 if (nodep->type == XML_DOCUMENT_NODE || nodep->type == XML_HTML_DOCUMENT_NODE) {
1943 nodep = xmlDocGetRootElement((xmlDocPtr) nodep);
1944 if (nodep == NULL) {
1945 RETURN_FALSE;
1946 }
1947 }
1948
1949 nsptr = xmlSearchNs(nodep->doc, nodep, NULL);
1950 if (nsptr && xmlStrEqual(nsptr->href, BAD_CAST uri)) {
1951 RETURN_TRUE;
1952 }
1953 }
1954
1955 RETURN_FALSE;
1956 }
1957
PHP_METHOD(Dom_Node,isDefaultNamespace)1958 PHP_METHOD(Dom_Node, isDefaultNamespace)
1959 {
1960 zval *id;
1961 xmlNodePtr nodep;
1962 dom_object *intern;
1963 size_t uri_len = 0;
1964 char *uri;
1965
1966 ZEND_PARSE_PARAMETERS_START(1, 1)
1967 Z_PARAM_STRING_OR_NULL(uri, uri_len)
1968 ZEND_PARSE_PARAMETERS_END();
1969
1970 DOM_GET_THIS_OBJ(nodep, id, xmlNodePtr, intern);
1971
1972 if (uri_len == 0) {
1973 uri = NULL;
1974 }
1975 const char *ns_uri = dom_locate_a_namespace(nodep, NULL);
1976 RETURN_BOOL(xmlStrEqual(BAD_CAST uri, BAD_CAST ns_uri));
1977 }
1978 /* }}} end dom_node_is_default_namespace */
1979
1980 /* {{{ URL: http://www.w3.org/TR/DOM-Level-3-Core/core.html#Node3-lookupNamespaceURI
1981 Modern spec URL: https://dom.spec.whatwg.org/#dom-node-lookupnamespaceuri
1982 Since: DOM Level 3
1983 */
PHP_METHOD(DOMNode,lookupNamespaceURI)1984 PHP_METHOD(DOMNode, lookupNamespaceURI)
1985 {
1986 zval *id;
1987 xmlNodePtr nodep;
1988 dom_object *intern;
1989 xmlNsPtr nsptr;
1990 zend_string *prefix;
1991
1992 id = ZEND_THIS;
1993 ZEND_PARSE_PARAMETERS_START(1, 1)
1994 Z_PARAM_STR_OR_NULL(prefix)
1995 ZEND_PARSE_PARAMETERS_END();
1996
1997 DOM_GET_OBJ(nodep, id, xmlNodePtr, intern);
1998
1999 if (php_dom_follow_spec_intern(intern)) {
2000 if (prefix != NULL && ZSTR_LEN(prefix) == 0) {
2001 prefix = NULL;
2002 }
2003 const char *ns_uri = dom_locate_a_namespace(nodep, prefix);
2004 if (ns_uri == NULL) {
2005 RETURN_NULL();
2006 } else {
2007 RETURN_STRING(ns_uri);
2008 }
2009 } else {
2010 if (nodep->type == XML_DOCUMENT_NODE || nodep->type == XML_HTML_DOCUMENT_NODE) {
2011 nodep = xmlDocGetRootElement((xmlDocPtr) nodep);
2012 if (nodep == NULL) {
2013 RETURN_NULL();
2014 }
2015 }
2016
2017 nsptr = xmlSearchNs(nodep->doc, nodep, BAD_CAST (prefix ? ZSTR_VAL(prefix) : NULL));
2018 if (nsptr && nsptr->href != NULL) {
2019 RETURN_STRING((char *) nsptr->href);
2020 }
2021 }
2022
2023 RETURN_NULL();
2024 }
2025 /* }}} end dom_node_lookup_namespace_uri */
2026
dom_canonicalize_node_parent_lookup_cb(void * user_data,xmlNodePtr node,xmlNodePtr parent)2027 static int dom_canonicalize_node_parent_lookup_cb(void *user_data, xmlNodePtr node, xmlNodePtr parent)
2028 {
2029 xmlNodePtr root = user_data;
2030 /* We have to unroll the first iteration because node->parent
2031 * is not necessarily equal to parent due to libxml2 tree rules (ns decls out of the tree for example). */
2032 if (node == root) {
2033 return 1;
2034 }
2035 node = parent;
2036 while (node != NULL) {
2037 if (node == root) {
2038 return 1;
2039 }
2040 node = node->parent;
2041 }
2042
2043 return 0;
2044 }
2045
dom_canonicalization(INTERNAL_FUNCTION_PARAMETERS,int mode)2046 static void dom_canonicalization(INTERNAL_FUNCTION_PARAMETERS, int mode) /* {{{ */
2047 {
2048 zval *id;
2049 zval *xpath_array=NULL, *ns_prefixes=NULL;
2050 xmlNodePtr nodep;
2051 xmlDocPtr docp;
2052 xmlNodeSetPtr nodeset = NULL;
2053 dom_object *intern;
2054 bool exclusive=0, with_comments=0;
2055 xmlChar **inclusive_ns_prefixes = NULL;
2056 char *file = NULL;
2057 int ret = -1;
2058 size_t file_len = 0;
2059 xmlOutputBufferPtr buf;
2060 xmlXPathContextPtr ctxp=NULL;
2061 xmlXPathObjectPtr xpathobjp=NULL;
2062
2063 id = ZEND_THIS;
2064 if (mode == 0) {
2065 if (zend_parse_parameters(ZEND_NUM_ARGS(),
2066 "|bba!a!", &exclusive, &with_comments,
2067 &xpath_array, &ns_prefixes) == FAILURE) {
2068 RETURN_THROWS();
2069 }
2070 } else {
2071 if (zend_parse_parameters(ZEND_NUM_ARGS(),
2072 "s|bba!a!", &file, &file_len, &exclusive,
2073 &with_comments, &xpath_array, &ns_prefixes) == FAILURE) {
2074 RETURN_THROWS();
2075 }
2076 }
2077
2078 DOM_GET_OBJ(nodep, id, xmlNodePtr, intern);
2079
2080 docp = nodep->doc;
2081
2082 if (! docp) {
2083 zend_throw_error(NULL, "Node must be associated with a document");
2084 RETURN_THROWS();
2085 }
2086
2087 bool simple_node_parent_lookup_callback = false;
2088 if (xpath_array == NULL) {
2089 /* Optimization: if the node is a document, all nodes may be included, no extra filtering or nodeset necessary. */
2090 if (nodep->type != XML_DOCUMENT_NODE && nodep->type != XML_HTML_DOCUMENT_NODE) {
2091 simple_node_parent_lookup_callback = true;
2092 }
2093 } else {
2094 /*xpath query from xpath_array */
2095 HashTable *ht = Z_ARRVAL_P(xpath_array);
2096 zval *tmp;
2097 char *xquery;
2098
2099 /* Find "query" key */
2100 tmp = zend_hash_find_deref(ht, ZSTR_KNOWN(ZEND_STR_QUERY));
2101 if (!tmp) {
2102 /* if mode == 0 then $xpath arg is 3, if mode == 1 then $xpath is 4 */
2103 zend_argument_value_error(3 + mode, "must have a \"query\" key");
2104 RETURN_THROWS();
2105 }
2106 if (Z_TYPE_P(tmp) != IS_STRING) {
2107 /* if mode == 0 then $xpath arg is 3, if mode == 1 then $xpath is 4 */
2108 zend_argument_type_error(3 + mode, "\"query\" option must be a string, %s given", zend_zval_value_name(tmp));
2109 RETURN_THROWS();
2110 }
2111 xquery = Z_STRVAL_P(tmp);
2112
2113 ctxp = xmlXPathNewContext(docp);
2114 ctxp->node = nodep;
2115
2116 tmp = zend_hash_str_find_deref(ht, "namespaces", sizeof("namespaces")-1);
2117 if (tmp && Z_TYPE_P(tmp) == IS_ARRAY && !HT_IS_PACKED(Z_ARRVAL_P(tmp))) {
2118 zval *tmpns;
2119 zend_string *prefix;
2120
2121 ZEND_HASH_MAP_FOREACH_STR_KEY_VAL(Z_ARRVAL_P(tmp), prefix, tmpns) {
2122 ZVAL_DEREF(tmpns);
2123 if (Z_TYPE_P(tmpns) == IS_STRING) {
2124 if (prefix) {
2125 xmlXPathRegisterNs(ctxp, BAD_CAST ZSTR_VAL(prefix), BAD_CAST Z_STRVAL_P(tmpns));
2126 }
2127 }
2128 } ZEND_HASH_FOREACH_END();
2129 }
2130
2131 xpathobjp = xmlXPathEvalExpression(BAD_CAST xquery, ctxp);
2132 ctxp->node = NULL;
2133 if (xpathobjp && xpathobjp->type == XPATH_NODESET) {
2134 nodeset = xpathobjp->nodesetval;
2135 } else {
2136 if (xpathobjp) {
2137 xmlXPathFreeObject(xpathobjp);
2138 }
2139 xmlXPathFreeContext(ctxp);
2140 zend_throw_error(NULL, "XPath query did not return a nodeset");
2141 RETURN_THROWS();
2142 }
2143 }
2144
2145 if (ns_prefixes != NULL) {
2146 if (exclusive) {
2147 zval *tmpns;
2148 int nscount = 0;
2149
2150 inclusive_ns_prefixes = safe_emalloc(zend_hash_num_elements(Z_ARRVAL_P(ns_prefixes)) + 1,
2151 sizeof(xmlChar *), 0);
2152 ZEND_HASH_FOREACH_VAL(Z_ARRVAL_P(ns_prefixes), tmpns) {
2153 ZVAL_DEREF(tmpns);
2154 if (Z_TYPE_P(tmpns) == IS_STRING) {
2155 inclusive_ns_prefixes[nscount++] = BAD_CAST Z_STRVAL_P(tmpns);
2156 }
2157 } ZEND_HASH_FOREACH_END();
2158 inclusive_ns_prefixes[nscount] = NULL;
2159 } else {
2160 php_error_docref(NULL, E_NOTICE,
2161 "Inclusive namespace prefixes only allowed in exclusive mode.");
2162 }
2163 }
2164
2165 if (mode == 1) {
2166 buf = xmlOutputBufferCreateFilename(file, NULL, 0);
2167 } else {
2168 buf = xmlAllocOutputBuffer(NULL);
2169 }
2170
2171 if (buf != NULL) {
2172 if (simple_node_parent_lookup_callback) {
2173 ret = xmlC14NExecute(docp, dom_canonicalize_node_parent_lookup_cb, nodep, exclusive, inclusive_ns_prefixes, with_comments, buf);
2174 } else {
2175 ret = xmlC14NDocSaveTo(docp, nodeset, exclusive, inclusive_ns_prefixes, with_comments, buf);
2176 }
2177 }
2178
2179 if (inclusive_ns_prefixes != NULL) {
2180 efree(inclusive_ns_prefixes);
2181 }
2182 if (xpathobjp != NULL) {
2183 xmlXPathFreeObject(xpathobjp);
2184 }
2185 if (ctxp != NULL) {
2186 xmlXPathFreeContext(ctxp);
2187 }
2188
2189 if (buf == NULL || ret < 0) {
2190 RETVAL_FALSE;
2191 } else {
2192 if (mode == 0) {
2193 size_t size = xmlOutputBufferGetSize(buf);
2194 if (size > 0) {
2195 RETVAL_STRINGL((char *) xmlOutputBufferGetContent(buf), size);
2196 } else {
2197 RETVAL_EMPTY_STRING();
2198 }
2199 }
2200 }
2201
2202 if (buf) {
2203 int bytes;
2204
2205 bytes = xmlOutputBufferClose(buf);
2206 if (mode == 1 && (ret >= 0)) {
2207 RETURN_LONG(bytes);
2208 }
2209 }
2210 }
2211 /* }}} */
2212
2213 /* {{{ Canonicalize nodes to a string */
PHP_METHOD(DOMNode,C14N)2214 PHP_METHOD(DOMNode, C14N)
2215 {
2216 dom_canonicalization(INTERNAL_FUNCTION_PARAM_PASSTHRU, 0);
2217 }
2218 /* }}} */
2219
2220 /* {{{ Canonicalize nodes to a file */
PHP_METHOD(DOMNode,C14NFile)2221 PHP_METHOD(DOMNode, C14NFile)
2222 {
2223 dom_canonicalization(INTERNAL_FUNCTION_PARAM_PASSTHRU, 1);
2224 }
2225 /* }}} */
2226
2227 /* {{{ Gets an xpath for a node */
dom_node_get_node_path(INTERNAL_FUNCTION_PARAMETERS,bool throw)2228 static void dom_node_get_node_path(INTERNAL_FUNCTION_PARAMETERS, bool throw)
2229 {
2230 zval *id;
2231 xmlNode *nodep;
2232 dom_object *intern;
2233 char *value;
2234
2235 ZEND_PARSE_PARAMETERS_NONE();
2236
2237 DOM_GET_THIS_OBJ(nodep, id, xmlNodePtr, intern);
2238
2239 value = (char *) xmlGetNodePath(nodep);
2240 if (value == NULL) {
2241 /* This is only possible when an invalid argument is passed (e.g. namespace declaration, but that's not the case for this call site),
2242 * or on allocation failure. So in other words, this only happens on allocation failure. */
2243 if (throw) {
2244 php_dom_throw_error(INVALID_STATE_ERR, /* strict */ true);
2245 RETURN_THROWS();
2246 }
2247 RETURN_NULL();
2248 } else {
2249 RETVAL_STRING(value);
2250 xmlFree(value);
2251 }
2252 }
2253
PHP_METHOD(DOMNode,getNodePath)2254 PHP_METHOD(DOMNode, getNodePath)
2255 {
2256 dom_node_get_node_path(INTERNAL_FUNCTION_PARAM_PASSTHRU, false);
2257 }
2258
PHP_METHOD(Dom_Node,getNodePath)2259 PHP_METHOD(Dom_Node, getNodePath)
2260 {
2261 dom_node_get_node_path(INTERNAL_FUNCTION_PARAM_PASSTHRU, true);
2262 }
2263 /* }}} */
2264
2265 /* {{{ Gets line number for a node */
PHP_METHOD(DOMNode,getLineNo)2266 PHP_METHOD(DOMNode, getLineNo)
2267 {
2268 zval *id;
2269 xmlNode *nodep;
2270 dom_object *intern;
2271
2272 ZEND_PARSE_PARAMETERS_NONE();
2273
2274 DOM_GET_THIS_OBJ(nodep, id, xmlNodePtr, intern);
2275
2276 RETURN_LONG(xmlGetLineNo(nodep));
2277 }
2278 /* }}} */
2279
2280 /* {{{ URL: https://dom.spec.whatwg.org/#dom-node-contains
2281 Since:
2282 */
dom_node_contains(xmlNodePtr thisp,xmlNodePtr otherp)2283 static bool dom_node_contains(xmlNodePtr thisp, xmlNodePtr otherp)
2284 {
2285 do {
2286 if (otherp == thisp) {
2287 return true;
2288 }
2289 otherp = otherp->parent;
2290 } while (otherp);
2291
2292 return false;
2293 }
2294
PHP_METHOD(DOMNode,contains)2295 PHP_METHOD(DOMNode, contains)
2296 {
2297 zval *other, *id;
2298 xmlNodePtr otherp, thisp;
2299 dom_object *unused_intern;
2300
2301 ZEND_PARSE_PARAMETERS_START(1, 1)
2302 Z_PARAM_OBJECT_OR_NULL(other)
2303 ZEND_PARSE_PARAMETERS_END();
2304
2305 if (other == NULL) {
2306 RETURN_FALSE;
2307 }
2308
2309 if (UNEXPECTED(!instanceof_function(Z_OBJCE_P(other), dom_node_class_entry) && !instanceof_function(Z_OBJCE_P(other), dom_namespace_node_class_entry))) {
2310 zend_argument_type_error(1, "must be of type DOMNode|DOMNameSpaceNode|null, %s given", zend_zval_value_name(other));
2311 RETURN_THROWS();
2312 }
2313
2314 DOM_GET_OBJ(otherp, other, xmlNodePtr, unused_intern);
2315 DOM_GET_THIS_OBJ(thisp, id, xmlNodePtr, unused_intern);
2316
2317 RETURN_BOOL(dom_node_contains(thisp, otherp));
2318 }
2319
PHP_METHOD(Dom_Node,contains)2320 PHP_METHOD(Dom_Node, contains)
2321 {
2322 zval *other, *id;
2323 xmlNodePtr otherp, thisp;
2324 dom_object *unused_intern;
2325
2326 ZEND_PARSE_PARAMETERS_START(1, 1)
2327 Z_PARAM_OBJECT_OF_CLASS_OR_NULL(other, dom_modern_node_class_entry)
2328 ZEND_PARSE_PARAMETERS_END();
2329
2330 if (other == NULL) {
2331 RETURN_FALSE;
2332 }
2333
2334 DOM_GET_OBJ(otherp, other, xmlNodePtr, unused_intern);
2335 DOM_GET_THIS_OBJ(thisp, id, xmlNodePtr, unused_intern);
2336
2337 RETURN_BOOL(dom_node_contains(thisp, otherp));
2338 }
2339 /* }}} */
2340
2341 /* {{{ URL: https://dom.spec.whatwg.org/#dom-node-getrootnode
2342 Since:
2343 */
PHP_METHOD(DOMNode,getRootNode)2344 PHP_METHOD(DOMNode, getRootNode)
2345 {
2346 zval *id;
2347 xmlNodePtr thisp;
2348 dom_object *intern;
2349 /* Unused now because we don't support the shadow DOM nodes. Options only influence shadow DOM nodes. */
2350 zval *options;
2351
2352 ZEND_PARSE_PARAMETERS_START(0, 1)
2353 Z_PARAM_OPTIONAL
2354 Z_PARAM_ARRAY_OR_NULL(options)
2355 ZEND_PARSE_PARAMETERS_END();
2356
2357 DOM_GET_THIS_OBJ(thisp, id, xmlNodePtr, intern);
2358
2359 while (thisp->parent) {
2360 thisp = thisp->parent;
2361 }
2362
2363 DOM_RET_OBJ(thisp, intern);
2364 }
2365 /* }}} */
2366
2367 /* {{{ URL: https://dom.spec.whatwg.org/#dom-node-comparedocumentposition (last check date 2023-07-24)
2368 Since:
2369 */
2370
2371 #define DOCUMENT_POSITION_DISCONNECTED 0x01
2372 #define DOCUMENT_POSITION_PRECEDING 0x02
2373 #define DOCUMENT_POSITION_FOLLOWING 0x04
2374 #define DOCUMENT_POSITION_CONTAINS 0x08
2375 #define DOCUMENT_POSITION_CONTAINED_BY 0x10
2376 #define DOCUMENT_POSITION_IMPLEMENTATION_SPECIFIC 0x20
2377
dom_node_compare_document_position(INTERNAL_FUNCTION_PARAMETERS,zend_class_entry * node_ce)2378 static void dom_node_compare_document_position(INTERNAL_FUNCTION_PARAMETERS, zend_class_entry *node_ce)
2379 {
2380 zval *id, *node_zval;
2381 xmlNodePtr other, this;
2382 dom_object *this_intern, *other_intern;
2383
2384 ZEND_PARSE_PARAMETERS_START(1, 1)
2385 Z_PARAM_OBJECT_OF_CLASS(node_zval, node_ce)
2386 ZEND_PARSE_PARAMETERS_END();
2387
2388 DOM_GET_THIS_OBJ(this, id, xmlNodePtr, this_intern);
2389 DOM_GET_OBJ(other, node_zval, xmlNodePtr, other_intern);
2390
2391 /* Step 1 */
2392 if (this == other) {
2393 RETURN_LONG(0);
2394 }
2395
2396 /* Step 2 */
2397 xmlNodePtr node1 = other;
2398 xmlNodePtr node2 = this;
2399
2400 /* Step 3 */
2401 xmlNodePtr attr1 = NULL;
2402 xmlNodePtr attr2 = NULL;
2403
2404 /* Step 4 */
2405 if (node1->type == XML_ATTRIBUTE_NODE) {
2406 attr1 = node1;
2407 node1 = attr1->parent;
2408 }
2409
2410 /* Step 5 */
2411 if (node2->type == XML_ATTRIBUTE_NODE) {
2412 /* 5.1 */
2413 attr2 = node2;
2414 node2 = attr2->parent;
2415
2416 /* 5.2 */
2417 if (attr1 != NULL && node1 != NULL && node2 == node1) {
2418 for (const xmlAttr *attr = node2->properties; attr != NULL; attr = attr->next) {
2419 if (php_dom_is_equal_attr(attr, (const xmlAttr *) attr1)) {
2420 RETURN_LONG(DOCUMENT_POSITION_IMPLEMENTATION_SPECIFIC | DOCUMENT_POSITION_PRECEDING);
2421 } else if (php_dom_is_equal_attr(attr, (const xmlAttr *) attr2)) {
2422 RETURN_LONG(DOCUMENT_POSITION_IMPLEMENTATION_SPECIFIC | DOCUMENT_POSITION_FOLLOWING);
2423 }
2424 }
2425 }
2426 }
2427
2428 /* Step 6 */
2429 /* We first check the first condition,
2430 * and as we need the root later anyway we'll cache the root and perform the root check after this if. */
2431 if (node1 == NULL || node2 == NULL) {
2432 goto disconnected;
2433 }
2434 bool node2_is_ancestor_of_node1 = false;
2435 size_t node1_depth = 0;
2436 xmlNodePtr node1_root = node1;
2437 while (node1_root->parent) {
2438 node1_root = node1_root->parent;
2439 if (node1_root == node2) {
2440 node2_is_ancestor_of_node1 = true;
2441 }
2442 node1_depth++;
2443 }
2444 bool node1_is_ancestor_of_node2 = false;
2445 size_t node2_depth = 0;
2446 xmlNodePtr node2_root = node2;
2447 while (node2_root->parent) {
2448 node2_root = node2_root->parent;
2449 if (node2_root == node1) {
2450 node1_is_ancestor_of_node2 = true;
2451 }
2452 node2_depth++;
2453 }
2454 /* Second condition from step 6 */
2455 if (node1_root != node2_root) {
2456 goto disconnected;
2457 }
2458
2459 /* Step 7 */
2460 if ((node1_is_ancestor_of_node2 && attr1 == NULL) || (node1 == node2 && attr2 != NULL)) {
2461 RETURN_LONG(DOCUMENT_POSITION_CONTAINS | DOCUMENT_POSITION_PRECEDING);
2462 }
2463
2464 /* Step 8 */
2465 if ((node2_is_ancestor_of_node1 && attr2 == NULL) || (node1 == node2 && attr1 != NULL)) {
2466 RETURN_LONG(DOCUMENT_POSITION_CONTAINED_BY | DOCUMENT_POSITION_FOLLOWING);
2467 }
2468
2469 /* Special case: comparing children and attributes.
2470 * They belong to a different tree and are therefore hard to compare, but spec demands attributes to precede children
2471 * according to the pre-order depth-first search ordering.
2472 * Because their tree is different, the node parents only meet at the common element instead of earlier.
2473 * Therefore, it seems that one is the ancestor of the other. */
2474 if (node1_is_ancestor_of_node2) {
2475 ZEND_ASSERT(attr1 != NULL); /* Would've been handled in step 7 otherwise */
2476 RETURN_LONG(DOCUMENT_POSITION_PRECEDING);
2477 } else if (node2_is_ancestor_of_node1) {
2478 ZEND_ASSERT(attr2 != NULL); /* Would've been handled in step 8 otherwise */
2479 RETURN_LONG(DOCUMENT_POSITION_FOLLOWING);
2480 }
2481
2482 /* Step 9 */
2483
2484 /* We'll use the following strategy (which was already prepared during step 6) to implement this efficiently:
2485 * 1. Move nodes upwards such that they are at the same depth.
2486 * 2. Then we move both nodes upwards simultaneously until their parents are equal.
2487 * 3. If we then move node1 to the next entry repeatedly and we encounter node2,
2488 * then we know node1 precedes node2. Otherwise, node2 must precede node1. */
2489 /* 1. */
2490 if (node1_depth > node2_depth) {
2491 do {
2492 node1 = node1->parent;
2493 node1_depth--;
2494 } while (node1_depth > node2_depth);
2495 } else if (node2_depth > node1_depth) {
2496 do {
2497 node2 = node2->parent;
2498 node2_depth--;
2499 } while (node2_depth > node1_depth);
2500 }
2501 /* 2. */
2502 while (node1->parent != node2->parent) {
2503 node1 = node1->parent;
2504 node2 = node2->parent;
2505 }
2506 /* 3. */
2507 ZEND_ASSERT(node1 != node2);
2508 ZEND_ASSERT(node1 != NULL);
2509 ZEND_ASSERT(node2 != NULL);
2510 do {
2511 node1 = node1->next;
2512 if (node1 == node2) {
2513 RETURN_LONG(DOCUMENT_POSITION_PRECEDING);
2514 }
2515 } while (node1 != NULL);
2516
2517 /* Step 10 */
2518 RETURN_LONG(DOCUMENT_POSITION_FOLLOWING);
2519
2520 disconnected:;
2521 zend_long ordering;
2522 if (node1 == node2) {
2523 /* Degenerate case, they're both NULL, but the ordering must be consistent... */
2524 ZEND_ASSERT(node1 == NULL);
2525 ordering = other_intern < this_intern ? DOCUMENT_POSITION_PRECEDING : DOCUMENT_POSITION_FOLLOWING;
2526 } else {
2527 ordering = node1 < node2 ? DOCUMENT_POSITION_PRECEDING : DOCUMENT_POSITION_FOLLOWING;
2528 }
2529 RETURN_LONG(DOCUMENT_POSITION_DISCONNECTED | DOCUMENT_POSITION_IMPLEMENTATION_SPECIFIC | ordering);
2530 }
2531
PHP_METHOD(DOMNode,compareDocumentPosition)2532 PHP_METHOD(DOMNode, compareDocumentPosition)
2533 {
2534 dom_node_compare_document_position(INTERNAL_FUNCTION_PARAM_PASSTHRU, dom_node_class_entry);
2535 }
2536
PHP_METHOD(Dom_Node,compareDocumentPosition)2537 PHP_METHOD(Dom_Node, compareDocumentPosition)
2538 {
2539 dom_node_compare_document_position(INTERNAL_FUNCTION_PARAM_PASSTHRU, dom_modern_node_class_entry);
2540 }
2541 /* }}} */
2542
2543 /**
2544 * We want to block the serialization and unserialization of DOM classes.
2545 * However, using @not-serializable makes the child classes also not serializable, even if the user implements the methods.
2546 * So instead, we implement the methods wherein we throw exceptions.
2547 * The reason we choose these methods is because:
2548 * - If the user implements __serialize / __unserialize, the respective throwing methods are not called.
2549 * - If the user implements __sleep / __wakeup, then it's also not a problem because they will not enter the throwing methods.
2550 */
2551
PHP_METHOD(Dom_Node,__construct)2552 PHP_METHOD(Dom_Node, __construct)
2553 {
2554 ZEND_UNREACHABLE();
2555 }
2556
PHP_METHOD(DOMNode,__sleep)2557 PHP_METHOD(DOMNode, __sleep)
2558 {
2559 ZEND_PARSE_PARAMETERS_NONE();
2560
2561 zend_throw_exception_ex(NULL, 0, "Serialization of '%s' is not allowed, unless serialization methods are implemented in a subclass", ZSTR_VAL(Z_OBJCE_P(ZEND_THIS)->name));
2562 RETURN_THROWS();
2563 }
2564
PHP_METHOD(DOMNode,__wakeup)2565 PHP_METHOD(DOMNode, __wakeup)
2566 {
2567 ZEND_PARSE_PARAMETERS_NONE();
2568
2569 zend_throw_exception_ex(NULL, 0, "Unserialization of '%s' is not allowed, unless unserialization methods are implemented in a subclass", ZSTR_VAL(Z_OBJCE_P(ZEND_THIS)->name));
2570 RETURN_THROWS();
2571 }
2572
2573 #endif
2574