1 : /*
2 : +----------------------------------------------------------------------+
3 : | PHP Version 5 |
4 : +----------------------------------------------------------------------+
5 : | Copyright (c) 1997-2009 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: text.c 272370 2008-12-31 11:15:49Z sebastian $ */
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 : {NULL, NULL, NULL}
62 : };
63 :
64 : /* {{{ proto void DOMText::__construct([string value]); */
65 : PHP_METHOD(domtext, __construct)
66 1 : {
67 :
68 : zval *id;
69 1 : xmlNodePtr nodep = NULL, oldnode = NULL;
70 : dom_object *intern;
71 1 : char *value = NULL;
72 : int value_len;
73 : zend_error_handling error_handling;
74 :
75 1 : zend_replace_error_handling(EH_THROW, dom_domexception_class_entry, &error_handling TSRMLS_CC);
76 1 : if (zend_parse_method_parameters(ZEND_NUM_ARGS() TSRMLS_CC, getThis(), "O|s", &id, dom_text_class_entry, &value, &value_len) == FAILURE) {
77 0 : zend_restore_error_handling(&error_handling TSRMLS_CC);
78 0 : return;
79 : }
80 :
81 1 : zend_restore_error_handling(&error_handling TSRMLS_CC);
82 1 : nodep = xmlNewText((xmlChar *) value);
83 :
84 1 : if (!nodep) {
85 0 : php_dom_throw_error(INVALID_STATE_ERR, 1 TSRMLS_CC);
86 0 : RETURN_FALSE;
87 : }
88 :
89 1 : intern = (dom_object *)zend_object_store_get_object(id TSRMLS_CC);
90 1 : if (intern != NULL) {
91 1 : oldnode = dom_object_get_node(intern);
92 1 : if (oldnode != NULL) {
93 0 : php_libxml_node_free_resource(oldnode TSRMLS_CC);
94 : }
95 1 : 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 : */
105 : int dom_text_whole_text_read(dom_object *obj, zval **retval TSRMLS_DC)
106 8 : {
107 : xmlNodePtr node;
108 8 : xmlChar *wholetext = NULL;
109 :
110 8 : node = dom_object_get_node(obj);
111 :
112 8 : if (node == NULL) {
113 0 : php_dom_throw_error(INVALID_STATE_ERR, 0 TSRMLS_CC);
114 0 : return FAILURE;
115 : }
116 :
117 : /* Find starting text node */
118 17 : while (node->prev && ((node->prev->type == XML_TEXT_NODE) || (node->prev->type == XML_CDATA_SECTION_NODE))) {
119 1 : node = node->prev;
120 : }
121 :
122 : /* concatenate all adjacent text and cdata nodes */
123 28 : while (node && ((node->type == XML_TEXT_NODE) || (node->type == XML_CDATA_SECTION_NODE))) {
124 12 : wholetext = xmlStrcat(wholetext, node->content);
125 12 : node = node->next;
126 : }
127 :
128 8 : ALLOC_ZVAL(*retval);
129 8 : if (wholetext != NULL) {
130 8 : ZVAL_STRING(*retval, wholetext, 1);
131 8 : xmlFree(wholetext);
132 : } else {
133 0 : ZVAL_EMPTY_STRING(*retval);
134 : }
135 :
136 8 : 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 : */
145 : PHP_FUNCTION(dom_text_split_text)
146 6 : {
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 6 : if (zend_parse_method_parameters(ZEND_NUM_ARGS() TSRMLS_CC, getThis(), "Ol", &id, dom_text_class_entry, &offset) == FAILURE) {
159 0 : return;
160 : }
161 6 : DOM_GET_OBJ(node, id, xmlNodePtr, intern);
162 :
163 6 : if (node->type != XML_TEXT_NODE) {
164 0 : RETURN_FALSE;
165 : }
166 :
167 6 : cur = xmlNodeGetContent(node);
168 6 : if (cur == NULL) {
169 0 : RETURN_FALSE;
170 : }
171 6 : length = xmlUTF8Strlen(cur);
172 :
173 6 : if (offset > length || offset < 0) {
174 0 : xmlFree(cur);
175 0 : RETURN_FALSE;
176 : }
177 :
178 6 : first = xmlUTF8Strndup(cur, offset);
179 6 : second = xmlUTF8Strsub(cur, offset, length - offset);
180 :
181 6 : xmlFree(cur);
182 :
183 6 : xmlNodeSetContent(node, first);
184 6 : nnode = xmlNewDocText(node->doc, second);
185 :
186 6 : xmlFree(first);
187 6 : xmlFree(second);
188 :
189 6 : if (nnode == NULL) {
190 0 : RETURN_FALSE;
191 : }
192 :
193 6 : if (node->parent != NULL) {
194 4 : nnode->type = XML_ELEMENT_NODE;
195 4 : xmlAddNextSibling(node, nnode);
196 4 : nnode->type = XML_TEXT_NODE;
197 : }
198 :
199 6 : return_value = php_dom_create_object(nnode, &ret, NULL, 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 : */
207 : PHP_FUNCTION(dom_text_is_whitespace_in_element_content)
208 1 : {
209 : zval *id;
210 : xmlNodePtr node;
211 : dom_object *intern;
212 :
213 1 : if (zend_parse_method_parameters(ZEND_NUM_ARGS() TSRMLS_CC, getThis(), "O", &id, dom_text_class_entry) == FAILURE) {
214 0 : return;
215 : }
216 1 : DOM_GET_OBJ(node, id, xmlNodePtr, intern);
217 :
218 1 : if (xmlIsBlankNode(node)) {
219 1 : RETURN_TRUE;
220 : } else {
221 0 : 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 : */
230 : PHP_FUNCTION(dom_text_replace_whole_text)
231 0 : {
232 0 : 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 : */
|