documentfragment.c 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175
  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. #ifdef HAVE_CONFIG_H
  18. #include "config.h"
  19. #endif
  20. #include "php.h"
  21. #if defined(HAVE_LIBXML) && defined(HAVE_DOM)
  22. #include "php_dom.h"
  23. /*
  24. * class DOMDocumentFragment extends DOMNode
  25. *
  26. * URL: https://www.w3.org/TR/2003/WD-DOM-Level-3-Core-20030226/DOM3-Core.html#ID-B63ED1A3
  27. * Since:
  28. */
  29. /* {{{ */
  30. PHP_METHOD(DOMDocumentFragment, __construct)
  31. {
  32. xmlNodePtr nodep = NULL, oldnode = NULL;
  33. dom_object *intern;
  34. if (zend_parse_parameters_none() == FAILURE) {
  35. RETURN_THROWS();
  36. }
  37. nodep = xmlNewDocFragment(NULL);
  38. if (!nodep) {
  39. php_dom_throw_error(INVALID_STATE_ERR, 1);
  40. RETURN_THROWS();
  41. }
  42. intern = Z_DOMOBJ_P(ZEND_THIS);
  43. oldnode = dom_object_get_node(intern);
  44. if (oldnode != NULL) {
  45. php_libxml_node_free_resource(oldnode );
  46. }
  47. /* php_dom_set_object(intern, nodep); */
  48. php_libxml_increment_node_ptr((php_libxml_node_object *)intern, nodep, (void *)intern);
  49. }
  50. /* }}} end DOMDocumentFragment::__construct */
  51. /* php_dom_xmlSetTreeDoc is a custom implementation of xmlSetTreeDoc
  52. needed for hack in appendXML due to libxml bug - no need to share this function */
  53. static void php_dom_xmlSetTreeDoc(xmlNodePtr tree, xmlDocPtr doc) /* {{{ */
  54. {
  55. xmlAttrPtr prop;
  56. xmlNodePtr cur;
  57. if (tree) {
  58. if(tree->type == XML_ELEMENT_NODE) {
  59. prop = tree->properties;
  60. while (prop != NULL) {
  61. prop->doc = doc;
  62. if (prop->children) {
  63. cur = prop->children;
  64. while (cur != NULL) {
  65. php_dom_xmlSetTreeDoc(cur, doc);
  66. cur = cur->next;
  67. }
  68. }
  69. prop = prop->next;
  70. }
  71. }
  72. if (tree->children != NULL) {
  73. cur = tree->children;
  74. while (cur != NULL) {
  75. php_dom_xmlSetTreeDoc(cur, doc);
  76. cur = cur->next;
  77. }
  78. }
  79. tree->doc = doc;
  80. }
  81. }
  82. /* }}} */
  83. /* {{{ */
  84. PHP_METHOD(DOMDocumentFragment, appendXML) {
  85. zval *id;
  86. xmlNode *nodep;
  87. dom_object *intern;
  88. char *data = NULL;
  89. size_t data_len = 0;
  90. int err;
  91. xmlNodePtr lst;
  92. id = ZEND_THIS;
  93. if (zend_parse_parameters(ZEND_NUM_ARGS(), "s", &data, &data_len) == FAILURE) {
  94. RETURN_THROWS();
  95. }
  96. DOM_GET_OBJ(nodep, id, xmlNodePtr, intern);
  97. if (dom_node_is_read_only(nodep) == SUCCESS) {
  98. php_dom_throw_error(NO_MODIFICATION_ALLOWED_ERR, dom_get_strict_error(intern->document));
  99. RETURN_FALSE;
  100. }
  101. if (data) {
  102. err = xmlParseBalancedChunkMemory(nodep->doc, NULL, NULL, 0, (xmlChar *) data, &lst);
  103. if (err != 0) {
  104. RETURN_FALSE;
  105. }
  106. /* Following needed due to bug in libxml2 <= 2.6.14
  107. ifdef after next libxml release as bug is fixed in their cvs */
  108. php_dom_xmlSetTreeDoc(lst, nodep->doc);
  109. /* End stupid hack */
  110. xmlAddChildList(nodep,lst);
  111. }
  112. RETURN_TRUE;
  113. }
  114. /* }}} */
  115. /* {{{ URL: https://dom.spec.whatwg.org/#dom-parentnode-append
  116. Since: DOM Living Standard (DOM4)
  117. */
  118. PHP_METHOD(DOMDocumentFragment, append)
  119. {
  120. int argc;
  121. zval *args, *id;
  122. dom_object *intern;
  123. xmlNode *context;
  124. if (zend_parse_parameters(ZEND_NUM_ARGS(), "+", &args, &argc) == FAILURE) {
  125. RETURN_THROWS();
  126. }
  127. id = ZEND_THIS;
  128. DOM_GET_OBJ(context, id, xmlNodePtr, intern);
  129. dom_parent_node_append(intern, args, argc);
  130. }
  131. /* }}} */
  132. /* {{{ URL: https://dom.spec.whatwg.org/#dom-parentnode-prepend
  133. Since: DOM Living Standard (DOM4)
  134. */
  135. PHP_METHOD(DOMDocumentFragment, prepend)
  136. {
  137. int argc;
  138. zval *args, *id;
  139. dom_object *intern;
  140. xmlNode *context;
  141. if (zend_parse_parameters(ZEND_NUM_ARGS(), "+", &args, &argc) == FAILURE) {
  142. RETURN_THROWS();
  143. }
  144. id = ZEND_THIS;
  145. DOM_GET_OBJ(context, id, xmlNodePtr, intern);
  146. dom_parent_node_prepend(intern, args, argc);
  147. }
  148. /* }}} */
  149. #endif