text.c 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237
  1. /*
  2. +----------------------------------------------------------------------+
  3. | PHP Version 7 |
  4. +----------------------------------------------------------------------+
  5. | Copyright (c) 1997-2018 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. #ifdef HAVE_CONFIG_H
  20. #include "config.h"
  21. #endif
  22. #include "php.h"
  23. #if HAVE_LIBXML && HAVE_DOM
  24. #include "php_dom.h"
  25. #include "dom_ce.h"
  26. /* {{{ arginfo */
  27. ZEND_BEGIN_ARG_INFO_EX(arginfo_dom_text_split_text, 0, 0, 1)
  28. ZEND_ARG_INFO(0, offset)
  29. ZEND_END_ARG_INFO();
  30. ZEND_BEGIN_ARG_INFO_EX(arginfo_dom_text_is_whitespace_in_element_content, 0, 0, 0)
  31. ZEND_END_ARG_INFO();
  32. ZEND_BEGIN_ARG_INFO_EX(arginfo_dom_text_replace_whole_text, 0, 0, 1)
  33. ZEND_ARG_INFO(0, content)
  34. ZEND_END_ARG_INFO();
  35. ZEND_BEGIN_ARG_INFO_EX(arginfo_dom_text_construct, 0, 0, 0)
  36. ZEND_ARG_INFO(0, value)
  37. ZEND_END_ARG_INFO();
  38. /* }}} */
  39. /*
  40. * class DOMText extends DOMCharacterData
  41. *
  42. * URL: https://www.w3.org/TR/2003/WD-DOM-Level-3-Core-20030226/DOM3-Core.html#ID-1312295772
  43. * Since:
  44. */
  45. const zend_function_entry php_dom_text_class_functions[] = {
  46. PHP_FALIAS(splitText, dom_text_split_text, arginfo_dom_text_split_text)
  47. PHP_FALIAS(isWhitespaceInElementContent, dom_text_is_whitespace_in_element_content, arginfo_dom_text_is_whitespace_in_element_content)
  48. PHP_FALIAS(isElementContentWhitespace, dom_text_is_whitespace_in_element_content, arginfo_dom_text_is_whitespace_in_element_content)
  49. PHP_FALIAS(replaceWholeText, dom_text_replace_whole_text, arginfo_dom_text_replace_whole_text)
  50. PHP_ME(domtext, __construct, arginfo_dom_text_construct, ZEND_ACC_PUBLIC)
  51. PHP_FE_END
  52. };
  53. /* {{{ proto DOMText::__construct([string value]); */
  54. PHP_METHOD(domtext, __construct)
  55. {
  56. zval *id = getThis();
  57. xmlNodePtr nodep = NULL, oldnode = NULL;
  58. dom_object *intern;
  59. char *value = NULL;
  60. size_t value_len;
  61. if (zend_parse_parameters_throw(ZEND_NUM_ARGS(), "|s", &value, &value_len) == FAILURE) {
  62. return;
  63. }
  64. nodep = xmlNewText((xmlChar *) value);
  65. if (!nodep) {
  66. php_dom_throw_error(INVALID_STATE_ERR, 1);
  67. RETURN_FALSE;
  68. }
  69. intern = Z_DOMOBJ_P(id);
  70. if (intern != NULL) {
  71. oldnode = dom_object_get_node(intern);
  72. if (oldnode != NULL) {
  73. php_libxml_node_free_resource(oldnode );
  74. }
  75. php_libxml_increment_node_ptr((php_libxml_node_object *)intern, nodep, (void *)intern);
  76. }
  77. }
  78. /* }}} end DOMText::__construct */
  79. /* {{{ wholeText string
  80. readonly=yes
  81. URL: http://www.w3.org/TR/2003/WD-DOM-Level-3-Core-20030226/DOM3-Core.html#core-Text3-wholeText
  82. Since: DOM Level 3
  83. */
  84. int dom_text_whole_text_read(dom_object *obj, zval *retval)
  85. {
  86. xmlNodePtr node;
  87. xmlChar *wholetext = NULL;
  88. node = dom_object_get_node(obj);
  89. if (node == NULL) {
  90. php_dom_throw_error(INVALID_STATE_ERR, 0);
  91. return FAILURE;
  92. }
  93. /* Find starting text node */
  94. while (node->prev && ((node->prev->type == XML_TEXT_NODE) || (node->prev->type == XML_CDATA_SECTION_NODE))) {
  95. node = node->prev;
  96. }
  97. /* concatenate all adjacent text and cdata nodes */
  98. while (node && ((node->type == XML_TEXT_NODE) || (node->type == XML_CDATA_SECTION_NODE))) {
  99. wholetext = xmlStrcat(wholetext, node->content);
  100. node = node->next;
  101. }
  102. if (wholetext != NULL) {
  103. ZVAL_STRING(retval, (char *) wholetext);
  104. xmlFree(wholetext);
  105. } else {
  106. ZVAL_EMPTY_STRING(retval);
  107. }
  108. return SUCCESS;
  109. }
  110. /* }}} */
  111. /* {{{ proto DOMText dom_text_split_text(int offset)
  112. URL: http://www.w3.org/TR/2003/WD-DOM-Level-3-Core-20030226/DOM3-Core.html#core-ID-38853C1D
  113. Since:
  114. */
  115. PHP_FUNCTION(dom_text_split_text)
  116. {
  117. zval *id;
  118. xmlChar *cur;
  119. xmlChar *first;
  120. xmlChar *second;
  121. xmlNodePtr node;
  122. xmlNodePtr nnode;
  123. zend_long offset;
  124. int length;
  125. dom_object *intern;
  126. if (zend_parse_method_parameters(ZEND_NUM_ARGS(), getThis(), "Ol", &id, dom_text_class_entry, &offset) == FAILURE) {
  127. return;
  128. }
  129. DOM_GET_OBJ(node, id, xmlNodePtr, intern);
  130. if (node->type != XML_TEXT_NODE && node->type != XML_CDATA_SECTION_NODE) {
  131. RETURN_FALSE;
  132. }
  133. cur = xmlNodeGetContent(node);
  134. if (cur == NULL) {
  135. RETURN_FALSE;
  136. }
  137. length = xmlUTF8Strlen(cur);
  138. if (ZEND_LONG_INT_OVFL(offset) || (int)offset > length || offset < 0) {
  139. xmlFree(cur);
  140. RETURN_FALSE;
  141. }
  142. first = xmlUTF8Strndup(cur, (int)offset);
  143. second = xmlUTF8Strsub(cur, (int)offset, (int)(length - offset));
  144. xmlFree(cur);
  145. xmlNodeSetContent(node, first);
  146. nnode = xmlNewDocText(node->doc, second);
  147. xmlFree(first);
  148. xmlFree(second);
  149. if (nnode == NULL) {
  150. RETURN_FALSE;
  151. }
  152. if (node->parent != NULL) {
  153. nnode->type = XML_ELEMENT_NODE;
  154. xmlAddNextSibling(node, nnode);
  155. nnode->type = XML_TEXT_NODE;
  156. }
  157. php_dom_create_object(nnode, return_value, intern);
  158. }
  159. /* }}} end dom_text_split_text */
  160. /* {{{ proto bool dom_text_is_whitespace_in_element_content()
  161. URL: http://www.w3.org/TR/2003/WD-DOM-Level-3-Core-20030226/DOM3-Core.html#core-Text3-isWhitespaceInElementContent
  162. Since: DOM Level 3
  163. */
  164. PHP_FUNCTION(dom_text_is_whitespace_in_element_content)
  165. {
  166. zval *id;
  167. xmlNodePtr node;
  168. dom_object *intern;
  169. if (zend_parse_method_parameters(ZEND_NUM_ARGS(), getThis(), "O", &id, dom_text_class_entry) == FAILURE) {
  170. return;
  171. }
  172. DOM_GET_OBJ(node, id, xmlNodePtr, intern);
  173. if (xmlIsBlankNode(node)) {
  174. RETURN_TRUE;
  175. } else {
  176. RETURN_FALSE;
  177. }
  178. }
  179. /* }}} end dom_text_is_whitespace_in_element_content */
  180. /* {{{ proto DOMText dom_text_replace_whole_text(string content)
  181. URL: http://www.w3.org/TR/2003/WD-DOM-Level-3-Core-20030226/DOM3-Core.html#core-Text3-replaceWholeText
  182. Since: DOM Level 3
  183. */
  184. PHP_FUNCTION(dom_text_replace_whole_text)
  185. {
  186. DOM_NOT_IMPLEMENTED();
  187. }
  188. /* }}} end dom_text_replace_whole_text */
  189. #endif
  190. /*
  191. * Local variables:
  192. * tab-width: 4
  193. * c-basic-offset: 4
  194. * End:
  195. * vim600: noet sw=4 ts=4 fdm=marker
  196. * vim<600: noet sw=4 ts=4
  197. */