xref: /PHP-7.4/ext/dom/nodelist.c (revision 92ac598a)
1 /*
2    +----------------------------------------------------------------------+
3    | PHP Version 7                                                        |
4    +----------------------------------------------------------------------+
5    | Copyright (c) The PHP Group                                          |
6    +----------------------------------------------------------------------+
7    | This source file is subject to version 3.01 of the PHP license,      |
8    | that is bundled with this package in the file LICENSE, and is        |
9    | available through the world-wide-web at the following url:           |
10    | http://www.php.net/license/3_01.txt                                  |
11    | If you did not receive a copy of the PHP license and are unable to   |
12    | obtain it through the world-wide-web, please send a note to          |
13    | license@php.net so we can mail you a copy immediately.               |
14    +----------------------------------------------------------------------+
15    | Authors: Christian Stocker <chregu@php.net>                          |
16    |          Rob Richards <rrichards@php.net>                            |
17    +----------------------------------------------------------------------+
18 */
19 
20 #ifdef HAVE_CONFIG_H
21 #include "config.h"
22 #endif
23 
24 #include "php.h"
25 #if HAVE_LIBXML && HAVE_DOM
26 #include "php_dom.h"
27 
28 
29 /* {{{ arginfo */
30 ZEND_BEGIN_ARG_INFO_EX(arginfo_dom_nodelist_item, 0, 0, 1)
31 	ZEND_ARG_INFO(0, index)
32 ZEND_END_ARG_INFO();
33 /* }}} */
34 
35 /* {{{ arginfo */
36 ZEND_BEGIN_ARG_INFO_EX(arginfo_dom_nodelist_count, 0, 0, 0)
37 ZEND_END_ARG_INFO();
38 /* }}} */
39 
40 /*
41 * class DOMNodeList
42 *
43 * URL: https://www.w3.org/TR/2003/WD-DOM-Level-3-Core-20030226/DOM3-Core.html#ID-536297177
44 * Since:
45 */
46 
47 const zend_function_entry php_dom_nodelist_class_functions[] = {
48 	PHP_FALIAS(item, dom_nodelist_item, arginfo_dom_nodelist_item)
49 	PHP_FALIAS(count, dom_nodelist_count, arginfo_dom_nodelist_count)
50 	PHP_FE_END
51 };
52 
53 
54 /* {{{ length	int
55 readonly=yes
56 URL: http://www.w3.org/TR/2003/WD-DOM-Level-3-Core-20030226/DOM3-Core.html#ID-203510337
57 Since:
58 */
dom_nodelist_length_read(dom_object * obj,zval * retval)59 int dom_nodelist_length_read(dom_object *obj, zval *retval)
60 {
61 	dom_nnodemap_object *objmap;
62 	xmlNodePtr nodep, curnode;
63 	int count = 0;
64 	HashTable *nodeht;
65 
66 	objmap = (dom_nnodemap_object *)obj->ptr;
67 	if (objmap != NULL) {
68 		if (objmap->ht) {
69 			count = xmlHashSize(objmap->ht);
70 		} else {
71 			if (objmap->nodetype == DOM_NODESET) {
72 				nodeht = HASH_OF(&objmap->baseobj_zv);
73 				count = zend_hash_num_elements(nodeht);
74 			} else {
75 				nodep = dom_object_get_node(objmap->baseobj);
76 				if (nodep) {
77 					if (objmap->nodetype == XML_ATTRIBUTE_NODE || objmap->nodetype == XML_ELEMENT_NODE) {
78 						curnode = nodep->children;
79 						if (curnode) {
80 							count++;
81 							while (curnode->next != NULL) {
82 								count++;
83 								curnode = curnode->next;
84 							}
85 						}
86 					} else {
87 						if (nodep->type == XML_DOCUMENT_NODE || nodep->type == XML_HTML_DOCUMENT_NODE) {
88 							nodep = xmlDocGetRootElement((xmlDoc *) nodep);
89 						} else {
90 							nodep = nodep->children;
91 						}
92 						curnode = dom_get_elements_by_tag_name_ns_raw(
93 							nodep, (char *) objmap->ns, (char *) objmap->local, &count, -1);
94 					}
95 				}
96 			}
97 		}
98 	}
99 
100 	ZVAL_LONG(retval, count);
101 	return SUCCESS;
102 }
103 
104 
105 /* {{{ proto int|bool dom_nodelist_count();
106 */
PHP_FUNCTION(dom_nodelist_count)107 PHP_FUNCTION(dom_nodelist_count)
108 {
109 	zval *id;
110 	dom_object *intern;
111 
112 	id = ZEND_THIS;
113 	if (zend_parse_parameters_none() == FAILURE) {
114 		return;
115 	}
116 
117 	intern = Z_DOMOBJ_P(id);
118 	if(dom_nodelist_length_read(intern, return_value) == FAILURE) {
119 		RETURN_FALSE;
120 	}
121 }
122 /* }}} end dom_nodelist_count */
123 
124 /* }}} */
125 
126 /* {{{ proto DOMNode dom_nodelist_item(int index);
127 URL: http://www.w3.org/TR/2003/WD-DOM-Level-3-Core-20030226/DOM3-Core.html#ID-844377136
128 Since:
129 */
PHP_FUNCTION(dom_nodelist_item)130 PHP_FUNCTION(dom_nodelist_item)
131 {
132 	zval *id;
133 	zend_long index;
134 	int ret;
135 	dom_object *intern;
136 	xmlNodePtr itemnode = NULL;
137 
138 	dom_nnodemap_object *objmap;
139 	xmlNodePtr nodep, curnode;
140 	int count = 0;
141 
142 	id = ZEND_THIS;
143 	if (zend_parse_parameters(ZEND_NUM_ARGS(), "l", &index) == FAILURE) {
144 		return;
145 	}
146 
147 	if (index >= 0) {
148 		intern = Z_DOMOBJ_P(id);
149 
150 		objmap = (dom_nnodemap_object *)intern->ptr;
151 		if (objmap != NULL) {
152 			if (objmap->ht) {
153 				if (objmap->nodetype == XML_ENTITY_NODE) {
154 					itemnode = php_dom_libxml_hash_iter(objmap->ht, index);
155 				} else {
156 					itemnode = php_dom_libxml_notation_iter(objmap->ht, index);
157 				}
158 			} else {
159 				if (objmap->nodetype == DOM_NODESET) {
160 					HashTable *nodeht = HASH_OF(&objmap->baseobj_zv);
161 					zval *entry = zend_hash_index_find(nodeht, index);
162 					if (entry) {
163 						ZVAL_COPY(return_value, entry);
164 						return;
165 					}
166 				} else if (objmap->baseobj) {
167 					nodep = dom_object_get_node(objmap->baseobj);
168 					if (nodep) {
169 						if (objmap->nodetype == XML_ATTRIBUTE_NODE || objmap->nodetype == XML_ELEMENT_NODE) {
170 							curnode = nodep->children;
171 							while (count < index && curnode != NULL) {
172 								count++;
173 								curnode = curnode->next;
174 							}
175 							itemnode = curnode;
176 						} else {
177 							if (nodep->type == XML_DOCUMENT_NODE || nodep->type == XML_HTML_DOCUMENT_NODE) {
178 								nodep = xmlDocGetRootElement((xmlDoc *) nodep);
179 							} else {
180 								nodep = nodep->children;
181 							}
182 							itemnode = dom_get_elements_by_tag_name_ns_raw(nodep, (char *) objmap->ns, (char *) objmap->local, &count, index);
183 						}
184 					}
185 				}
186 			}
187 		}
188 
189 		if (itemnode) {
190 			DOM_RET_OBJ(itemnode, &ret, objmap->baseobj);
191 			return;
192 		}
193 	}
194 
195 	RETVAL_NULL();
196 }
197 /* }}} end dom_nodelist_item */
198 
199 
200 #endif
201