xref: /PHP-7.3/ext/dom/text.c (revision 8d3f8ca1)
1 /*
2    +----------------------------------------------------------------------+
3    | PHP Version 7                                                        |
4    +----------------------------------------------------------------------+
5    | Copyright (c) 1997-2018 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 
66 	zval *id = getThis();
67 	xmlNodePtr nodep = NULL, oldnode = NULL;
68 	dom_object *intern;
69 	char *value = NULL;
70 	size_t value_len;
71 
72 	if (zend_parse_parameters_throw(ZEND_NUM_ARGS(), "|s", &value, &value_len) == FAILURE) {
73 		return;
74 	}
75 
76 	nodep = xmlNewText((xmlChar *) value);
77 
78 	if (!nodep) {
79 		php_dom_throw_error(INVALID_STATE_ERR, 1);
80 		RETURN_FALSE;
81 	}
82 
83 	intern = Z_DOMOBJ_P(id);
84 	if (intern != NULL) {
85 		oldnode = dom_object_get_node(intern);
86 		if (oldnode != NULL) {
87 			php_libxml_node_free_resource(oldnode );
88 		}
89 		php_libxml_increment_node_ptr((php_libxml_node_object *)intern, nodep, (void *)intern);
90 	}
91 }
92 /* }}} end DOMText::__construct */
93 
94 /* {{{ wholeText	string
95 readonly=yes
96 URL: http://www.w3.org/TR/2003/WD-DOM-Level-3-Core-20030226/DOM3-Core.html#core-Text3-wholeText
97 Since: DOM Level 3
98 */
dom_text_whole_text_read(dom_object * obj,zval * retval)99 int dom_text_whole_text_read(dom_object *obj, zval *retval)
100 {
101 	xmlNodePtr node;
102 	xmlChar *wholetext = NULL;
103 
104 	node = dom_object_get_node(obj);
105 
106 	if (node == NULL) {
107 		php_dom_throw_error(INVALID_STATE_ERR, 0);
108 		return FAILURE;
109 	}
110 
111 	/* Find starting text node */
112 	while (node->prev && ((node->prev->type == XML_TEXT_NODE) || (node->prev->type == XML_CDATA_SECTION_NODE))) {
113 		node = node->prev;
114 	}
115 
116 	/* concatenate all adjacent text and cdata nodes */
117 	while (node && ((node->type == XML_TEXT_NODE) || (node->type == XML_CDATA_SECTION_NODE))) {
118 		wholetext = xmlStrcat(wholetext, node->content);
119 		node = node->next;
120 	}
121 
122 	if (wholetext != NULL) {
123 		ZVAL_STRING(retval, (char *) wholetext);
124 		xmlFree(wholetext);
125 	} else {
126 		ZVAL_EMPTY_STRING(retval);
127 	}
128 
129 	return SUCCESS;
130 }
131 
132 /* }}} */
133 
134 /* {{{ proto DOMText dom_text_split_text(int offset)
135 URL: http://www.w3.org/TR/2003/WD-DOM-Level-3-Core-20030226/DOM3-Core.html#core-ID-38853C1D
136 Since:
137 */
PHP_FUNCTION(dom_text_split_text)138 PHP_FUNCTION(dom_text_split_text)
139 {
140 	zval       *id;
141 	xmlChar    *cur;
142 	xmlChar    *first;
143 	xmlChar    *second;
144 	xmlNodePtr  node;
145 	xmlNodePtr  nnode;
146 	zend_long        offset;
147 	int         length;
148 	dom_object	*intern;
149 
150 	if (zend_parse_method_parameters(ZEND_NUM_ARGS(), getThis(), "Ol", &id, dom_text_class_entry, &offset) == FAILURE) {
151 		return;
152 	}
153 	DOM_GET_OBJ(node, id, xmlNodePtr, intern);
154 
155 	if (node->type != XML_TEXT_NODE && node->type != XML_CDATA_SECTION_NODE) {
156 		RETURN_FALSE;
157 	}
158 
159 	cur = xmlNodeGetContent(node);
160 	if (cur == NULL) {
161 		RETURN_FALSE;
162 	}
163 	length = xmlUTF8Strlen(cur);
164 
165 	if (ZEND_LONG_INT_OVFL(offset) || (int)offset > length || offset < 0) {
166 		xmlFree(cur);
167 		RETURN_FALSE;
168 	}
169 
170 	first = xmlUTF8Strndup(cur, (int)offset);
171 	second = xmlUTF8Strsub(cur, (int)offset, (int)(length - offset));
172 
173 	xmlFree(cur);
174 
175 	xmlNodeSetContent(node, first);
176 	nnode = xmlNewDocText(node->doc, second);
177 
178 	xmlFree(first);
179 	xmlFree(second);
180 
181 	if (nnode == NULL) {
182 		RETURN_FALSE;
183 	}
184 
185 	if (node->parent != NULL) {
186 		nnode->type = XML_ELEMENT_NODE;
187 		xmlAddNextSibling(node, nnode);
188 		nnode->type = XML_TEXT_NODE;
189 	}
190 
191 	php_dom_create_object(nnode, return_value, intern);
192 }
193 /* }}} end dom_text_split_text */
194 
195 /* {{{ proto bool dom_text_is_whitespace_in_element_content()
196 URL: http://www.w3.org/TR/2003/WD-DOM-Level-3-Core-20030226/DOM3-Core.html#core-Text3-isWhitespaceInElementContent
197 Since: DOM Level 3
198 */
PHP_FUNCTION(dom_text_is_whitespace_in_element_content)199 PHP_FUNCTION(dom_text_is_whitespace_in_element_content)
200 {
201 	zval       *id;
202 	xmlNodePtr  node;
203 	dom_object	*intern;
204 
205 	if (zend_parse_method_parameters(ZEND_NUM_ARGS(), getThis(), "O", &id, dom_text_class_entry) == 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 
230 /*
231  * Local variables:
232  * tab-width: 4
233  * c-basic-offset: 4
234  * End:
235  * vim600: noet sw=4 ts=4 fdm=marker
236  * vim<600: noet sw=4 ts=4
237  */
238