1 : /*
2 : +----------------------------------------------------------------------+
3 : | PHP Version 5 |
4 : +----------------------------------------------------------------------+
5 : | Copyright (c) 1997-2009 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: nodelist.c 272370 2008-12-31 11:15:49Z sebastian $ */
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 : {NULL, NULL, NULL}
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 : */
54 : int dom_nodelist_length_read(dom_object *obj, zval **retval TSRMLS_DC)
55 62 : {
56 : dom_nnodemap_object *objmap;
57 : xmlNodePtr nodep, curnode;
58 62 : int count = 0;
59 : HashTable *nodeht;
60 :
61 62 : objmap = (dom_nnodemap_object *)obj->ptr;
62 62 : if (objmap != NULL) {
63 62 : if (objmap->ht) {
64 0 : count = xmlHashSize(objmap->ht);
65 : } else {
66 62 : if (objmap->nodetype == DOM_NODESET) {
67 6 : nodeht = HASH_OF(objmap->baseobjptr);
68 6 : count = zend_hash_num_elements(nodeht);
69 : } else {
70 56 : nodep = dom_object_get_node(objmap->baseobj);
71 56 : if (nodep) {
72 107 : if (objmap->nodetype == XML_ATTRIBUTE_NODE || objmap->nodetype == XML_ELEMENT_NODE) {
73 51 : curnode = nodep->children;
74 51 : if (curnode) {
75 51 : count++;
76 198 : while (curnode->next != NULL) {
77 96 : count++;
78 96 : curnode = curnode->next;
79 : }
80 : }
81 : } else {
82 8 : if (nodep->type == XML_DOCUMENT_NODE || nodep->type == XML_HTML_DOCUMENT_NODE) {
83 3 : nodep = xmlDocGetRootElement((xmlDoc *) nodep);
84 : } else {
85 2 : nodep = nodep->children;
86 : }
87 5 : curnode = dom_get_elements_by_tag_name_ns_raw(nodep, objmap->ns, objmap->local, &count, -1);
88 : }
89 : }
90 : }
91 : }
92 : }
93 :
94 62 : MAKE_STD_ZVAL(*retval);
95 62 : ZVAL_LONG(*retval, count);
96 62 : 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 : */
105 : PHP_FUNCTION(dom_nodelist_item)
106 47 : {
107 47 : zval *id, *rv = NULL;
108 : long index;
109 : int ret;
110 : dom_object *intern;
111 47 : xmlNodePtr itemnode = NULL;
112 :
113 : dom_nnodemap_object *objmap;
114 : xmlNodePtr nodep, curnode;
115 47 : int count = 0;
116 : HashTable *nodeht;
117 : zval **entry;
118 :
119 47 : if (zend_parse_method_parameters(ZEND_NUM_ARGS() TSRMLS_CC, getThis(), "Ol", &id, dom_nodelist_class_entry, &index) == FAILURE) {
120 0 : return;
121 : }
122 :
123 47 : if (index >= 0) {
124 47 : intern = (dom_object *)zend_object_store_get_object(id TSRMLS_CC);
125 :
126 47 : objmap = (dom_nnodemap_object *)intern->ptr;
127 47 : if (objmap != NULL) {
128 47 : if (objmap->ht) {
129 0 : if (objmap->nodetype == XML_ENTITY_NODE) {
130 0 : itemnode = php_dom_libxml_hash_iter(objmap->ht, index);
131 : } else {
132 0 : itemnode = php_dom_libxml_notation_iter(objmap->ht, index);
133 : }
134 : } else {
135 47 : if (objmap->nodetype == DOM_NODESET) {
136 7 : nodeht = HASH_OF(objmap->baseobjptr);
137 7 : if (zend_hash_index_find(nodeht, index, (void **) &entry)==SUCCESS) {
138 7 : *return_value = **entry;
139 7 : zval_copy_ctor(return_value);
140 7 : return;
141 : }
142 40 : } else if (objmap->baseobj) {
143 39 : nodep = dom_object_get_node(objmap->baseobj);
144 39 : if (nodep) {
145 61 : if (objmap->nodetype == XML_ATTRIBUTE_NODE || objmap->nodetype == XML_ELEMENT_NODE) {
146 22 : curnode = nodep->children;
147 78 : while (count < index && curnode != NULL) {
148 34 : count++;
149 34 : curnode = curnode->next;
150 : }
151 22 : itemnode = curnode;
152 : } else {
153 26 : if (nodep->type == XML_DOCUMENT_NODE || nodep->type == XML_HTML_DOCUMENT_NODE) {
154 9 : nodep = xmlDocGetRootElement((xmlDoc *) nodep);
155 : } else {
156 8 : nodep = nodep->children;
157 : }
158 17 : itemnode = dom_get_elements_by_tag_name_ns_raw(nodep, objmap->ns, objmap->local, &count, index);
159 : }
160 : }
161 : }
162 : }
163 : }
164 :
165 40 : if (itemnode) {
166 39 : DOM_RET_OBJ(rv, itemnode, &ret, objmap->baseobj);
167 39 : return;
168 : }
169 : }
170 :
171 1 : 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 : */
|