xref: /PHP-8.3/ext/dom/nodelist.c (revision 9cb23a3d)
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 "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 
objmap_cache_release_cached_obj(dom_nnodemap_object * objmap)34 static zend_always_inline void objmap_cache_release_cached_obj(dom_nnodemap_object *objmap)
35 {
36 	if (objmap->cached_obj) {
37 		/* Since the DOM is a tree there can be no cycles. */
38 		if (GC_DELREF(&objmap->cached_obj->std) == 0) {
39 			zend_objects_store_del(&objmap->cached_obj->std);
40 		}
41 		objmap->cached_obj = NULL;
42 		objmap->cached_obj_index = 0;
43 	}
44 }
45 
reset_objmap_cache(dom_nnodemap_object * objmap)46 static zend_always_inline void reset_objmap_cache(dom_nnodemap_object *objmap)
47 {
48 	objmap_cache_release_cached_obj(objmap);
49 	objmap->cached_length = -1;
50 }
51 
dom_nodelist_iter_start_first_child(xmlNodePtr nodep)52 xmlNodePtr dom_nodelist_iter_start_first_child(xmlNodePtr nodep)
53 {
54 	if (nodep->type == XML_ENTITY_REF_NODE) {
55 		/* See entityreference.c */
56 		dom_entity_reference_fetch_and_sync_declaration(nodep);
57 	}
58 
59 	return nodep->children;
60 }
61 
php_dom_get_nodelist_length(dom_object * obj)62 int php_dom_get_nodelist_length(dom_object *obj)
63 {
64 	dom_nnodemap_object *objmap = (dom_nnodemap_object *) obj->ptr;
65 	if (!objmap) {
66 		return 0;
67 	}
68 
69 	if (objmap->ht) {
70 		return xmlHashSize(objmap->ht);
71 	}
72 
73 	if (objmap->nodetype == DOM_NODESET) {
74 		HashTable *nodeht = HASH_OF(&objmap->baseobj_zv);
75 		return zend_hash_num_elements(nodeht);
76 	}
77 
78 	xmlNodePtr nodep = dom_object_get_node(objmap->baseobj);
79 	if (!nodep) {
80 		return 0;
81 	}
82 
83 	if (!php_dom_is_cache_tag_stale_from_node(&objmap->cache_tag, nodep)) {
84 		if (objmap->cached_length >= 0) {
85 			return objmap->cached_length;
86 		}
87 		/* Only the length is out-of-date, the cache tag is still valid.
88 		 * Therefore, only overwrite the length and keep the currently cached object. */
89 	} else {
90 		php_dom_mark_cache_tag_up_to_date_from_node(&objmap->cache_tag, nodep);
91 		reset_objmap_cache(objmap);
92 	}
93 
94 	zend_long count = 0;
95 	if (objmap->nodetype == XML_ATTRIBUTE_NODE || objmap->nodetype == XML_ELEMENT_NODE) {
96 		xmlNodePtr curnode = dom_nodelist_iter_start_first_child(nodep);
97 		if (curnode) {
98 			count++;
99 			while (curnode->next != NULL) {
100 				count++;
101 				curnode = curnode->next;
102 			}
103 		}
104 	} else {
105 		xmlNodePtr basep = nodep;
106 		if (nodep->type == XML_DOCUMENT_NODE || nodep->type == XML_HTML_DOCUMENT_NODE) {
107 			nodep = xmlDocGetRootElement((xmlDoc *) nodep);
108 		} else {
109 			nodep = nodep->children;
110 		}
111 		dom_get_elements_by_tag_name_ns_raw(
112 			basep, nodep, (char *) objmap->ns, (char *) objmap->local, &count, ZEND_LONG_MAX - 1 /* because of <= */);
113 	}
114 
115 	objmap->cached_length = count;
116 
117 	return count;
118 }
119 
120 /* {{{ length	int
121 readonly=yes
122 URL: http://www.w3.org/TR/2003/WD-DOM-Level-3-Core-20030226/DOM3-Core.html#ID-203510337
123 Since:
124 */
dom_nodelist_length_read(dom_object * obj,zval * retval)125 int dom_nodelist_length_read(dom_object *obj, zval *retval)
126 {
127 	ZVAL_LONG(retval, php_dom_get_nodelist_length(obj));
128 	return SUCCESS;
129 }
130 
131 
132 /* {{{ */
PHP_METHOD(DOMNodeList,count)133 PHP_METHOD(DOMNodeList, count)
134 {
135 	zval *id;
136 	dom_object *intern;
137 
138 	id = ZEND_THIS;
139 	if (zend_parse_parameters_none() == FAILURE) {
140 		RETURN_THROWS();
141 	}
142 
143 	intern = Z_DOMOBJ_P(id);
144 	RETURN_LONG(php_dom_get_nodelist_length(intern));
145 }
146 /* }}} end dom_nodelist_count */
147 
php_dom_nodelist_get_item_into_zval(dom_nnodemap_object * objmap,zend_long index,zval * return_value)148 void php_dom_nodelist_get_item_into_zval(dom_nnodemap_object *objmap, zend_long index, zval *return_value)
149 {
150 	int ret;
151 	xmlNodePtr itemnode = NULL;
152 	bool cache_itemnode = false;
153 	if (index >= 0) {
154 		if (objmap != NULL) {
155 			if (objmap->ht) {
156 				if (objmap->nodetype == XML_ENTITY_NODE) {
157 					itemnode = php_dom_libxml_hash_iter(objmap->ht, index);
158 				} else {
159 					itemnode = php_dom_libxml_notation_iter(objmap->ht, index);
160 				}
161 			} else {
162 				if (objmap->nodetype == DOM_NODESET) {
163 					HashTable *nodeht = HASH_OF(&objmap->baseobj_zv);
164 					zval *entry = zend_hash_index_find(nodeht, index);
165 					if (entry) {
166 						ZVAL_COPY(return_value, entry);
167 						return;
168 					}
169 				} else if (objmap->baseobj) {
170 					xmlNodePtr basep = dom_object_get_node(objmap->baseobj);
171 					if (basep) {
172 						xmlNodePtr nodep = basep;
173 						/* For now we're only able to use cache for forward search.
174 						 * TODO: in the future we could extend the logic of the node list such that backwards searches
175 						 *       are also possible. */
176 						bool restart = true;
177 						zend_long relative_index = index;
178 						if (index >= objmap->cached_obj_index && objmap->cached_obj && !php_dom_is_cache_tag_stale_from_node(&objmap->cache_tag, nodep)) {
179 							xmlNodePtr cached_obj_xml_node = dom_object_get_node(objmap->cached_obj);
180 
181 							/* The node cannot be NULL if the cache is valid. If it is NULL, then it means we
182 							 * forgot an invalidation somewhere. Take the defensive programming approach and invalidate
183 							 * it here if it's NULL (except in debug mode where we would want to catch this). */
184 							if (UNEXPECTED(cached_obj_xml_node == NULL)) {
185 #if ZEND_DEBUG
186 								ZEND_UNREACHABLE();
187 #endif
188 								reset_objmap_cache(objmap);
189 							} else {
190 								restart = false;
191 								relative_index -= objmap->cached_obj_index;
192 								nodep = cached_obj_xml_node;
193 							}
194 						}
195 						zend_long count = 0;
196 						if (objmap->nodetype == XML_ATTRIBUTE_NODE || objmap->nodetype == XML_ELEMENT_NODE) {
197 							if (restart) {
198 								nodep = dom_nodelist_iter_start_first_child(nodep);
199 							}
200 							while (count < relative_index && nodep != NULL) {
201 								count++;
202 								nodep = nodep->next;
203 							}
204 							itemnode = nodep;
205 						} else {
206 							if (restart) {
207 								if (basep->type == XML_DOCUMENT_NODE || basep->type == XML_HTML_DOCUMENT_NODE) {
208 									nodep = xmlDocGetRootElement((xmlDoc*) basep);
209 								} else {
210 									nodep = basep->children;
211 								}
212 							}
213 							itemnode = dom_get_elements_by_tag_name_ns_raw(basep, nodep, (char *) objmap->ns, (char *) objmap->local, &count, relative_index);
214 						}
215 						cache_itemnode = true;
216 					}
217 				}
218 			}
219 		}
220 
221 		if (itemnode) {
222 			DOM_RET_OBJ(itemnode, &ret, objmap->baseobj);
223 			if (cache_itemnode) {
224 				/* Hold additional reference for the cache, must happen before releasing the cache
225 				 * because we might be the last reference holder.
226 				 * Instead of storing and copying zvals, we store the object pointer directly.
227 				 * This saves us some bytes because a pointer is smaller than a zval.
228 				 * This also means we have to manually refcount the objects here, and remove the reference count
229 				 * in reset_objmap_cache() and the destructor. */
230 				dom_object *cached_obj = Z_DOMOBJ_P(return_value);
231 				GC_ADDREF(&cached_obj->std);
232 				/* If the tag is stale, all cached data is useless. Otherwise only the cached object is useless. */
233 				if (php_dom_is_cache_tag_stale_from_node(&objmap->cache_tag, itemnode)) {
234 					php_dom_mark_cache_tag_up_to_date_from_node(&objmap->cache_tag, itemnode);
235 					reset_objmap_cache(objmap);
236 				} else {
237 					objmap_cache_release_cached_obj(objmap);
238 				}
239 				objmap->cached_obj_index = index;
240 				objmap->cached_obj = cached_obj;
241 			}
242 			return;
243 		}
244 	}
245 
246 	RETVAL_NULL();
247 }
248 
249 /* {{{ URL: http://www.w3.org/TR/2003/WD-DOM-Level-3-Core-20030226/DOM3-Core.html#ID-844377136
250 Since:
251 */
PHP_METHOD(DOMNodeList,item)252 PHP_METHOD(DOMNodeList, item)
253 {
254 	zend_long index;
255 	ZEND_PARSE_PARAMETERS_START(1, 1)
256 		Z_PARAM_LONG(index)
257 	ZEND_PARSE_PARAMETERS_END();
258 
259 	zval *id = ZEND_THIS;
260 	dom_object *intern = Z_DOMOBJ_P(id);
261 	dom_nnodemap_object *objmap = intern->ptr;
262 	php_dom_nodelist_get_item_into_zval(objmap, index, return_value);
263 }
264 /* }}} end dom_nodelist_item */
265 
ZEND_METHOD(DOMNodeList,getIterator)266 ZEND_METHOD(DOMNodeList, getIterator)
267 {
268 	if (zend_parse_parameters_none() == FAILURE) {
269 		return;
270 	}
271 
272 	zend_create_internal_iterator_zval(return_value, ZEND_THIS);
273 }
274 
275 #endif
276