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_ce.h"
26 #include "dom_properties.h"
27
28 /*
29 * class DOMText extends DOMCharacterData
30 *
31 * URL: https://www.w3.org/TR/2003/WD-DOM-Level-3-Core-20030226/DOM3-Core.html#ID-1312295772
32 * Since:
33 */
34
35 /* {{{ */
PHP_METHOD(DOMText,__construct)36 PHP_METHOD(DOMText, __construct)
37 {
38 xmlNodePtr nodep = NULL, oldnode = NULL;
39 dom_object *intern;
40 char *value = NULL;
41 size_t value_len;
42
43 if (zend_parse_parameters(ZEND_NUM_ARGS(), "|s", &value, &value_len) == FAILURE) {
44 RETURN_THROWS();
45 }
46
47 nodep = xmlNewText(BAD_CAST value);
48
49 if (!nodep) {
50 php_dom_throw_error(INVALID_STATE_ERR, true);
51 RETURN_THROWS();
52 }
53
54 intern = Z_DOMOBJ_P(ZEND_THIS);
55 oldnode = dom_object_get_node(intern);
56 if (oldnode != NULL) {
57 php_libxml_node_decrement_resource((php_libxml_node_object *)intern);
58 }
59 php_libxml_increment_node_ptr((php_libxml_node_object *)intern, nodep, (void *)intern);
60 }
61 /* }}} end DOMText::__construct */
62
63 /* {{{ wholeText string
64 readonly=yes
65 URL: http://www.w3.org/TR/2003/WD-DOM-Level-3-Core-20030226/DOM3-Core.html#core-Text3-wholeText
66 Since: DOM Level 3
67 */
dom_text_whole_text_read(dom_object * obj,zval * retval)68 zend_result dom_text_whole_text_read(dom_object *obj, zval *retval)
69 {
70 DOM_PROP_NODE(xmlNodePtr, node, obj);
71
72 xmlChar *wholetext = NULL;
73
74 /* Find starting text node */
75 while (node->prev && ((node->prev->type == XML_TEXT_NODE) || (node->prev->type == XML_CDATA_SECTION_NODE))) {
76 node = node->prev;
77 }
78
79 /* concatenate all adjacent text and cdata nodes */
80 while (node && ((node->type == XML_TEXT_NODE) || (node->type == XML_CDATA_SECTION_NODE))) {
81 wholetext = xmlStrcat(wholetext, node->content);
82 node = node->next;
83 }
84
85 if (wholetext != NULL) {
86 ZVAL_STRING(retval, (char *) wholetext);
87 xmlFree(wholetext);
88 } else {
89 ZVAL_EMPTY_STRING(retval);
90 }
91
92 return SUCCESS;
93 }
94
95 /* }}} */
96
97 /* {{{ URL: http://www.w3.org/TR/2003/WD-DOM-Level-3-Core-20030226/DOM3-Core.html#core-ID-38853C1D
98 Modern spec URL: https://dom.spec.whatwg.org/#dom-text-splittext
99 Since:
100 */
PHP_METHOD(DOMText,splitText)101 PHP_METHOD(DOMText, splitText)
102 {
103 zval *id;
104 xmlChar *cur;
105 xmlChar *first;
106 xmlChar *second;
107 xmlNodePtr node;
108 xmlNodePtr nnode;
109 zend_long offset;
110 int length;
111 dom_object *intern;
112
113 id = ZEND_THIS;
114 if (zend_parse_parameters(ZEND_NUM_ARGS(), "l", &offset) == FAILURE) {
115 RETURN_THROWS();
116 }
117 DOM_GET_OBJ(node, id, xmlNodePtr, intern);
118
119 if (offset < 0) {
120 zend_argument_value_error(1, "must be greater than or equal to 0");
121 RETURN_THROWS();
122 }
123
124 cur = node->content;
125 if (cur == NULL) {
126 /* TODO: is this even possible? */
127 cur = BAD_CAST "";
128 }
129 length = xmlUTF8Strlen(cur);
130
131 if (ZEND_LONG_INT_OVFL(offset) || (int)offset > length) {
132 if (php_dom_follow_spec_intern(intern)) {
133 php_dom_throw_error(INDEX_SIZE_ERR, /* strict */ true);
134 }
135 RETURN_FALSE;
136 }
137
138 first = xmlUTF8Strndup(cur, (int)offset);
139 second = xmlUTF8Strsub(cur, (int)offset, (int)(length - offset));
140
141 xmlNodeSetContent(node, first);
142 nnode = xmlNewDocText(node->doc, second);
143
144 xmlFree(first);
145 xmlFree(second);
146
147 if (nnode == NULL) {
148 php_dom_throw_error(INVALID_STATE_ERR, /* strict */ true);
149 RETURN_THROWS();
150 }
151
152 if (node->parent != NULL) {
153 nnode->type = XML_ELEMENT_NODE;
154 xmlAddNextSibling(node, nnode);
155 nnode->type = XML_TEXT_NODE;
156 }
157
158 php_dom_create_object(nnode, return_value, intern);
159 }
160 /* }}} end dom_text_split_text */
161
162 /* {{{ URL: http://www.w3.org/TR/2003/WD-DOM-Level-3-Core-20030226/DOM3-Core.html#core-Text3-isWhitespaceInElementContent
163 Since: DOM Level 3
164 */
PHP_METHOD(DOMText,isWhitespaceInElementContent)165 PHP_METHOD(DOMText, isWhitespaceInElementContent)
166 {
167 zval *id;
168 xmlNodePtr node;
169 dom_object *intern;
170
171 id = ZEND_THIS;
172 if (zend_parse_parameters_none() == FAILURE) {
173 RETURN_THROWS();
174 }
175 DOM_GET_OBJ(node, id, xmlNodePtr, intern);
176
177 if (xmlIsBlankNode(node)) {
178 RETURN_TRUE;
179 } else {
180 RETURN_FALSE;
181 }
182 }
183 /* }}} end dom_text_is_whitespace_in_element_content */
184
185 #endif
186