Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix bug #81642: DOMChildNode::replaceWith() bug when replacing a node with itself #11363

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
5 changes: 2 additions & 3 deletions ext/dom/element.c
Expand Up @@ -1234,7 +1234,7 @@ PHP_METHOD(DOMElement, prepend)
}
/* }}} end DOMElement::prepend */

/* {{{ URL: https://dom.spec.whatwg.org/#dom-parentnode-prepend
/* {{{ URL: https://dom.spec.whatwg.org/#dom-parentnode-replacechildren
Since: DOM Living Standard (DOM4)
*/
PHP_METHOD(DOMElement, replaceWith)
Expand All @@ -1251,8 +1251,7 @@ PHP_METHOD(DOMElement, replaceWith)
id = ZEND_THIS;
DOM_GET_OBJ(context, id, xmlNodePtr, intern);

dom_parent_node_after(intern, args, argc);
dom_child_node_remove(intern);
dom_child_replace_with(intern, args, argc);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Unrelated, but the argc variable should be of type uint32_t

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, something for a follow-up.

}
/* }}} end DOMElement::prepend */

Expand Down
66 changes: 43 additions & 23 deletions ext/dom/node.c
Expand Up @@ -943,12 +943,20 @@ PHP_METHOD(DOMNode, insertBefore)
return;
}
}
new_child = xmlAddPrevSibling(refp, child);
if (UNEXPECTED(NULL == new_child)) {
goto cannot_add;
}
} else if (child->type == XML_DOCUMENT_FRAG_NODE) {
xmlNodePtr last = child->last;
new_child = _php_dom_insert_fragment(parentp, refp->prev, refp, child, intern, childobj);
}

if (new_child == NULL) {
dom_reconcile_ns_list(parentp->doc, new_child, last);
} else {
new_child = xmlAddPrevSibling(refp, child);
if (UNEXPECTED(NULL == new_child)) {
goto cannot_add;
}
dom_reconcile_ns(parentp->doc, new_child);
}
} else {
if (child->parent != NULL){
Expand Down Expand Up @@ -985,23 +993,28 @@ PHP_METHOD(DOMNode, insertBefore)
return;
}
}
new_child = xmlAddChild(parentp, child);
if (UNEXPECTED(NULL == new_child)) {
goto cannot_add;
}
} else if (child->type == XML_DOCUMENT_FRAG_NODE) {
xmlNodePtr last = child->last;
new_child = _php_dom_insert_fragment(parentp, parentp->last, NULL, child, intern, childobj);
}
if (new_child == NULL) {
dom_reconcile_ns_list(parentp->doc, new_child, last);
} else {
new_child = xmlAddChild(parentp, child);
if (UNEXPECTED(NULL == new_child)) {
goto cannot_add;
}
dom_reconcile_ns(parentp->doc, new_child);
}
}

if (NULL == new_child) {
zend_throw_error(NULL, "Cannot add newnode as the previous sibling of refnode");
RETURN_THROWS();
}

dom_reconcile_ns(parentp->doc, new_child);

DOM_RET_OBJ(new_child, &ret, intern);

return;
cannot_add:
zend_throw_error(NULL, "Cannot add newnode as the previous sibling of refnode");
RETURN_THROWS();
}
/* }}} end dom_node_insert_before */

Expand Down Expand Up @@ -1066,9 +1079,10 @@ PHP_METHOD(DOMNode, replaceChild)

xmlUnlinkNode(oldchild);

xmlNodePtr last = newchild->last;
newchild = _php_dom_insert_fragment(nodep, prevsib, nextsib, newchild, intern, newchildobj);
if (newchild) {
dom_reconcile_ns(nodep->doc, newchild);
dom_reconcile_ns_list(nodep->doc, newchild, last);
}
} else if (oldchild != newchild) {
xmlDtdPtr intSubset = xmlGetIntSubset(nodep->doc);
Expand Down Expand Up @@ -1215,22 +1229,28 @@ PHP_METHOD(DOMNode, appendChild)
php_libxml_node_free_resource((xmlNodePtr) lastattr);
}
}
new_child = xmlAddChild(nodep, child);
if (UNEXPECTED(new_child == NULL)) {
goto cannot_add;
}
} else if (child->type == XML_DOCUMENT_FRAG_NODE) {
xmlNodePtr last = child->last;
new_child = _php_dom_insert_fragment(nodep, nodep->last, NULL, child, intern, childobj);
}

if (new_child == NULL) {
dom_reconcile_ns_list(nodep->doc, new_child, last);
} else {
new_child = xmlAddChild(nodep, child);
if (new_child == NULL) {
// TODO Convert to Error?
php_error_docref(NULL, E_WARNING, "Couldn't append node");
RETURN_FALSE;
if (UNEXPECTED(new_child == NULL)) {
goto cannot_add;
}
dom_reconcile_ns(nodep->doc, new_child);
}

dom_reconcile_ns(nodep->doc, new_child);

DOM_RET_OBJ(new_child, &ret, intern);
return;
cannot_add:
// TODO Convert to Error?
php_error_docref(NULL, E_WARNING, "Couldn't append node");
RETURN_FALSE;
}
/* }}} end dom_node_append_child */

