xref: /php-src/ext/dom/attr.c (revision cf0a44a3)
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 #include "dom_properties.h"
28 #include "internal_helpers.h"
29 
30 /*
31 * class DOMAttr extends DOMNode
32 *
33 * URL: https://www.w3.org/TR/2003/WD-DOM-Level-3-Core-20030226/DOM3-Core.html#ID-637646024
34 * Since:
35 */
36 
37 /* {{{ */
PHP_METHOD(DOMAttr,__construct)38 PHP_METHOD(DOMAttr, __construct)
39 {
40 	xmlAttrPtr nodep = NULL;
41 	xmlNodePtr oldnode = NULL;
42 	dom_object *intern;
43 	char *name, *value = NULL;
44 	size_t name_len, value_len, name_valid;
45 
46 	if (zend_parse_parameters(ZEND_NUM_ARGS(), "s|s", &name, &name_len, &value, &value_len) == FAILURE) {
47 		RETURN_THROWS();
48 	}
49 
50 	intern = Z_DOMOBJ_P(ZEND_THIS);
51 
52 	name_valid = xmlValidateName(BAD_CAST name, 0);
53 	if (name_valid != 0) {
54 		php_dom_throw_error(INVALID_CHARACTER_ERR, true);
55 		RETURN_THROWS();
56 	}
57 
58 	nodep = xmlNewProp(NULL, BAD_CAST name, BAD_CAST value);
59 
60 	if (!nodep) {
61 		php_dom_throw_error(INVALID_STATE_ERR, true);
62 		RETURN_THROWS();
63 	}
64 
65 	oldnode = dom_object_get_node(intern);
66 	if (oldnode != NULL) {
67 		php_libxml_node_decrement_resource((php_libxml_node_object *)intern);
68 	}
69 	php_libxml_increment_node_ptr((php_libxml_node_object *)intern, (xmlNodePtr)nodep, (void *)intern);
70 }
71 
72 /* }}} end DOMAttr::__construct */
73 
74 /* {{{ name	string
75 readonly=yes
76 URL: http://www.w3.org/TR/2003/WD-DOM-Level-3-Core-20030226/DOM3-Core.html#ID-1112119403
77 Modern spec URL: https://dom.spec.whatwg.org/#dom-attr-name
78 Since:
79 */
dom_attr_name_read(dom_object * obj,zval * retval)80 zend_result dom_attr_name_read(dom_object *obj, zval *retval)
81 {
82 	DOM_PROP_NODE(xmlAttrPtr, attrp, obj);
83 
84 	if (php_dom_follow_spec_intern(obj)) {
85 		zend_string *str = dom_node_get_node_name_attribute_or_element((xmlNodePtr) attrp, false);
86 		ZVAL_NEW_STR(retval, str);
87 	} else {
88 		ZVAL_STRING(retval, (char *) attrp->name);
89 	}
90 
91 	return SUCCESS;
92 }
93 
94 /* }}} */
95 
96 /* {{{ specified	boolean
97 readonly=yes
98 URL: http://www.w3.org/TR/2003/WD-DOM-Level-3-Core-20030226/DOM3-Core.html#ID-862529273
99 Since:
100 */
dom_attr_specified_read(dom_object * obj,zval * retval)101 zend_result dom_attr_specified_read(dom_object *obj, zval *retval)
102 {
103 	/* From spec: "useless; always returns true" */
104 	ZVAL_TRUE(retval);
105 	return SUCCESS;
106 }
107 
108 /* }}} */
109 
dom_attr_value_will_change(dom_object * obj,xmlAttrPtr attrp)110 void dom_attr_value_will_change(dom_object *obj, xmlAttrPtr attrp)
111 {
112 	if (attrp->atype == XML_ATTRIBUTE_ID) {
113 		xmlRemoveID(attrp->doc, attrp);
114 		attrp->atype = XML_ATTRIBUTE_ID;
115 	}
116 
117 	dom_mark_ids_modified(obj->document);
118 }
119 
120 /* {{{ value	string
121 readonly=no
122 URL: http://www.w3.org/TR/2003/WD-DOM-Level-3-Core-20030226/DOM3-Core.html#ID-221662474
123 Since:
124 */
dom_attr_value_read(dom_object * obj,zval * retval)125 zend_result dom_attr_value_read(dom_object *obj, zval *retval)
126 {
127 	DOM_PROP_NODE(xmlNodePtr, attrp, obj);
128 	php_dom_get_content_into_zval(attrp, retval, false);
129 	return SUCCESS;
130 }
131 
dom_attr_value_write(dom_object * obj,zval * newval)132 zend_result dom_attr_value_write(dom_object *obj, zval *newval)
133 {
134 	DOM_PROP_NODE(xmlAttrPtr, attrp, obj);
135 
136 	dom_attr_value_will_change(obj, attrp);
137 
138 	/* Typed property, this is already a string */
139 	ZEND_ASSERT(Z_TYPE_P(newval) == IS_STRING);
140 	zend_string *str = Z_STR_P(newval);
141 
142 	dom_remove_all_children((xmlNodePtr) attrp);
143 
144 	if (php_dom_follow_spec_intern(obj)) {
145 		xmlNodePtr node = xmlNewDocTextLen(attrp->doc, BAD_CAST ZSTR_VAL(str), ZSTR_LEN(str));
146 		xmlAddChild((xmlNodePtr) attrp, node);
147 	} else {
148 		xmlNodeSetContentLen((xmlNodePtr) attrp, BAD_CAST ZSTR_VAL(str), ZSTR_LEN(str));
149 	}
150 
151 	return SUCCESS;
152 }
153 
154 /* }}} */
155 
156 /* {{{ ownerElement	DOMElement
157 readonly=yes
158 URL: http://www.w3.org/TR/2003/WD-DOM-Level-3-Core-20030226/DOM3-Core.html#Attr-ownerElement
159 Since: DOM Level 2
160 */
dom_attr_owner_element_read(dom_object * obj,zval * retval)161 zend_result dom_attr_owner_element_read(dom_object *obj, zval *retval)
162 {
163 	DOM_PROP_NODE(xmlNodePtr, nodep, obj);
164 
165 	xmlNodePtr nodeparent = nodep->parent;
166 
167 	php_dom_create_nullable_object(nodeparent, retval, obj);
168 	return SUCCESS;
169 }
170 
171 /* }}} */
172 
173 /* {{{ schemaTypeInfo	DOMTypeInfo
174 readonly=yes
175 URL: http://www.w3.org/TR/2003/WD-DOM-Level-3-Core-20030226/DOM3-Core.html#Attr-schemaTypeInfo
176 Since: DOM Level 3
177 */
dom_attr_schema_type_info_read(dom_object * obj,zval * retval)178 zend_result dom_attr_schema_type_info_read(dom_object *obj, zval *retval)
179 {
180 	/* This was never implemented, and is dropped from the modern-day DOM spec.
181 	 * It only exists in old DOM to preserve BC. */
182 	ZVAL_NULL(retval);
183 	return SUCCESS;
184 }
185 
186 /* }}} */
187 
188 /* {{{ URL: http://www.w3.org/TR/2003/WD-DOM-Level-3-Core-20030226/DOM3-Core.html#Attr-isId
189 Since: DOM Level 3
190 */
PHP_METHOD(DOMAttr,isId)191 PHP_METHOD(DOMAttr, isId)
192 {
193 	dom_object *intern;
194 	xmlAttrPtr attrp;
195 
196 	ZEND_PARSE_PARAMETERS_NONE();
197 	DOM_GET_OBJ(attrp, ZEND_THIS, xmlAttrPtr, intern);
198 	RETURN_BOOL(attrp->atype == XML_ATTRIBUTE_ID);
199 }
200 /* }}} end dom_attr_is_id */
201 
dom_compare_value(const xmlAttr * attr,const xmlChar * value)202 bool dom_compare_value(const xmlAttr *attr, const xmlChar *value)
203 {
204 	bool free;
205 	xmlChar *attr_value = php_libxml_attr_value(attr, &free);
206 	bool result = xmlStrEqual(attr_value, value);
207 	if (free) {
208 		xmlFree(attr_value);
209 	}
210 	return result;
211 }
212 
213 #endif
214