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