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