Skip to content

Commit 0b5da66

Browse files
committed
Avoid some extra work and fix default in list
1 parent 0ca8664 commit 0b5da66

File tree

1 file changed

+27
-11
lines changed

1 file changed

+27
-11
lines changed

ext/dom/php_dom.c

Lines changed: 27 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1478,17 +1478,28 @@ static void dom_libxml_reconcile_ensure_namespaces_are_declared(xmlNodePtr nodep
14781478
xmlDOMWrapReconcileNamespaces(&dummy_ctxt, nodep, /* options */ 0);
14791479
}
14801480

1481-
static void dom_check_default_namespace(xmlDocPtr doc, xmlNodePtr nodep)
1481+
static bool dom_must_replace_namespace_by_empty_default(xmlDocPtr doc, xmlNodePtr nodep)
1482+
{
1483+
xmlNsPtr default_ns = xmlSearchNs(doc, nodep->parent, NULL);
1484+
return default_ns != NULL && default_ns->href != NULL && default_ns->href[0] != '\0';
1485+
}
1486+
1487+
static void dom_replace_namespace_by_empty_default(xmlDocPtr doc, xmlNodePtr nodep)
14821488
{
14831489
if (nodep->ns == NULL) {
1484-
xmlNsPtr default_ns = xmlSearchNs(doc, nodep->parent, NULL);
1485-
if (default_ns != NULL && default_ns->href != NULL && default_ns->href[0] != '\0') {
1486-
/* The node uses the default empty namespace, but the current default namespace is non-empty.
1487-
* We can't unconditionally do this because otherwise libxml2 creates an xmlns="" declaration.
1488-
* Note: there's no point searching the oldNs list, because we haven't found it in the tree anyway.
1489-
* Ideally this would be pre-allocated but unfortunately libxml2 doesn't offer such a functionality. */
1490-
xmlSetNs(nodep, xmlNewNs(nodep, (const xmlChar *) "", NULL));
1491-
}
1490+
/* The node uses the default empty namespace, but the current default namespace is non-empty.
1491+
* We can't unconditionally do this because otherwise libxml2 creates an xmlns="" declaration.
1492+
* Note: there's no point searching the oldNs list, because we haven't found it in the tree anyway.
1493+
* Ideally this would be pre-allocated but unfortunately libxml2 doesn't offer such a functionality. */
1494+
xmlSetNs(nodep, xmlNewNs(nodep, (const xmlChar *) "", NULL));
1495+
}
1496+
}
1497+
1498+
static void dom_check_default_namespace(xmlDocPtr doc, xmlNodePtr nodep)
1499+
{
1500+
/* Check nodep->ns first to avoid an expensive lookup. */
1501+
if (nodep->ns == NULL && dom_must_replace_namespace_by_empty_default(doc, nodep)) {
1502+
dom_replace_namespace_by_empty_default(doc, nodep);
14921503
}
14931504
}
14941505

@@ -1498,8 +1509,8 @@ void dom_reconcile_ns(xmlDocPtr doc, xmlNodePtr nodep) /* {{{ */
14981509
* we still want to do the internal reconciliation conditionally. */
14991510
if (nodep->type == XML_ELEMENT_NODE) {
15001511
dom_reconcile_ns_internal(doc, nodep, nodep->parent);
1501-
dom_check_default_namespace(doc, nodep);
15021512
dom_libxml_reconcile_ensure_namespaces_are_declared(nodep);
1513+
dom_check_default_namespace(doc, nodep);
15031514
}
15041515
}
15051516
/* }}} */
@@ -1523,13 +1534,18 @@ static void dom_reconcile_ns_list_internal(xmlDocPtr doc, xmlNodePtr nodep, xmlN
15231534

15241535
void dom_reconcile_ns_list(xmlDocPtr doc, xmlNodePtr nodep, xmlNodePtr last)
15251536
{
1537+
bool must_replace_namespace_by_empty_default = dom_must_replace_namespace_by_empty_default(doc, nodep);
15261538
dom_reconcile_ns_list_internal(doc, nodep, last, nodep->parent);
1527-
dom_check_default_namespace(doc, nodep);
15281539
/* The loop is outside of the recursion in the above call because
15291540
* dom_libxml_reconcile_ensure_namespaces_are_declared() performs its own recursion. */
15301541
while (true) {
15311542
/* The internal libxml2 call will already check the node type, no need for us to do it here. */
15321543
dom_libxml_reconcile_ensure_namespaces_are_declared(nodep);
1544+
/* We don't have to handle the children, because if their ns's are NULL they'll just take on the default
1545+
* which should've been reconciled before. */
1546+
if (must_replace_namespace_by_empty_default) {
1547+
dom_replace_namespace_by_empty_default(doc, nodep);
1548+
}
15331549
if (nodep == last) {
15341550
break;
15351551
}

0 commit comments

Comments
 (0)