characterdata.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419
  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. /* {{{ arginfo */
  27. ZEND_BEGIN_ARG_INFO_EX(arginfo_dom_characterdata_substring_data, 0, 0, 2)
  28. ZEND_ARG_INFO(0, offset)
  29. ZEND_ARG_INFO(0, count)
  30. ZEND_END_ARG_INFO();
  31. ZEND_BEGIN_ARG_INFO_EX(arginfo_dom_characterdata_append_data, 0, 0, 1)
  32. ZEND_ARG_INFO(0, arg)
  33. ZEND_END_ARG_INFO();
  34. ZEND_BEGIN_ARG_INFO_EX(arginfo_dom_characterdata_insert_data, 0, 0, 2)
  35. ZEND_ARG_INFO(0, offset)
  36. ZEND_ARG_INFO(0, arg)
  37. ZEND_END_ARG_INFO();
  38. ZEND_BEGIN_ARG_INFO_EX(arginfo_dom_characterdata_delete_data, 0, 0, 2)
  39. ZEND_ARG_INFO(0, offset)
  40. ZEND_ARG_INFO(0, count)
  41. ZEND_END_ARG_INFO();
  42. ZEND_BEGIN_ARG_INFO_EX(arginfo_dom_characterdata_replace_data, 0, 0, 3)
  43. ZEND_ARG_INFO(0, offset)
  44. ZEND_ARG_INFO(0, count)
  45. ZEND_ARG_INFO(0, arg)
  46. ZEND_END_ARG_INFO();
  47. /* }}} */
  48. /*
  49. * class DOMCharacterData extends DOMNode
  50. *
  51. * URL: http://www.w3.org/TR/2003/WD-DOM-Level-3-Core-20030226/DOM3-Core.html#core-ID-FF21A306
  52. * Since:
  53. */
  54. const zend_function_entry php_dom_characterdata_class_functions[] = {
  55. PHP_FALIAS(substringData, dom_characterdata_substring_data, arginfo_dom_characterdata_substring_data)
  56. PHP_FALIAS(appendData, dom_characterdata_append_data, arginfo_dom_characterdata_append_data)
  57. PHP_FALIAS(insertData, dom_characterdata_insert_data, arginfo_dom_characterdata_insert_data)
  58. PHP_FALIAS(deleteData, dom_characterdata_delete_data, arginfo_dom_characterdata_delete_data)
  59. PHP_FALIAS(replaceData, dom_characterdata_replace_data, arginfo_dom_characterdata_replace_data)
  60. PHP_FE_END
  61. };
  62. /* {{{ data string
  63. readonly=no
  64. URL: http://www.w3.org/TR/2003/WD-DOM-Level-3-Core-20030226/DOM3-Core.html#core-ID-72AB8359
  65. Since:
  66. */
  67. int dom_characterdata_data_read(dom_object *obj, zval **retval TSRMLS_DC)
  68. {
  69. xmlNodePtr nodep;
  70. xmlChar *content;
  71. nodep = dom_object_get_node(obj);
  72. if (nodep == NULL) {
  73. php_dom_throw_error(INVALID_STATE_ERR, 0 TSRMLS_CC);
  74. return FAILURE;
  75. }
  76. ALLOC_ZVAL(*retval);
  77. if ((content = xmlNodeGetContent(nodep)) != NULL) {
  78. ZVAL_STRING(*retval, content, 1);
  79. xmlFree(content);
  80. } else {
  81. ZVAL_EMPTY_STRING(*retval);
  82. }
  83. return SUCCESS;
  84. }
  85. int dom_characterdata_data_write(dom_object *obj, zval *newval TSRMLS_DC)
  86. {
  87. zval value_copy;
  88. xmlNode *nodep;
  89. nodep = dom_object_get_node(obj);
  90. if (nodep == NULL) {
  91. php_dom_throw_error(INVALID_STATE_ERR, 0 TSRMLS_CC);
  92. return FAILURE;
  93. }
  94. convert_to_string_copy(newval, value_copy);
  95. xmlNodeSetContentLen(nodep, Z_STRVAL_P(newval), Z_STRLEN_P(newval) + 1);
  96. if (newval == &value_copy) {
  97. zval_dtor(newval);
  98. }
  99. return SUCCESS;
  100. }
  101. /* }}} */
  102. /* {{{ length long
  103. readonly=yes
  104. URL: http://www.w3.org/TR/2003/WD-DOM-Level-3-Core-20030226/DOM3-Core.html#core-ID-7D61178C
  105. Since:
  106. */
  107. int dom_characterdata_length_read(dom_object *obj, zval **retval TSRMLS_DC)
  108. {
  109. xmlNodePtr nodep;
  110. xmlChar *content;
  111. long length = 0;
  112. nodep = dom_object_get_node(obj);
  113. if (nodep == NULL) {
  114. php_dom_throw_error(INVALID_STATE_ERR, 0 TSRMLS_CC);
  115. return FAILURE;
  116. }
  117. ALLOC_ZVAL(*retval);
  118. content = xmlNodeGetContent(nodep);
  119. if (content) {
  120. length = xmlUTF8Strlen(content);
  121. xmlFree(content);
  122. }
  123. ZVAL_LONG(*retval, length);
  124. return SUCCESS;
  125. }
  126. /* }}} */
  127. /* {{{ proto string dom_characterdata_substring_data(int offset, int count);
  128. URL: http://www.w3.org/TR/2003/WD-DOM-Level-3-Core-20030226/DOM3-Core.html#core-ID-6531BCCF
  129. Since:
  130. */
  131. PHP_FUNCTION(dom_characterdata_substring_data)
  132. {
  133. zval *id;
  134. xmlChar *cur;
  135. xmlChar *substring;
  136. xmlNodePtr node;
  137. long offset, count;
  138. int length;
  139. dom_object *intern;
  140. if (zend_parse_method_parameters(ZEND_NUM_ARGS() TSRMLS_CC, getThis(), "Oll", &id, dom_characterdata_class_entry, &offset, &count) == FAILURE) {
  141. return;
  142. }
  143. DOM_GET_OBJ(node, id, xmlNodePtr, intern);
  144. cur = xmlNodeGetContent(node);
  145. if (cur == NULL) {
  146. RETURN_FALSE;
  147. }
  148. length = xmlUTF8Strlen(cur);
  149. if (offset < 0 || count < 0 || offset > length) {
  150. xmlFree(cur);
  151. php_dom_throw_error(INDEX_SIZE_ERR, dom_get_strict_error(intern->document) TSRMLS_CC);
  152. RETURN_FALSE;
  153. }
  154. if ((offset + count) > length) {
  155. count = length - offset;
  156. }
  157. substring = xmlUTF8Strsub(cur, offset, count);
  158. xmlFree(cur);
  159. if (substring) {
  160. RETVAL_STRING(substring, 1);
  161. xmlFree(substring);
  162. } else {
  163. RETVAL_EMPTY_STRING();
  164. }
  165. }
  166. /* }}} end dom_characterdata_substring_data */
  167. /* {{{ proto void dom_characterdata_append_data(string arg);
  168. URL: http://www.w3.org/TR/2003/WD-DOM-Level-3-Core-20030226/DOM3-Core.html#core-ID-32791A2F
  169. Since:
  170. */
  171. PHP_FUNCTION(dom_characterdata_append_data)
  172. {
  173. zval *id;
  174. xmlNode *nodep;
  175. dom_object *intern;
  176. char *arg;
  177. int arg_len;
  178. if (zend_parse_method_parameters(ZEND_NUM_ARGS() TSRMLS_CC, getThis(), "Os", &id, dom_characterdata_class_entry, &arg, &arg_len) == FAILURE) {
  179. return;
  180. }
  181. DOM_GET_OBJ(nodep, id, xmlNodePtr, intern);
  182. #if LIBXML_VERSION < 20627
  183. /* Implement logic from libxml xmlTextConcat to add suport for comments and PI */
  184. if ((nodep->content == (xmlChar *) &(nodep->properties)) ||
  185. ((nodep->doc != NULL) && (nodep->doc->dict != NULL) &&
  186. xmlDictOwns(nodep->doc->dict, nodep->content))) {
  187. nodep->content = xmlStrncatNew(nodep->content, arg, arg_len);
  188. } else {
  189. nodep->content = xmlStrncat(nodep->content, arg, arg_len);
  190. }
  191. nodep->properties = NULL;
  192. #else
  193. xmlTextConcat(nodep, arg, arg_len);
  194. #endif
  195. RETURN_TRUE;
  196. }
  197. /* }}} end dom_characterdata_append_data */
  198. /* {{{ proto void dom_characterdata_insert_data(int offset, string arg);
  199. URL: http://www.w3.org/TR/2003/WD-DOM-Level-3-Core-20030226/DOM3-Core.html#core-ID-3EDB695F
  200. Since:
  201. */
  202. PHP_FUNCTION(dom_characterdata_insert_data)
  203. {
  204. zval *id;
  205. xmlChar *cur, *first, *second;
  206. xmlNodePtr node;
  207. char *arg;
  208. long offset;
  209. int length, arg_len;
  210. dom_object *intern;
  211. if (zend_parse_method_parameters(ZEND_NUM_ARGS() TSRMLS_CC, getThis(), "Ols", &id, dom_characterdata_class_entry, &offset, &arg, &arg_len) == FAILURE) {
  212. return;
  213. }
  214. DOM_GET_OBJ(node, id, xmlNodePtr, intern);
  215. cur = xmlNodeGetContent(node);
  216. if (cur == NULL) {
  217. RETURN_FALSE;
  218. }
  219. length = xmlUTF8Strlen(cur);
  220. if (offset < 0 || offset > length) {
  221. xmlFree(cur);
  222. php_dom_throw_error(INDEX_SIZE_ERR, dom_get_strict_error(intern->document) TSRMLS_CC);
  223. RETURN_FALSE;
  224. }
  225. first = xmlUTF8Strndup(cur, offset);
  226. second = xmlUTF8Strsub(cur, offset, length - offset);
  227. xmlFree(cur);
  228. xmlNodeSetContent(node, first);
  229. xmlNodeAddContent(node, arg);
  230. xmlNodeAddContent(node, second);
  231. xmlFree(first);
  232. xmlFree(second);
  233. RETURN_TRUE;
  234. }
  235. /* }}} end dom_characterdata_insert_data */
  236. /* {{{ proto void dom_characterdata_delete_data(int offset, int count);
  237. URL: http://www.w3.org/TR/2003/WD-DOM-Level-3-Core-20030226/DOM3-Core.html#core-ID-7C603781
  238. Since:
  239. */
  240. PHP_FUNCTION(dom_characterdata_delete_data)
  241. {
  242. zval *id;
  243. xmlChar *cur, *substring, *second;
  244. xmlNodePtr node;
  245. long offset, count;
  246. int length;
  247. dom_object *intern;
  248. if (zend_parse_method_parameters(ZEND_NUM_ARGS() TSRMLS_CC, getThis(), "Oll", &id, dom_characterdata_class_entry, &offset, &count) == FAILURE) {
  249. return;
  250. }
  251. DOM_GET_OBJ(node, id, xmlNodePtr, intern);
  252. cur = xmlNodeGetContent(node);
  253. if (cur == NULL) {
  254. RETURN_FALSE;
  255. }
  256. length = xmlUTF8Strlen(cur);
  257. if (offset < 0 || count < 0 || offset > length) {
  258. xmlFree(cur);
  259. php_dom_throw_error(INDEX_SIZE_ERR, dom_get_strict_error(intern->document) TSRMLS_CC);
  260. RETURN_FALSE;
  261. }
  262. if (offset > 0) {
  263. substring = xmlUTF8Strsub(cur, 0, offset);
  264. } else {
  265. substring = NULL;
  266. }
  267. if ((offset + count) > length) {
  268. count = length - offset;
  269. }
  270. second = xmlUTF8Strsub(cur, offset + count, length - offset);
  271. substring = xmlStrcat(substring, second);
  272. xmlNodeSetContent(node, substring);
  273. xmlFree(cur);
  274. xmlFree(second);
  275. xmlFree(substring);
  276. RETURN_TRUE;
  277. }
  278. /* }}} end dom_characterdata_delete_data */
  279. /* {{{ proto void dom_characterdata_replace_data(int offset, int count, string arg);
  280. URL: http://www.w3.org/TR/2003/WD-DOM-Level-3-Core-20030226/DOM3-Core.html#core-ID-E5CBA7FB
  281. Since:
  282. */
  283. PHP_FUNCTION(dom_characterdata_replace_data)
  284. {
  285. zval *id;
  286. xmlChar *cur, *substring, *second = NULL;
  287. xmlNodePtr node;
  288. char *arg;
  289. long offset, count;
  290. int length, arg_len;
  291. dom_object *intern;
  292. if (zend_parse_method_parameters(ZEND_NUM_ARGS() TSRMLS_CC, getThis(), "Olls", &id, dom_characterdata_class_entry, &offset, &count, &arg, &arg_len) == FAILURE) {
  293. return;
  294. }
  295. DOM_GET_OBJ(node, id, xmlNodePtr, intern);
  296. cur = xmlNodeGetContent(node);
  297. if (cur == NULL) {
  298. RETURN_FALSE;
  299. }
  300. length = xmlUTF8Strlen(cur);
  301. if (offset < 0 || count < 0 || offset > length) {
  302. xmlFree(cur);
  303. php_dom_throw_error(INDEX_SIZE_ERR, dom_get_strict_error(intern->document) TSRMLS_CC);
  304. RETURN_FALSE;
  305. }
  306. if (offset > 0) {
  307. substring = xmlUTF8Strsub(cur, 0, offset);
  308. } else {
  309. substring = NULL;
  310. }
  311. if ((offset + count) > length) {
  312. count = length - offset;
  313. }
  314. if (offset < length) {
  315. second = xmlUTF8Strsub(cur, offset + count, length - offset);
  316. }
  317. substring = xmlStrcat(substring, arg);
  318. substring = xmlStrcat(substring, second);
  319. xmlNodeSetContent(node, substring);
  320. xmlFree(cur);
  321. if (second) {
  322. xmlFree(second);
  323. }
  324. xmlFree(substring);
  325. RETURN_TRUE;
  326. }
  327. /* }}} end dom_characterdata_replace_data */
  328. #endif
  329. /*
  330. * Local variables:
  331. * tab-width: 4
  332. * c-basic-offset: 4
  333. * End:
  334. * vim600: noet sw=4 ts=4 fdm=marker
  335. * vim<600: noet sw=4 ts=4
  336. */