Expand Down
87 changes: 70 additions & 17 deletions ext/dom/parentnode.c
Expand Up @@ -298,13 +298,14 @@ void dom_parent_node_append(dom_object *context, zval *nodes, int nodesc)
parentNode->children = newchild;
}

parentNode->last = fragment->last;
xmlNodePtr last = fragment->last;
parentNode->last = last;

newchild->prev = prevsib;

dom_fragment_assign_parent_node(parentNode, fragment);

dom_reconcile_ns(parentNode->doc, newchild);
dom_reconcile_ns_list(parentNode->doc, newchild, last);
}

xmlFree(fragment);
Expand Down Expand Up @@ -335,13 +336,14 @@ void dom_parent_node_prepend(dom_object *context, zval *nodes, int nodesc)
nextsib = parentNode->children;

if (newchild) {
xmlNodePtr last = fragment->last;
parentNode->children = newchild;
fragment->last->next = nextsib;
nextsib->prev = fragment->last;
nextsib->prev = last;

dom_fragment_assign_parent_node(parentNode, fragment);

dom_reconcile_ns(parentNode->doc, newchild);
dom_reconcile_ns_list(parentNode->doc, newchild, last);
}

xmlFree(fragment);
Expand Down Expand Up @@ -414,11 +416,13 @@ void dom_parent_node_after(dom_object *context, zval *nodes, int nodesc)
newchild = fragment->children;

if (newchild) {
xmlNodePtr last = fragment->last;

/* Step 5: place fragment into the parent before viable_next_sibling */
dom_pre_insert(viable_next_sibling, parentNode, newchild, fragment);

dom_fragment_assign_parent_node(parentNode, fragment);
dom_reconcile_ns(doc, newchild);
dom_reconcile_ns_list(doc, newchild, last);
}

xmlFree(fragment);
Expand Down Expand Up @@ -463,6 +467,8 @@ void dom_parent_node_before(dom_object *context, zval *nodes, int nodesc)
newchild = fragment->children;

if (newchild) {
xmlNodePtr last = fragment->last;

/* Step 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;
Expand All @@ -473,41 +479,51 @@ void dom_parent_node_before(dom_object *context, zval *nodes, int nodesc)
dom_pre_insert(viable_previous_sibling, parentNode, newchild, fragment);

dom_fragment_assign_parent_node(parentNode, fragment);
dom_reconcile_ns(doc, newchild);
dom_reconcile_ns_list(doc, newchild, last);
}

xmlFree(fragment);
}

void dom_child_node_remove(dom_object *context)
static zend_result dom_child_removal_preconditions(const xmlNodePtr child, int stricterror)
{
xmlNode *child = dom_object_get_node(context);
xmlNodePtr children;
int stricterror;

stricterror = dom_get_strict_error(context->document);

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, stricterror);
return;
return FAILURE;
}

if (!child->parent) {
php_dom_throw_error(NOT_FOUND_ERR, stricterror);
return;
return FAILURE;
}

if (dom_node_children_valid(child->parent) == FAILURE) {
return;
return FAILURE;
}

children = child->parent->children;
xmlNodePtr children = child->parent->children;
if (!children) {
php_dom_throw_error(NOT_FOUND_ERR, stricterror);
return FAILURE;
}

return SUCCESS;
}

void dom_child_node_remove(dom_object *context)
{
xmlNode *child = dom_object_get_node(context);
xmlNodePtr children;
int stricterror;

stricterror = dom_get_strict_error(context->document);

if (UNEXPECTED(dom_child_removal_preconditions(child, stricterror) != SUCCESS)) {
return;
}

children = child->parent->children;
while (children) {
if (children == child) {
xmlUnlinkNode(child);
Expand All @@ -519,4 +535,41 @@ void dom_child_node_remove(dom_object *context)
php_dom_throw_error(NOT_FOUND_ERR, stricterror);
}

void dom_child_replace_with(dom_object *context, zval *nodes, int nodesc)
{
xmlNodePtr child = dom_object_get_node(context);
xmlNodePtr parentNode = child->parent;

int stricterror = dom_get_strict_error(context->document);
if (UNEXPECTED(dom_child_removal_preconditions(child, stricterror) != SUCCESS)) {
return;
}

xmlNodePtr insertion_point = child->next;

xmlNodePtr fragment = dom_zvals_to_fragment(context->document, parentNode, nodes, nodesc);
if (UNEXPECTED(fragment == NULL)) {
return;
}

xmlNodePtr newchild = fragment->children;
xmlDocPtr doc = parentNode->doc;

if (newchild) {
xmlNodePtr last = fragment->last;

/* Unlink and free it unless it became a part of the fragment. */
if (child->parent != fragment) {
xmlUnlinkNode(child);
}

dom_pre_insert(insertion_point, parentNode, newchild, fragment);

dom_fragment_assign_parent_node(parentNode, fragment);
dom_reconcile_ns_list(doc, newchild, last);
}

