1
- import { XFORMS_NAMESPACE_URI } from '@getodk/common/constants/xmlns.ts' ;
1
+ import {
2
+ XFORMS_NAMESPACE_URI ,
3
+ XMLNS_NAMESPACE_URI ,
4
+ XMLNS_PREFIX ,
5
+ } from '@getodk/common/constants/xmlns.ts' ;
2
6
import { InspectableComparisonError } from '@getodk/common/test/assertions/helpers.ts' ;
3
7
import type { SimpleAssertionResult } from '@getodk/common/test/assertions/vitest/shared-extension-types.ts' ;
4
8
import { ComparableAssertableValue } from '../comparable/ComparableAssertableValue.ts' ;
@@ -8,38 +12,53 @@ class ComparableXMLQualifiedName {
8
12
9
13
constructor (
10
14
readonly namespaceURI : string | null ,
15
+ readonly nodeName : string ,
11
16
readonly localName : string
12
17
) {
13
- this . sortKey = JSON . stringify ( { namespaceURI, localName } ) ;
18
+ let namespaceDeclarationType : string ;
19
+
20
+ if ( namespaceURI === XMLNS_NAMESPACE_URI ) {
21
+ if ( nodeName === XMLNS_PREFIX ) {
22
+ namespaceDeclarationType = 'default' ;
23
+ } else {
24
+ namespaceDeclarationType = 'non-default' ;
25
+ }
26
+ } else {
27
+ namespaceDeclarationType = 'none' ;
28
+ }
29
+
30
+ this . sortKey = JSON . stringify ( {
31
+ namespaceDeclarationType,
32
+ namespaceURI,
33
+ localName,
34
+ } ) ;
14
35
}
15
36
16
- /**
17
- * @todo prefix re-serialization
18
- */
19
37
toString ( ) : string {
20
- const { namespaceURI } = this ;
38
+ const { namespaceURI, nodeName } = this ;
21
39
22
40
if ( namespaceURI == null || namespaceURI === XFORMS_NAMESPACE_URI ) {
23
41
return this . localName ;
24
42
}
25
43
26
- return this . sortKey ;
44
+ return nodeName ;
27
45
}
28
46
}
29
47
30
48
class ComparableXMLAttribute {
31
49
static from ( attr : Attr ) : ComparableXMLAttribute {
32
- return new this ( attr . namespaceURI , attr . localName , attr . value ) ;
50
+ return new this ( attr . namespaceURI , attr . nodeName , attr . localName , attr . value ) ;
33
51
}
34
52
35
53
readonly qualifiedName : ComparableXMLQualifiedName ;
36
54
37
55
private constructor (
38
56
namespaceURI : string | null ,
57
+ nodeName : string ,
39
58
localName : string ,
40
59
readonly value : string
41
60
) {
42
- this . qualifiedName = new ComparableXMLQualifiedName ( namespaceURI , localName ) ;
61
+ this . qualifiedName = new ComparableXMLQualifiedName ( namespaceURI , nodeName , localName ) ;
43
62
}
44
63
45
64
/**
@@ -59,17 +78,23 @@ const comparableXMLElementAttributes = (element: Element): readonly ComparableXM
59
78
return ComparableXMLAttribute . from ( attr ) ;
60
79
} ) ;
61
80
62
- return attributes . sort ( ( { qualifiedName : a } , { qualifiedName : b } ) => {
63
- if ( a > b ) {
64
- return 1 ;
81
+ return attributes . sort (
82
+ (
83
+ // prettier-ignore
84
+ { qualifiedName : { sortKey : a } } ,
85
+ { qualifiedName : { sortKey : b } }
86
+ ) => {
87
+ if ( a > b ) {
88
+ return 1 ;
89
+ }
90
+
91
+ if ( b > a ) {
92
+ return - 1 ;
93
+ }
94
+
95
+ return 0 ;
65
96
}
66
-
67
- if ( b > a ) {
68
- return - 1 ;
69
- }
70
-
71
- return 0 ;
72
- } ) ;
97
+ ) ;
73
98
} ;
74
99
75
100
const isElement = ( node : ChildNode ) : node is Element => {
@@ -118,18 +143,25 @@ class ComparableXMLElement {
118
143
const attributes = comparableXMLElementAttributes ( element ) ;
119
144
const children = comparableXMLElementChildren ( element ) ;
120
145
121
- return new this ( element . namespaceURI , element . localName , attributes , children ) ;
146
+ return new this (
147
+ element . namespaceURI ,
148
+ element . nodeName ,
149
+ element . localName ,
150
+ attributes ,
151
+ children
152
+ ) ;
122
153
}
123
154
124
155
readonly qualifiedName : ComparableXMLQualifiedName ;
125
156
126
157
private constructor (
127
158
namespaceURI : string | null ,
159
+ nodeName : string ,
128
160
localName : string ,
129
161
readonly attributes : readonly ComparableXMLAttribute [ ] ,
130
162
readonly children : readonly ComparableXMLElementChild [ ]
131
163
) {
132
- this . qualifiedName = new ComparableXMLQualifiedName ( namespaceURI , localName ) ;
164
+ this . qualifiedName = new ComparableXMLQualifiedName ( namespaceURI , nodeName , localName ) ;
133
165
}
134
166
135
167
toString ( ) : string {
0 commit comments