xref: /PHP-8.0/ext/dom/namednodemap.c (revision 8fef83dd)
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    | http://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 DOMNamedNodeMap
29 *
30 * URL: https://www.w3.org/TR/2003/WD-DOM-Level-3-Core-20030226/DOM3-Core.html#core-ID-1780488922
31 * Since:
32 */
33 
34 /* {{{ length	int
35 readonly=yes
36 URL: http://www.w3.org/TR/2003/WD-DOM-Level-3-Core-20030226/DOM3-Core.html#core-ID-6D0FB19E
37 Since:
38 */
dom_namednodemap_length_read(dom_object * obj,zval * retval)39 int dom_namednodemap_length_read(dom_object *obj, zval *retval)
40 {
41 	dom_nnodemap_object *objmap;
42 	xmlAttrPtr curnode;
43 	xmlNodePtr nodep;
44 	int count = 0;
45 
46 	objmap = (dom_nnodemap_object *)obj->ptr;
47 
48 	if (objmap != NULL) {
49 		if ((objmap->nodetype == XML_NOTATION_NODE) ||
50 			objmap->nodetype == XML_ENTITY_NODE) {
51 			if (objmap->ht) {
52 				count = xmlHashSize(objmap->ht);
53 			}
54 		} else {
55 			nodep = dom_object_get_node(objmap->baseobj);
56 			if (nodep) {
57 				curnode = nodep->properties;
58 				if (curnode) {
59 					count++;
60 					while (curnode->next != NULL) {
61 						count++;
62 						curnode = curnode->next;
63 					}
64 				}
65 			}
66 		}
67 	}
68 
69 	ZVAL_LONG(retval, count);
70 	return SUCCESS;
71 }
72 
73 /* }}} */
74 
75 /* {{{ URL: http://www.w3.org/TR/2003/WD-DOM-Level-3-Core-20030226/DOM3-Core.html#core-ID-1074577549
76 Since:
77 */
PHP_METHOD(DOMNamedNodeMap,getNamedItem)78 PHP_METHOD(DOMNamedNodeMap, getNamedItem)
79 {
80 	zval *id;
81 	int ret;
82 	size_t namedlen=0;
83 	dom_object *intern;
84 	xmlNodePtr itemnode = NULL;
85 	char *named;
86 
87 	dom_nnodemap_object *objmap;
88 	xmlNodePtr nodep;
89 	xmlNotation *notep = NULL;
90 
91 	id = ZEND_THIS;
92 	if (zend_parse_parameters(ZEND_NUM_ARGS(), "s", &named, &namedlen) == FAILURE) {
93 		RETURN_THROWS();
94 	}
95 
96 	intern = Z_DOMOBJ_P(id);
97 
98 	objmap = (dom_nnodemap_object *)intern->ptr;
99 
100 	if (objmap != NULL) {
101 		if ((objmap->nodetype == XML_NOTATION_NODE) ||
102 			objmap->nodetype == XML_ENTITY_NODE) {
103 			if (objmap->ht) {
104 				if (objmap->nodetype == XML_ENTITY_NODE) {
105 					itemnode = (xmlNodePtr)xmlHashLookup(objmap->ht, (xmlChar *) named);
106 				} else {
107 					notep = (xmlNotation *)xmlHashLookup(objmap->ht, (xmlChar *) named);
108 					if (notep) {
109 						itemnode = create_notation(notep->name, notep->PublicID, notep->SystemID);
110 					}
111 				}
112 			}
113 		} else {
114 			nodep = dom_object_get_node(objmap->baseobj);
115 			if (nodep) {
116 				itemnode = (xmlNodePtr)xmlHasProp(nodep, (xmlChar *) named);
117 			}
118 		}
119 	}
120 
121 	if (itemnode) {
122 		DOM_RET_OBJ(itemnode, &ret, objmap->baseobj);
123 		return;
124 	} else {
125 		RETVAL_NULL();
126 	}
127 }
128 /* }}} end dom_namednodemap_get_named_item */
129 
130 /* {{{ URL: http://www.w3.org/TR/2003/WD-DOM-Level-3-Core-20030226/DOM3-Core.html#core-ID-349467F9
131 Since:
132 */
PHP_METHOD(DOMNamedNodeMap,item)133 PHP_METHOD(DOMNamedNodeMap, item)
134 {
135 	zval *id;
136 	zend_long index;
137 	int ret;
138 	dom_object *intern;
139 	xmlNodePtr itemnode = NULL;
140 
141 	dom_nnodemap_object *objmap;
142 	xmlNodePtr nodep, curnode;
143 	int count;
144 
145 	id = ZEND_THIS;
146 	if (zend_parse_parameters(ZEND_NUM_ARGS(), "l", &index) == FAILURE) {
147 		RETURN_THROWS();
148 	}
149 	if (index < 0 || ZEND_LONG_INT_OVFL(index)) {
150 		zend_argument_value_error(1, "must be between 0 and %d", INT_MAX);
151 		RETURN_THROWS();
152 	}
153 
154 	intern = Z_DOMOBJ_P(id);
155 
156 	objmap = (dom_nnodemap_object *)intern->ptr;
157 
158 	if (objmap != NULL) {
159 		if ((objmap->nodetype == XML_NOTATION_NODE) ||
160 			objmap->nodetype == XML_ENTITY_NODE) {
161 			if (objmap->ht) {
162 				if (objmap->nodetype == XML_ENTITY_NODE) {
163 					itemnode = php_dom_libxml_hash_iter(objmap->ht, index);
164 				} else {
165 					itemnode = php_dom_libxml_notation_iter(objmap->ht, index);
166 				}
167 			}
168 		} else {
169 			nodep = dom_object_get_node(objmap->baseobj);
170 			if (nodep) {
171 				curnode = (xmlNodePtr)nodep->properties;
172 				count = 0;
173 				while (count < index && curnode != NULL) {
174 					count++;
175 					curnode = (xmlNodePtr)curnode->next;
176 				}
177 				itemnode = curnode;
178 			}
179 		}
180 	}
181 
182 	if (itemnode) {
183 		DOM_RET_OBJ(itemnode, &ret, objmap->baseobj);
184 		return;
185 	}
186 
187 	RETVAL_NULL();
188 }
189 /* }}} end dom_namednodemap_item */
190 
191 /* {{{ URL: http://www.w3.org/TR/2003/WD-DOM-Level-3-Core-20030226/DOM3-Core.html#core-ID-getNamedItemNS
192 Since: DOM Level 2
193 */
PHP_METHOD(DOMNamedNodeMap,getNamedItemNS)194 PHP_METHOD(DOMNamedNodeMap, getNamedItemNS)
195 {
196 	zval *id;
197 	int ret;
198 	size_t namedlen=0, urilen=0;
199 	dom_object *intern;
200 	xmlNodePtr itemnode = NULL;
201 	char *uri, *named;
202 
203 	dom_nnodemap_object *objmap;
204 	xmlNodePtr nodep;
205 	xmlNotation *notep = NULL;
206 
207 	id = ZEND_THIS;
208 	if (zend_parse_parameters(ZEND_NUM_ARGS(), "s!s", &uri, &urilen, &named, &namedlen) == FAILURE) {
209 		RETURN_THROWS();
210 	}
211 
212 	intern = Z_DOMOBJ_P(id);
213 
214 	objmap = (dom_nnodemap_object *)intern->ptr;
215 
216 	if (objmap != NULL) {
217 		if ((objmap->nodetype == XML_NOTATION_NODE) ||
218 			objmap->nodetype == XML_ENTITY_NODE) {
219 			if (objmap->ht) {
220 				if (objmap->nodetype == XML_ENTITY_NODE) {
221 					itemnode = (xmlNodePtr)xmlHashLookup(objmap->ht, (xmlChar *) named);
222 				} else {
223 					notep = (xmlNotation *)xmlHashLookup(objmap->ht, (xmlChar *) named);
224 					if (notep) {
225 						itemnode = create_notation(notep->name, notep->PublicID, notep->SystemID);
226 					}
227 				}
228 			}
229 		} else {
230 			nodep = dom_object_get_node(objmap->baseobj);
231 			if (nodep) {
232 				itemnode = (xmlNodePtr)xmlHasNsProp(nodep, (xmlChar *) named, (xmlChar *) uri);
233 			}
234 		}
235 	}
236 
237 	if (itemnode) {
238 		DOM_RET_OBJ(itemnode, &ret, objmap->baseobj);
239 		return;
240 	} else {
241 		RETVAL_NULL();
242 	}
243 }
244 /* }}} end dom_namednodemap_get_named_item_ns */
245 
246 /* {{{ */
PHP_METHOD(DOMNamedNodeMap,count)247 PHP_METHOD(DOMNamedNodeMap, count)
248 {
249 	zval *id;
250 	dom_object *intern;
251 
252 	id = ZEND_THIS;
253 	if (zend_parse_parameters_none() == FAILURE) {
254 		RETURN_THROWS();
255 	}
256 
257 	intern = Z_DOMOBJ_P(id);
258 	if(dom_namednodemap_length_read(intern, return_value) == FAILURE) {
259 		RETURN_FALSE;
260 	}
261 }
262 /* }}} end dom_namednodemap_count */
263 
PHP_METHOD(DOMNamedNodeMap,getIterator)264 PHP_METHOD(DOMNamedNodeMap, getIterator)
265 {
266 	if (zend_parse_parameters_none() == FAILURE) {
267 		return;
268 	}
269 
270 	zend_create_internal_iterator_zval(return_value, ZEND_THIS);
271 }
272 
273 #endif
274