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