1/*
2   +----------------------------------------------------------------------+
3   | PHP Version 7                                                        |
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 */
32ZEND_BEGIN_ARG_INFO_EX(arginfo_dom_text_split_text, 0, 0, 1)
33    ZEND_ARG_INFO(0, offset)
34ZEND_END_ARG_INFO();
35
36ZEND_BEGIN_ARG_INFO_EX(arginfo_dom_text_is_whitespace_in_element_content, 0, 0, 0)
37ZEND_END_ARG_INFO();
38
39ZEND_BEGIN_ARG_INFO_EX(arginfo_dom_text_replace_whole_text, 0, 0, 1)
40    ZEND_ARG_INFO(0, content)
41ZEND_END_ARG_INFO();
42
43ZEND_BEGIN_ARG_INFO_EX(arginfo_dom_text_construct, 0, 0, 0)
44    ZEND_ARG_INFO(0, value)
45ZEND_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
55const 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]); */
65PHP_METHOD(domtext, __construct)
66{
67
68    zval *id;
69    xmlNodePtr nodep = NULL, oldnode = NULL;
70    dom_object *intern;
71    char *value = NULL;
72    size_t 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 = Z_DOMOBJ_P(id);
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
101readonly=yes
102URL: http://www.w3.org/TR/2003/WD-DOM-Level-3-Core-20030226/DOM3-Core.html#core-Text3-wholeText
103Since: DOM Level 3
104*/
105int 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    if (wholetext != NULL) {
129        ZVAL_STRING(retval, (char *) wholetext);
130        xmlFree(wholetext);
131    } else {
132        ZVAL_EMPTY_STRING(retval);
133    }
134
135    return SUCCESS;
136}
137
138/* }}} */
139
140/* {{{ proto DOMText dom_text_split_text(int offset);
141URL: http://www.w3.org/TR/2003/WD-DOM-Level-3-Core-20030226/DOM3-Core.html#core-ID-38853C1D
142Since:
143*/
144PHP_FUNCTION(dom_text_split_text)
145{
146    zval       *id;
147    xmlChar    *cur;
148    xmlChar    *first;
149    xmlChar    *second;
150    xmlNodePtr  node;
151    xmlNodePtr  nnode;
152    zend_long        offset;
153    int         length;
154    dom_object  *intern;
155
156    if (zend_parse_method_parameters(ZEND_NUM_ARGS() TSRMLS_CC, getThis(), "Ol", &id, dom_text_class_entry, &offset) == FAILURE) {
157        return;
158    }
159    DOM_GET_OBJ(node, id, xmlNodePtr, intern);
160
161    if (node->type != XML_TEXT_NODE && node->type != XML_CDATA_SECTION_NODE) {
162        RETURN_FALSE;
163    }
164
165    cur = xmlNodeGetContent(node);
166    if (cur == NULL) {
167        RETURN_FALSE;
168    }
169    length = xmlUTF8Strlen(cur);
170
171    if (offset > length || offset < 0) {
172        xmlFree(cur);
173        RETURN_FALSE;
174    }
175
176    first = xmlUTF8Strndup(cur, offset);
177    second = xmlUTF8Strsub(cur, offset, length - offset);
178
179    xmlFree(cur);
180
181    xmlNodeSetContent(node, first);
182    nnode = xmlNewDocText(node->doc, second);
183
184    xmlFree(first);
185    xmlFree(second);
186
187    if (nnode == NULL) {
188        RETURN_FALSE;
189    }
190
191    if (node->parent != NULL) {
192        nnode->type = XML_ELEMENT_NODE;
193        xmlAddNextSibling(node, nnode);
194        nnode->type = XML_TEXT_NODE;
195    }
196
197    php_dom_create_object(nnode, return_value, intern TSRMLS_CC);
198}
199/* }}} end dom_text_split_text */
200
201/* {{{ proto boolean dom_text_is_whitespace_in_element_content();
202URL: http://www.w3.org/TR/2003/WD-DOM-Level-3-Core-20030226/DOM3-Core.html#core-Text3-isWhitespaceInElementContent
203Since: DOM Level 3
204*/
205PHP_FUNCTION(dom_text_is_whitespace_in_element_content)
206{
207    zval       *id;
208    xmlNodePtr  node;
209    dom_object  *intern;
210
211    if (zend_parse_method_parameters(ZEND_NUM_ARGS() TSRMLS_CC, getThis(), "O", &id, dom_text_class_entry) == FAILURE) {
212        return;
213    }
214    DOM_GET_OBJ(node, id, xmlNodePtr, intern);
215
216    if (xmlIsBlankNode(node)) {
217        RETURN_TRUE;
218    } else {
219        RETURN_FALSE;
220    }
221}
222/* }}} end dom_text_is_whitespace_in_element_content */
223
224/* {{{ proto DOMText dom_text_replace_whole_text(string content);
225URL: http://www.w3.org/TR/2003/WD-DOM-Level-3-Core-20030226/DOM3-Core.html#core-Text3-replaceWholeText
226Since: DOM Level 3
227*/
228PHP_FUNCTION(dom_text_replace_whole_text)
229{
230 DOM_NOT_IMPLEMENTED();
231}
232/* }}} end dom_text_replace_whole_text */
233
234#endif
235
236/*
237 * Local variables:
238 * tab-width: 4
239 * c-basic-offset: 4
240 * End:
241 * vim600: noet sw=4 ts=4 fdm=marker
242 * vim<600: noet sw=4 ts=4
243 */
244