@flighthq/xml 0.5.1-next.973.788d3d7 β†’ 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.
@@ -0,0 +1,206 @@
1
+ import { describe, expect, it } from 'vitest';
2
+
3
+ import { parseXmlAttributes, parseXmlDocument } from './contract.ts';
4
+ import {
5
+ createXmlRecord,
6
+ createXmlScanner,
7
+ decodeXmlEntitiesNative,
8
+ expandXmlEntitiesNative,
9
+ getXmlRecordValue,
10
+ parseXmlAttributesNative,
11
+ parseXmlNumberNative,
12
+ prepareXmlDocumentNative,
13
+ pushXmlElementContent,
14
+ pushXmlTextContent,
15
+ setXmlRecordValue,
16
+ xmlIsWhitespace,
17
+ xmlRecordIsEmpty,
18
+ xmlScannerFind,
19
+ xmlScannerLength,
20
+ xmlScannerStartsWith,
21
+ xmlScannerUnitAt,
22
+ xmlStringFromCodePoint,
23
+ xmlTrim,
24
+ xmlUtf16Slice,
25
+ } from './xmlNative.ts';
26
+
27
+ describe('createXmlRecord', () => {
28
+ it('creates an empty record', () => {
29
+ expect(createXmlRecord()).toEqual({});
30
+ });
31
+ });
32
+
33
+ describe('createXmlScanner', () => {
34
+ it('preserves the source used by scanner operations', () => {
35
+ const scanner = createXmlScanner('<root/>');
36
+ expect(xmlScannerLength(scanner)).toBe(7);
37
+ });
38
+ });
39
+
40
+ describe('decodeXmlEntitiesNative', () => {
41
+ it('decodes named, decimal, and hexadecimal entities', () => {
42
+ expect(decodeXmlEntitiesNative('&lt;&#65;&#x42;')).toBe('<AB');
43
+ });
44
+ });
45
+
46
+ describe('expandXmlEntitiesNative', () => {
47
+ it('expands nested document entities', () => {
48
+ expect(expandXmlEntitiesNative('&outer;', { inner: 'value', outer: '&inner;' })).toBe('value');
49
+ });
50
+ });
51
+
52
+ describe('getXmlRecordValue', () => {
53
+ it('distinguishes stored values from missing keys', () => {
54
+ expect(getXmlRecordValue({ present: '' }, 'present')).toBe('');
55
+ expect(getXmlRecordValue({ present: '' }, 'missing')).toBeNull();
56
+ });
57
+ });
58
+
59
+ describe('parseXmlAttributesNative', () => {
60
+ it('parses quoted attributes and decodes their values', () => {
61
+ expect(parseXmlAttributesNative(`first="&lt;" second='value'`)).toEqual({ first: '<', second: 'value' });
62
+ });
63
+ });
64
+
65
+ describe('parseXmlNumberNative', () => {
66
+ it('uses JavaScript number parsing with an explicit invalid result', () => {
67
+ expect(parseXmlNumberNative('12.5')).toBe(12.5);
68
+ expect(parseXmlNumberNative('not-a-number')).toBeNull();
69
+ });
70
+ });
71
+
72
+ describe('prepareXmlDocumentNative', () => {
73
+ it('normalizes markup and collects internal entities', () => {
74
+ const entities = createXmlRecord();
75
+ const xml = '<?xml version="1.0"?><!DOCTYPE root [<!ENTITY value "ok">]><!-- note --><root/>';
76
+ expect(prepareXmlDocumentNative(xml, entities)).toBe('<root/>');
77
+ expect(entities.value).toBe('ok');
78
+ });
79
+ });
80
+
81
+ describe('pushXmlElementContent', () => {
82
+ it('preserves element identity in mixed content', () => {
83
+ const element = parseXmlDocument('<child/>')!;
84
+ const content: typeof element.content = [];
85
+ pushXmlElementContent(content, element);
86
+ expect(content[0]).toBe(element);
87
+ });
88
+ });
89
+
90
+ describe('pushXmlTextContent', () => {
91
+ it('appends text to mixed content', () => {
92
+ const element = parseXmlDocument('<root/>')!;
93
+ const content: typeof element.content = [];
94
+ pushXmlTextContent(content, 'text');
95
+ expect(content).toEqual(['text']);
96
+ });
97
+ });
98
+
99
+ describe('setXmlRecordValue', () => {
100
+ it('sets and replaces record values', () => {
101
+ const record = createXmlRecord();
102
+ setXmlRecordValue(record, 'name', 'first');
103
+ setXmlRecordValue(record, 'name', 'second');
104
+ expect(record).toEqual({ name: 'second' });
105
+ });
106
+ });
107
+
108
+ describe('XML target representations', () => {
109
+ it('creates an empty attribute record for empty input', () => {
110
+ expect(Object.keys(parseXmlAttributes(''))).toEqual([]);
111
+ });
112
+
113
+ it('shares the same child between children and content', () => {
114
+ const document = parseXmlDocument('<root><child/></root>')!;
115
+ expect(document.content[0]).toBe(document.children[0]);
116
+ });
117
+
118
+ it('observes child updates through both projections', () => {
119
+ const document = parseXmlDocument('<root><child/></root>')!;
120
+ document.children[0].name = 'renamed';
121
+ expect((document.content[0] as { name: string }).name).toBe('renamed');
122
+ });
123
+
124
+ it('preserves the order of mixed text and child elements', () => {
125
+ const document = parseXmlDocument('<text>Hello <tspan>world</tspan>!</text>')!;
126
+ expect(document.content[0]).toBe('Hello ');
127
+ expect((document.content[1] as { name: string }).name).toBe('tspan');
128
+ expect(document.content[2]).toBe('!');
129
+ expect(document.text).toBe('Hello!');
130
+ });
131
+
132
+ it('preserves markup and comment syntax inside CDATA as text', () => {
133
+ const document = parseXmlDocument('<root><![CDATA[<tag>&amp;<!-- literal -->]]><child/>tail</root>')!;
134
+ expect(document.children.map((child) => child.name)).toEqual(['child']);
135
+ expect(document.content[0]).toBe('<tag>&amp;<!-- literal -->');
136
+ expect(document.text).toBe('<tag>&amp;<!-- literal -->tail');
137
+ });
138
+
139
+ it('preserves attribute insertion order and duplicate position', () => {
140
+ const attributes = parseXmlAttributes('z="first" a="second" z="updated"');
141
+ expect(Object.keys(attributes)).toEqual(['z', 'a']);
142
+ expect(attributes.z).toBe('updated');
143
+ });
144
+ });
145
+
146
+ describe('xmlIsWhitespace', () => {
147
+ it('classifies UTF-16 code units with JavaScript whitespace semantics', () => {
148
+ expect(xmlIsWhitespace('\n'.charCodeAt(0))).toBe(true);
149
+ expect(xmlIsWhitespace('x'.charCodeAt(0))).toBe(false);
150
+ });
151
+ });
152
+
153
+ describe('xmlRecordIsEmpty', () => {
154
+ it('detects own enumerable keys', () => {
155
+ expect(xmlRecordIsEmpty({})).toBe(true);
156
+ expect(xmlRecordIsEmpty({ present: '' })).toBe(false);
157
+ });
158
+ });
159
+
160
+ describe('xmlScannerFind', () => {
161
+ it('returns UTF-16 offsets and null for missing patterns', () => {
162
+ const scanner = createXmlScanner('aπŸ˜€tail');
163
+ expect(xmlScannerFind(scanner, 'tail', 0)).toBe(3);
164
+ expect(xmlScannerFind(scanner, 'missing', 0)).toBeNull();
165
+ });
166
+ });
167
+
168
+ describe('xmlScannerLength', () => {
169
+ it('measures UTF-16 code units', () => {
170
+ expect(xmlScannerLength(createXmlScanner('aπŸ˜€'))).toBe(3);
171
+ });
172
+ });
173
+
174
+ describe('xmlScannerStartsWith', () => {
175
+ it('matches a pattern at the requested offset', () => {
176
+ const scanner = createXmlScanner('<root/>');
177
+ expect(xmlScannerStartsWith(scanner, 1, 'root')).toBe(true);
178
+ expect(xmlScannerStartsWith(scanner, 2, 'root')).toBe(false);
179
+ });
180
+ });
181
+
182
+ describe('xmlScannerUnitAt', () => {
183
+ it('reads a UTF-16 code unit at the requested offset', () => {
184
+ expect(xmlScannerUnitAt(createXmlScanner('<root/>'), 0)).toBe('<'.charCodeAt(0));
185
+ });
186
+ });
187
+
188
+ describe('xmlStringFromCodePoint', () => {
189
+ it('rejects invalid Unicode scalar values', () => {
190
+ expect(xmlStringFromCodePoint(0x1f600)).toBe('πŸ˜€');
191
+ expect(xmlStringFromCodePoint(0xd800)).toBeNull();
192
+ expect(xmlStringFromCodePoint(0x110000)).toBeNull();
193
+ });
194
+ });
195
+
196
+ describe('xmlTrim', () => {
197
+ it('trims JavaScript whitespace', () => {
198
+ expect(xmlTrim(' \nvalue\t')).toBe('value');
199
+ });
200
+ });
201
+
202
+ describe('xmlUtf16Slice', () => {
203
+ it('slices by UTF-16 code-unit offsets', () => {
204
+ expect(xmlUtf16Slice('aπŸ˜€b', 1, 3)).toBe('πŸ˜€');
205
+ });
206
+ });