xref: /PHP-5.4/ext/dom/text.c (revision c0d060f5)
1 /*
2    +----------------------------------------------------------------------+
3    | PHP Version 5                                                        |
4    +----------------------------------------------------------------------+
5    | Copyright (c) 1997-2014 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 /* $Id$ */
21 
22 #ifdef HAVE_CONFIG_H
23 #include "config.h"
24 #endif
25 
26 #include "php.h"
27 #if HAVE_LIBXML && HAVE_DOM
28 #include "php_dom.h"
29 #include "dom_ce.h"
30 
31 /* {{{ arginfo */
32 ZEND_BEGIN_ARG_INFO_EX(arginfo_dom_text_split_text, 0, 0, 1)
33 	ZEND_ARG_INFO(0, offset)
34 ZEND_END_ARG_INFO();
35 
36 ZEND_BEGIN_ARG_INFO_EX(arginfo_dom_text_is_whitespace_in_element_content, 0, 0, 0)
37 ZEND_END_ARG_INFO();
38 
39 ZEND_BEGIN_ARG_INFO_EX(arginfo_dom_text_replace_whole_text, 0, 0, 1)
40 	ZEND_ARG_INFO(0, content)
41 ZEND_END_ARG_INFO();
42 
43 ZEND_BEGIN_ARG_INFO_EX(arginfo_dom_text_construct, 0, 0, 0)
44 	ZEND_ARG_INFO(0, value)
45 ZEND_END_ARG_INFO();
46 /* }}} */
47 
48 /*
49 * class DOMText extends DOMCharacterData
50 *
51 * URL: http://www.w3.org/TR/2003/WD-DOM-Level-3-Core-20030226/DOM3-Core.html#ID-1312295772
52 * Since:
53 */
54 
55 const zend_function_entry php_dom_text_class_functions[] = {
56 	PHP_FALIAS(splitText, dom_text_split_text, arginfo_dom_text_split_text)
57 	PHP_FALIAS(isWhitespaceInElementContent, dom_text_is_whitespace_in_element_content, arginfo_dom_text_is_whitespace_in_element_content)
58 	PHP_FALIAS(isElementContentWhitespace, dom_text_is_whitespace_in_element_content, arginfo_dom_text_is_whitespace_in_element_content)
59 	PHP_FALIAS(replaceWholeText, dom_text_replace_whole_text, arginfo_dom_text_replace_whole_text)
60 	PHP_ME(domtext, __construct, arginfo_dom_text_construct, ZEND_ACC_PUBLIC)
61 	PHP_FE_END
62 };
63 
64 /* {{{ proto void DOMText::__construct([string value]); */
PHP_METHOD(domtext,__construct)65 PHP_METHOD(domtext, __construct)
66 {
67 
68 	zval *id;
69 	xmlNodePtr nodep = NULL, oldnode = NULL;
70 	dom_object *intern;
71 	char *value = NULL;
72 	int value_len;
73 	zend_error_handling error_handling;
74 
75 	zend_replace_error_handling(EH_THROW, dom_domexception_class_entry, &error_handling TSRMLS_CC);
76 	if (zend_parse_method_parameters(ZEND_NUM_ARGS() TSRMLS_CC, getThis(), "O|s", &id, dom_text_class_entry, &value, &value_len) == FAILURE) {
77 		zend_restore_error_handling(&error_handling TSRMLS_CC);
78 		return;
79 	}
80 
81 	zend_restore_error_handling(&error_handling TSRMLS_CC);
82 	nodep = xmlNewText((xmlChar *) value);
83 
84 	if (!nodep) {
85 		php_dom_throw_error(INVALID_STATE_ERR, 1 TSRMLS_CC);
86 		RETURN_FALSE;
87 	}
88 
89 	intern = (dom_object *)zend_object_store_get_object(id TSRMLS_CC);
90 	if (intern != NULL) {
91 		oldnode = dom_object_get_node(intern);
92 		if (oldnode != NULL) {
93 			php_libxml_node_free_resource(oldnode  TSRMLS_CC);
94 		}
95 		php_libxml_increment_node_ptr((php_libxml_node_object *)intern, nodep, (void *)intern TSRMLS_CC);
96 	}
97 }
98 /* }}} end DOMText::__construct */
99 
100 /* {{{ wholeText	string
101 readonly=yes
102 URL: http://www.w3.org/TR/2003/WD-DOM-Level-3-Core-20030226/DOM3-Core.html#core-Text3-wholeText
103 Since: DOM Level 3
104 */
dom_text_whole_text_read(dom_object * obj,zval ** retval TSRMLS_DC)105 int dom_text_whole_text_read(dom_object *obj, zval **retval TSRMLS_DC)
106 {
107 	xmlNodePtr node;
108 	xmlChar *wholetext = NULL;
109 
110 	node = dom_object_get_node(obj);
111 
112 	if (node == NULL) {
113 		php_dom_throw_error(INVALID_STATE_ERR, 0 TSRMLS_CC);
114 		return FAILURE;
115 	}
116 
117 	/* Find starting text node */
118 	while (node->prev && ((node->prev->type == XML_TEXT_NODE) || (node->prev->type == XML_CDATA_SECTION_NODE))) {
119 		node = node->prev;
120 	}
121 
122 	/* concatenate all adjacent text and cdata nodes */
123 	while (node && ((node->type == XML_TEXT_NODE) || (node->type == XML_CDATA_SECTION_NODE))) {
124 		wholetext = xmlStrcat(wholetext, node->content);
125 		node = node->next;
126 	}
127 
128 	ALLOC_ZVAL(*retval);
129 	if (wholetext != NULL) {
130 		ZVAL_STRING(*retval, wholetext, 1);
131 		xmlFree(wholetext);
132 	} else {
133 		ZVAL_EMPTY_STRING(*retval);
134 	}
135 
136 	return SUCCESS;
137 }
138 
139 /* }}} */
140 
141 /* {{{ proto DOMText dom_text_split_text(int offset);
142 URL: http://www.w3.org/TR/2003/WD-DOM-Level-3-Core-20030226/DOM3-Core.html#core-ID-38853C1D
143 Since:
144 */
PHP_FUNCTION(dom_text_split_text)145 PHP_FUNCTION(dom_text_split_text)
146 {
147 	zval       *id;
148 	xmlChar    *cur;
149 	xmlChar    *first;
150 	xmlChar    *second;
151 	xmlNodePtr  node;
152 	xmlNodePtr  nnode;
153 	long        offset;
154 	int         ret;
155 	int         length;
156 	dom_object	*intern;
157 
158 	if (zend_parse_method_parameters(ZEND_NUM_ARGS() TSRMLS_CC, getThis(), "Ol", &id, dom_text_class_entry, &offset) == FAILURE) {
159 		return;
160 	}
161 	DOM_GET_OBJ(node, id, xmlNodePtr, intern);
162 
163 	if (node->type != XML_TEXT_NODE && node->type != XML_CDATA_SECTION_NODE) {
164 		RETURN_FALSE;
165 	}
166 
167 	cur = xmlNodeGetContent(node);
168 	if (cur == NULL) {
169 		RETURN_FALSE;
170 	}
171 	length = xmlUTF8Strlen(cur);
172 
173 	if (offset > length || offset < 0) {
174 		xmlFree(cur);
175 		RETURN_FALSE;
176 	}
177 
178 	first = xmlUTF8Strndup(cur, offset);
179 	second = xmlUTF8Strsub(cur, offset, length - offset);
180 
181 	xmlFree(cur);
182 
183 	xmlNodeSetContent(node, first);
184 	nnode = xmlNewDocText(node->doc, second);
185 
186 	xmlFree(first);
187 	xmlFree(second);
188 
189 	if (nnode == NULL) {
190 		RETURN_FALSE;
191 	}
192 
193 	if (node->parent != NULL) {
194 		nnode->type = XML_ELEMENT_NODE;
195 		xmlAddNextSibling(node, nnode);
196 		nnode->type = XML_TEXT_NODE;
197 	}
198 
199 	return_value = php_dom_create_object(nnode, &ret, return_value, intern TSRMLS_CC);
200 }
201 /* }}} end dom_text_split_text */
202 
203 /* {{{ proto boolean dom_text_is_whitespace_in_element_content();
204 URL: http://www.w3.org/TR/2003/WD-DOM-Level-3-Core-20030226/DOM3-Core.html#core-Text3-isWhitespaceInElementContent
205 Since: DOM Level 3
206 */
PHP_FUNCTION(dom_text_is_whitespace_in_element_content)207 PHP_FUNCTION(dom_text_is_whitespace_in_element_content)
208 {
209 	zval       *id;
210 	xmlNodePtr  node;
211 	dom_object	*intern;
212 
213 	if (zend_parse_method_parameters(ZEND_NUM_ARGS() TSRMLS_CC, getThis(), "O", &id, dom_text_class_entry) == FAILURE) {
214 		return;
215 	}
216 	DOM_GET_OBJ(node, id, xmlNodePtr, intern);
217 
218 	if (xmlIsBlankNode(node)) {
219 		RETURN_TRUE;
220 	} else {
221 		RETURN_FALSE;
222 	}
223 }
224 /* }}} end dom_text_is_whitespace_in_element_content */
225 
226 /* {{{ proto DOMText dom_text_replace_whole_text(string content);
227 URL: http://www.w3.org/TR/2003/WD-DOM-Level-3-Core-20030226/DOM3-Core.html#core-Text3-replaceWholeText
228 Since: DOM Level 3
229 */
PHP_FUNCTION(dom_text_replace_whole_text)230 PHP_FUNCTION(dom_text_replace_whole_text)
231 {
232  DOM_NOT_IMPLEMENTED();
233 }
234 /* }}} end dom_text_replace_whole_text */
235 
236 #endif
237 
238 /*
239  * Local variables:
240  * tab-width: 4
241  * c-basic-offset: 4
242  * End:
243  * vim600: noet sw=4 ts=4 fdm=marker
244  * vim<600: noet sw=4 ts=4
245  */
246