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 272374 2008-12-31 11:17: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 : static
33 : ZEND_BEGIN_ARG_INFO_EX(arginfo_dom_nodelist_item, 0, 0, 1)
34 : ZEND_ARG_INFO(0, index)
35 : ZEND_END_ARG_INFO();
36 : /* }}} */
37 :
38 : /*
39 : * class DOMNodeList
40 : *
41 : * URL: http://www.w3.org/TR/2003/WD-DOM-Level-3-Core-20030226/DOM3-Core.html#ID-536297177
42 : * Since:
43 : */
44 :
45 : zend_function_entry php_dom_nodelist_class_functions[] = {
46 : PHP_FALIAS(item, dom_nodelist_item, arginfo_dom_nodelist_item)
47 : {NULL, NULL, NULL}
48 : };
49 :
50 : /* {{{ length int
51 : readonly=yes
52 : URL: http://www.w3.org/TR/2003/WD-DOM-Level-3-Core-20030226/DOM3-Core.html#ID-203510337
53 : Since:
54 : */
55 : int dom_nodelist_length_read(dom_object *obj, zval **retval TSRMLS_DC)
56 62 : {
57 : dom_nnodemap_object *objmap;
58 : xmlNodePtr nodep, curnode;
59 62 : int count = 0;
60 : HashTable *nodeht;
61 :
62 62 : objmap = (dom_nnodemap_object *)obj->ptr;
63 62 : if (objmap != NULL) {
64 62 : if (objmap->ht) {
65 0 : count = xmlHashSize(objmap->ht);
66 : } else {
67 62 : if (objmap->nodetype == DOM_NODESET) {
68 6 : nodeht = HASH_OF(objmap->baseobjptr);
69 6 : count = zend_hash_num_elements(nodeht);
70 : } else {
71 56 : nodep = dom_object_get_node(objmap->baseobj);
72 56 : if (nodep) {
73 107 : if (objmap->nodetype == XML_ATTRIBUTE_NODE || objmap->nodetype == XML_ELEMENT_NODE) {
74 51 : curnode = nodep->children;
75 51 : if (curnode) {
76 51 : count++;
77 198 : while (curnode->next != NULL) {
78 96 : count++;
79 96 : curnode = curnode->next;
80 : }
81 : }
82 : } else {
83 8 : if (nodep->type == XML_DOCUMENT_NODE || nodep->type == XML_HTML_DOCUMENT_NODE) {
84 3 : nodep = xmlDocGetRootElement((xmlDoc *) nodep);
85 : } else {
86 2 : nodep = nodep->children;
87 : }
88 5 : curnode = dom_get_elements_by_tag_name_ns_raw(nodep, objmap->ns, objmap->local, &count, -1);
89 : }
90 : }
91 : }
92 : }
93 : }
94 :
95 62 : MAKE_STD_ZVAL(*retval);
96 62 : ZVAL_LONG(*retval, count);
97 62 : return SUCCESS;
98 : }
99 :
100 : /* }}} */
101 :
102 :
103 : /* {{{ proto DOMNode dom_nodelist_item(int index);
104 : URL: http://www.w3.org/TR/2003/WD-DOM-Level-3-Core-20030226/DOM3-Core.html#ID-844377136
105 : Since:
106 : */
107 : PHP_FUNCTION(dom_nodelist_item)
108 46 : {
109 46 : zval *id, *rv = NULL;
110 : long index;
111 : int ret;
112 : dom_object *intern;
113 46 : xmlNodePtr itemnode = NULL;
114 :
115 : dom_nnodemap_object *objmap;
116 : xmlNodePtr nodep, curnode;
117 46 : int count = 0;
118 : HashTable *nodeht;
119 : zval **entry;
120 :
121 46 : if (zend_parse_method_parameters(ZEND_NUM_ARGS() TSRMLS_CC, getThis(), "Ol", &id, dom_nodelist_class_entry, &index) == FAILURE) {
122 0 : return;
123 : }
124 :
125 46 : if (index >= 0) {
126 46 : intern = (dom_object *)zend_object_store_get_object(id TSRMLS_CC);
127 :
128 46 : objmap = (dom_nnodemap_object *)intern->ptr;
129 46 : if (objmap != NULL) {
130 46 : if (objmap->ht) {
131 0 : if (objmap->nodetype == XML_ENTITY_NODE) {
132 0 : itemnode = php_dom_libxml_hash_iter(objmap->ht, index);
133 : } else {
134 0 : itemnode = php_dom_libxml_notation_iter(objmap->ht, index);
135 : }
136 : } else {
137 46 : if (objmap->nodetype == DOM_NODESET) {
138 6 : nodeht = HASH_OF(objmap->baseobjptr);
139 6 : if (zend_hash_index_find(nodeht, index, (void **) &entry)==SUCCESS) {
140 6 : *return_value = **entry;
141 6 : zval_copy_ctor(return_value);
142 6 : return;
143 : }
144 40 : } else if (objmap->baseobj) {
145 39 : nodep = dom_object_get_node(objmap->baseobj);
146 39 : if (nodep) {
147 61 : if (objmap->nodetype == XML_ATTRIBUTE_NODE || objmap->nodetype == XML_ELEMENT_NODE) {
148 22 : curnode = nodep->children;
149 78 : while (count < index && curnode != NULL) {
150 34 : count++;
151 34 : curnode = curnode->next;
152 : }
153 22 : itemnode = curnode;
154 : } else {
155 26 : if (nodep->type == XML_DOCUMENT_NODE || nodep->type == XML_HTML_DOCUMENT_NODE) {
156 9 : nodep = xmlDocGetRootElement((xmlDoc *) nodep);
157 : } else {
158 8 : nodep = nodep->children;
159 : }
160 17 : itemnode = dom_get_elements_by_tag_name_ns_raw(nodep, objmap->ns, objmap->local, &count, index);
161 : }
162 : }
163 : }
164 : }
165 : }
166 :
167 40 : if (itemnode) {
168 39 : DOM_RET_OBJ(rv, itemnode, &ret, objmap->baseobj);
169 39 : return;
170 : }
171 : }
172 :
173 1 : RETVAL_NULL();
174 : }
175 : /* }}} end dom_nodelist_item */
176 : #endif
|