xmlFree(fragment);
}

#endif
68 changes: 48 additions & 20 deletions ext/dom/php_dom.c
Expand Up @@ -1385,38 +1385,66 @@ void dom_set_old_ns(xmlDoc *doc, xmlNs *ns) {
}
/* }}} end dom_set_old_ns */

void dom_reconcile_ns(xmlDocPtr doc, xmlNodePtr nodep) /* {{{ */
static void dom_reconcile_ns_internal(xmlDocPtr doc, xmlNodePtr nodep)
{
xmlNsPtr nsptr, nsdftptr, curns, prevns = NULL;

if (nodep->type == XML_ELEMENT_NODE) {
/* Following if block primarily used for inserting nodes created via createElementNS */
if (nodep->nsDef != NULL) {
curns = nodep->nsDef;
while (curns) {
nsdftptr = curns->next;
if (curns->href != NULL) {
if((nsptr = xmlSearchNsByHref(doc, nodep->parent, curns->href)) &&
(curns->prefix == NULL || xmlStrEqual(nsptr->prefix, curns->prefix))) {
curns->next = NULL;
if (prevns == NULL) {
nodep->nsDef = nsdftptr;
} else {
prevns->next = nsdftptr;
}
dom_set_old_ns(doc, curns);
curns = prevns;
/* Following if block primarily used for inserting nodes created via createElementNS */
if (nodep->nsDef != NULL) {
curns = nodep->nsDef;
while (curns) {
nsdftptr = curns->next;
if (curns->href != NULL) {
if((nsptr = xmlSearchNsByHref(doc, nodep->parent, curns->href)) &&
(curns->prefix == NULL || xmlStrEqual(nsptr->prefix, curns->prefix))) {
curns->next = NULL;
if (prevns == NULL) {
nodep->nsDef = nsdftptr;
} else {
prevns->next = nsdftptr;
}
dom_set_old_ns(doc, curns);
curns = prevns;
}
prevns = curns;
curns = nsdftptr;
}
prevns = curns;
curns = nsdftptr;
}
}
}

void dom_reconcile_ns(xmlDocPtr doc, xmlNodePtr nodep) /* {{{ */
{
if (nodep->type == XML_ELEMENT_NODE) {
dom_reconcile_ns_internal(doc, nodep);
xmlReconciliateNs(doc, nodep);
}
}
/* }}} */

static void dom_reconcile_ns_list_internal(xmlDocPtr doc, xmlNodePtr nodep, xmlNodePtr last)
{
ZEND_ASSERT(nodep != NULL);
while (true) {
if (nodep->type == XML_ELEMENT_NODE) {
dom_reconcile_ns_internal(doc, nodep);
if (nodep->children) {
dom_reconcile_ns_list_internal(doc, nodep->children, nodep->last /* process the whole children list */);
}
}
if (nodep == last) {
break;
}
nodep = nodep->next;
}
}

void dom_reconcile_ns_list(xmlDocPtr doc, xmlNodePtr nodep, xmlNodePtr last)
{
dom_reconcile_ns_list_internal(doc, nodep, last);
xmlReconciliateNs(doc, nodep->parent /* to handle the whole inserted subtree instead of only the first child */);
}

/*
http://www.w3.org/TR/2004/REC-DOM-Level-3-Core-20040407/core.html#ID-DocCrElNS

Expand Down
2 changes: 2 additions & 0 deletions ext/dom/php_dom.h
Expand Up @@ -110,6 +110,7 @@ int dom_check_qname(char *qname, char **localname, char **prefix, int uri_len, i
xmlNsPtr dom_get_ns(xmlNodePtr node, char *uri, int *errorcode, char *prefix);
void dom_set_old_ns(xmlDoc *doc, xmlNs *ns);
void dom_reconcile_ns(xmlDocPtr doc, xmlNodePtr nodep);
void dom_reconcile_ns_list(xmlDocPtr doc, xmlNodePtr nodep, xmlNodePtr last);
xmlNsPtr dom_get_nsdecl(xmlNode *node, xmlChar *localName);
void dom_normalize (xmlNodePtr nodep);
xmlNode *dom_get_elements_by_tag_name_ns_raw(xmlNodePtr nodep, char *ns, char *local, int *cur, int index);
Expand All @@ -131,6 +132,7 @@ void dom_parent_node_append(dom_object *context, zval *nodes, int nodesc);
void dom_parent_node_after(dom_object *context, zval *nodes, int nodesc);
void dom_parent_node_before(dom_object *context, zval *nodes, int nodesc);
void dom_child_node_remove(dom_object *context);
void dom_child_replace_with(dom_object *context, zval *nodes, int nodesc);

#define DOM_GET_OBJ(__ptr, __id, __prtype, __intern) { \
__intern = Z_DOMOBJ_P(__id); \
Expand Down