Skip to content

Commit 021af6b

Browse files
committed
Fix ordering of namespaces
1 parent 3fb465c commit 021af6b

File tree

3 files changed

+33
-25
lines changed

3 files changed

+33
-25
lines changed

ext/dom/element.c

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1751,7 +1751,15 @@ static void dom_element_get_in_scope_namespace_info(php_dom_libxml_ns_mapper *ns
17511751
/* https://www.w3.org/TR/1999/REC-xpath-19991116/#namespace-nodes */
17521752
for (const xmlNode *cur = nodep; cur != NULL; cur = cur->parent) {
17531753
if (cur->type == XML_ELEMENT_NODE) {
1754+
/* Find the last attribute */
1755+
const xmlAttr *last = NULL;
17541756
for (const xmlAttr *attr = cur->properties; attr != NULL; attr = attr->next) {
1757+
last = attr;
1758+
}
1759+
1760+
/* Reversed loop because the parent traversal is reversed as well,
1761+
* this will keep the ordering consistent. */
1762+
for (const xmlAttr *attr = last; attr != NULL; attr = attr->prev) {
17551763
if (attr->ns != NULL && php_dom_ns_is_fast_ex(attr->ns, php_dom_ns_is_xmlns_magic_token)
17561764
&& attr->children != NULL && attr->children->content != NULL) {
17571765
const char *prefix = attr->ns->prefix == NULL ? NULL : (const char *) attr->name;
@@ -1765,7 +1773,7 @@ static void dom_element_get_in_scope_namespace_info(php_dom_libxml_ns_mapper *ns
17651773

17661774
xmlNsPtr ns;
17671775
zend_string *prefix;
1768-
ZEND_HASH_MAP_FOREACH_STR_KEY_PTR(&prefix_to_ns_table, prefix, ns) {
1776+
ZEND_HASH_MAP_REVERSE_FOREACH_STR_KEY_PTR(&prefix_to_ns_table, prefix, ns) {
17691777
if (ZSTR_LEN(prefix) == 0 && (ns == NULL || ns->href == NULL || *ns->href == '\0')) {
17701778
/* Exception: "the value of the xmlns attribute for the nearest such element is non-empty" */
17711779
continue;

ext/dom/tests/modern/extensions/Element_getDescendantNamespaces.phpt

Lines changed: 16 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,10 @@ element->nodeName: string(7) "c:child"
5151
---
5252

5353
=== b:sibling ===
54+
prefix: NULL
55+
namespaceURI: string(5) "urn:a"
56+
element->nodeName: string(9) "b:sibling"
57+
---
5458
prefix: string(1) "b"
5559
namespaceURI: string(5) "urn:b"
5660
element->nodeName: string(9) "b:sibling"
@@ -61,32 +65,28 @@ element->nodeName: string(9) "b:sibling"
6165
---
6266
prefix: NULL
6367
namespaceURI: string(5) "urn:a"
64-
element->nodeName: string(9) "b:sibling"
65-
---
66-
prefix: string(1) "d"
67-
namespaceURI: string(6) "urn:d2"
6868
element->nodeName: string(7) "d:child"
6969
---
7070
prefix: string(1) "b"
7171
namespaceURI: string(5) "urn:b"
7272
element->nodeName: string(7) "d:child"
7373
---
74-
prefix: NULL
75-
namespaceURI: string(5) "urn:a"
74+
prefix: string(1) "d"
75+
namespaceURI: string(6) "urn:d2"
7676
element->nodeName: string(7) "d:child"
7777
---
7878

7979
=== d:child ===
80-
prefix: string(1) "d"
81-
namespaceURI: string(6) "urn:d2"
80+
prefix: NULL
81+
namespaceURI: string(5) "urn:a"
8282
element->nodeName: string(7) "d:child"
8383
---
8484
prefix: string(1) "b"
8585
namespaceURI: string(5) "urn:b"
8686
element->nodeName: string(7) "d:child"
8787
---
88-
prefix: NULL
89-
namespaceURI: string(5) "urn:a"
88+
prefix: string(1) "d"
89+
namespaceURI: string(6) "urn:d2"
9090
element->nodeName: string(7) "d:child"
9191
---
9292

@@ -99,6 +99,10 @@ prefix: string(1) "c"
9999
namespaceURI: string(5) "urn:c"
100100
element->nodeName: string(7) "c:child"
101101
---
102+
prefix: NULL
103+
namespaceURI: string(5) "urn:a"
104+
element->nodeName: string(9) "b:sibling"
105+
---
102106
prefix: string(1) "b"
103107
namespaceURI: string(5) "urn:b"
104108
element->nodeName: string(9) "b:sibling"
@@ -109,17 +113,13 @@ element->nodeName: string(9) "b:sibling"
109113
---
110114
prefix: NULL
111115
namespaceURI: string(5) "urn:a"
112-
element->nodeName: string(9) "b:sibling"
113-
---
114-
prefix: string(1) "d"
115-
namespaceURI: string(6) "urn:d2"
116116
element->nodeName: string(7) "d:child"
117117
---
118118
prefix: string(1) "b"
119119
namespaceURI: string(5) "urn:b"
120120
element->nodeName: string(7) "d:child"
121121
---
122-
prefix: NULL
123-
namespaceURI: string(5) "urn:a"
122+
prefix: string(1) "d"
123+
namespaceURI: string(6) "urn:d2"
124124
element->nodeName: string(7) "d:child"
125125
---

ext/dom/tests/modern/extensions/Element_getInScopeNamespaces.phpt

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,10 @@ element->nodeName: string(7) "c:child"
4747
=== child ===
4848

4949
=== b:sibling ===
50+
prefix: NULL
51+
namespaceURI: string(5) "urn:a"
52+
element->nodeName: string(9) "b:sibling"
53+
---
5054
prefix: string(1) "b"
5155
namespaceURI: string(5) "urn:b"
5256
element->nodeName: string(9) "b:sibling"
@@ -55,22 +59,18 @@ prefix: string(1) "d"
5559
namespaceURI: string(5) "urn:d"
5660
element->nodeName: string(9) "b:sibling"
5761
---
58-
prefix: NULL
59-
namespaceURI: string(5) "urn:a"
60-
element->nodeName: string(9) "b:sibling"
61-
---
6262

6363
=== d:child ===
64-
prefix: string(1) "d"
65-
namespaceURI: string(6) "urn:d2"
64+
prefix: NULL
65+
namespaceURI: string(5) "urn:a"
6666
element->nodeName: string(7) "d:child"
6767
---
6868
prefix: string(1) "b"
6969
namespaceURI: string(5) "urn:b"
7070
element->nodeName: string(7) "d:child"
7171
---
72-
prefix: NULL
73-
namespaceURI: string(5) "urn:a"
72+
prefix: string(1) "d"
73+
namespaceURI: string(6) "urn:d2"
7474
element->nodeName: string(7) "d:child"
7575
---
7676

0 commit comments

Comments
 (0)