xref: /PHP-8.1/ext/dom/text.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 #if defined(HAVE_LIBXML) && defined(HAVE_DOM)
24 #include "php_dom.h"
25 #include "dom_ce.h"
26 
27 /*
28 * class DOMText extends DOMCharacterData
29 *
30 * URL: https://www.w3.org/TR/2003/WD-DOM-Level-3-Core-20030226/DOM3-Core.html#ID-1312295772
31 * Since:
32 */
33 
34 /* {{{ */
PHP_METHOD(DOMText,__construct)35 PHP_METHOD(DOMText, __construct)
36 {
37 	xmlNodePtr nodep = NULL, oldnode = NULL;
38 	dom_object *intern;
39 	char *value = NULL;
40 	size_t value_len;
41 
42 	if (zend_parse_parameters(ZEND_NUM_ARGS(), "|s", &value, &value_len) == FAILURE) {
43 		RETURN_THROWS();
44 	}
45 
46 	nodep = xmlNewText((xmlChar *) value);
47 
48 	if (!nodep) {
49 		php_dom_throw_error(INVALID_STATE_ERR, 1);
50 		RETURN_THROWS();
51 	}
52 
53 	intern = Z_DOMOBJ_P(ZEND_THIS);
54 	oldnode = dom_object_get_node(intern);
55 	if (oldnode != NULL) {
56 		php_libxml_node_decrement_resource((php_libxml_node_object *)intern);
57 	}
58 	php_libxml_increment_node_ptr((php_libxml_node_object *)intern, nodep, (void *)intern);
59 }
60 /* }}} end DOMText::__construct */
61 
62 /* {{{ wholeText	string
63 readonly=yes
64 URL: http://www.w3.org/TR/2003/WD-DOM-Level-3-Core-20030226/DOM3-Core.html#core-Text3-wholeText
65 Since: DOM Level 3
66 */
dom_text_whole_text_read(dom_object * obj,zval * retval)67 int dom_text_whole_text_read(dom_object *obj, zval *retval)
68 {
69 	xmlNodePtr node;
70 	xmlChar *wholetext = NULL;
71 
72 	node = dom_object_get_node(obj);
73 
74 	if (node == NULL) {
75 		php_dom_throw_error(INVALID_STATE_ERR, 1);
76 		return FAILURE;
77 	}
78 
79 	/* Find starting text node */
80 	while (node->prev && ((node->prev->type == XML_TEXT_NODE) || (node->prev->type == XML_CDATA_SECTION_NODE))) {
81 		node = node->prev;
82 	}
83 
84 	/* concatenate all adjacent text and cdata nodes */
85 	while (node && ((node->type == XML_TEXT_NODE) || (node->type == XML_CDATA_SECTION_NODE))) {
86 		wholetext = xmlStrcat(wholetext, node->content);
87 		node = node->next;
88 	}
89 
90 	if (wholetext != NULL) {
91 		ZVAL_STRING(retval, (char *) wholetext);
92 		xmlFree(wholetext);
93 	} else {
94 		ZVAL_EMPTY_STRING(retval);
95 	}
96 
97 	return SUCCESS;
98 }
99 
100 /* }}} */
101 
102 /* {{{ URL: http://www.w3.org/TR/2003/WD-DOM-Level-3-Core-20030226/DOM3-Core.html#core-ID-38853C1D
103 Since:
104 */
PHP_METHOD(DOMText,splitText)105 PHP_METHOD(DOMText, splitText)
106 {
107 	zval       *id;
108 	xmlChar    *cur;
109 	xmlChar    *first;
110 	xmlChar    *second;
111 	xmlNodePtr  node;
112 	xmlNodePtr  nnode;
113 	zend_long        offset;
114 	int         length;
115 	dom_object	*intern;
116 
117 	id = ZEND_THIS;
118 	if (zend_parse_parameters(ZEND_NUM_ARGS(), "l", &offset) == FAILURE) {
119 		RETURN_THROWS();
120 	}
121 	DOM_GET_OBJ(node, id, xmlNodePtr, intern);
122 
123 	if (offset < 0) {
124 		zend_argument_value_error(1, "must be greater than or equal to 0");
125 		RETURN_THROWS();
126 	}
127 
128 	if (node->type != XML_TEXT_NODE && node->type != XML_CDATA_SECTION_NODE) {
129 		/* TODO Add warning? */
130 		RETURN_FALSE;
131 	}
132 
133 	cur = xmlNodeGetContent(node);
134 	if (cur == NULL) {
135 		/* TODO Add warning? */
136 		RETURN_FALSE;
137 	}
138 	length = xmlUTF8Strlen(cur);
139 
140 	if (ZEND_LONG_INT_OVFL(offset) || (int)offset > length) {
141 		/* TODO Add warning? */
142 		xmlFree(cur);
143 		RETURN_FALSE;
144 	}
145 
146 	first = xmlUTF8Strndup(cur, (int)offset);
147 	second = xmlUTF8Strsub(cur, (int)offset, (int)(length - offset));
148 
149 	xmlFree(cur);
150 
151 	xmlNodeSetContent(node, first);
152 	nnode = xmlNewDocText(node->doc, second);
153 
154 	xmlFree(first);
155 	xmlFree(second);
156 
157 	if (nnode == NULL) {
158 		/* TODO Add warning? */
159 		RETURN_FALSE;
160 	}
161 
162 	if (node->parent != NULL) {
163 		nnode->type = XML_ELEMENT_NODE;
164 		xmlAddNextSibling(node, nnode);
165 		nnode->type = XML_TEXT_NODE;
166 	}
167 
168 	php_dom_create_object(nnode, return_value, intern);
169 }
170 /* }}} end dom_text_split_text */
171 
172 /* {{{ URL: http://www.w3.org/TR/2003/WD-DOM-Level-3-Core-20030226/DOM3-Core.html#core-Text3-isWhitespaceInElementContent
173 Since: DOM Level 3
174 */
PHP_METHOD(DOMText,isWhitespaceInElementContent)175 PHP_METHOD(DOMText, isWhitespaceInElementContent)
176 {
177 	zval       *id;
178 	xmlNodePtr  node;
179 	dom_object	*intern;
180 
181 	id = ZEND_THIS;
182 	if (zend_parse_parameters_none() == FAILURE) {
183 		RETURN_THROWS();
184 	}
185 	DOM_GET_OBJ(node, id, xmlNodePtr, intern);
186 
187 	if (xmlIsBlankNode(node)) {
188 		RETURN_TRUE;
189 	} else {
190 		RETURN_FALSE;
191 	}
192 }
193 /* }}} end dom_text_is_whitespace_in_element_content */
194 
195 #endif
196