xref: /PHP-7.1/ext/dom/attr.c (revision ccd4716e)
1 /*
2    +----------------------------------------------------------------------+
3    | PHP Version 7                                                        |
4    +----------------------------------------------------------------------+
5    | Copyright (c) 1997-2018 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 
28 #if HAVE_LIBXML && HAVE_DOM
29 
30 #include "php_dom.h"
31 
32 /* {{{ arginfo */
33 ZEND_BEGIN_ARG_INFO_EX(arginfo_dom_attr_is_id, 0, 0, 0)
34 ZEND_END_ARG_INFO();
35 
36 ZEND_BEGIN_ARG_INFO_EX(arginfo_dom_attr_construct, 0, 0, 1)
37 	ZEND_ARG_INFO(0, name)
38 	ZEND_ARG_INFO(0, value)
39 ZEND_END_ARG_INFO();
40 /* }}} */
41 
42 /*
43 * class DOMAttr extends DOMNode
44 *
45 * URL: http://www.w3.org/TR/2003/WD-DOM-Level-3-Core-20030226/DOM3-Core.html#ID-637646024
46 * Since:
47 */
48 
49 const zend_function_entry php_dom_attr_class_functions[] = {
50 	PHP_FALIAS(isId, dom_attr_is_id, arginfo_dom_attr_is_id)
51 	PHP_ME(domattr, __construct, arginfo_dom_attr_construct, ZEND_ACC_PUBLIC)
52 	PHP_FE_END
53 };
54 
55 /* {{{ proto void DOMAttr::__construct(string name, [string value]) */
PHP_METHOD(domattr,__construct)56 PHP_METHOD(domattr, __construct)
57 {
58 	zval *id = getThis();
59 	xmlAttrPtr nodep = NULL;
60 	xmlNodePtr oldnode = NULL;
61 	dom_object *intern;
62 	char *name, *value = NULL;
63 	size_t name_len, value_len, name_valid;
64 
65 	if (zend_parse_parameters_throw(ZEND_NUM_ARGS(), "s|s", &name, &name_len, &value, &value_len) == FAILURE) {
66 		return;
67 	}
68 
69 	intern = Z_DOMOBJ_P(id);
70 
71 	name_valid = xmlValidateName((xmlChar *) name, 0);
72 	if (name_valid != 0) {
73 		php_dom_throw_error(INVALID_CHARACTER_ERR, 1);
74 		RETURN_FALSE;
75 	}
76 
77 	nodep = xmlNewProp(NULL, (xmlChar *) name, (xmlChar *) value);
78 
79 	if (!nodep) {
80 		php_dom_throw_error(INVALID_STATE_ERR, 1);
81 		RETURN_FALSE;
82 	}
83 
84 	oldnode = dom_object_get_node(intern);
85 	if (oldnode != NULL) {
86 		php_libxml_node_free_resource(oldnode );
87 	}
88 	php_libxml_increment_node_ptr((php_libxml_node_object *)intern, (xmlNodePtr)nodep, (void *)intern);
89 }
90 
91 /* }}} end DOMAttr::__construct */
92 
93 /* {{{ name	string
94 readonly=yes
95 URL: http://www.w3.org/TR/2003/WD-DOM-Level-3-Core-20030226/DOM3-Core.html#ID-1112119403
96 Since:
97 */
dom_attr_name_read(dom_object * obj,zval * retval)98 int dom_attr_name_read(dom_object *obj, zval *retval)
99 {
100 	xmlAttrPtr attrp;
101 
102 	attrp = (xmlAttrPtr) dom_object_get_node(obj);
103 
104 	if (attrp == NULL) {
105 		php_dom_throw_error(INVALID_STATE_ERR, 0);
106 		return FAILURE;
107 	}
108 
109 	ZVAL_STRING(retval, (char *) attrp->name);
110 
111 	return SUCCESS;
112 }
113 
114 /* }}} */
115 
116 /* {{{ specified	boolean
117 readonly=yes
118 URL: http://www.w3.org/TR/2003/WD-DOM-Level-3-Core-20030226/DOM3-Core.html#ID-862529273
119 Since:
120 */
dom_attr_specified_read(dom_object * obj,zval * retval)121 int dom_attr_specified_read(dom_object *obj, zval *retval)
122 {
123 	/* TODO */
124 	ZVAL_TRUE(retval);
125 	return SUCCESS;
126 }
127 
128 /* }}} */
129 
130 /* {{{ value	string
131 readonly=no
132 URL: http://www.w3.org/TR/2003/WD-DOM-Level-3-Core-20030226/DOM3-Core.html#ID-221662474
133 Since:
134 */
dom_attr_value_read(dom_object * obj,zval * retval)135 int dom_attr_value_read(dom_object *obj, zval *retval)
136 {
137 	xmlAttrPtr attrp = (xmlAttrPtr) dom_object_get_node(obj);
138 	xmlChar *content;
139 
140 	if (attrp == NULL) {
141 		php_dom_throw_error(INVALID_STATE_ERR, 0);
142 		return FAILURE;
143 	}
144 
145 	if ((content = xmlNodeGetContent((xmlNodePtr) attrp)) != NULL) {
146 		ZVAL_STRING(retval, (char *) content);
147 		xmlFree(content);
148 	} else {
149 		ZVAL_EMPTY_STRING(retval);
150 	}
151 
152 	return SUCCESS;
153 
154 }
155 
dom_attr_value_write(dom_object * obj,zval * newval)156 int dom_attr_value_write(dom_object *obj, zval *newval)
157 {
158 	zend_string *str;
159 	xmlAttrPtr attrp = (xmlAttrPtr) dom_object_get_node(obj);
160 
161 	if (attrp == NULL) {
162 		php_dom_throw_error(INVALID_STATE_ERR, 0);
163 		return FAILURE;
164 	}
165 
166 	if (attrp->children) {
167 		node_list_unlink(attrp->children);
168 	}
169 
170 	str = zval_get_string(newval);
171 
172 	xmlNodeSetContentLen((xmlNodePtr) attrp, (xmlChar *) ZSTR_VAL(str), ZSTR_LEN(str) + 1);
173 
174 	zend_string_release(str);
175 	return SUCCESS;
176 }
177 
178 /* }}} */
179 
180 /* {{{ ownerElement	DOMElement
181 readonly=yes
182 URL: http://www.w3.org/TR/2003/WD-DOM-Level-3-Core-20030226/DOM3-Core.html#Attr-ownerElement
183 Since: DOM Level 2
184 */
dom_attr_owner_element_read(dom_object * obj,zval * retval)185 int dom_attr_owner_element_read(dom_object *obj, zval *retval)
186 {
187 	xmlNodePtr nodep, nodeparent;
188 
189 	nodep = dom_object_get_node(obj);
190 
191 	if (nodep == NULL) {
192 		php_dom_throw_error(INVALID_STATE_ERR, 0);
193 		return FAILURE;
194 	}
195 
196 	nodeparent = nodep->parent;
197 	if (!nodeparent) {
198 		ZVAL_NULL(retval);
199 		return SUCCESS;
200 	}
201 
202 	php_dom_create_object(nodeparent, retval, obj);
203 	return SUCCESS;
204 
205 }
206 
207 /* }}} */
208 
209 /* {{{ schemaTypeInfo	DOMTypeInfo
210 readonly=yes
211 URL: http://www.w3.org/TR/2003/WD-DOM-Level-3-Core-20030226/DOM3-Core.html#Attr-schemaTypeInfo
212 Since: DOM Level 3
213 */
dom_attr_schema_type_info_read(dom_object * obj,zval * retval)214 int dom_attr_schema_type_info_read(dom_object *obj, zval *retval)
215 {
216 	/* TODO */
217 	ZVAL_NULL(retval);
218 	return SUCCESS;
219 }
220 
221 /* }}} */
222 
223 /* {{{ proto boolean dom_attr_is_id()
224 URL: http://www.w3.org/TR/2003/WD-DOM-Level-3-Core-20030226/DOM3-Core.html#Attr-isId
225 Since: DOM Level 3
226 */
PHP_FUNCTION(dom_attr_is_id)227 PHP_FUNCTION(dom_attr_is_id)
228 {
229 	zval *id;
230 	dom_object *intern;
231 	xmlAttrPtr attrp;
232 
233 	if (zend_parse_method_parameters(ZEND_NUM_ARGS(), getThis(), "O", &id, dom_attr_class_entry) == FAILURE) {
234 		return;
235 	}
236 
237 	DOM_GET_OBJ(attrp, id, xmlAttrPtr, intern);
238 
239 	if (attrp->atype == XML_ATTRIBUTE_ID) {
240 		RETURN_TRUE;
241 	} else {
242 		RETURN_FALSE;
243 	}
244 }
245 /* }}} end dom_attr_is_id */
246 
247 #endif
248 
249 /*
250  * Local variables:
251  * tab-width: 4
252  * c-basic-offset: 4
253  * End:
254  * vim600: noet sw=4 ts=4 fdm=marker
255  * vim<600: noet sw=4 ts=4
256  */
257