parentnode.c 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411
  1. /*
  2. +----------------------------------------------------------------------+
  3. | PHP Version 7 |
  4. +----------------------------------------------------------------------+
  5. | Copyright (c) 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. | https://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: Benjamin Eberlei <beberlei@php.net> |
  16. +----------------------------------------------------------------------+
  17. */
  18. #ifdef HAVE_CONFIG_H
  19. #include "config.h"
  20. #endif
  21. #include "php.h"
  22. #if defined(HAVE_LIBXML) && defined(HAVE_DOM)
  23. #include "php_dom.h"
  24. /* {{{ firstElementChild DomParentNode
  25. readonly=yes
  26. URL: https://www.w3.org/TR/dom/#dom-parentnode-firstelementchild
  27. */
  28. int dom_parent_node_first_element_child_read(dom_object *obj, zval *retval)
  29. {
  30. xmlNode *nodep, *first = NULL;
  31. nodep = dom_object_get_node(obj);
  32. if (nodep == NULL) {
  33. php_dom_throw_error(INVALID_STATE_ERR, 1);
  34. return FAILURE;
  35. }
  36. if (dom_node_children_valid(nodep) == SUCCESS) {
  37. first = nodep->children;
  38. while (first && first->type != XML_ELEMENT_NODE) {
  39. first = first->next;
  40. }
  41. }
  42. if (!first) {
  43. ZVAL_NULL(retval);
  44. return SUCCESS;
  45. }
  46. php_dom_create_object(first, retval, obj);
  47. return SUCCESS;
  48. }
  49. /* }}} */
  50. /* {{{ lastElementChild DomParentNode
  51. readonly=yes
  52. URL: https://www.w3.org/TR/dom/#dom-parentnode-lastelementchild
  53. */
  54. int dom_parent_node_last_element_child_read(dom_object *obj, zval *retval)
  55. {
  56. xmlNode *nodep, *last = NULL;
  57. nodep = dom_object_get_node(obj);
  58. if (nodep == NULL) {
  59. php_dom_throw_error(INVALID_STATE_ERR, 1);
  60. return FAILURE;
  61. }
  62. if (dom_node_children_valid(nodep) == SUCCESS) {
  63. last = nodep->last;
  64. while (last && last->type != XML_ELEMENT_NODE) {
  65. last = last->prev;
  66. }
  67. }
  68. if (!last) {
  69. ZVAL_NULL(retval);
  70. return SUCCESS;
  71. }
  72. php_dom_create_object(last, retval, obj);
  73. return SUCCESS;
  74. }
  75. /* }}} */
  76. /* {{{ childElementCount DomParentNode
  77. readonly=yes
  78. https://www.w3.org/TR/dom/#dom-parentnode-childelementcount
  79. */
  80. int dom_parent_node_child_element_count(dom_object *obj, zval *retval)
  81. {
  82. xmlNode *nodep, *first = NULL;
  83. zend_long count = 0;
  84. nodep = dom_object_get_node(obj);
  85. if (nodep == NULL) {
  86. php_dom_throw_error(INVALID_STATE_ERR, 1);
  87. return FAILURE;
  88. }
  89. if (dom_node_children_valid(nodep) == SUCCESS) {
  90. first = nodep->children;
  91. while (first != NULL) {
  92. if (first->type == XML_ELEMENT_NODE) {
  93. count++;
  94. }
  95. first = first->next;
  96. }
  97. }
  98. ZVAL_LONG(retval, count);
  99. return SUCCESS;
  100. }
  101. /* }}} */
  102. xmlNode* dom_zvals_to_fragment(php_libxml_ref_obj *document, xmlNode *contextNode, zval *nodes, int nodesc)
  103. {
  104. int i;
  105. xmlDoc *documentNode;
  106. xmlNode *fragment;
  107. xmlNode *newNode;
  108. zend_class_entry *ce;
  109. dom_object *newNodeObj;
  110. int stricterror;
  111. if (document == NULL) {
  112. php_dom_throw_error(HIERARCHY_REQUEST_ERR, 1);
  113. return NULL;
  114. }
  115. if (contextNode->type == XML_DOCUMENT_NODE || contextNode->type == XML_HTML_DOCUMENT_NODE) {
  116. documentNode = (xmlDoc *) contextNode;
  117. } else {
  118. documentNode = contextNode->doc;
  119. }
  120. fragment = xmlNewDocFragment(documentNode);
  121. if (!fragment) {
  122. return NULL;
  123. }
  124. stricterror = dom_get_strict_error(document);
  125. for (i = 0; i < nodesc; i++) {
  126. if (Z_TYPE(nodes[i]) == IS_OBJECT) {
  127. ce = Z_OBJCE(nodes[i]);
  128. if (instanceof_function(ce, dom_node_class_entry)) {
  129. newNodeObj = Z_DOMOBJ_P(&nodes[i]);
  130. newNode = dom_object_get_node(newNodeObj);
  131. if (newNode->doc != documentNode) {
  132. xmlFree(fragment);
  133. php_dom_throw_error(WRONG_DOCUMENT_ERR, stricterror);
  134. return NULL;
  135. }
  136. if (newNode->parent != NULL) {
  137. xmlUnlinkNode(newNode);
  138. }
  139. newNodeObj->document = document;
  140. xmlSetTreeDoc(newNode, documentNode);
  141. if (newNode->type == XML_ATTRIBUTE_NODE) {
  142. xmlFree(fragment);
  143. php_dom_throw_error(HIERARCHY_REQUEST_ERR, stricterror);
  144. return NULL;
  145. }
  146. if (!xmlAddChild(fragment, newNode)) {
  147. xmlFree(fragment);
  148. php_dom_throw_error(HIERARCHY_REQUEST_ERR, stricterror);
  149. return NULL;
  150. }
  151. continue;
  152. } else {
  153. xmlFree(fragment);
  154. zend_argument_type_error(i + 1, "must be of type DOMNode|string, %s given", zend_zval_type_name(&nodes[i]));
  155. return NULL;
  156. }
  157. } else if (Z_TYPE(nodes[i]) == IS_STRING) {
  158. newNode = xmlNewDocText(documentNode, (xmlChar *) Z_STRVAL(nodes[i]));
  159. xmlSetTreeDoc(newNode, documentNode);
  160. if (!xmlAddChild(fragment, newNode)) {
  161. xmlFree(fragment);
  162. return NULL;
  163. }
  164. } else {
  165. xmlFree(fragment);
  166. zend_argument_type_error(i + 1, "must be of type DOMNode|string, %s given", zend_zval_type_name(&nodes[i]));
  167. return NULL;
  168. }
  169. }
  170. return fragment;
  171. }
  172. static void dom_fragment_assign_parent_node(xmlNodePtr parentNode, xmlNodePtr fragment)
  173. {
  174. xmlNodePtr node = fragment->children;
  175. while (node != NULL) {
  176. node->parent = parentNode;
  177. if (node == fragment->last) {
  178. break;
  179. }
  180. node = node->next;
  181. }
  182. fragment->children = NULL;
  183. fragment->last = NULL;
  184. }
  185. void dom_parent_node_append(dom_object *context, zval *nodes, int nodesc)
  186. {
  187. xmlNode *parentNode = dom_object_get_node(context);
  188. xmlNodePtr newchild, prevsib;
  189. xmlNode *fragment = dom_zvals_to_fragment(context->document, parentNode, nodes, nodesc);
  190. if (fragment == NULL) {
  191. return;
  192. }
  193. newchild = fragment->children;
  194. prevsib = parentNode->last;
  195. if (newchild) {
  196. if (prevsib != NULL) {
  197. prevsib->next = newchild;
  198. } else {
  199. parentNode->children = newchild;
  200. }
  201. parentNode->last = fragment->last;
  202. newchild->prev = prevsib;
  203. dom_fragment_assign_parent_node(parentNode, fragment);
  204. dom_reconcile_ns(parentNode->doc, newchild);
  205. }
  206. xmlFree(fragment);
  207. }
  208. void dom_parent_node_prepend(dom_object *context, zval *nodes, int nodesc)
  209. {
  210. xmlNode *parentNode = dom_object_get_node(context);
  211. if (parentNode->children == NULL) {
  212. dom_parent_node_append(context, nodes, nodesc);
  213. return;
  214. }
  215. xmlNodePtr newchild, nextsib;
  216. xmlNode *fragment = dom_zvals_to_fragment(context->document, parentNode, nodes, nodesc);
  217. if (fragment == NULL) {
  218. return;
  219. }
  220. newchild = fragment->children;
  221. nextsib = parentNode->children;
  222. if (newchild) {
  223. parentNode->children = newchild;
  224. fragment->last->next = nextsib;
  225. nextsib->prev = fragment->last;
  226. dom_fragment_assign_parent_node(parentNode, fragment);
  227. dom_reconcile_ns(parentNode->doc, newchild);
  228. }
  229. xmlFree(fragment);
  230. }
  231. void dom_parent_node_after(dom_object *context, zval *nodes, int nodesc)
  232. {
  233. xmlNode *prevsib = dom_object_get_node(context);
  234. xmlNodePtr newchild, parentNode;
  235. xmlNode *fragment;
  236. int stricterror = dom_get_strict_error(context->document);
  237. if (!prevsib->parent) {
  238. php_dom_throw_error(NO_MODIFICATION_ALLOWED_ERR, stricterror);
  239. return;
  240. }
  241. parentNode = prevsib->parent;
  242. fragment = dom_zvals_to_fragment(context->document, parentNode, nodes, nodesc);
  243. if (fragment == NULL) {
  244. return;
  245. }
  246. newchild = fragment->children;
  247. if (newchild) {
  248. fragment->last->next = prevsib->next;
  249. prevsib->next = newchild;
  250. newchild->prev = prevsib;
  251. dom_fragment_assign_parent_node(parentNode, fragment);
  252. dom_reconcile_ns(prevsib->doc, newchild);
  253. }
  254. xmlFree(fragment);
  255. }
  256. void dom_parent_node_before(dom_object *context, zval *nodes, int nodesc)
  257. {
  258. xmlNode *nextsib = dom_object_get_node(context);
  259. xmlNodePtr newchild, prevsib, parentNode;
  260. xmlNode *fragment;
  261. prevsib = nextsib->prev;
  262. parentNode = nextsib->parent;
  263. fragment = dom_zvals_to_fragment(context->document, parentNode, nodes, nodesc);
  264. if (fragment == NULL) {
  265. return;
  266. }
  267. newchild = fragment->children;
  268. if (newchild) {
  269. if (parentNode->children == nextsib) {
  270. parentNode->children = newchild;
  271. } else {
  272. prevsib->next = newchild;
  273. }
  274. fragment->last->next = nextsib;
  275. nextsib->prev = fragment->last;
  276. newchild->prev = prevsib;
  277. dom_fragment_assign_parent_node(parentNode, fragment);
  278. dom_reconcile_ns(nextsib->doc, newchild);
  279. }
  280. xmlFree(fragment);
  281. }
  282. void dom_child_node_remove(dom_object *context)
  283. {
  284. xmlNode *child = dom_object_get_node(context);
  285. xmlNodePtr children;
  286. int stricterror;
  287. stricterror = dom_get_strict_error(context->document);
  288. if (dom_node_is_read_only(child) == SUCCESS ||
  289. (child->parent != NULL && dom_node_is_read_only(child->parent) == SUCCESS)) {
  290. php_dom_throw_error(NO_MODIFICATION_ALLOWED_ERR, stricterror);
  291. return;
  292. }
  293. if (!child->parent) {
  294. php_dom_throw_error(NOT_FOUND_ERR, stricterror);
  295. return;
  296. }
  297. if (dom_node_children_valid(child->parent) == FAILURE) {
  298. return;
  299. }
  300. children = child->parent->children;
  301. if (!children) {
  302. php_dom_throw_error(NOT_FOUND_ERR, stricterror);
  303. return;
  304. }
  305. while (children) {
  306. if (children == child) {
  307. xmlUnlinkNode(child);
  308. return;
  309. }
  310. children = children->next;
  311. }
  312. php_dom_throw_error(NOT_FOUND_ERR, stricterror);
  313. }
  314. #endif