Skip to content

Commit 5459b78

Browse files
committed
Reuse namespaces from doc->oldNs if possible in dom_get_ns()
1 parent 03b1ddc commit 5459b78

File tree

1 file changed

+16
-3
lines changed

1 file changed

+16
-3
lines changed

ext/dom/php_dom.c

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1518,20 +1518,33 @@ NAMESPACE_ERR: Raised if
15181518
xmlNsPtr dom_get_ns(xmlNodePtr nodep, char *uri, int *errorcode, char *prefix) {
15191519
xmlNsPtr nsptr = NULL;
15201520

1521-
*errorcode = 0;
1522-
15231521
if (! ((prefix && !strcmp (prefix, "xml") && strcmp(uri, (char *)XML_XML_NAMESPACE)) ||
15241522
(prefix && !strcmp (prefix, "xmlns") && strcmp(uri, (char *)DOM_XMLNS_NAMESPACE)) ||
15251523
(prefix && !strcmp(uri, (char *)DOM_XMLNS_NAMESPACE) && strcmp (prefix, "xmlns")))) {
1524+
/* Reuse the old namespaces from doc->oldNs if possible, before creating a new one */
1525+
xmlDocPtr doc = nodep->doc;
1526+
if (doc && doc->oldNs != NULL) {
1527+
xmlNsPtr cur = doc->oldNs;
1528+
do {
1529+
if (xmlStrEqual(cur->prefix, (xmlChar *)prefix) && xmlStrEqual(cur->href, (xmlChar *)uri)) {
1530+
nsptr = cur;
1531+
goto out;
1532+
}
1533+
cur = cur->next;
1534+
} while (cur);
1535+
}
1536+
/* Couldn't reuse one, create a new one. */
15261537
nsptr = xmlNewNs(nodep, (xmlChar *)uri, (xmlChar *)prefix);
15271538
}
15281539

15291540
if (nsptr == NULL) {
15301541
*errorcode = NAMESPACE_ERR;
1542+
return NULL;
15311543
}
15321544

1545+
out:
1546+
*errorcode = 0;
15331547
return nsptr;
1534-
15351548
}
15361549
/* }}} end dom_get_ns */
15371550

0 commit comments

Comments
 (0)