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