xref: /PHP-7.0/ext/dom/nodelist.c (revision 478f119a)
1 /*
2    +----------------------------------------------------------------------+
3    | PHP Version 7                                                        |
4    +----------------------------------------------------------------------+
5    | Copyright (c) 1997-2017 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 /* $Id$ */
21 
22 #ifdef HAVE_CONFIG_H
23 #include "config.h"
24 #endif
25 
26 #include "php.h"
27 #if HAVE_LIBXML && HAVE_DOM
28 #include "php_dom.h"
29 
30 
31 /* {{{ arginfo */
32 ZEND_BEGIN_ARG_INFO_EX(arginfo_dom_nodelist_item, 0, 0, 1)
33 	ZEND_ARG_INFO(0, index)
34 ZEND_END_ARG_INFO();
35 /* }}} */
36 
37 /*
38 * class DOMNodeList
39 *
40 * URL: http://www.w3.org/TR/2003/WD-DOM-Level-3-Core-20030226/DOM3-Core.html#ID-536297177
41 * Since:
42 */
43 
44 const zend_function_entry php_dom_nodelist_class_functions[] = {
45 	PHP_FALIAS(item, dom_nodelist_item, arginfo_dom_nodelist_item)
46 	PHP_FE_END
47 };
48 
49 /* {{{ length	int
50 readonly=yes
51 URL: http://www.w3.org/TR/2003/WD-DOM-Level-3-Core-20030226/DOM3-Core.html#ID-203510337
52 Since:
53 */
dom_nodelist_length_read(dom_object * obj,zval * retval)54 int dom_nodelist_length_read(dom_object *obj, zval *retval)
55 {
56 	dom_nnodemap_object *objmap;
57 	xmlNodePtr nodep, curnode;
58 	int count = 0;
59 	HashTable *nodeht;
60 
61 	objmap = (dom_nnodemap_object *)obj->ptr;
62 	if (objmap != NULL) {
63 		if (objmap->ht) {
64 			count = xmlHashSize(objmap->ht);
65 		} else {
66 			if (objmap->nodetype == DOM_NODESET) {
67 				nodeht = HASH_OF(&objmap->baseobj_zv);
68 				count = zend_hash_num_elements(nodeht);
69 			} else {
70 				nodep = dom_object_get_node(objmap->baseobj);
71 				if (nodep) {
72 					if (objmap->nodetype == XML_ATTRIBUTE_NODE || objmap->nodetype == XML_ELEMENT_NODE) {
73 						curnode = nodep->children;
74 						if (curnode) {
75 							count++;
76 							while (curnode->next != NULL) {
77 								count++;
78 								curnode = curnode->next;
79 							}
80 						}
81 					} else {
82 						if (nodep->type == XML_DOCUMENT_NODE || nodep->type == XML_HTML_DOCUMENT_NODE) {
83 							nodep = xmlDocGetRootElement((xmlDoc *) nodep);
84 						} else {
85 							nodep = nodep->children;
86 						}
87 						curnode = dom_get_elements_by_tag_name_ns_raw(
88 							nodep, (char *) objmap->ns, (char *) objmap->local, &count, -1);
89 					}
90 				}
91 			}
92 		}
93 	}
94 
95 	ZVAL_LONG(retval, count);
96 	return SUCCESS;
97 }
98 
99 /* }}} */
100 
101 /* {{{ proto DOMNode dom_nodelist_item(int index);
102 URL: http://www.w3.org/TR/2003/WD-DOM-Level-3-Core-20030226/DOM3-Core.html#ID-844377136
103 Since:
104 */
PHP_FUNCTION(dom_nodelist_item)105 PHP_FUNCTION(dom_nodelist_item)
106 {
107 	zval *id;
108 	zend_long index;
109 	int ret;
110 	dom_object *intern;
111 	xmlNodePtr itemnode = NULL;
112 
113 	dom_nnodemap_object *objmap;
114 	xmlNodePtr nodep, curnode;
115 	int count = 0;
116 
117 	if (zend_parse_method_parameters(ZEND_NUM_ARGS(), getThis(), "Ol", &id, dom_nodelist_class_entry, &index) == FAILURE) {
118 		return;
119 	}
120 
121 	if (index >= 0) {
122 		intern = Z_DOMOBJ_P(id);
123 
124 		objmap = (dom_nnodemap_object *)intern->ptr;
125 		if (objmap != NULL) {
126 			if (objmap->ht) {
127 				if (objmap->nodetype == XML_ENTITY_NODE) {
128 					itemnode = php_dom_libxml_hash_iter(objmap->ht, index);
129 				} else {
130 					itemnode = php_dom_libxml_notation_iter(objmap->ht, index);
131 				}
132 			} else {
133 				if (objmap->nodetype == DOM_NODESET) {
134 					HashTable *nodeht = HASH_OF(&objmap->baseobj_zv);
135 					zval *entry = zend_hash_index_find(nodeht, index);
136 					if (entry) {
137 						ZVAL_COPY(return_value, entry);
138 						return;
139 					}
140 				} else if (objmap->baseobj) {
141 					nodep = dom_object_get_node(objmap->baseobj);
142 					if (nodep) {
143 						if (objmap->nodetype == XML_ATTRIBUTE_NODE || objmap->nodetype == XML_ELEMENT_NODE) {
144 							curnode = nodep->children;
145 							while (count < index && curnode != NULL) {
146 								count++;
147 								curnode = curnode->next;
148 							}
149 							itemnode = curnode;
150 						} else {
151 							if (nodep->type == XML_DOCUMENT_NODE || nodep->type == XML_HTML_DOCUMENT_NODE) {
152 								nodep = xmlDocGetRootElement((xmlDoc *) nodep);
153 							} else {
154 								nodep = nodep->children;
155 							}
156 							itemnode = dom_get_elements_by_tag_name_ns_raw(nodep, (char *) objmap->ns, (char *) objmap->local, &count, index);
157 						}
158 					}
159 				}
160 			}
161 		}
162 
163 		if (itemnode) {
164 			DOM_RET_OBJ(itemnode, &ret, objmap->baseobj);
165 			return;
166 		}
167 	}
168 
169 	RETVAL_NULL();
170 }
171 /* }}} end dom_nodelist_item */
172 
173 #endif
174 
175 /*
176  * Local variables:
177  * tab-width: 4
178  * c-basic-offset: 4
179  * End:
180  * vim600: noet sw=4 ts=4 fdm=marker
181  * vim<600: noet sw=4 ts=4
182  */
183