xref: /PHP-8.0/ext/dom/nodelist.c (revision 2b5de6f8)
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    | http://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 "zend_interfaces.h"
26 
27 /*
28 * class DOMNodeList
29 *
30 * URL: https://www.w3.org/TR/2003/WD-DOM-Level-3-Core-20030226/DOM3-Core.html#ID-536297177
31 * Since:
32 */
33 
34 /* {{{ length	int
35 readonly=yes
36 URL: http://www.w3.org/TR/2003/WD-DOM-Level-3-Core-20030226/DOM3-Core.html#ID-203510337
37 Since:
38 */
dom_nodelist_length_read(dom_object * obj,zval * retval)39 int dom_nodelist_length_read(dom_object *obj, zval *retval)
40 {
41 	dom_nnodemap_object *objmap;
42 	xmlNodePtr nodep, curnode;
43 	int count = 0;
44 	HashTable *nodeht;
45 
46 	objmap = (dom_nnodemap_object *)obj->ptr;
47 	if (objmap != NULL) {
48 		if (objmap->ht) {
49 			count = xmlHashSize(objmap->ht);
50 		} else {
51 			if (objmap->nodetype == DOM_NODESET) {
52 				nodeht = HASH_OF(&objmap->baseobj_zv);
53 				count = zend_hash_num_elements(nodeht);
54 			} else {
55 				nodep = dom_object_get_node(objmap->baseobj);
56 				if (nodep) {
57 					if (objmap->nodetype == XML_ATTRIBUTE_NODE || objmap->nodetype == XML_ELEMENT_NODE) {
58 						curnode = nodep->children;
59 						if (curnode) {
60 							count++;
61 							while (curnode->next != NULL) {
62 								count++;
63 								curnode = curnode->next;
64 							}
65 						}
66 					} else {
67 						if (nodep->type == XML_DOCUMENT_NODE || nodep->type == XML_HTML_DOCUMENT_NODE) {
68 							nodep = xmlDocGetRootElement((xmlDoc *) nodep);
69 						} else {
70 							nodep = nodep->children;
71 						}
72 						curnode = dom_get_elements_by_tag_name_ns_raw(
73 							nodep, (char *) objmap->ns, (char *) objmap->local, &count, -1);
74 					}
75 				}
76 			}
77 		}
78 	}
79 
80 	ZVAL_LONG(retval, count);
81 	return SUCCESS;
82 }
83 
84 
85 /* {{{ */
PHP_METHOD(DOMNodeList,count)86 PHP_METHOD(DOMNodeList, count)
87 {
88 	zval *id;
89 	dom_object *intern;
90 
91 	id = ZEND_THIS;
92 	if (zend_parse_parameters_none() == FAILURE) {
93 		RETURN_THROWS();
94 	}
95 
96 	intern = Z_DOMOBJ_P(id);
97 	if(dom_nodelist_length_read(intern, return_value) == FAILURE) {
98 		RETURN_FALSE;
99 	}
100 }
101 /* }}} end dom_nodelist_count */
102 
103 /* }}} */
104 
105 /* {{{ URL: http://www.w3.org/TR/2003/WD-DOM-Level-3-Core-20030226/DOM3-Core.html#ID-844377136
106 Since:
107 */
PHP_METHOD(DOMNodeList,item)108 PHP_METHOD(DOMNodeList, item)
109 {
110 	zval *id;
111 	zend_long index;
112 	int ret;
113 	dom_object *intern;
114 	xmlNodePtr itemnode = NULL;
115 
116 	dom_nnodemap_object *objmap;
117 	xmlNodePtr nodep, curnode;
118 	int count = 0;
119 
120 	id = ZEND_THIS;
121 	if (zend_parse_parameters(ZEND_NUM_ARGS(), "l", &index) == FAILURE) {
122 		RETURN_THROWS();
123 	}
124 
125 	if (index >= 0) {
126 		intern = Z_DOMOBJ_P(id);
127 
128 		objmap = (dom_nnodemap_object *)intern->ptr;
129 		if (objmap != NULL) {
130 			if (objmap->ht) {
131 				if (objmap->nodetype == XML_ENTITY_NODE) {
132 					itemnode = php_dom_libxml_hash_iter(objmap->ht, index);
133 				} else {
134 					itemnode = php_dom_libxml_notation_iter(objmap->ht, index);
135 				}
136 			} else {
137 				if (objmap->nodetype == DOM_NODESET) {
138 					HashTable *nodeht = HASH_OF(&objmap->baseobj_zv);
139 					zval *entry = zend_hash_index_find(nodeht, index);
140 					if (entry) {
141 						ZVAL_COPY(return_value, entry);
142 						return;
143 					}
144 				} else if (objmap->baseobj) {
145 					nodep = dom_object_get_node(objmap->baseobj);
146 					if (nodep) {
147 						if (objmap->nodetype == XML_ATTRIBUTE_NODE || objmap->nodetype == XML_ELEMENT_NODE) {
148 							curnode = nodep->children;
149 							while (count < index && curnode != NULL) {
150 								count++;
151 								curnode = curnode->next;
152 							}
153 							itemnode = curnode;
154 						} else {
155 							if (nodep->type == XML_DOCUMENT_NODE || nodep->type == XML_HTML_DOCUMENT_NODE) {
156 								nodep = xmlDocGetRootElement((xmlDoc *) nodep);
157 							} else {
158 								nodep = nodep->children;
159 							}
160 							itemnode = dom_get_elements_by_tag_name_ns_raw(nodep, (char *) objmap->ns, (char *) objmap->local, &count, index);
161 						}
162 					}
163 				}
164 			}
165 		}
166 
167 		if (itemnode) {
168 			DOM_RET_OBJ(itemnode, &ret, objmap->baseobj);
169 			return;
170 		}
171 	}
172 
173 	RETVAL_NULL();
174 }
175 /* }}} end dom_nodelist_item */
176 
ZEND_METHOD(DOMNodeList,getIterator)177 ZEND_METHOD(DOMNodeList, getIterator)
178 {
179 	if (zend_parse_parameters_none() == FAILURE) {
180 		return;
181 	}
182 
183 	zend_create_internal_iterator_zval(return_value, ZEND_THIS);
184 }
185 
186 #endif
187