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