xref: /PHP-7.4/ext/dom/attr.c (revision 457392fa)
1 /*
2    +----------------------------------------------------------------------+
3    | PHP Version 7                                                        |
4    +----------------------------------------------------------------------+
5    | Copyright (c) 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 #ifdef HAVE_CONFIG_H
21 #include "config.h"
22 #endif
23 
24 #include "php.h"
25 
26 #if HAVE_LIBXML && HAVE_DOM
27 
28 #include "php_dom.h"
29 
30 /* {{{ arginfo */
31 ZEND_BEGIN_ARG_INFO_EX(arginfo_dom_attr_is_id, 0, 0, 0)
32 ZEND_END_ARG_INFO();
33 
34 ZEND_BEGIN_ARG_INFO_EX(arginfo_dom_attr_construct, 0, 0, 1)
35 	ZEND_ARG_INFO(0, name)
36 	ZEND_ARG_INFO(0, value)
37 ZEND_END_ARG_INFO();
38 /* }}} */
39 
40 /*
41 * class DOMAttr extends DOMNode
42 *
43 * URL: https://www.w3.org/TR/2003/WD-DOM-Level-3-Core-20030226/DOM3-Core.html#ID-637646024
44 * Since:
45 */
46 
47 const zend_function_entry php_dom_attr_class_functions[] = {
48 	PHP_FALIAS(isId, dom_attr_is_id, arginfo_dom_attr_is_id)
49 	PHP_ME(domattr, __construct, arginfo_dom_attr_construct, ZEND_ACC_PUBLIC)
50 	PHP_FE_END
51 };
52 
53 /* {{{ proto DOMAttr::__construct(string name, [string value]) */
PHP_METHOD(domattr,__construct)54 PHP_METHOD(domattr, __construct)
55 {
56 	xmlAttrPtr nodep = NULL;
57 	xmlNodePtr oldnode = NULL;
58 	dom_object *intern;
59 	char *name, *value = NULL;
60 	size_t name_len, value_len, name_valid;
61 
62 	if (zend_parse_parameters_throw(ZEND_NUM_ARGS(), "s|s", &name, &name_len, &value, &value_len) == FAILURE) {
63 		return;
64 	}
65 
66 	intern = Z_DOMOBJ_P(ZEND_THIS);
67 
68 	name_valid = xmlValidateName((xmlChar *) name, 0);
69 	if (name_valid != 0) {
70 		php_dom_throw_error(INVALID_CHARACTER_ERR, 1);
71 		RETURN_FALSE;
72 	}
73 
74 	nodep = xmlNewProp(NULL, (xmlChar *) name, (xmlChar *) value);
75 
76 	if (!nodep) {
77 		php_dom_throw_error(INVALID_STATE_ERR, 1);
78 		RETURN_FALSE;
79 	}
80 
81 	oldnode = dom_object_get_node(intern);
82 	if (oldnode != NULL) {
83 		php_libxml_node_free_resource(oldnode );
84 	}
85 	php_libxml_increment_node_ptr((php_libxml_node_object *)intern, (xmlNodePtr)nodep, (void *)intern);
86 }
87 
88 /* }}} end DOMAttr::__construct */
89 
90 /* {{{ name	string
91 readonly=yes
92 URL: http://www.w3.org/TR/2003/WD-DOM-Level-3-Core-20030226/DOM3-Core.html#ID-1112119403
93 Since:
94 */
dom_attr_name_read(dom_object * obj,zval * retval)95 int dom_attr_name_read(dom_object *obj, zval *retval)
96 {
97 	xmlAttrPtr attrp;
98 
99 	attrp = (xmlAttrPtr) dom_object_get_node(obj);
100 
101 	if (attrp == NULL) {
102 		php_dom_throw_error(INVALID_STATE_ERR, 0);
103 		return FAILURE;
104 	}
105 
106 	ZVAL_STRING(retval, (char *) attrp->name);
107 
108 	return SUCCESS;
109 }
110 
111 /* }}} */
112 
113 /* {{{ specified	boolean
114 readonly=yes
115 URL: http://www.w3.org/TR/2003/WD-DOM-Level-3-Core-20030226/DOM3-Core.html#ID-862529273
116 Since:
117 */
dom_attr_specified_read(dom_object * obj,zval * retval)118 int dom_attr_specified_read(dom_object *obj, zval *retval)
119 {
120 	/* TODO */
121 	ZVAL_TRUE(retval);
122 	return SUCCESS;
123 }
124 
125 /* }}} */
126 
127 /* {{{ value	string
128 readonly=no
129 URL: http://www.w3.org/TR/2003/WD-DOM-Level-3-Core-20030226/DOM3-Core.html#ID-221662474
130 Since:
131 */
dom_attr_value_read(dom_object * obj,zval * retval)132 int dom_attr_value_read(dom_object *obj, zval *retval)
133 {
134 	xmlAttrPtr attrp = (xmlAttrPtr) dom_object_get_node(obj);
135 	xmlChar *content;
136 
137 	if (attrp == NULL) {
138 		php_dom_throw_error(INVALID_STATE_ERR, 0);
139 		return FAILURE;
140 	}
141 
142 	if ((content = xmlNodeGetContent((xmlNodePtr) attrp)) != NULL) {
143 		ZVAL_STRING(retval, (char *) content);
144 		xmlFree(content);
145 	} else {
146 		ZVAL_EMPTY_STRING(retval);
147 	}
148 
149 	return SUCCESS;
150 
151 }
152 
dom_attr_value_write(dom_object * obj,zval * newval)153 int dom_attr_value_write(dom_object *obj, zval *newval)
154 {
155 	zend_string *str;
156 	xmlAttrPtr attrp = (xmlAttrPtr) dom_object_get_node(obj);
157 
158 	if (attrp == NULL) {
159 		php_dom_throw_error(INVALID_STATE_ERR, 0);
160 		return FAILURE;
161 	}
162 
163 	str = zval_try_get_string(newval);
164 	if (UNEXPECTED(!str)) {
165 		return FAILURE;
166 	}
167 
168 	if (attrp->children) {
169 		node_list_unlink(attrp->children);
170 	}
171 
172 	xmlNodeSetContentLen((xmlNodePtr) attrp, (xmlChar *) ZSTR_VAL(str), ZSTR_LEN(str) + 1);
173 
174 	zend_string_release_ex(str, 0);
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 bool 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 	id = ZEND_THIS;
234 	if (zend_parse_parameters_none() == FAILURE) {
235 		return;
236 	}
237 
238 	DOM_GET_OBJ(attrp, id, xmlAttrPtr, intern);
239 
240 	if (attrp->atype == XML_ATTRIBUTE_ID) {
241 		RETURN_TRUE;
242 	} else {
243 		RETURN_FALSE;
244 	}
245 }
246 /* }}} end dom_attr_is_id */
247 
248 #endif
249