xref: /php-src/ext/dom/text.c (revision 1fdbb0ab)
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 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(BAD_CAST value);
47 
48 	if (!nodep) {
49 		php_dom_throw_error(INVALID_STATE_ERR, true);
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 zend_result dom_text_whole_text_read(dom_object *obj, zval *retval)
68 {
69 	DOM_PROP_NODE(xmlNodePtr, node, obj);
70 
71 	xmlChar *wholetext = NULL;
72 
73 	/* Find starting text node */
74 	while (node->prev && ((node->prev->type == XML_TEXT_NODE) || (node->prev->type == XML_CDATA_SECTION_NODE))) {
75 		node = node->prev;
76 	}
77 
78 	/* concatenate all adjacent text and cdata nodes */
79 	while (node && ((node->type == XML_TEXT_NODE) || (node->type == XML_CDATA_SECTION_NODE))) {
80 		wholetext = xmlStrcat(wholetext, node->content);
81 		node = node->next;
82 	}
83 
84 	if (wholetext != NULL) {
85 		ZVAL_STRING(retval, (char *) wholetext);
86 		xmlFree(wholetext);
87 	} else {
88 		ZVAL_EMPTY_STRING(retval);
89 	}
90 
91 	return SUCCESS;
92 }
93 
94 /* }}} */
95 
96 /* {{{ URL: http://www.w3.org/TR/2003/WD-DOM-Level-3-Core-20030226/DOM3-Core.html#core-ID-38853C1D
97 Modern spec URL: https://dom.spec.whatwg.org/#dom-text-splittext
98 Since:
99 */
PHP_METHOD(DOMText,splitText)100 PHP_METHOD(DOMText, splitText)
101 {
102 	zval       *id;
103 	xmlChar    *cur;
104 	xmlChar    *first;
105 	xmlChar    *second;
106 	xmlNodePtr  node;
107 	xmlNodePtr  nnode;
108 	zend_long        offset;
109 	int         length;
110 	dom_object	*intern;
111 
112 	id = ZEND_THIS;
113 	if (zend_parse_parameters(ZEND_NUM_ARGS(), "l", &offset) == FAILURE) {
114 		RETURN_THROWS();
115 	}
116 	DOM_GET_OBJ(node, id, xmlNodePtr, intern);
117 
118 	if (offset < 0) {
119 		zend_argument_value_error(1, "must be greater than or equal to 0");
120 		RETURN_THROWS();
121 	}
122 
123 	cur = node->content;
124 	if (cur == NULL) {
125 		/* TODO: is this even possible? */
126 		cur = BAD_CAST "";
127 	}
128 	length = xmlUTF8Strlen(cur);
129 
130 	if (ZEND_LONG_INT_OVFL(offset) || (int)offset > length) {
131 		if (php_dom_follow_spec_intern(intern)) {
132 			php_dom_throw_error(INDEX_SIZE_ERR, /* strict */ true);
133 		}
134 		RETURN_FALSE;
135 	}
136 
137 	first = xmlUTF8Strndup(cur, (int)offset);
138 	second = xmlUTF8Strsub(cur, (int)offset, (int)(length - offset));
139 
140 	xmlNodeSetContent(node, first);
141 	nnode = xmlNewDocText(node->doc, second);
142 
143 	xmlFree(first);
144 	xmlFree(second);
145 
146 	if (nnode == NULL) {
147 		php_dom_throw_error(INVALID_STATE_ERR, /* strict */ true);
148 		RETURN_THROWS();
149 	}
150 
151 	if (node->parent != NULL) {
152 		nnode->type = XML_ELEMENT_NODE;
153 		xmlAddNextSibling(node, nnode);
154 		nnode->type = XML_TEXT_NODE;
155 	}
156 
157 	php_dom_create_object(nnode, return_value, intern);
158 }
159 /* }}} end dom_text_split_text */
160 
161 /* {{{ URL: http://www.w3.org/TR/2003/WD-DOM-Level-3-Core-20030226/DOM3-Core.html#core-Text3-isWhitespaceInElementContent
162 Since: DOM Level 3
163 */
PHP_METHOD(DOMText,isWhitespaceInElementContent)164 PHP_METHOD(DOMText, isWhitespaceInElementContent)
165 {
166 	zval       *id;
167 	xmlNodePtr  node;
168 	dom_object	*intern;
169 
170 	id = ZEND_THIS;
171 	if (zend_parse_parameters_none() == FAILURE) {
172 		RETURN_THROWS();
173 	}
174 	DOM_GET_OBJ(node, id, xmlNodePtr, intern);
175 
176 	if (xmlIsBlankNode(node)) {
177 		RETURN_TRUE;
178 	} else {
179 		RETURN_FALSE;
180 	}
181 }
182 /* }}} end dom_text_is_whitespace_in_element_content */
183 
184 #endif
185