php-internal-docs 8.4.8
Unofficial docs for php/php-src
Loading...
Searching...
No Matches
nodelist.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 "nodelist.h"
26#include "zend_interfaces.h"
27
28/*
29* class DOMNodeList
30*
31* URL: https://www.w3.org/TR/2003/WD-DOM-Level-3-Core-20030226/DOM3-Core.html#ID-536297177
32* Since:
33*/
34
35static zend_always_inline void objmap_cache_release_cached_obj(dom_nnodemap_object *objmap)
36{
37 if (objmap->cached_obj) {
38 /* Since the DOM is a tree there can be no cycles. */
39 if (GC_DELREF(&objmap->cached_obj->std) == 0) {
41 }
42 objmap->cached_obj = NULL;
43 objmap->cached_obj_index = 0;
44 }
45}
46
47static zend_always_inline void reset_objmap_cache(dom_nnodemap_object *objmap)
48{
49 objmap_cache_release_cached_obj(objmap);
50 objmap->cached_length = -1;
51}
52
53xmlNodePtr dom_nodelist_iter_start_first_child(xmlNodePtr nodep)
54{
55 if (nodep->type == XML_ENTITY_REF_NODE) {
56 /* See entityreference.c */
58 }
59
60 return nodep->children;
61}
62
64{
66 if (!objmap) {
67 return 0;
68 }
69
70 if (objmap->ht) {
71 return xmlHashSize(objmap->ht);
72 }
73
74 if (objmap->nodetype == DOM_NODESET) {
75 HashTable *nodeht = HASH_OF(&objmap->baseobj_zv);
76 return zend_hash_num_elements(nodeht);
77 }
78
79 xmlNodePtr nodep = dom_object_get_node(objmap->baseobj);
80 if (!nodep) {
81 return 0;
82 }
83
84 if (!php_dom_is_cache_tag_stale_from_node(&objmap->cache_tag, nodep)) {
85 if (objmap->cached_length >= 0) {
86 return objmap->cached_length;
87 }
88 /* Only the length is out-of-date, the cache tag is still valid.
89 * Therefore, only overwrite the length and keep the currently cached object. */
90 } else {
91 php_dom_mark_cache_tag_up_to_date_from_node(&objmap->cache_tag, nodep);
92 reset_objmap_cache(objmap);
93 }
94
95 zend_long count = 0;
96 if (objmap->nodetype == XML_ATTRIBUTE_NODE || objmap->nodetype == XML_ELEMENT_NODE) {
97 xmlNodePtr curnode = dom_nodelist_iter_start_first_child(nodep);
98 if (curnode) {
99 count++;
100 while (curnode->next != NULL) {
101 count++;
102 curnode = curnode->next;
103 }
104 }
105 } else {
106 xmlNodePtr basep = nodep;
107 nodep = php_dom_first_child_of_container_node(basep);
109 basep, nodep, objmap->ns, objmap->local, objmap->local_lower, &count, ZEND_LONG_MAX - 1 /* because of <= */);
110 }
111
112 objmap->cached_length = count;
113
114 return count;
115}
116
117/* {{{ length int
118readonly=yes
119URL: http://www.w3.org/TR/2003/WD-DOM-Level-3-Core-20030226/DOM3-Core.html#ID-203510337
120Since:
121*/
123{
125 return SUCCESS;
126}
127/* }}} */
128
129/* {{{ */
131{
135}
136/* }}} end dom_nodelist_count */
137
139{
140 xmlNodePtr itemnode = NULL;
141 bool cache_itemnode = false;
142 if (index >= 0) {
143 if (objmap != NULL) {
144 if (objmap->ht) {
145 itemnode = php_dom_libxml_hash_iter(objmap, index);
146 } else {
147 if (objmap->nodetype == DOM_NODESET) {
148 HashTable *nodeht = HASH_OF(&objmap->baseobj_zv);
149 zval *entry = zend_hash_index_find(nodeht, index);
150 if (entry) {
151 ZVAL_COPY(return_value, entry);
152 return;
153 }
154 } else if (objmap->baseobj) {
155 xmlNodePtr basep = dom_object_get_node(objmap->baseobj);
156 if (basep) {
157 xmlNodePtr nodep = basep;
158 /* For now we're only able to use cache for forward search.
159 * TODO: in the future we could extend the logic of the node list such that backwards searches
160 * are also possible. */
161 bool restart = true;
162 zend_long relative_index = index;
163 if (index >= objmap->cached_obj_index && objmap->cached_obj && !php_dom_is_cache_tag_stale_from_node(&objmap->cache_tag, nodep)) {
164 xmlNodePtr cached_obj_xml_node = dom_object_get_node(objmap->cached_obj);
165
166 /* The node cannot be NULL if the cache is valid. If it is NULL, then it means we
167 * forgot an invalidation somewhere. Take the defensive programming approach and invalidate
168 * it here if it's NULL (except in debug mode where we would want to catch this). */
169 if (UNEXPECTED(cached_obj_xml_node == NULL)) {
170#if ZEND_DEBUG
172#endif
173 reset_objmap_cache(objmap);
174 } else {
175 restart = false;
176 relative_index -= objmap->cached_obj_index;
177 nodep = cached_obj_xml_node;
178 }
179 }
180 zend_long count = 0;
181 if (objmap->nodetype == XML_ATTRIBUTE_NODE || objmap->nodetype == XML_ELEMENT_NODE) {
182 if (restart) {
184 }
185 while (count < relative_index && nodep != NULL) {
186 count++;
187 nodep = nodep->next;
188 }
189 itemnode = nodep;
190 } else {
191 if (restart) {
192 nodep = php_dom_first_child_of_container_node(basep);
193 }
194 itemnode = dom_get_elements_by_tag_name_ns_raw(basep, nodep, objmap->ns, objmap->local, objmap->local_lower, &count, relative_index);
195 }
196 cache_itemnode = true;
197 }
198 }
199 }
200 }
201
202 if (itemnode) {
203 DOM_RET_OBJ(itemnode, objmap->baseobj);
204 if (cache_itemnode) {
205 /* Hold additional reference for the cache, must happen before releasing the cache
206 * because we might be the last reference holder.
207 * Instead of storing and copying zvals, we store the object pointer directly.
208 * This saves us some bytes because a pointer is smaller than a zval.
209 * This also means we have to manually refcount the objects here, and remove the reference count
210 * in reset_objmap_cache() and the destructor. */
211 dom_object *cached_obj = Z_DOMOBJ_P(return_value);
212 GC_ADDREF(&cached_obj->std);
213 /* If the tag is stale, all cached data is useless. Otherwise only the cached object is useless. */
214 if (php_dom_is_cache_tag_stale_from_node(&objmap->cache_tag, itemnode)) {
215 php_dom_mark_cache_tag_up_to_date_from_node(&objmap->cache_tag, itemnode);
216 reset_objmap_cache(objmap);
217 } else {
218 objmap_cache_release_cached_obj(objmap);
219 }
220 objmap->cached_obj_index = index;
221 objmap->cached_obj = cached_obj;
222 }
223 return;
224 }
225 }
226
227 RETVAL_NULL();
228}
229
230/* {{{ URL: http://www.w3.org/TR/2003/WD-DOM-Level-3-Core-20030226/DOM3-Core.html#ID-844377136
231Since:
232*/
234{
235 zend_long index;
237 Z_PARAM_LONG(index)
239
240 zval *id = ZEND_THIS;
241 dom_object *intern = Z_DOMOBJ_P(id);
242 dom_nnodemap_object *objmap = intern->ptr;
244}
245/* }}} end dom_nodelist_item */
246
247ZEND_METHOD(DOMNodeList, getIterator)
248{
251}
252
254{
256
258
259 if (Z_TYPE_P(offset) == IS_LONG) {
261 ret.lval = Z_LVAL_P(offset);
262 } else if (Z_TYPE_P(offset) == IS_DOUBLE) {
264 ret.lval = zend_dval_to_lval_safe(Z_DVAL_P(offset));
265 } else if (Z_TYPE_P(offset) == IS_STRING) {
266 zend_ulong lval;
267 if (ZEND_HANDLE_NUMERIC(Z_STR_P(offset), lval)) {
269 ret.lval = (zend_long) lval;
270 } else {
272 ret.str = Z_STR_P(offset);
273 }
274 } else {
276 }
277
278 return ret;
279}
280
282{
283 if (UNEXPECTED(!offset)) {
284 zend_throw_error(NULL, "Cannot append to %s", ZSTR_VAL(object->ce->name));
285 return NULL;
286 }
287
291 return NULL;
292 }
293
294 php_dom_nodelist_get_item_into_zval(php_dom_obj_from_obj(object)->ptr, index.lval, rv);
295 return rv;
296}
297
298int dom_modern_nodelist_has_dimension(zend_object *object, zval *member, int check_empty)
299{
300 /* If it exists, it cannot be empty because nodes aren't empty. */
301 ZEND_IGNORE_VALUE(check_empty);
302
306 return 0;
307 }
308
309 return index.lval >= 0 && index.lval < php_dom_get_nodelist_length(php_dom_obj_from_obj(object));
310}
311
312#endif
count(Countable|array $value, int $mode=COUNT_NORMAL)
zend_result dom_nodelist_length_read(dom_object *obj, zval *retval)
zend_ffi_type * type
Definition ffi.c:3812
void * ptr
Definition ffi.c:3814
zend_long offset
#define NULL
Definition gdcache.h:45
#define SUCCESS
Definition hash_sha3.c:261
zval * dom_modern_nodelist_read_dimension(zend_object *object, zval *offset, int type, zval *rv)
dom_nodelist_dimension_index dom_modern_nodelist_get_index(const zval *offset)
@ DOM_NODELIST_DIM_STRING
Definition nodelist.h:22
@ DOM_NODELIST_DIM_ILLEGAL
Definition nodelist.h:21
@ DOM_NODELIST_DIM_LONG
Definition nodelist.h:23
zend_long php_dom_get_nodelist_length(dom_object *obj)
void php_dom_nodelist_get_item_into_zval(dom_nnodemap_object *objmap, zend_long index, zval *return_value)
int dom_modern_nodelist_has_dimension(zend_object *object, zval *member, int check_empty)
#define PHP_METHOD
Definition php.h:365
xmlEntityPtr dom_entity_reference_fetch_and_sync_declaration(xmlNodePtr reference)
#define DOM_NODESET
Definition php_dom.h:65
xmlNode * php_dom_libxml_hash_iter(dom_nnodemap_object *objmap, int index)
xmlNode * dom_get_elements_by_tag_name_ns_raw(xmlNodePtr basep, xmlNodePtr nodep, xmlChar *ns, xmlChar *local, xmlChar *local_lower, zend_long *cur, zend_long index)
xmlNodePtr dom_nodelist_iter_start_first_child(xmlNodePtr nodep)
const XML_ELEMENT_NODE
const XML_ATTRIBUTE_NODE
const XML_ENTITY_REF_NODE
zval rv
Definition session.c:1024
void * ptr
Definition xml_common.h:26
zend_object std
Definition xml_common.h:29
zend_long cached_obj_index
Definition php_dom.h:90
dom_object * baseobj
Definition php_dom.h:81
xmlHashTable * ht
Definition php_dom.h:85
dom_object * cached_obj
Definition php_dom.h:89
php_libxml_cache_tag cache_tag
Definition php_dom.h:88
xmlChar * local_lower
Definition php_dom.h:86
xmlChar * local
Definition php_dom.h:86
enum dom_nodelist_dimension_index_type type
Definition nodelist.h:31
PHP_DOM_EXPORT xmlNodePtr dom_object_get_node(dom_object *obj)
struct _dom_object dom_object
#define DOM_RET_OBJ(obj, domobject)
Definition xml_common.h:76
#define Z_DOMOBJ_P(zv)
Definition xml_common.h:36
ZEND_API ZEND_COLD void zend_throw_error(zend_class_entry *exception_ce, const char *format,...)
Definition zend.c:1772
ZEND_API ZEND_COLD void zend_illegal_container_offset(const zend_string *container, const zval *offset, int type)
Definition zend.c:1802
#define ZEND_PARSE_PARAMETERS_END()
Definition zend_API.h:1641
#define ZEND_PARSE_PARAMETERS_NONE()
Definition zend_API.h:1623
#define ZEND_PARSE_PARAMETERS_START(min_num_args, max_num_args)
Definition zend_API.h:1620
#define Z_PARAM_LONG(dest)
Definition zend_API.h:1896
#define RETURN_LONG(l)
Definition zend_API.h:1037
#define RETVAL_NULL()
Definition zend_API.h:1010
#define ZEND_METHOD(classname, name)
Definition zend_API.h:76
#define HASH_OF(p)
Definition zend_API.h:1062
#define ZEND_THIS
Definition zend_API.h:523
struct _zval_struct zval
#define BP_VAR_IS
ZEND_API zval *ZEND_FASTCALL zend_hash_index_find(const HashTable *ht, zend_ulong h)
Definition zend_hash.c:2701
#define ZEND_HANDLE_NUMERIC(key, idx)
Definition zend_hash.h:420
ZEND_API zend_result zend_create_internal_iterator_zval(zval *return_value, zval *obj)
int32_t zend_long
Definition zend_long.h:42
uint32_t zend_ulong
Definition zend_long.h:43
#define ZEND_LONG_MAX
Definition zend_long.h:45
ZEND_API void ZEND_FASTCALL zend_objects_store_del(zend_object *object)
#define ZEND_IGNORE_VALUE(x)
#define zend_always_inline
#define ZEND_UNREACHABLE()
#define UNEXPECTED(condition)
struct _zend_object zend_object
#define ZSTR_VAL(zstr)
Definition zend_string.h:68
#define Z_TYPE_P(zval_p)
Definition zend_types.h:660
#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_DOUBLE
Definition zend_types.h:605
#define Z_STR_P(zval_p)
Definition zend_types.h:972
#define GC_DELREF(p)
Definition zend_types.h:710
#define GC_ADDREF(p)
Definition zend_types.h:709
#define IS_LONG
Definition zend_types.h:604
#define ZVAL_COPY(z, v)
ZEND_RESULT_CODE zend_result
Definition zend_types.h:64
#define Z_DVAL_P(zval_p)
Definition zend_types.h:969
#define Z_LVAL_P(zval_p)
Definition zend_types.h:966
zval retval
zval * return_value
object
zval * ret