@flighthq/xml 0.5.1-next.920.da8f530 → 0.6.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +2 -2
- package/dist/contract.d.ts +3 -2
- package/dist/contract.d.ts.map +1 -1
- package/dist/contract.js +4 -2
- package/dist/contract.js.map +1 -1
- package/dist/index.d.ts +2 -1
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +3 -1
- package/dist/index.js.map +1 -1
- package/dist/types.d.ts +14 -0
- package/dist/types.d.ts.map +1 -0
- package/dist/types.js +2 -0
- package/dist/types.js.map +1 -0
- package/dist/xmlNative.d.ts +24 -0
- package/dist/xmlNative.d.ts.map +1 -0
- package/dist/xmlNative.js +198 -0
- package/dist/xmlNative.js.map +1 -0
- package/dist/xmlParse.d.ts +12 -7
- package/dist/xmlParse.d.ts.map +1 -1
- package/dist/xmlParse.js +111 -226
- package/dist/xmlParse.js.map +1 -1
- package/dist/xmlQuery.d.ts +16 -6
- package/dist/xmlQuery.d.ts.map +1 -1
- package/dist/xmlQuery.js +42 -16
- package/dist/xmlQuery.js.map +1 -1
- package/package.json +4 -5
- package/src/xmlNative.test.ts +206 -0
- package/src/xmlParse.test.ts +170 -212
- package/src/xmlQuery.test.ts +38 -37
package/src/xmlParse.test.ts
CHANGED
|
@@ -1,298 +1,256 @@
|
|
|
1
|
-
|
|
1
|
+
// Generated from crates/flight-xml/tests/xml_parse_test.rs by flight-compiler.
|
|
2
|
+
// Rust is authoritative; run `npm run generate` after changing it.
|
|
3
|
+
|
|
4
|
+
import { describe, expect, it } from 'vitest';
|
|
5
|
+
|
|
6
|
+
import { parseXmlAttributes, parseXmlDocument } from './contract.ts';
|
|
7
|
+
|
|
8
|
+
function repeated(value: string, count: number): string {
|
|
9
|
+
let result = '';
|
|
10
|
+
let index: number = 0;
|
|
11
|
+
while (index < count) {
|
|
12
|
+
result += value;
|
|
13
|
+
index += 1;
|
|
14
|
+
}
|
|
15
|
+
return result;
|
|
16
|
+
}
|
|
2
17
|
|
|
3
18
|
describe('parseXmlAttributes', () => {
|
|
4
|
-
it('parses double
|
|
19
|
+
it('parses double quoted attributes', () => {
|
|
5
20
|
const attrs = parseXmlAttributes('name="hero" x="10" y="20"');
|
|
6
|
-
expect(attrs['name']).
|
|
7
|
-
expect(attrs['x']).
|
|
8
|
-
expect(attrs['y']).
|
|
21
|
+
expect(attrs['name']).toEqual('hero');
|
|
22
|
+
expect(attrs['x']).toEqual('10');
|
|
23
|
+
expect(attrs['y']).toEqual('20');
|
|
9
24
|
});
|
|
10
|
-
|
|
11
|
-
it('parses single-quoted attributes', () => {
|
|
25
|
+
it('parses single quoted attributes', () => {
|
|
12
26
|
const attrs = parseXmlAttributes("name='hero' x='10'");
|
|
13
|
-
expect(attrs['name']).
|
|
14
|
-
expect(attrs['x']).
|
|
27
|
+
expect(attrs['name']).toEqual('hero');
|
|
28
|
+
expect(attrs['x']).toEqual('10');
|
|
15
29
|
});
|
|
16
|
-
|
|
17
|
-
it('decodes XML entity &', () => {
|
|
30
|
+
it('decodes xml entity amp', () => {
|
|
18
31
|
const attrs = parseXmlAttributes('name="a&b"');
|
|
19
|
-
expect(attrs['name']).
|
|
32
|
+
expect(attrs['name']).toEqual('a&b');
|
|
20
33
|
});
|
|
21
|
-
|
|
22
|
-
it('decodes XML entity < and >', () => {
|
|
34
|
+
it('decodes xml entity lt and gt', () => {
|
|
23
35
|
const attrs = parseXmlAttributes('name="a<b>c"');
|
|
24
|
-
expect(attrs['name']).
|
|
36
|
+
expect(attrs['name']).toEqual('a<b>c');
|
|
25
37
|
});
|
|
26
|
-
|
|
27
|
-
it('decodes XML entity "', () => {
|
|
38
|
+
it('decodes xml entity quot', () => {
|
|
28
39
|
const attrs = parseXmlAttributes("name='say "hi"'");
|
|
29
|
-
expect(attrs['name']).
|
|
40
|
+
expect(attrs['name']).toEqual('say "hi"');
|
|
30
41
|
});
|
|
31
|
-
|
|
32
|
-
it('decodes XML entity '', () => {
|
|
42
|
+
it('decodes xml entity apos', () => {
|
|
33
43
|
const attrs = parseXmlAttributes('name="it's"');
|
|
34
|
-
expect(attrs['name']).
|
|
44
|
+
expect(attrs['name']).toEqual("it's");
|
|
35
45
|
});
|
|
36
|
-
|
|
37
46
|
it('decodes numeric character references', () => {
|
|
38
47
|
const attrs = parseXmlAttributes('name="A"');
|
|
39
|
-
expect(attrs['name']).
|
|
48
|
+
expect(attrs['name']).toEqual('A');
|
|
40
49
|
});
|
|
41
|
-
|
|
42
|
-
it('preserves out-of-range numeric references without throwing', () => {
|
|
50
|
+
it('preserves out of range numeric references without panicking', () => {
|
|
43
51
|
const attrs = parseXmlAttributes('decimal="�" hex="�"');
|
|
44
|
-
expect(attrs).toEqual(
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
it('returns empty object for empty string', () => {
|
|
48
|
-
expect(parseXmlAttributes('')).toEqual({});
|
|
52
|
+
expect(attrs['decimal']).toEqual('�');
|
|
53
|
+
expect(attrs['hex']).toEqual('�');
|
|
49
54
|
});
|
|
50
55
|
});
|
|
51
56
|
|
|
52
57
|
describe('parseXmlDocument', () => {
|
|
53
|
-
it('returns the first top
|
|
54
|
-
const root = parseXmlDocument('<TextureAtlas imagePath="sheet.png"></TextureAtlas>')
|
|
55
|
-
expect(root).
|
|
56
|
-
expect(root
|
|
57
|
-
expect(root
|
|
58
|
-
expect(root?.children).toEqual([]);
|
|
58
|
+
it('returns the first top level element as the root', () => {
|
|
59
|
+
const root = parseXmlDocument('<TextureAtlas imagePath="sheet.png"></TextureAtlas>')!;
|
|
60
|
+
expect(root.name).toEqual('TextureAtlas');
|
|
61
|
+
expect(root.attributes['imagePath']).toEqual('sheet.png');
|
|
62
|
+
expect(root.children.length === 0).toBe(true);
|
|
59
63
|
});
|
|
60
|
-
|
|
61
64
|
it('parses multiple attributes on a single element', () => {
|
|
62
|
-
const root = parseXmlDocument('<sub x="10" y="20" width="32" height="64"/>')
|
|
63
|
-
expect(root
|
|
65
|
+
const root = parseXmlDocument('<sub x="10" y="20" width="32" height="64"/>')!;
|
|
66
|
+
expect(root.attributes['x']).toEqual('10');
|
|
67
|
+
expect(root.attributes['y']).toEqual('20');
|
|
68
|
+
expect(root.attributes['width']).toEqual('32');
|
|
69
|
+
expect(root.attributes['height']).toEqual('64');
|
|
64
70
|
});
|
|
65
|
-
|
|
66
71
|
it('accepts both single and double quoted attribute values', () => {
|
|
67
|
-
const root = parseXmlDocument(
|
|
68
|
-
expect(root
|
|
72
|
+
const root = parseXmlDocument('<node a=\'single\' b="double"/>')!;
|
|
73
|
+
expect(root.attributes['a']).toEqual('single');
|
|
74
|
+
expect(root.attributes['b']).toEqual('double');
|
|
69
75
|
});
|
|
70
|
-
|
|
71
76
|
it('nests children under their parent open tag', () => {
|
|
72
77
|
const xml =
|
|
73
|
-
'<TextureAtlas imagePath="s.png">'
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
expect(root
|
|
79
|
-
expect(root
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
expect(root?.children[1].attributes.name).toBe('b');
|
|
83
|
-
});
|
|
84
|
-
|
|
85
|
-
it('treats self-closing tags as leaves rather than opening a scope', () => {
|
|
78
|
+
'<TextureAtlas imagePath="s.png"><SubTexture name="a" x="0" y="0"/><SubTexture name="b" x="8" y="8"/></TextureAtlas>';
|
|
79
|
+
const root = parseXmlDocument(xml)!;
|
|
80
|
+
expect(root.name).toEqual('TextureAtlas');
|
|
81
|
+
expect(root.children.length).toEqual(2);
|
|
82
|
+
expect(root.children[0]!.name).toEqual('SubTexture');
|
|
83
|
+
expect(root.children[0]!.attributes['name']).toEqual('a');
|
|
84
|
+
expect(root.children[1]!.attributes['name']).toEqual('b');
|
|
85
|
+
});
|
|
86
|
+
it('treats self closing tags as leaves rather than opening a scope', () => {
|
|
86
87
|
const xml = '<root><leaf/><sibling/></root>';
|
|
87
|
-
const root = parseXmlDocument(xml)
|
|
88
|
-
|
|
89
|
-
expect(
|
|
88
|
+
const root = parseXmlDocument(xml)!;
|
|
89
|
+
const names: string[] = root.children.map((c) => c!.name);
|
|
90
|
+
expect(names).toEqual(['leaf', 'sibling']);
|
|
90
91
|
});
|
|
91
|
-
|
|
92
92
|
it('supports deep nesting across multiple levels', () => {
|
|
93
93
|
const xml = '<a><b><c value="deep"/></b></a>';
|
|
94
|
-
const root = parseXmlDocument(xml)
|
|
95
|
-
expect(root
|
|
96
|
-
const b = root
|
|
97
|
-
expect(b
|
|
98
|
-
const c = b
|
|
99
|
-
expect(c
|
|
100
|
-
expect(c
|
|
94
|
+
const root = parseXmlDocument(xml)!;
|
|
95
|
+
expect(root.name).toEqual('a');
|
|
96
|
+
const b = root.children[0]!;
|
|
97
|
+
expect(b.name).toEqual('b');
|
|
98
|
+
const c = b.children[0]!;
|
|
99
|
+
expect(c.name).toEqual('c');
|
|
100
|
+
expect(c.attributes['value']).toEqual('deep');
|
|
101
101
|
});
|
|
102
|
-
|
|
103
102
|
it('accepts element nesting with broad margin for authored documents', () => {
|
|
104
103
|
const depth = 128;
|
|
105
|
-
const xml = '<node>'
|
|
106
|
-
|
|
107
|
-
expect(parseXmlDocument(xml)).not.toBeNull();
|
|
104
|
+
const xml = repeated('<node>', depth) + '<leaf/>' + repeated('</node>', depth);
|
|
105
|
+
expect(parseXmlDocument(xml) !== null).toBe(true);
|
|
108
106
|
});
|
|
109
|
-
|
|
110
|
-
it('returns null instead of exhausting the call stack on excessive element nesting', () => {
|
|
107
|
+
it('returns none instead of exhausting the call stack on excessive element nesting', () => {
|
|
111
108
|
const depth = 300;
|
|
112
|
-
const xml = '<node>'
|
|
113
|
-
|
|
114
|
-
expect(() => parseXmlDocument(xml)).not.toThrow();
|
|
115
|
-
expect(parseXmlDocument(xml)).toBeNull();
|
|
109
|
+
const xml = repeated('<node>', depth) + '<leaf/>' + repeated('</node>', depth);
|
|
110
|
+
expect(parseXmlDocument(xml) === null).toBe(true);
|
|
116
111
|
});
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
expect(parseXmlDocument('')).
|
|
120
|
-
expect(parseXmlDocument(' just text, no tags ')).toBeNull();
|
|
112
|
+
it('returns none when the input contains no recognizable element', () => {
|
|
113
|
+
expect(parseXmlDocument('') === null).toBe(true);
|
|
114
|
+
expect(parseXmlDocument(' just text, no tags ') === null).toBe(true);
|
|
121
115
|
});
|
|
122
|
-
|
|
123
116
|
it('tolerates unbalanced close tags without underflowing the stack', () => {
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
expect(root?.name).toBe('a');
|
|
117
|
+
const root = parseXmlDocument('<a></a></b>')!;
|
|
118
|
+
expect(root.name).toEqual('a');
|
|
127
119
|
});
|
|
128
|
-
|
|
129
120
|
it('returns the first element when several share the top level', () => {
|
|
130
|
-
const root = parseXmlDocument('<first/><second/>')
|
|
131
|
-
expect(root
|
|
121
|
+
const root = parseXmlDocument('<first/><second/>')!;
|
|
122
|
+
expect(root.name).toEqual('first');
|
|
132
123
|
});
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
expect(root
|
|
137
|
-
expect(root?.attributes.data_key).toBe('x');
|
|
124
|
+
it('allows dots hyphens and underscores in tag and attribute names', () => {
|
|
125
|
+
const root = parseXmlDocument('<my-tag.v2 data_key="x"/>')!;
|
|
126
|
+
expect(root.name).toEqual('my-tag.v2');
|
|
127
|
+
expect(root.attributes['data_key']).toEqual('x');
|
|
138
128
|
});
|
|
139
|
-
|
|
140
|
-
it('strips XML comments', () => {
|
|
129
|
+
it('strips xml comments', () => {
|
|
141
130
|
const xml = '<root><!-- This is a comment --><child/></root>';
|
|
142
|
-
const doc = parseXmlDocument(xml)
|
|
143
|
-
expect(doc
|
|
144
|
-
expect(doc
|
|
131
|
+
const doc = parseXmlDocument(xml)!;
|
|
132
|
+
expect(doc.children.length).toEqual(1);
|
|
133
|
+
expect(doc.children[0]!.name).toEqual('child');
|
|
145
134
|
});
|
|
146
|
-
|
|
147
|
-
it('strips the XML declaration', () => {
|
|
135
|
+
it('strips the xml declaration', () => {
|
|
148
136
|
const xml = '<?xml version="1.0" encoding="UTF-8"?><root/>';
|
|
149
|
-
const doc = parseXmlDocument(xml)
|
|
150
|
-
expect(doc
|
|
137
|
+
const doc = parseXmlDocument(xml)!;
|
|
138
|
+
expect(doc.name).toEqual('root');
|
|
151
139
|
});
|
|
152
|
-
|
|
153
140
|
it('parses text content', () => {
|
|
154
141
|
const xml = '<root><key>hello</key></root>';
|
|
155
|
-
const doc = parseXmlDocument(xml)
|
|
156
|
-
expect(doc
|
|
142
|
+
const doc = parseXmlDocument(xml)!;
|
|
143
|
+
expect(doc.children[0]!.text).toEqual('hello');
|
|
157
144
|
});
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
expect(
|
|
163
|
-
expect((doc?.content[1] as { name: string } | undefined)?.name).toBe('tspan');
|
|
164
|
-
expect(doc?.content[2]).toBe('!');
|
|
165
|
-
expect(doc?.text).toBe('Hello!');
|
|
145
|
+
it('keeps a gt inside a quoted attribute value as data not tag end', () => {
|
|
146
|
+
const root = parseXmlDocument('<node attr="a>b"/>')!;
|
|
147
|
+
expect(root.name).toEqual('node');
|
|
148
|
+
expect(root.attributes['attr']).toEqual('a>b');
|
|
149
|
+
expect(root.children.length === 0).toBe(true);
|
|
166
150
|
});
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
expect(doc?.children.map((child) => child.name)).toEqual(['child']);
|
|
172
|
-
expect(doc?.content[0]).toBe('<tag>&<!-- literal -->');
|
|
173
|
-
expect(doc?.text).toBe('<tag>&<!-- literal -->tail');
|
|
151
|
+
it('keeps a gt inside a single quoted attribute value and still parses children', () => {
|
|
152
|
+
const root = parseXmlDocument("<root><child path='x>y'/></root>")!;
|
|
153
|
+
expect(root.children.length).toEqual(1);
|
|
154
|
+
expect(root.children[0]!.attributes['path']).toEqual('x>y');
|
|
174
155
|
});
|
|
175
|
-
|
|
176
|
-
it('keeps a > inside a quoted attribute value as data, not tag-end', () => {
|
|
177
|
-
const root = parseXmlDocument('<node attr="a>b"/>');
|
|
178
|
-
expect(root?.name).toBe('node');
|
|
179
|
-
expect(root?.attributes.attr).toBe('a>b');
|
|
180
|
-
expect(root?.children).toEqual([]);
|
|
181
|
-
});
|
|
182
|
-
|
|
183
|
-
it('keeps a > inside a single-quoted attribute value and still parses children', () => {
|
|
184
|
-
const root = parseXmlDocument(`<root><child path='x>y'/></root>`);
|
|
185
|
-
expect(root?.children.length).toBe(1);
|
|
186
|
-
expect(root?.children[0].attributes.path).toBe('x>y');
|
|
187
|
-
});
|
|
188
|
-
|
|
189
|
-
it('strips a DOCTYPE that carries an internal subset', () => {
|
|
156
|
+
it('strips a doctype that carries an internal subset', () => {
|
|
190
157
|
const xml =
|
|
191
158
|
'<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" ' +
|
|
192
159
|
'"http://www.apple.com/DTDs/PropertyList-1.0.dtd" [ <!ENTITY x "y"> ]>' +
|
|
193
160
|
'<plist version="1.0"/>';
|
|
194
|
-
const doc = parseXmlDocument(xml)
|
|
195
|
-
expect(doc
|
|
196
|
-
expect(doc
|
|
161
|
+
const doc = parseXmlDocument(xml)!;
|
|
162
|
+
expect(doc.name).toEqual('plist');
|
|
163
|
+
expect(doc.attributes['version']).toEqual('1.0');
|
|
197
164
|
});
|
|
198
|
-
|
|
199
|
-
it('strips an internal subset when an entity value contains ] and >', () => {
|
|
165
|
+
it('strips an internal subset when an entity value contains bracket and gt', () => {
|
|
200
166
|
const xml = '<!DOCTYPE root [<!ENTITY tricky "a ] > b"><!ELEMENT root (#PCDATA)>]>' + '<root value="ok"/>';
|
|
201
|
-
const doc = parseXmlDocument(xml)
|
|
202
|
-
expect(doc
|
|
203
|
-
expect(doc
|
|
167
|
+
const doc = parseXmlDocument(xml)!;
|
|
168
|
+
expect(doc.name).toEqual('root');
|
|
169
|
+
expect(doc.attributes['value']).toEqual('ok');
|
|
204
170
|
});
|
|
205
|
-
|
|
206
|
-
it('strips a DOCTYPE when its external identifier contains >', () => {
|
|
171
|
+
it('strips a doctype when its external identifier contains gt', () => {
|
|
207
172
|
const xml = '<!DOCTYPE root SYSTEM "https://example.invalid/a>b.dtd"><root/>';
|
|
208
|
-
const doc = parseXmlDocument(xml)
|
|
209
|
-
expect(doc
|
|
173
|
+
const doc = parseXmlDocument(xml)!;
|
|
174
|
+
expect(doc.name).toEqual('root');
|
|
210
175
|
});
|
|
211
176
|
});
|
|
212
177
|
|
|
213
|
-
|
|
214
|
-
|
|
215
|
-
|
|
216
|
-
|
|
217
|
-
`<?xml version="1.0"?><!DOCTYPE root [${subset}]><root>${body}</root>`;
|
|
218
|
-
|
|
178
|
+
describe('parseXmlDocumentInternalDtdEntities', () => {
|
|
179
|
+
function doctype(subset: string, body: string): string {
|
|
180
|
+
return `<?xml version="1.0"?><!DOCTYPE root [${subset}]><root>${body}</root>`;
|
|
181
|
+
}
|
|
219
182
|
it('expands an entity whose replacement is an element', () => {
|
|
220
|
-
const
|
|
221
|
-
|
|
222
|
-
expect(root.children).
|
|
223
|
-
expect(root.children[0]
|
|
224
|
-
expect(root.children[0]
|
|
183
|
+
const xml = doctype('<!ENTITY dot "<circle r=\'4\'/>">', '˙');
|
|
184
|
+
const root = parseXmlDocument(xml)!;
|
|
185
|
+
expect(root.children.length).toEqual(1);
|
|
186
|
+
expect(root.children[0]!.name).toEqual('circle');
|
|
187
|
+
expect(root.children[0]!.attributes['r']).toEqual('4');
|
|
225
188
|
});
|
|
226
|
-
|
|
227
189
|
it('expands the same entity at every reference', () => {
|
|
228
|
-
const
|
|
229
|
-
|
|
230
|
-
expect(root.children).
|
|
190
|
+
const xml = doctype('<!ENTITY dot "<circle/>">', '˙˙˙');
|
|
191
|
+
const root = parseXmlDocument(xml)!;
|
|
192
|
+
expect(root.children.length).toEqual(3);
|
|
231
193
|
});
|
|
232
|
-
|
|
233
194
|
it('expands an entity declared with single quotes', () => {
|
|
234
|
-
const
|
|
235
|
-
|
|
236
|
-
expect(root.children[0]
|
|
195
|
+
const xml = doctype("<!ENTITY dot '<circle/>'>", '˙');
|
|
196
|
+
const root = parseXmlDocument(xml)!;
|
|
197
|
+
expect(root.children[0]!.name).toEqual('circle');
|
|
237
198
|
});
|
|
238
|
-
|
|
239
199
|
it('expands an entity that references another entity', () => {
|
|
240
200
|
const subset = '<!ENTITY inner "<circle/>"><!ENTITY outer "<g>&inner;</g>">';
|
|
241
|
-
const
|
|
242
|
-
|
|
243
|
-
|
|
244
|
-
expect(
|
|
201
|
+
const xml = doctype(subset, '&outer;');
|
|
202
|
+
const root = parseXmlDocument(xml)!;
|
|
203
|
+
const outer = root.children[0]!;
|
|
204
|
+
expect(outer.name).toEqual('g');
|
|
205
|
+
expect(outer.children[0]!.name).toEqual('circle');
|
|
245
206
|
});
|
|
246
|
-
|
|
247
207
|
it('expands an entity used as an attribute value', () => {
|
|
248
|
-
const
|
|
249
|
-
|
|
250
|
-
expect(root.children[0]
|
|
208
|
+
const xml = doctype('<!ENTITY w "40">', '<rect width="&w;"/>');
|
|
209
|
+
const root = parseXmlDocument(xml)!;
|
|
210
|
+
expect(root.children[0]!.attributes['width']).toEqual('40');
|
|
251
211
|
});
|
|
252
|
-
|
|
253
212
|
it('leaves an undeclared reference alone rather than dropping it', () => {
|
|
254
|
-
const
|
|
255
|
-
|
|
256
|
-
expect(root.text).
|
|
257
|
-
});
|
|
258
|
-
|
|
259
|
-
|
|
260
|
-
const root = parseXmlDocument(
|
|
261
|
-
|
|
262
|
-
|
|
263
|
-
|
|
264
|
-
|
|
265
|
-
|
|
266
|
-
|
|
267
|
-
|
|
268
|
-
|
|
269
|
-
|
|
270
|
-
|
|
271
|
-
|
|
272
|
-
|
|
273
|
-
|
|
274
|
-
|
|
275
|
-
|
|
213
|
+
const xml = doctype('<!ENTITY dot "<circle/>">', '&missing;');
|
|
214
|
+
const root = parseXmlDocument(xml)!;
|
|
215
|
+
expect(root.text).toEqual('&missing;');
|
|
216
|
+
});
|
|
217
|
+
it('still decodes the predefined entities which no subset needs to declare', () => {
|
|
218
|
+
const xml = doctype('<!ENTITY dot "<circle/>">', 'a & b');
|
|
219
|
+
const root = parseXmlDocument(xml)!;
|
|
220
|
+
expect(root.text).toEqual('a & b');
|
|
221
|
+
});
|
|
222
|
+
it('does not honor external system entity', () => {
|
|
223
|
+
const xml = doctype('<!ENTITY x SYSTEM "/etc/passwd">', '&x;');
|
|
224
|
+
const root = parseXmlDocument(xml)!;
|
|
225
|
+
expect(root.children.length === 0).toBe(true);
|
|
226
|
+
expect(root.text).toEqual('&x;');
|
|
227
|
+
});
|
|
228
|
+
it('does not honor external public entity', () => {
|
|
229
|
+
const xml = doctype('<!ENTITY x PUBLIC "-//X//EN" "http://example.test/x">', '&x;');
|
|
230
|
+
const root = parseXmlDocument(xml)!;
|
|
231
|
+
expect(root.children.length === 0).toBe(true);
|
|
232
|
+
expect(root.text).toEqual('&x;');
|
|
233
|
+
});
|
|
234
|
+
it('does not honor parameter entity', () => {
|
|
235
|
+
const xml = doctype('<!ENTITY % x "<circle/>">', '&x;');
|
|
236
|
+
const root = parseXmlDocument(xml)!;
|
|
237
|
+
expect(root.children.length === 0).toBe(true);
|
|
238
|
+
expect(root.text).toEqual('&x;');
|
|
276
239
|
});
|
|
277
|
-
|
|
278
|
-
// Entities that reference each other expand exponentially. The budget is what keeps a hostile
|
|
279
|
-
// document costing bounded work; it stops expanding and keeps what resolved.
|
|
280
240
|
it('does not expand a nested entity without bound', () => {
|
|
281
|
-
// Six levels, each ten references deep: fully expanded this is ten million characters, so an
|
|
282
|
-
// unbudgeted parser materializes it and a budgeted one stops well short.
|
|
283
241
|
let subset = '<!ENTITY e0 "aaaaaaaaaa">';
|
|
284
242
|
for (let level = 1; level <= 6; level++) {
|
|
285
|
-
|
|
243
|
+
const prevRef = `&e${level - 1};`;
|
|
244
|
+
const references = repeated(prevRef, 10);
|
|
245
|
+
subset += `<!ENTITY e${level} "${references}">`;
|
|
286
246
|
}
|
|
287
|
-
|
|
288
|
-
const root = parseXmlDocument(
|
|
289
|
-
|
|
290
|
-
expect(root.text.length).toBeLessThan(200000);
|
|
247
|
+
const xml = doctype(subset, '&e6;');
|
|
248
|
+
const root = parseXmlDocument(xml)!;
|
|
249
|
+
expect(root.text.length < 200_000).toBe(true);
|
|
291
250
|
});
|
|
292
|
-
|
|
293
251
|
it('terminates on an entity that references itself', () => {
|
|
294
|
-
const
|
|
295
|
-
|
|
296
|
-
expect(root).
|
|
252
|
+
const xml = doctype('<!ENTITY loop "&loop;">', '&loop;');
|
|
253
|
+
const root = parseXmlDocument(xml);
|
|
254
|
+
expect(root !== null).toBe(true);
|
|
297
255
|
});
|
|
298
256
|
});
|
package/src/xmlQuery.test.ts
CHANGED
|
@@ -1,68 +1,69 @@
|
|
|
1
|
-
|
|
1
|
+
// Generated from crates/flight-xml/tests/xml_query_test.rs by flight-compiler.
|
|
2
|
+
// Rust is authoritative; run `npm run generate` after changing it.
|
|
3
|
+
|
|
4
|
+
import { describe, expect, it } from 'vitest';
|
|
5
|
+
|
|
2
6
|
import {
|
|
3
7
|
getXmlElementAttribute,
|
|
4
8
|
getXmlElementAttributeNumber,
|
|
5
9
|
getXmlElementChildByName,
|
|
6
10
|
getXmlElementChildrenByName,
|
|
7
|
-
|
|
11
|
+
parseXmlDocument,
|
|
12
|
+
} from './contract.ts';
|
|
8
13
|
|
|
9
14
|
describe('getXmlElementAttribute', () => {
|
|
10
15
|
it('returns the attribute value when present', () => {
|
|
11
|
-
const root = parseXmlDocument('<node name="hero" x="10"/>')
|
|
12
|
-
expect(getXmlElementAttribute(root
|
|
16
|
+
const root = parseXmlDocument('<node name="hero" x="10"/>')!;
|
|
17
|
+
expect(getXmlElementAttribute(root, 'name')).toEqual('hero');
|
|
13
18
|
});
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
expect(getXmlElementAttribute(root!, 'missing')).toBeNull();
|
|
19
|
+
it('returns none when the attribute is absent', () => {
|
|
20
|
+
const root = parseXmlDocument('<node name="hero"/>')!;
|
|
21
|
+
expect(getXmlElementAttribute(root, 'missing') === null).toBe(true);
|
|
18
22
|
});
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
expect(getXmlElementAttribute(root!, 'label')).toBe('');
|
|
23
|
+
it('returns an empty string attribute rather than none', () => {
|
|
24
|
+
const root = parseXmlDocument('<node label=""/>')!;
|
|
25
|
+
expect(getXmlElementAttribute(root, 'label')).toEqual('');
|
|
23
26
|
});
|
|
24
27
|
});
|
|
25
28
|
|
|
26
29
|
describe('getXmlElementAttributeNumber', () => {
|
|
27
30
|
it('parses a numeric attribute', () => {
|
|
28
|
-
const root = parseXmlDocument('<node x="10" scale="1.5"/>')
|
|
29
|
-
expect(getXmlElementAttributeNumber(root
|
|
30
|
-
expect(getXmlElementAttributeNumber(root
|
|
31
|
+
const root = parseXmlDocument('<node x="10" scale="1.5"/>')!;
|
|
32
|
+
expect(getXmlElementAttributeNumber(root, 'x')).toEqual(10);
|
|
33
|
+
expect(getXmlElementAttributeNumber(root, 'scale')).toEqual(1.5);
|
|
31
34
|
});
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
expect(getXmlElementAttributeNumber(root!, 'missing')).toBeNull();
|
|
35
|
+
it('returns none when the attribute is absent', () => {
|
|
36
|
+
const root = parseXmlDocument('<node x="10"/>')!;
|
|
37
|
+
expect(getXmlElementAttributeNumber(root, 'missing') === null).toBe(true);
|
|
36
38
|
});
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
expect(getXmlElementAttributeNumber(root
|
|
41
|
-
expect(getXmlElementAttributeNumber(root!, 'y')).toBeNull();
|
|
39
|
+
it('returns none when the value is not a finite number', () => {
|
|
40
|
+
const root = parseXmlDocument('<node x="abc" y=""/>')!;
|
|
41
|
+
expect(getXmlElementAttributeNumber(root, 'x') === null).toBe(true);
|
|
42
|
+
expect(getXmlElementAttributeNumber(root, 'y') === null).toBe(true);
|
|
42
43
|
});
|
|
43
44
|
});
|
|
44
45
|
|
|
45
46
|
describe('getXmlElementChildByName', () => {
|
|
46
47
|
it('returns the first matching child element', () => {
|
|
47
|
-
const root = parseXmlDocument('<root><a id="1"/><b/><a id="2"/></root>')
|
|
48
|
-
|
|
48
|
+
const root = parseXmlDocument('<root><a id="1"/><b/><a id="2"/></root>')!;
|
|
49
|
+
const child = getXmlElementChildByName(root, 'a')!;
|
|
50
|
+
expect(child!.attributes['id']).toEqual('1');
|
|
49
51
|
});
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
expect(getXmlElementChildByName(root!, 'z')).toBeNull();
|
|
52
|
+
it('returns none when no child matches', () => {
|
|
53
|
+
const root = parseXmlDocument('<root><a/></root>')!;
|
|
54
|
+
expect(getXmlElementChildByName(root, 'z') === null).toBe(true);
|
|
54
55
|
});
|
|
55
56
|
});
|
|
56
57
|
|
|
57
58
|
describe('getXmlElementChildrenByName', () => {
|
|
58
59
|
it('returns all matching children in document order', () => {
|
|
59
|
-
const root = parseXmlDocument('<root><a id="1"/><b/><a id="2"/></root>')
|
|
60
|
-
const matches = getXmlElementChildrenByName(root
|
|
61
|
-
|
|
60
|
+
const root = parseXmlDocument('<root><a id="1"/><b/><a id="2"/></root>')!;
|
|
61
|
+
const matches = getXmlElementChildrenByName(root, 'a');
|
|
62
|
+
const ids: string[] = matches.map((c) => c!.attributes['id']);
|
|
63
|
+
expect(ids).toEqual(['1', '2']);
|
|
62
64
|
});
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
expect(getXmlElementChildrenByName(root!, 'z')).toEqual([]);
|
|
65
|
+
it('returns an empty vec when no child matches', () => {
|
|
66
|
+
const root = parseXmlDocument('<root><a/></root>')!;
|
|
67
|
+
expect(getXmlElementChildrenByName(root, 'z').length === 0).toBe(true);
|
|
67
68
|
});
|
|
68
69
|
});
|