xref: /PHP-5.4/ext/dom/nodelist.c (revision c0d060f5)
1 /*
2    +----------------------------------------------------------------------+
3    | PHP Version 5                                                        |
4    +----------------------------------------------------------------------+
5    | Copyright (c) 1997-2014 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 TSRMLS_DC)54 int dom_nodelist_length_read(dom_object *obj, zval **retval TSRMLS_DC)
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->baseobjptr);
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(nodep, objmap->ns, objmap->local, &count, -1);
88 					}
89 				}
90 			}
91 		}
92 	}
93 
94 	MAKE_STD_ZVAL(*retval);
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 	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 	HashTable *nodeht;
117 	zval **entry;
118 
119 	if (zend_parse_method_parameters(ZEND_NUM_ARGS() TSRMLS_CC, getThis(), "Ol", &id, dom_nodelist_class_entry, &index) == FAILURE) {
120 		return;
121 	}
122 
123 	if (index >= 0) {
124 		intern = (dom_object *)zend_object_store_get_object(id TSRMLS_CC);
125 
126 		objmap = (dom_nnodemap_object *)intern->ptr;
127 		if (objmap != NULL) {
128 			if (objmap->ht) {
129 				if (objmap->nodetype == XML_ENTITY_NODE) {
130 					itemnode = php_dom_libxml_hash_iter(objmap->ht, index);
131 				} else {
132 					itemnode = php_dom_libxml_notation_iter(objmap->ht, index);
133 				}
134 			} else {
135 				if (objmap->nodetype == DOM_NODESET) {
136 					nodeht = HASH_OF(objmap->baseobjptr);
137 					if (zend_hash_index_find(nodeht, index, (void **) &entry)==SUCCESS) {
138 						*return_value = **entry;
139 						zval_copy_ctor(return_value);
140 						return;
141 					}
142 				} else if (objmap->baseobj) {
143 					nodep = dom_object_get_node(objmap->baseobj);
144 					if (nodep) {
145 						if (objmap->nodetype == XML_ATTRIBUTE_NODE || objmap->nodetype == XML_ELEMENT_NODE) {
146 							curnode = nodep->children;
147 							while (count < index && curnode != NULL) {
148 								count++;
149 								curnode = curnode->next;
150 							}
151 							itemnode = curnode;
152 						} else {
153 							if (nodep->type == XML_DOCUMENT_NODE || nodep->type == XML_HTML_DOCUMENT_NODE) {
154 								nodep = xmlDocGetRootElement((xmlDoc *) nodep);
155 							} else {
156 								nodep = nodep->children;
157 							}
158 							itemnode = dom_get_elements_by_tag_name_ns_raw(nodep, objmap->ns, objmap->local, &count, index);
159 						}
160 					}
161 				}
162 			}
163 		}
164 
165 		if (itemnode) {
166 			DOM_RET_OBJ(itemnode, &ret, objmap->baseobj);
167 			return;
168 		}
169 	}
170 
171 	RETVAL_NULL();
172 }
173 /* }}} end dom_nodelist_item */
174 
175 #endif
176 
177 /*
178  * Local variables:
179  * tab-width: 4
180  * c-basic-offset: 4
181  * End:
182  * vim600: noet sw=4 ts=4 fdm=marker
183  * vim<600: noet sw=4 ts=4
184  */
185