1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804
|
/*
+----------------------------------------------------------------------+
| Copyright (c) The PHP Group |
+----------------------------------------------------------------------+
| This source file is subject to version 3.01 of the PHP license, |
| that is bundled with this package in the file LICENSE, and is |
| available through the world-wide-web at the following url: |
| https://www.php.net/license/3_01.txt |
| If you did not receive a copy of the PHP license and are unable to |
| obtain it through the world-wide-web, please send a note to |
| license@php.net so we can mail you a copy immediately. |
+----------------------------------------------------------------------+
| Authors: Benjamin Eberlei <beberlei@php.net> |
| Niels Dossche <nielsdos@php.net> |
+----------------------------------------------------------------------+
*/
#ifdef HAVE_CONFIG_H
#include <config.h>
#endif
#include "php.h"
#if defined(HAVE_LIBXML) && defined(HAVE_DOM)
#include "../php_dom.h"
#include "../internal_helpers.h"
#include "../dom_properties.h"
/* {{{ firstElementChild DomParentNode
readonly=yes
URL: https://www.w3.org/TR/dom/#dom-parentnode-firstelementchild
*/
zend_result dom_parent_node_first_element_child_read(dom_object *obj, zval *retval)
{
DOM_PROP_NODE(xmlNodePtr, nodep, obj);
xmlNodePtr first = nodep->children;
while (first && first->type != XML_ELEMENT_NODE) {
first = first->next;
}
php_dom_create_nullable_object(first, retval, obj);
return SUCCESS;
}
/* }}} */
/* {{{ lastElementChild DomParentNode
readonly=yes
URL: https://www.w3.org/TR/dom/#dom-parentnode-lastelementchild
*/
zend_result dom_parent_node_last_element_child_read(dom_object *obj, zval *retval)
{
DOM_PROP_NODE(xmlNodePtr, nodep, obj);
xmlNodePtr last = nodep->last;
while (last && last->type != XML_ELEMENT_NODE) {
last = last->prev;
}
php_dom_create_nullable_object(last, retval, obj);
return SUCCESS;
}
/* }}} */
/* {{{ childElementCount DomParentNode
readonly=yes
https://www.w3.org/TR/dom/#dom-parentnode-childelementcount
*/
zend_result dom_parent_node_child_element_count(dom_object *obj, zval *retval)
{
DOM_PROP_NODE(xmlNodePtr, nodep, obj);
zend_long count = 0;
xmlNodePtr first = nodep->children;
while (first != NULL) {
if (first->type == XML_ELEMENT_NODE) {
count++;
}
first = first->next;
}
ZVAL_LONG(retval, count);
return SUCCESS;
}
/* }}} */
static ZEND_COLD void dom_cannot_create_temp_nodes(void)
{
php_dom_throw_error_with_message(INVALID_MODIFICATION_ERR, "Unable to allocate temporary nodes", /* strict */ true);
}
static bool dom_is_node_in_list(const zval *nodes, uint32_t nodesc, const xmlNode *node_to_find)
{
for (uint32_t i = 0; i < nodesc; i++) {
if (Z_TYPE(nodes[i]) == IS_OBJECT) {
if (dom_object_get_node(Z_DOMOBJ_P(nodes + i)) == node_to_find) {
return true;
}
}
}
return false;
}
static xmlDocPtr dom_doc_from_context_node(xmlNodePtr contextNode)
{
if (contextNode->type == XML_DOCUMENT_NODE || contextNode->type == XML_HTML_DOCUMENT_NODE) {
return (xmlDocPtr) contextNode;
} else {
return contextNode->doc;
}
}
/* Citing from the docs (https://gnome.pages.gitlab.gnome.org/libxml2/devhelp/libxml2-tree.html#xmlAddChild):
* "Add a new node to @parent, at the end of the child (or property) list merging adjacent TEXT nodes (in which case @cur is freed)".
* So we must use a custom way of adding that does not merge. */
static void dom_add_child_without_merging(xmlNodePtr parent, xmlNodePtr child)
{
if (parent->children == NULL) {
parent->children = child;
} else {
xmlNodePtr last = parent->last;
last->next = child;
child->prev = last;
}
parent->last = child;
child->parent = parent;
}
static void dom_fragment_assign_parent_node(xmlNodePtr parentNode, xmlNodePtr fragment)
{
xmlNodePtr node = fragment->children;
while (node != NULL) {
node->parent = parentNode;
if (node == fragment->last) {
break;
}
node = node->next;
}
}
/* This part is common logic between the pre-insertion validity and replaceChild code. */
static bool dom_fragment_common_hierarchy_check_part(xmlNodePtr node, bool *seen_element)
{
/* If node has more than one element child or has a Text node child. */
xmlNodePtr iter = node->children;
*seen_element = false;
while (iter != NULL) {
if (iter->type == XML_ELEMENT_NODE) {
if (*seen_element) {
php_dom_throw_error_with_message(HIERARCHY_REQUEST_ERR, "Cannot have more than one element child in a document", /* strict */ true);
return false;
}
*seen_element = true;
} else if (iter->type == XML_TEXT_NODE || iter->type == XML_CDATA_SECTION_NODE) {
php_dom_throw_error_with_message(HIERARCHY_REQUEST_ERR, "Cannot insert text as a child of a document", /* strict */ true);
return false;
}
iter = iter->next;
}
return true;
}
/* https://dom.spec.whatwg.org/#concept-node-ensure-pre-insertion-validity
* DocumentFragment validation part. */
bool php_dom_fragment_insertion_hierarchy_check_pre_insertion(xmlNodePtr parent, xmlNodePtr node, xmlNodePtr child)
{
bool seen_element;
if (!dom_fragment_common_hierarchy_check_part(node, &seen_element)) {
return false;
}
/* Otherwise, if node has one element child
* and either parent has an element child, child is a doctype, or child is non-null and a doctype is following child. */
if (seen_element) {
if (php_dom_has_child_of_type(parent, XML_ELEMENT_NODE)) {
php_dom_throw_error_with_message(HIERARCHY_REQUEST_ERR, "Cannot have more than one element child in a document", /* strict */ true);
return false;
}
if (child != NULL && (child->type == XML_DTD_NODE || php_dom_has_sibling_following_node(child, XML_DTD_NODE))) {
php_dom_throw_error_with_message(HIERARCHY_REQUEST_ERR, "Document types must be the first child in a document", /* strict */ true);
return false;
}
}
return true;
}
/* https://dom.spec.whatwg.org/#concept-node-replace
* DocumentFragment validation part. */
bool php_dom_fragment_insertion_hierarchy_check_replace(xmlNodePtr parent, xmlNodePtr node, xmlNodePtr child)
{
bool seen_element;
if (!dom_fragment_common_hierarchy_check_part(node, &seen_element)) {
return false;
}
/* Otherwise, if node has one element child
* and either parent has an element child that is not child or a doctype is following child. */
if (seen_element) {
xmlNodePtr iter = parent->children;
while (iter != NULL) {
if (iter->type == XML_ELEMENT_NODE && iter != child) {
php_dom_throw_error_with_message(HIERARCHY_REQUEST_ERR, "Cannot have more than one element child in a document", /* strict */ true);
return false;
}
iter = iter->next;
}
ZEND_ASSERT(child != NULL);
if (php_dom_has_sibling_following_node(child, XML_DTD_NODE)) {
php_dom_throw_error_with_message(HIERARCHY_REQUEST_ERR, "Document types must be the first child in a document", /* strict */ true);
return false;
}
}
return true;
}
/* https://dom.spec.whatwg.org/#concept-node-ensure-pre-insertion-validity */
bool php_dom_pre_insert_is_parent_invalid(xmlNodePtr parent)
{
return parent->type != XML_DOCUMENT_NODE
&& parent->type != XML_HTML_DOCUMENT_NODE
&& parent->type != XML_ELEMENT_NODE
&& parent->type != XML_DOCUMENT_FRAG_NODE;
}
/* https://dom.spec.whatwg.org/#concept-node-ensure-pre-insertion-validity */
static bool dom_is_pre_insert_valid_without_step_1(php_libxml_ref_obj *document, xmlNodePtr parentNode, xmlNodePtr node, xmlNodePtr child, xmlDocPtr documentNode)
{
ZEND_ASSERT(parentNode != NULL);
/* 1. If parent is not a Document, DocumentFragment, or Element node, then throw a "HierarchyRequestError" DOMException.
* => This is possible because we can grab children of attributes etc... (see e.g. GH-16594) */
if (php_dom_pre_insert_is_parent_invalid(parentNode)) {
php_dom_throw_error(HIERARCHY_REQUEST_ERR, dom_get_strict_error(document));
return false;
}
if (node->doc != documentNode) {
php_dom_throw_error(WRONG_DOCUMENT_ERR, dom_get_strict_error(document));
return false;
}
/* 3. If child is non-null and its parent is not parent, then throw a "NotFoundError" DOMException. */
if (child != NULL && child->parent != parentNode) {
php_dom_throw_error(NOT_FOUND_ERR, dom_get_strict_error(document));
return false;
}
bool parent_is_document = parentNode->type == XML_DOCUMENT_NODE || parentNode->type == XML_HTML_DOCUMENT_NODE;
if (/* 2. If node is a host-including inclusive ancestor of parent, then throw a "HierarchyRequestError" DOMException. */
dom_hierarchy(parentNode, node) != SUCCESS
/* 4. If node is not a DocumentFragment, DocumentType, Element, or CharacterData node, then throw a "HierarchyRequestError" DOMException. */
|| node->type == XML_ATTRIBUTE_NODE
|| (php_dom_follow_spec_doc_ref(document) && (
node->type == XML_ENTITY_REF_NODE
|| node->type == XML_ENTITY_NODE
|| node->type == XML_NOTATION_NODE
|| node->type == XML_DOCUMENT_NODE
|| node->type == XML_HTML_DOCUMENT_NODE
|| node->type >= XML_ELEMENT_DECL))) {
php_dom_throw_error(HIERARCHY_REQUEST_ERR, dom_get_strict_error(document));
return false;
}
if (php_dom_follow_spec_doc_ref(document)) {
/* 5. If either node is a Text node and parent is a document... */
if (parent_is_document && (node->type == XML_TEXT_NODE || node->type == XML_CDATA_SECTION_NODE)) {
php_dom_throw_error_with_message(HIERARCHY_REQUEST_ERR, "Cannot insert text as a child of a document", /* strict */ true);
return false;
}
/* 5. ..., or node is a doctype and parent is not a document, then throw a "HierarchyRequestError" DOMException. */
if (!parent_is_document && node->type == XML_DTD_NODE) {
php_dom_throw_error_with_message(HIERARCHY_REQUEST_ERR, "Cannot insert a document type into anything other than a document", /* strict */ true);
return false;
}
/* 6. If parent is a document, and any of the statements below, switched on the interface node implements,
* are true, then throw a "HierarchyRequestError" DOMException. */
if (parent_is_document) {
/* DocumentFragment */
if (node->type == XML_DOCUMENT_FRAG_NODE) {
if (!php_dom_fragment_insertion_hierarchy_check_pre_insertion(parentNode, node, child)) {
return false;
}
}
/* Element */
else if (node->type == XML_ELEMENT_NODE) {
/* parent has an element child, child is a doctype, or child is non-null and a doctype is following child. */
if (php_dom_has_child_of_type(parentNode, XML_ELEMENT_NODE)) {
php_dom_throw_error_with_message(HIERARCHY_REQUEST_ERR, "Cannot have more than one element child in a document", /* strict */ true);
return false;
}
if (child != NULL && (child->type == XML_DTD_NODE || php_dom_has_sibling_following_node(child, XML_DTD_NODE))) {
php_dom_throw_error_with_message(HIERARCHY_REQUEST_ERR, "Document types must be the first child in a document", /* strict */ true);
return false;
}
}
/* DocumentType */
else if (node->type == XML_DTD_NODE) {
/* parent has a doctype child, child is non-null and an element is preceding child, or child is null and parent has an element child. */
if (php_dom_has_child_of_type(parentNode, XML_DTD_NODE)) {
php_dom_throw_error_with_message(HIERARCHY_REQUEST_ERR, "Cannot have more than one document type", /* strict */ true);
return false;
}
if ((child != NULL && php_dom_has_sibling_preceding_node(child, XML_ELEMENT_NODE))
|| (child == NULL && php_dom_has_child_of_type(parentNode, XML_ELEMENT_NODE))) {
php_dom_throw_error_with_message(HIERARCHY_REQUEST_ERR, "Document types must be the first child in a document", /* strict */ true);
return false;
}
}
}
}
return true;
}
static void dom_free_node_after_zval_single_node_creation(xmlNodePtr node)
{
/* For the object cases, the user did provide them, so they don't have to be freed as there are still references.
* For the newly created text nodes, we do have to free them. */
xmlNodePtr next;
for (xmlNodePtr child = node->children; child != NULL; child = next) {
next = child->next;
xmlUnlinkNode(child);
if (child->_private == NULL) {
xmlFreeNode(child);
}
}
}
/* https://dom.spec.whatwg.org/#converting-nodes-into-a-node */
xmlNode* dom_zvals_to_single_node(php_libxml_ref_obj *document, xmlNode *contextNode, zval *nodes, uint32_t nodesc)
{
xmlDoc *documentNode;
xmlNode *newNode;
dom_object *newNodeObj;
documentNode = dom_doc_from_context_node(contextNode);
/* 1. Let node be null. */
xmlNodePtr node = NULL;
/* 2. => handled in the loop. */
/* 3. If nodes contains one node, then set node to nodes[0]. */
if (nodesc == 1) {
/* ... and return */
if (Z_TYPE_P(nodes) == IS_OBJECT) {
return dom_object_get_node(Z_DOMOBJ_P(nodes));
} else {
ZEND_ASSERT(Z_TYPE_P(nodes) == IS_STRING);
node = xmlNewDocTextLen(documentNode, BAD_CAST Z_STRVAL_P(nodes), Z_STRLEN_P(nodes));
if (UNEXPECTED(node == NULL)) {
dom_cannot_create_temp_nodes();
}
return node;
}
}
node = xmlNewDocFragment(documentNode);
if (UNEXPECTED(!node)) {
dom_cannot_create_temp_nodes();
return NULL;
}
/* 4. Otherwise, set node to a new DocumentFragment node whose node document is document,
* and then append each node in nodes, if any, to it. */
for (uint32_t i = 0; i < nodesc; i++) {
if (Z_TYPE(nodes[i]) == IS_OBJECT) {
newNodeObj = Z_DOMOBJ_P(&nodes[i]);
newNode = dom_object_get_node(newNodeObj);
if (UNEXPECTED(!newNode)) {
php_dom_throw_error(INVALID_STATE_ERR, /* strict */ true);
goto err;
}
if (!dom_is_pre_insert_valid_without_step_1(document, node, newNode, NULL, documentNode)) {
goto err;
}
if (newNode->parent != NULL) {
xmlUnlinkNode(newNode);
}
ZEND_ASSERT(newNodeObj->document == document);
if (newNode->type == XML_DOCUMENT_FRAG_NODE) {
/* Unpack document fragment nodes, the behaviour differs for different libxml2 versions. */
newNode = newNode->children;
while (newNode) {
xmlNodePtr next = newNode->next;
xmlUnlinkNode(newNode);
dom_add_child_without_merging(node, newNode);
newNode = next;
}
} else {
dom_add_child_without_merging(node, newNode);
}
} else {
/* 2. Replace each string in nodes with a new Text node whose data is the string and node document is document. */
ZEND_ASSERT(Z_TYPE(nodes[i]) == IS_STRING);
/* Text nodes can't violate the hierarchy at this point. */
newNode = xmlNewDocTextLen(documentNode, BAD_CAST Z_STRVAL(nodes[i]), Z_STRLEN(nodes[i]));
if (UNEXPECTED(newNode == NULL)) {
dom_cannot_create_temp_nodes();
goto err;
}
dom_add_child_without_merging(node, newNode);
}
}
/* 5. Return node. */
return node;
err:
/* For the object cases, the user did provide them, so they don't have to be freed as there are still references.
* For the newly created text nodes, we do have to free them. */
dom_free_node_after_zval_single_node_creation(node);
xmlFree(node);
return NULL;
}
static zend_result dom_sanity_check_node_list_types(zval *nodes, uint32_t nodesc, zend_class_entry *node_ce)
{
for (uint32_t i = 0; i < nodesc; i++) {
zend_uchar type = Z_TYPE(nodes[i]);
if (type == IS_OBJECT) {
const zend_class_entry *ce = Z_OBJCE(nodes[i]);
if (!instanceof_function(ce, node_ce)) {
zend_argument_type_error(i + 1, "must be of type %s|string, %s given", ZSTR_VAL(node_ce->name), zend_zval_type_name(&nodes[i]));
return FAILURE;
}
} else if (type == IS_STRING) {
if (Z_STRLEN(nodes[i]) > INT_MAX) {
zend_argument_value_error(i + 1, "must be less than or equal to %d bytes long", INT_MAX);
return FAILURE;
}
} else {
zend_argument_type_error(i + 1, "must be of type %s|string, %s given", ZSTR_VAL(node_ce->name), zend_zval_type_name(&nodes[i]));
return FAILURE;
}
}
return SUCCESS;
}
static void php_dom_pre_insert_helper(xmlNodePtr insertion_point, xmlNodePtr parentNode, xmlNodePtr newchild, xmlNodePtr last)
{
if (!insertion_point) {
/* Place it as last node */
if (parentNode->children) {
/* There are children */
newchild->prev = parentNode->last;
parentNode->last->next = newchild;
} else {
/* No children, because they moved out when they became a fragment */
parentNode->children = newchild;
}
parentNode->last = last;
} else {
/* Insert fragment before insertion_point */
last->next = insertion_point;
if (insertion_point->prev) {
insertion_point->prev->next = newchild;
newchild->prev = insertion_point->prev;
}
insertion_point->prev = last;
if (parentNode->children == insertion_point) {
parentNode->children = newchild;
}
}
}
static void dom_insert_node_list_cleanup(xmlNodePtr node)
{
if (node->_private != NULL) {
/* Not a temporary node. */
return;
}
if (node->type == XML_DOCUMENT_FRAG_NODE) {
dom_free_node_after_zval_single_node_creation(node);
xmlFree(node); /* Don't free the children, now-empty fragment! */
} else if (node->type == XML_TEXT_NODE) {
ZEND_ASSERT(node->parent == NULL);
xmlFreeNode(node);
} else {
/* Must have been a directly-passed node. */
ZEND_UNREACHABLE();
}
}
/* https://dom.spec.whatwg.org/#concept-node-pre-insert */
static void dom_insert_node_list_unchecked(php_libxml_ref_obj *document, xmlNodePtr node, xmlNodePtr parent, xmlNodePtr insertion_point)
{
/* Step 1 should be checked by the caller. */
if (node->type == XML_DOCUMENT_FRAG_NODE) {
/* Steps 2-3 are not applicable here, the condition is impossible. */
xmlNodePtr newchild = node->children;
/* 4. Insert node into parent before referenceChild (i.e. insertion_point here because of the impossible condition). */
if (newchild) {
xmlNodePtr last = node->last;
php_dom_pre_insert_helper(insertion_point, parent, newchild, last);
dom_fragment_assign_parent_node(parent, node);
if (!php_dom_follow_spec_doc_ref(document)) {
dom_reconcile_ns_list(parent->doc, newchild, last);
}
if (parent->doc && newchild->type == XML_DTD_NODE) {
parent->doc->intSubset = (xmlDtdPtr) newchild;
newchild->parent = (xmlNodePtr) parent->doc;
}
}
if (node->_private == NULL) {
xmlFree(node);
} else {
node->children = NULL;
node->last = NULL;
}
} else {
/* 2. Let referenceChild be child.
* 3. If referenceChild is node, then set referenceChild to node’s next sibling. */
if (insertion_point == node) {
insertion_point = node->next;
}
/* 4. Insert node into parent before referenceChild. */
xmlUnlinkNode(node);
php_dom_pre_insert_helper(insertion_point, parent, node, node);
node->parent = parent;
if (parent->doc && node->type == XML_DTD_NODE) {
parent->doc->intSubset = (xmlDtdPtr) node;
node->parent = (xmlNodePtr) parent->doc;
} else {
if (!php_dom_follow_spec_doc_ref(document)) {
dom_reconcile_ns(parent->doc, node);
}
}
}
}
/* https://dom.spec.whatwg.org/#concept-node-pre-insert */
bool php_dom_pre_insert(php_libxml_ref_obj *document, xmlNodePtr node, xmlNodePtr parent, xmlNodePtr insertion_point)
{
if (UNEXPECTED(node == NULL)) {
return false;
}
/* Step 1 checked here, other steps delegated to other function. */
if (dom_is_pre_insert_valid_without_step_1(document, parent, node, insertion_point, parent->doc)) {
dom_insert_node_list_unchecked(document, node, parent, insertion_point);
return true;
} else {
dom_insert_node_list_cleanup(node);
return false;
}
}
/* https://dom.spec.whatwg.org/#concept-node-append */
void php_dom_node_append(php_libxml_ref_obj *document, xmlNodePtr node, xmlNodePtr parent)
{
php_dom_pre_insert(document, node, parent, NULL);
}
/* https://dom.spec.whatwg.org/#dom-parentnode-append */
void dom_parent_node_append(dom_object *context, zval *nodes, uint32_t nodesc)
{
if (UNEXPECTED(dom_sanity_check_node_list_types(nodes, nodesc, dom_get_node_ce(php_dom_follow_spec_doc_ref(context->document))) != SUCCESS)) {
return;
}
xmlNode *parentNode = dom_object_get_node(context);
php_libxml_invalidate_node_list_cache(context->document);
/* 1. Let node be the result of converting nodes into a node given nodes and this’s node document. */
xmlNodePtr node = dom_zvals_to_single_node(context->document, parentNode, nodes, nodesc);
if (UNEXPECTED(node == NULL)) {
return;
}
/* 2. Append node to this. */
php_dom_node_append(context->document, node, parentNode);
}
/* https://dom.spec.whatwg.org/#dom-parentnode-prepend */
void dom_parent_node_prepend(dom_object *context, zval *nodes, uint32_t nodesc)
{
xmlNode *parentNode = dom_object_get_node(context);
if (parentNode->children == NULL) {
dom_parent_node_append(context, nodes, nodesc);
return;
}
if (UNEXPECTED(dom_sanity_check_node_list_types(nodes, nodesc, dom_get_node_ce(php_dom_follow_spec_doc_ref(context->document))) != SUCCESS)) {
return;
}
php_libxml_invalidate_node_list_cache(context->document);
/* 1. Let node be the result of converting nodes into a node given nodes and this’s node document. */
xmlNodePtr node = dom_zvals_to_single_node(context->document, parentNode, nodes, nodesc);
if (UNEXPECTED(node == NULL)) {
return;
}
/* 2. Pre-insert node into this before this’s first child. */
php_dom_pre_insert(context->document, node, parentNode, parentNode->children);
}
/* https://dom.spec.whatwg.org/#dom-childnode-after */
void dom_parent_node_after(dom_object *context, zval *nodes, uint32_t nodesc)
{
if (UNEXPECTED(dom_sanity_check_node_list_types(nodes, nodesc, dom_get_node_ce(php_dom_follow_spec_doc_ref(context->document))) != SUCCESS)) {
return;
}
xmlNode *thisp = dom_object_get_node(context);
/* 1. Let parent be this’s parent. */
xmlNodePtr parentNode = thisp->parent;
/* 2. If parent is null, then return. */
if (UNEXPECTED(parentNode == NULL)) {
return;
}
/* 3. Let viableNextSibling be this’s first following sibling not in nodes; otherwise null. */
xmlNodePtr viable_next_sibling = thisp->next;
while (viable_next_sibling && dom_is_node_in_list(nodes, nodesc, viable_next_sibling)) {
viable_next_sibling = viable_next_sibling->next;
}
php_libxml_invalidate_node_list_cache(context->document);
/* 4. Let node be the result of converting nodes into a node, given nodes and this’s node document. */
xmlNodePtr fragment = dom_zvals_to_single_node(context->document, parentNode, nodes, nodesc);
/* 5. Pre-insert node into parent before viableNextSibling. */
php_dom_pre_insert(context->document, fragment, parentNode, viable_next_sibling);
}
/* https://dom.spec.whatwg.org/#dom-childnode-before */
void dom_parent_node_before(dom_object *context, zval *nodes, uint32_t nodesc)
{
if (UNEXPECTED(dom_sanity_check_node_list_types(nodes, nodesc, dom_get_node_ce(php_dom_follow_spec_doc_ref(context->document))) != SUCCESS)) {
return;
}
xmlNode *thisp = dom_object_get_node(context);
/* 1. Let parent be this’s parent. */
xmlNodePtr parentNode = thisp->parent;
/* 2. If parent is null, then return. */
if (UNEXPECTED(parentNode == NULL)) {
return;
}
/* 3. Let viablePreviousSibling be this’s first preceding sibling not in nodes; otherwise null. */
xmlNodePtr viable_previous_sibling = thisp->prev;
while (viable_previous_sibling && dom_is_node_in_list(nodes, nodesc, viable_previous_sibling)) {
viable_previous_sibling = viable_previous_sibling->prev;
}
php_libxml_invalidate_node_list_cache(context->document);
/* 4. Let node be the result of converting nodes into a node, given nodes and this’s node document. */
xmlNodePtr fragment = dom_zvals_to_single_node(context->document, parentNode, nodes, nodesc);
/* 5. If viable_previous_sibling is null, set it to the parent's first child, otherwise viable_previous_sibling's next sibling. */
if (!viable_previous_sibling) {
viable_previous_sibling = parentNode->children;
} else {
viable_previous_sibling = viable_previous_sibling->next;
}
/* 6. Pre-insert node into parent before viablePreviousSibling. */
php_dom_pre_insert(context->document, fragment, parentNode, viable_previous_sibling);
}
static zend_result dom_child_removal_preconditions(const xmlNode *child, const dom_object *context)
{
if (dom_node_is_read_only(child) == SUCCESS ||
(child->parent != NULL && dom_node_is_read_only(child->parent) == SUCCESS)) {
php_dom_throw_error(NO_MODIFICATION_ALLOWED_ERR, dom_get_strict_error(context->document));
return FAILURE;
}
if (!child->parent) {
php_dom_throw_error(NOT_FOUND_ERR, dom_get_strict_error(context->document));
return FAILURE;
}
return SUCCESS;
}
void dom_child_node_remove(dom_object *context)
{
xmlNode *child = dom_object_get_node(context);
if (UNEXPECTED(dom_child_removal_preconditions(child, context) != SUCCESS)) {
return;
}
php_libxml_invalidate_node_list_cache(context->document);
xmlUnlinkNode(child);
}
/* https://dom.spec.whatwg.org/#dom-childnode-replacewith */
void dom_child_replace_with(dom_object *context, zval *nodes, uint32_t nodesc)
{
if (UNEXPECTED(dom_sanity_check_node_list_types(nodes, nodesc, dom_get_node_ce(php_dom_follow_spec_doc_ref(context->document))) != SUCCESS)) {
return;
}
xmlNodePtr child = dom_object_get_node(context);
/* 1. Let parent be this’s parent. */
xmlNodePtr parentNode = child->parent;
/* 2. If parent is null, then return. */
if (UNEXPECTED(parentNode == NULL)) {
return;
}
/* 3. Let viableNextSibling be this’s first following sibling not in nodes; otherwise null. */
xmlNodePtr viable_next_sibling = child->next;
while (viable_next_sibling && dom_is_node_in_list(nodes, nodesc, viable_next_sibling)) {
viable_next_sibling = viable_next_sibling->next;
}
if (UNEXPECTED(dom_child_removal_preconditions(child, context) != SUCCESS)) {
return;
}
php_libxml_invalidate_node_list_cache(context->document);
/* 4. Let node be the result of converting nodes into a node, given nodes and this’s node document. */
xmlNodePtr node = dom_zvals_to_single_node(context->document, parentNode, nodes, nodesc);
if (UNEXPECTED(node == NULL)) {
return;
}
/* Spec step 5-6: perform the replacement */
if (dom_is_pre_insert_valid_without_step_1(context->document, parentNode, node, viable_next_sibling, parentNode->doc)) {
/* Unlink it unless it became a part of the fragment.
* Freeing will be taken care of by the lifetime of the returned dom object. */
if (child->parent != node) {
xmlUnlinkNode(child);
}
dom_insert_node_list_unchecked(context->document, node, parentNode, viable_next_sibling);
} else {
dom_insert_node_list_cleanup(node);
}
}
/* https://dom.spec.whatwg.org/#dom-parentnode-replacechildren */
void dom_parent_node_replace_children(dom_object *context, zval *nodes, uint32_t nodesc)
{
if (UNEXPECTED(dom_sanity_check_node_list_types(nodes, nodesc, dom_get_node_ce(php_dom_follow_spec_doc_ref(context->document))) != SUCCESS)) {
return;
}
xmlNodePtr thisp = dom_object_get_node(context);
php_libxml_invalidate_node_list_cache(context->document);
/* 1. Let node be the result of converting nodes into a node given nodes and this’s node document. */
xmlNodePtr node = dom_zvals_to_single_node(context->document, thisp, nodes, nodesc);
if (UNEXPECTED(node == NULL)) {
return;
}
/* Spec steps 2-3: replace all */
if (dom_is_pre_insert_valid_without_step_1(context->document, thisp, node, NULL, thisp->doc)) {
dom_remove_all_children(thisp);
php_dom_pre_insert(context->document, node, thisp, NULL);
} else {
dom_insert_node_list_cleanup(node);
}
}
#endif
|