php-internal-docs 8.4.8
Unofficial docs for php/php-src
Loading...
Searching...
No Matches
attr.c
Go to the documentation of this file.
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/* {{{ */
38PHP_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) {
48 }
49
50 intern = Z_DOMOBJ_P(ZEND_THIS);
51
52 name_valid = xmlValidateName(BAD_CAST name, 0);
53 if (name_valid != 0) {
56 }
57
58 nodep = xmlNewProp(NULL, BAD_CAST name, BAD_CAST value);
59
60 if (!nodep) {
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
75readonly=yes
76URL: http://www.w3.org/TR/2003/WD-DOM-Level-3-Core-20030226/DOM3-Core.html#ID-1112119403
77Modern spec URL: https://dom.spec.whatwg.org/#dom-attr-name
78Since:
79*/
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
97readonly=yes
98URL: http://www.w3.org/TR/2003/WD-DOM-Level-3-Core-20030226/DOM3-Core.html#ID-862529273
99Since:
100*/
102{
103 /* From spec: "useless; always returns true" */
105 return SUCCESS;
106}
107
108/* }}} */
109
110void 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
121readonly=no
122URL: http://www.w3.org/TR/2003/WD-DOM-Level-3-Core-20030226/DOM3-Core.html#ID-221662474
123Since:
124*/
126{
127 DOM_PROP_NODE(xmlNodePtr, attrp, obj);
129 return SUCCESS;
130}
131
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
157readonly=yes
158URL: http://www.w3.org/TR/2003/WD-DOM-Level-3-Core-20030226/DOM3-Core.html#Attr-ownerElement
159Since: DOM Level 2
160*/
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
174readonly=yes
175URL: http://www.w3.org/TR/2003/WD-DOM-Level-3-Core-20030226/DOM3-Core.html#Attr-schemaTypeInfo
176Since: DOM Level 3
177*/
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. */
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
189Since: DOM Level 3
190*/
191PHP_METHOD(DOMAttr, isId)
192{
193 dom_object *intern;
194 xmlAttrPtr attrp;
195
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
202bool 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
zend_result dom_attr_schema_type_info_read(dom_object *obj, zval *retval)
zend_result dom_attr_value_read(dom_object *obj, zval *retval)
zend_result dom_attr_name_read(dom_object *obj, zval *retval)
zend_result dom_attr_value_write(dom_object *obj, zval *newval)
#define DOM_PROP_NODE(type, name, obj)
zend_result dom_attr_owner_element_read(dom_object *obj, zval *retval)
zend_result dom_attr_specified_read(dom_object *obj, zval *retval)
void php_dom_throw_error(dom_exception_code error_code, bool strict_error)
@ INVALID_STATE_ERR
@ INVALID_CHARACTER_ERR
new_type attr
Definition ffi.c:4364
#define NULL
Definition gdcache.h:45
#define SUCCESS
Definition hash_sha3.c:261
#define PHP_METHOD
Definition php.h:365
void dom_attr_value_will_change(dom_object *obj, xmlAttrPtr attrp)
bool dom_compare_value(const xmlAttr *attr, const xmlChar *value)
#define DOM_GET_OBJ(__ptr, __id, __prtype, __intern)
Definition php_dom.h:237
bool php_dom_create_nullable_object(xmlNodePtr obj, zval *return_value, dom_object *domobj)
void php_dom_get_content_into_zval(const xmlNode *nodep, zval *target, bool default_is_null)
zend_string * dom_node_get_node_name_attribute_or_element(const xmlNode *nodep, bool uppercase)
void dom_remove_all_children(xmlNodePtr nodep)
const XML_ATTRIBUTE_ID
php_libxml_ref_obj * document
Definition xml_common.h:27
PHP_DOM_EXPORT xmlNodePtr dom_object_get_node(dom_object *obj)
struct _dom_object dom_object
#define Z_DOMOBJ_P(zv)
Definition xml_common.h:36
ZEND_API zend_result zend_parse_parameters(uint32_t num_args, const char *type_spec,...)
Definition zend_API.c:1300
#define ZEND_NUM_ARGS()
Definition zend_API.h:530
#define ZEND_PARSE_PARAMETERS_NONE()
Definition zend_API.h:1623
#define ZVAL_STRING(z, s)
Definition zend_API.h:956
#define RETURN_BOOL(b)
Definition zend_API.h:1035
#define RETURN_THROWS()
Definition zend_API.h:1060
#define ZEND_THIS
Definition zend_API.h:523
struct _zval_struct zval
struct _zend_string zend_string
#define ZEND_ASSERT(c)
#define ZSTR_VAL(zstr)
Definition zend_string.h:68
#define ZSTR_LEN(zstr)
Definition zend_string.h:69
#define Z_TYPE_P(zval_p)
Definition zend_types.h:660
#define ZVAL_TRUE(z)
#define ZVAL_NULL(z)
#define IS_STRING
Definition zend_types.h:606
#define Z_STR_P(zval_p)
Definition zend_types.h:972
@ FAILURE
Definition zend_types.h:61
#define ZVAL_NEW_STR(z, s)
ZEND_RESULT_CODE zend_result
Definition zend_types.h:64
zval retval
zend_string * name
bool result
value