Skip to content

Fix bug #67440: append_node of a DOMDocumentFragment does not reconcile namespaces #11362

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

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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
66 changes: 43 additions & 23 deletions ext/dom/node.c
Original file line number Diff line number Diff line change
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
18 changes: 12 additions & 6 deletions ext/dom/parentnode.c
Original file line number Diff line number Diff line change
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,7 +479,7 @@ 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);
Expand Down
75 changes: 55 additions & 20 deletions ext/dom/php_dom.c
Original file line number Diff line number Diff line change
Expand Up @@ -1385,38 +1385,73 @@ 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);
/* Outside of the recursion above because xmlReconciliateNs() performs its own recursion. */
while (true) {
xmlReconciliateNs(doc, nodep);
if (nodep == last) {
break;
}
nodep = nodep->next;
}
}

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

Expand Down
1 change: 1 addition & 0 deletions ext/dom/php_dom.h
Original file line number Diff line number Diff line change
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 Down
Loading