text.c 6.9 KB

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