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