xref: /php-src/ext/dom/processinginstruction.c (revision 11accb5c)
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 #if defined(HAVE_LIBXML) && defined(HAVE_DOM)
24 #include "php_dom.h"
25 #include "dom_properties.h"
26 
27 /*
28 * class DOMProcessingInstruction extends DOMNode
29 *
30 * URL: https://www.w3.org/TR/2003/WD-DOM-Level-3-Core-20030226/DOM3-Core.html#ID-1004215813
31 * Since:
32 */
33 
34 /* {{{ */
PHP_METHOD(DOMProcessingInstruction,__construct)35 PHP_METHOD(DOMProcessingInstruction, __construct)
36 {
37 	xmlNodePtr nodep = NULL, oldnode = NULL;
38 	dom_object *intern;
39 	char *name, *value = NULL;
40 	size_t name_len, value_len;
41 	int name_valid;
42 
43 	if (zend_parse_parameters(ZEND_NUM_ARGS(), "s|s", &name, &name_len, &value, &value_len) == FAILURE) {
44 		RETURN_THROWS();
45 	}
46 
47 	name_valid = xmlValidateName(BAD_CAST name, 0);
48 	if (name_valid != 0) {
49 		php_dom_throw_error(INVALID_CHARACTER_ERR, true);
50 		RETURN_THROWS();
51 	}
52 
53 	nodep = xmlNewPI(BAD_CAST name, BAD_CAST value);
54 
55 	if (!nodep) {
56 		php_dom_throw_error(INVALID_STATE_ERR, true);
57 		RETURN_THROWS();
58 	}
59 
60 	intern = Z_DOMOBJ_P(ZEND_THIS);
61 	oldnode = dom_object_get_node(intern);
62 	if (oldnode != NULL) {
63 		php_libxml_node_decrement_resource((php_libxml_node_object *)intern);
64 	}
65 	php_libxml_increment_node_ptr((php_libxml_node_object *)intern, nodep, (void *)intern);
66 }
67 /* }}} end DOMProcessingInstruction::__construct */
68 
69 /* {{{ target	string
70 readonly=yes
71 URL: http://www.w3.org/TR/2003/WD-DOM-Level-3-Core-20030226/DOM3-Core.html#ID-1478689192
72 Since:
73 */
dom_processinginstruction_target_read(dom_object * obj,zval * retval)74 zend_result dom_processinginstruction_target_read(dom_object *obj, zval *retval)
75 {
76 	DOM_PROP_NODE(xmlNodePtr, nodep, obj);
77 	ZVAL_STRING(retval, (const char *) nodep->name);
78 	return SUCCESS;
79 }
80 
81 /* }}} */
82 
83 /* {{{ data	string
84 readonly=no
85 URL: http://www.w3.org/TR/2003/WD-DOM-Level-3-Core-20030226/DOM3-Core.html#ID-837822393
86 Since:
87 */
dom_processinginstruction_data_read(dom_object * obj,zval * retval)88 zend_result dom_processinginstruction_data_read(dom_object *obj, zval *retval)
89 {
90 	DOM_PROP_NODE(xmlNodePtr, nodep, obj);
91 	php_dom_get_content_into_zval(nodep, retval, false);
92 	return SUCCESS;
93 }
94 
dom_processinginstruction_data_write(dom_object * obj,zval * newval)95 zend_result dom_processinginstruction_data_write(dom_object *obj, zval *newval)
96 {
97 	DOM_PROP_NODE(xmlNodePtr, nodep, obj);
98 
99 	/* Typed property, this is already a string */
100 	ZEND_ASSERT(Z_TYPE_P(newval) == IS_STRING);
101 	zend_string *str = Z_STR_P(newval);
102 
103 	xmlNodeSetContentLen(nodep, BAD_CAST ZSTR_VAL(str), ZSTR_LEN(str));
104 
105 	return SUCCESS;
106 }
107 
108 /* }}} */
109 
110 #endif
111