xref: /PHP-8.1/ext/dom/attr.c (revision 08c4db7f)
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    | https://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 
24 #if defined(HAVE_LIBXML) && defined(HAVE_DOM)
25 
26 #include "php_dom.h"
27 
28 /*
29 * class DOMAttr extends DOMNode
30 *
31 * URL: https://www.w3.org/TR/2003/WD-DOM-Level-3-Core-20030226/DOM3-Core.html#ID-637646024
32 * Since:
33 */
34 
35 /* {{{ */
PHP_METHOD(DOMAttr,__construct)36 PHP_METHOD(DOMAttr, __construct)
37 {
38 	xmlAttrPtr nodep = NULL;
39 	xmlNodePtr oldnode = NULL;
40 	dom_object *intern;
41 	char *name, *value = NULL;
42 	size_t name_len, value_len, name_valid;
43 
44 	if (zend_parse_parameters(ZEND_NUM_ARGS(), "s|s", &name, &name_len, &value, &value_len) == FAILURE) {
45 		RETURN_THROWS();
46 	}
47 
48 	intern = Z_DOMOBJ_P(ZEND_THIS);
49 
50 	name_valid = xmlValidateName((xmlChar *) name, 0);
51 	if (name_valid != 0) {
52 		php_dom_throw_error(INVALID_CHARACTER_ERR, 1);
53 		RETURN_THROWS();
54 	}
55 
56 	nodep = xmlNewProp(NULL, (xmlChar *) name, (xmlChar *) value);
57 
58 	if (!nodep) {
59 		php_dom_throw_error(INVALID_STATE_ERR, 1);
60 		RETURN_THROWS();
61 	}
62 
63 	oldnode = dom_object_get_node(intern);
64 	if (oldnode != NULL) {
65 		php_libxml_node_decrement_resource((php_libxml_node_object *)intern);
66 	}
67 	php_libxml_increment_node_ptr((php_libxml_node_object *)intern, (xmlNodePtr)nodep, (void *)intern);
68 }
69 
70 /* }}} end DOMAttr::__construct */
71 
72 /* {{{ name	string
73 readonly=yes
74 URL: http://www.w3.org/TR/2003/WD-DOM-Level-3-Core-20030226/DOM3-Core.html#ID-1112119403
75 Since:
76 */
dom_attr_name_read(dom_object * obj,zval * retval)77 int dom_attr_name_read(dom_object *obj, zval *retval)
78 {
79 	xmlAttrPtr attrp;
80 
81 	attrp = (xmlAttrPtr) dom_object_get_node(obj);
82 
83 	if (attrp == NULL) {
84 		php_dom_throw_error(INVALID_STATE_ERR, 1);
85 		return FAILURE;
86 	}
87 
88 	ZVAL_STRING(retval, (char *) attrp->name);
89 
90 	return SUCCESS;
91 }
92 
93 /* }}} */
94 
95 /* {{{ specified	boolean
96 readonly=yes
97 URL: http://www.w3.org/TR/2003/WD-DOM-Level-3-Core-20030226/DOM3-Core.html#ID-862529273
98 Since:
99 */
dom_attr_specified_read(dom_object * obj,zval * retval)100 int dom_attr_specified_read(dom_object *obj, zval *retval)
101 {
102 	/* TODO */
103 	ZVAL_TRUE(retval);
104 	return SUCCESS;
105 }
106 
107 /* }}} */
108 
109 /* {{{ value	string
110 readonly=no
111 URL: http://www.w3.org/TR/2003/WD-DOM-Level-3-Core-20030226/DOM3-Core.html#ID-221662474
112 Since:
113 */
dom_attr_value_read(dom_object * obj,zval * retval)114 int dom_attr_value_read(dom_object *obj, zval *retval)
115 {
116 	xmlAttrPtr attrp = (xmlAttrPtr) dom_object_get_node(obj);
117 	xmlChar *content;
118 
119 	if (attrp == NULL) {
120 		php_dom_throw_error(INVALID_STATE_ERR, 1);
121 		return FAILURE;
122 	}
123 
124 	if ((content = xmlNodeGetContent((xmlNodePtr) attrp)) != NULL) {
125 		ZVAL_STRING(retval, (char *) content);
126 		xmlFree(content);
127 	} else {
128 		ZVAL_EMPTY_STRING(retval);
129 	}
130 
131 	return SUCCESS;
132 
133 }
134 
dom_attr_value_write(dom_object * obj,zval * newval)135 int dom_attr_value_write(dom_object *obj, zval *newval)
136 {
137 	zend_string *str;
138 	xmlAttrPtr attrp = (xmlAttrPtr) dom_object_get_node(obj);
139 
140 	if (attrp == NULL) {
141 		php_dom_throw_error(INVALID_STATE_ERR, 1);
142 		return FAILURE;
143 	}
144 
145 	str = zval_try_get_string(newval);
146 	if (UNEXPECTED(!str)) {
147 		return FAILURE;
148 	}
149 
150 	if (attrp->children) {
151 		node_list_unlink(attrp->children);
152 	}
153 
154 	xmlNodeSetContentLen((xmlNodePtr) attrp, (xmlChar *) ZSTR_VAL(str), ZSTR_LEN(str) + 1);
155 
156 	zend_string_release_ex(str, 0);
157 	return SUCCESS;
158 }
159 
160 /* }}} */
161 
162 /* {{{ ownerElement	DOMElement
163 readonly=yes
164 URL: http://www.w3.org/TR/2003/WD-DOM-Level-3-Core-20030226/DOM3-Core.html#Attr-ownerElement
165 Since: DOM Level 2
166 */
dom_attr_owner_element_read(dom_object * obj,zval * retval)167 int dom_attr_owner_element_read(dom_object *obj, zval *retval)
168 {
169 	xmlNodePtr nodep, nodeparent;
170 
171 	nodep = dom_object_get_node(obj);
172 
173 	if (nodep == NULL) {
174 		php_dom_throw_error(INVALID_STATE_ERR, 1);
175 		return FAILURE;
176 	}
177 
178 	nodeparent = nodep->parent;
179 	if (!nodeparent) {
180 		ZVAL_NULL(retval);
181 		return SUCCESS;
182 	}
183 
184 	php_dom_create_object(nodeparent, retval, obj);
185 	return SUCCESS;
186 
187 }
188 
189 /* }}} */
190 
191 /* {{{ schemaTypeInfo	DOMTypeInfo
192 readonly=yes
193 URL: http://www.w3.org/TR/2003/WD-DOM-Level-3-Core-20030226/DOM3-Core.html#Attr-schemaTypeInfo
194 Since: DOM Level 3
195 */
dom_attr_schema_type_info_read(dom_object * obj,zval * retval)196 int dom_attr_schema_type_info_read(dom_object *obj, zval *retval)
197 {
198 	/* TODO */
199 	ZVAL_NULL(retval);
200 	return SUCCESS;
201 }
202 
203 /* }}} */
204 
205 /* {{{ URL: http://www.w3.org/TR/2003/WD-DOM-Level-3-Core-20030226/DOM3-Core.html#Attr-isId
206 Since: DOM Level 3
207 */
PHP_METHOD(DOMAttr,isId)208 PHP_METHOD(DOMAttr, isId)
209 {
210 	zval *id;
211 	dom_object *intern;
212 	xmlAttrPtr attrp;
213 
214 	id = ZEND_THIS;
215 	if (zend_parse_parameters_none() == FAILURE) {
216 		RETURN_THROWS();
217 	}
218 
219 	DOM_GET_OBJ(attrp, id, xmlAttrPtr, intern);
220 
221 	if (attrp->atype == XML_ATTRIBUTE_ID) {
222 		RETURN_TRUE;
223 	} else {
224 		RETURN_FALSE;
225 	}
226 }
227 /* }}} end dom_attr_is_id */
228 
229 #endif
230