Fix GH-21544: Dom\XMLDocument::C14N() drops namespace declarations on…#21561
Fix GH-21544: Dom\XMLDocument::C14N() drops namespace declarations on…#21561devnexen wants to merge 3 commits intophp:PHP-8.4from
Conversation
… on DOM-built documents. For programmatically built documents (e.g. createElementNS), namespaces live on node->ns without corresponding xmlns attributes. Create temporary synthetic nsDef entries so C14N can find them, complementing the existing xmlns attribute to nsDef conversion.
| xmlNsPtr ns = xmlMalloc(sizeof(*ns)); | ||
| if (!ns) { | ||
| return; | ||
| } | ||
|
|
||
| zval *zv = zend_hash_index_lookup(links, (zend_ulong) node); | ||
| if (Z_ISNULL_P(zv)) { | ||
| ZVAL_LONG(zv, 1); | ||
| } else { | ||
| Z_LVAL_P(zv)++; | ||
| } | ||
|
|
||
| memset(ns, 0, sizeof(*ns)); | ||
| ns->type = XML_LOCAL_NAMESPACE; | ||
| ns->href = xmlStrdup(src_ns->href); | ||
| ns->prefix = src_ns->prefix ? xmlStrdup(src_ns->prefix) : NULL; | ||
| ns->next = node->nsDef; | ||
| node->nsDef = ns; |
There was a problem hiding this comment.
Perhaps it's a good idea to factor this out to a different function.
| * entries during cleanup. */ | ||
| static void dom_add_synthetic_ns_decl(HashTable *links, xmlNodePtr node, xmlNsPtr src_ns) | ||
| { | ||
| for (xmlNsPtr existing = node->nsDef; existing; existing = existing->next) { |
There was a problem hiding this comment.
Do we need this loop? I believe that for Dom\XMLDocument this is normally never filled in.
There was a problem hiding this comment.
The loop checks existing nsDef entries to avoid duplicates. While pre-existing nsDef is indeed empty for Dom\XMLDocument, the loop is still needed:
dom_add_synthetic_ns_decl is called multiple times per node (once for node->ns, once per attr->ns), and if the element and an attribute share the same namespace
prefix, the loop prevents adding a duplicate synthetic entry.
There was a problem hiding this comment.
Do you think it's possible to avoid this in certain cases?
I believe we only need this for attributes, no? That would cut down on the overhead of this loop.
|
ping :) |
|
Put on my TODO list to re-check tomorrow. |
… DOM-built documents.
For programmatically built documents (e.g. createElementNS), namespaces live on node->ns without corresponding xmlns attributes. Create temporary synthetic nsDef entries so C14N can find them, complementing the existing xmlns attribute to nsDef conversion.