xref: /PHP-7.4/ext/dom/text.c (revision 92ac598a)
1 /*
2    +----------------------------------------------------------------------+
3    | PHP Version 7                                                        |
4    +----------------------------------------------------------------------+
5    | Copyright (c) The PHP Group                                          |
6    +----------------------------------------------------------------------+
7    | This source file is subject to version 3.01 of the PHP license,      |
8    | that is bundled with this package in the file LICENSE, and is        |
9    | available through the world-wide-web at the following url:           |
10    | http://www.php.net/license/3_01.txt                                  |
11    | If you did not receive a copy of the PHP license and are unable to   |
12    | obtain it through the world-wide-web, please send a note to          |
13    | license@php.net so we can mail you a copy immediately.               |
14    +----------------------------------------------------------------------+
15    | Authors: Christian Stocker <chregu@php.net>                          |
16    |          Rob Richards <rrichards@php.net>                            |
17    +----------------------------------------------------------------------+
18 */
19 
20 #ifdef HAVE_CONFIG_H
21 #include "config.h"
22 #endif
23 
24 #include "php.h"
25 #if HAVE_LIBXML && HAVE_DOM
26 #include "php_dom.h"
27 #include "dom_ce.h"
28 
29 /* {{{ arginfo */
30 ZEND_BEGIN_ARG_INFO_EX(arginfo_dom_text_split_text, 0, 0, 1)
31 	ZEND_ARG_INFO(0, offset)
32 ZEND_END_ARG_INFO();
33 
34 ZEND_BEGIN_ARG_INFO_EX(arginfo_dom_text_is_whitespace_in_element_content, 0, 0, 0)
35 ZEND_END_ARG_INFO();
36 
37 ZEND_BEGIN_ARG_INFO_EX(arginfo_dom_text_replace_whole_text, 0, 0, 1)
38 	ZEND_ARG_INFO(0, content)
39 ZEND_END_ARG_INFO();
40 
41 ZEND_BEGIN_ARG_INFO_EX(arginfo_dom_text_construct, 0, 0, 0)
42 	ZEND_ARG_INFO(0, value)
43 ZEND_END_ARG_INFO();
44 /* }}} */
45 
46 /*
47 * class DOMText extends DOMCharacterData
48 *
49 * URL: https://www.w3.org/TR/2003/WD-DOM-Level-3-Core-20030226/DOM3-Core.html#ID-1312295772
50 * Since:
51 */
52 
53 const zend_function_entry php_dom_text_class_functions[] = {
54 	PHP_FALIAS(splitText, dom_text_split_text, arginfo_dom_text_split_text)
55 	PHP_FALIAS(isWhitespaceInElementContent, dom_text_is_whitespace_in_element_content, arginfo_dom_text_is_whitespace_in_element_content)
56 	PHP_FALIAS(isElementContentWhitespace, dom_text_is_whitespace_in_element_content, arginfo_dom_text_is_whitespace_in_element_content)
57 	PHP_FALIAS(replaceWholeText, dom_text_replace_whole_text, arginfo_dom_text_replace_whole_text)
58 	PHP_ME(domtext, __construct, arginfo_dom_text_construct, ZEND_ACC_PUBLIC)
59 	PHP_FE_END
60 };
61 
62 /* {{{ proto DOMText::__construct([string value]); */
PHP_METHOD(domtext,__construct)63 PHP_METHOD(domtext, __construct)
64 {
65 	xmlNodePtr nodep = NULL, oldnode = NULL;
66 	dom_object *intern;
67 	char *value = NULL;
68 	size_t value_len;
69 
70 	if (zend_parse_parameters_throw(ZEND_NUM_ARGS(), "|s", &value, &value_len) == FAILURE) {
71 		return;
72 	}
73 
74 	nodep = xmlNewText((xmlChar *) value);
75 
76 	if (!nodep) {
77 		php_dom_throw_error(INVALID_STATE_ERR, 1);
78 		RETURN_FALSE;
79 	}
80 
81 	intern = Z_DOMOBJ_P(ZEND_THIS);
82 	if (intern != NULL) {
83 		oldnode = dom_object_get_node(intern);
84 		if (oldnode != NULL) {
85 			php_libxml_node_free_resource(oldnode );
86 		}
87 		php_libxml_increment_node_ptr((php_libxml_node_object *)intern, nodep, (void *)intern);
88 	}
89 }
90 /* }}} end DOMText::__construct */
91 
92 /* {{{ wholeText	string
93 readonly=yes
94 URL: http://www.w3.org/TR/2003/WD-DOM-Level-3-Core-20030226/DOM3-Core.html#core-Text3-wholeText
95 Since: DOM Level 3
96 */
dom_text_whole_text_read(dom_object * obj,zval * retval)97 int dom_text_whole_text_read(dom_object *obj, zval *retval)
98 {
99 	xmlNodePtr node;
100 	xmlChar *wholetext = NULL;
101 
102 	node = dom_object_get_node(obj);
103 
104 	if (node == NULL) {
105 		php_dom_throw_error(INVALID_STATE_ERR, 0);
106 		return FAILURE;
107 	}
108 
109 	/* Find starting text node */
110 	while (node->prev && ((node->prev->type == XML_TEXT_NODE) || (node->prev->type == XML_CDATA_SECTION_NODE))) {
111 		node = node->prev;
112 	}
113 
114 	/* concatenate all adjacent text and cdata nodes */
115 	while (node && ((node->type == XML_TEXT_NODE) || (node->type == XML_CDATA_SECTION_NODE))) {
116 		wholetext = xmlStrcat(wholetext, node->content);
117 		node = node->next;
118 	}
119 
120 	if (wholetext != NULL) {
121 		ZVAL_STRING(retval, (char *) wholetext);
122 		xmlFree(wholetext);
123 	} else {
124 		ZVAL_EMPTY_STRING(retval);
125 	}
126 
127 	return SUCCESS;
128 }
129 
130 /* }}} */
131 
132 /* {{{ proto DOMText dom_text_split_text(int offset)
133 URL: http://www.w3.org/TR/2003/WD-DOM-Level-3-Core-20030226/DOM3-Core.html#core-ID-38853C1D
134 Since:
135 */
PHP_FUNCTION(dom_text_split_text)136 PHP_FUNCTION(dom_text_split_text)
137 {
138 	zval       *id;
139 	xmlChar    *cur;
140 	xmlChar    *first;
141 	xmlChar    *second;
142 	xmlNodePtr  node;
143 	xmlNodePtr  nnode;
144 	zend_long        offset;
145 	int         length;
146 	dom_object	*intern;
147 
148 	id = ZEND_THIS;
149 	if (zend_parse_parameters(ZEND_NUM_ARGS(), "l", &offset) == FAILURE) {
150 		return;
151 	}
152 	DOM_GET_OBJ(node, id, xmlNodePtr, intern);
153 
154 	if (node->type != XML_TEXT_NODE && node->type != XML_CDATA_SECTION_NODE) {
155 		RETURN_FALSE;
156 	}
157 
158 	cur = xmlNodeGetContent(node);
159 	if (cur == NULL) {
160 		RETURN_FALSE;
161 	}
162 	length = xmlUTF8Strlen(cur);
163 
164 	if (ZEND_LONG_INT_OVFL(offset) || (int)offset > length || offset < 0) {
165 		xmlFree(cur);
166 		RETURN_FALSE;
167 	}
168 
169 	first = xmlUTF8Strndup(cur, (int)offset);
170 	second = xmlUTF8Strsub(cur, (int)offset, (int)(length - offset));
171 
172 	xmlFree(cur);
173 
174 	xmlNodeSetContent(node, first);
175 	nnode = xmlNewDocText(node->doc, second);
176 
177 	xmlFree(first);
178 	xmlFree(second);
179 
180 	if (nnode == NULL) {
181 		RETURN_FALSE;
182 	}
183 
184 	if (node->parent != NULL) {
185 		nnode->type = XML_ELEMENT_NODE;
186 		xmlAddNextSibling(node, nnode);
187 		nnode->type = XML_TEXT_NODE;
188 	}
189 
190 	php_dom_create_object(nnode, return_value, intern);
191 }
192 /* }}} end dom_text_split_text */
193 
194 /* {{{ proto bool dom_text_is_whitespace_in_element_content()
195 URL: http://www.w3.org/TR/2003/WD-DOM-Level-3-Core-20030226/DOM3-Core.html#core-Text3-isWhitespaceInElementContent
196 Since: DOM Level 3
197 */
PHP_FUNCTION(dom_text_is_whitespace_in_element_content)198 PHP_FUNCTION(dom_text_is_whitespace_in_element_content)
199 {
200 	zval       *id;
201 	xmlNodePtr  node;
202 	dom_object	*intern;
203 
204 	id = ZEND_THIS;
205 	if (zend_parse_parameters_none() == FAILURE) {
206 		return;
207 	}
208 	DOM_GET_OBJ(node, id, xmlNodePtr, intern);
209 
210 	if (xmlIsBlankNode(node)) {
211 		RETURN_TRUE;
212 	} else {
213 		RETURN_FALSE;
214 	}
215 }
216 /* }}} end dom_text_is_whitespace_in_element_content */
217 
218 /* {{{ proto DOMText dom_text_replace_whole_text(string content)
219 URL: http://www.w3.org/TR/2003/WD-DOM-Level-3-Core-20030226/DOM3-Core.html#core-Text3-replaceWholeText
220 Since: DOM Level 3
221 */
PHP_FUNCTION(dom_text_replace_whole_text)222 PHP_FUNCTION(dom_text_replace_whole_text)
223 {
224  DOM_NOT_IMPLEMENTED();
225 }
226 /* }}} end dom_text_replace_whole_text */
227 
228 #endif
229