|
1 |
| -import test from 'tape' |
| 1 | +import assert from 'node:assert/strict' |
| 2 | +import test from 'node:test' |
2 | 3 | import {x} from 'xastscript'
|
3 | 4 | import {u} from 'unist-builder'
|
4 | 5 | import {toXml} from '../index.js'
|
5 | 6 |
|
6 |
| -test('`element` attributes', (t) => { |
7 |
| - t.deepEqual( |
| 7 | +test('`element` attributes', async (t) => { |
| 8 | + assert.deepEqual( |
8 | 9 | toXml(u('element', {name: 'y'}, [])),
|
9 | 10 | '<y></y>',
|
10 | 11 | 'should ignore missing attributes'
|
11 | 12 | )
|
12 | 13 |
|
13 |
| - t.deepEqual( |
| 14 | + assert.deepEqual( |
14 | 15 | toXml(u('element', {name: 'y', attributes: {a: null}}, [])),
|
15 | 16 | '<y></y>',
|
16 | 17 | 'should ignore attributes set to `null`'
|
17 | 18 | )
|
18 | 19 |
|
19 |
| - t.deepEqual( |
| 20 | + assert.deepEqual( |
20 | 21 | toXml(u('element', {name: 'y', attributes: {a: undefined}}, [])),
|
21 | 22 | '<y></y>',
|
22 | 23 | 'should ignore attributes set to `undefined`'
|
23 | 24 | )
|
24 | 25 |
|
25 |
| - t.deepEqual( |
| 26 | + assert.deepEqual( |
26 | 27 | // @ts-expect-error runtime.
|
27 | 28 | toXml(u('element', {name: 'y', attributes: {a: Number.NaN}}, [])),
|
28 | 29 | '<y a="NaN"></y>',
|
29 | 30 | 'should include attributes set to `NaN`'
|
30 | 31 | )
|
31 | 32 |
|
32 |
| - t.deepEqual( |
| 33 | + assert.deepEqual( |
33 | 34 | // @ts-expect-error runtime.
|
34 | 35 | toXml(u('element', {name: 'y', attributes: {a: true}}, [])),
|
35 | 36 | '<y a="true"></y>',
|
36 | 37 | 'should include attributes set to `true`'
|
37 | 38 | )
|
38 | 39 |
|
39 |
| - t.deepEqual( |
| 40 | + assert.deepEqual( |
40 | 41 | toXml(u('element', {name: 'y', attributes: {a: 'b'}}, [])),
|
41 | 42 | '<y a="b"></y>',
|
42 | 43 | 'should include attributes set to a string'
|
43 | 44 | )
|
44 | 45 |
|
45 |
| - t.deepEqual( |
| 46 | + assert.deepEqual( |
46 | 47 | // @ts-expect-error runtime.
|
47 | 48 | toXml(u('element', {name: 'y', attributes: {a: ['b', 'c']}}, [])),
|
48 | 49 | '<y a="b,c"></y>',
|
49 | 50 | 'should include attributes set to an array'
|
50 | 51 | )
|
51 | 52 |
|
52 |
| - t.deepEqual( |
| 53 | + assert.deepEqual( |
53 | 54 | // @ts-expect-error runtime.
|
54 | 55 | toXml(u('element', {name: 'y', attributes: {a: 1}}, [])),
|
55 | 56 | '<y a="1"></y>',
|
56 | 57 | 'should include attributes set to a number'
|
57 | 58 | )
|
58 | 59 |
|
59 |
| - t.deepEqual( |
| 60 | + assert.deepEqual( |
60 | 61 | // @ts-expect-error runtime.
|
61 | 62 | toXml(u('element', {name: 'y', attributes: {a: 0}}, [])),
|
62 | 63 | '<y a="0"></y>',
|
63 | 64 | 'should include attributes set to `0`'
|
64 | 65 | )
|
65 | 66 |
|
66 |
| - t.deepEqual( |
| 67 | + assert.deepEqual( |
67 | 68 | // @ts-expect-error runtime.
|
68 | 69 | toXml(u('element', {name: 'y', attributes: {a: {toString}}}, [])),
|
69 | 70 | '<y a="yup"></y>',
|
70 | 71 | 'should stringify unknowns set to objects'
|
71 | 72 | )
|
72 | 73 |
|
73 |
| - t.end() |
74 |
| - |
75 |
| - t.test('quote', (st) => { |
76 |
| - st.deepEqual( |
| 74 | + await t.test('quote', () => { |
| 75 | + assert.deepEqual( |
77 | 76 | toXml(x('y', {a: 'b'})),
|
78 | 77 | '<y a="b"></y>',
|
79 | 78 | 'should quote attribute values with double quotes by default'
|
80 | 79 | )
|
81 | 80 |
|
82 |
| - st.deepEqual( |
| 81 | + assert.deepEqual( |
83 | 82 | toXml(x('y', {a: 'b'}), {quote: "'"}),
|
84 | 83 | "<y a='b'></y>",
|
85 | 84 | 'should quote attribute values with single quotes if `quote: "\'"`'
|
86 | 85 | )
|
87 | 86 |
|
88 |
| - st.deepEqual( |
| 87 | + assert.deepEqual( |
89 | 88 | toXml(x('y', {a: 'b'}), {quote: '"'}),
|
90 | 89 | '<y a="b"></y>',
|
91 | 90 | "should quote attribute values with double quotes if `quote: '\"'`"
|
92 | 91 | )
|
93 | 92 |
|
94 |
| - st.deepEqual( |
| 93 | + assert.deepEqual( |
95 | 94 | toXml(x('y', {a: "'b'"}), {quote: "'"}),
|
96 | 95 | "<y a=''b''></y>",
|
97 | 96 | 'should quote attribute values with single quotes if `quote: "\'"` even if they occur in value'
|
98 | 97 | )
|
99 | 98 |
|
100 |
| - st.deepEqual( |
| 99 | + assert.deepEqual( |
101 | 100 | toXml(x('y', {a: '"b"'}), {quote: '"'}),
|
102 | 101 | '<y a=""b""></y>',
|
103 | 102 | "should quote attribute values with double quotes if `quote: '\"'` even if they occur in value"
|
104 | 103 | )
|
105 | 104 |
|
106 |
| - st.throws( |
| 105 | + assert.throws( |
107 | 106 | () => {
|
108 | 107 | // @ts-expect-error runtime.
|
109 | 108 | toXml(x('y'), {quote: '`'})
|
110 | 109 | },
|
111 | 110 | /Invalid quote ```, expected `'` or `"`/,
|
112 | 111 | 'should throw on invalid quotes'
|
113 | 112 | )
|
114 |
| - |
115 |
| - st.end() |
116 | 113 | })
|
117 | 114 |
|
118 |
| - t.test('quoteSmart', (st) => { |
119 |
| - st.deepEqual( |
| 115 | + await t.test('quoteSmart', () => { |
| 116 | + assert.deepEqual( |
120 | 117 | toXml(x('y', {a: 'b'}), {quoteSmart: true}),
|
121 | 118 | '<y a="b"></y>',
|
122 | 119 | 'should quote attribute values with primary quotes by default (`"`)'
|
123 | 120 | )
|
124 | 121 |
|
125 |
| - st.deepEqual( |
| 122 | + assert.deepEqual( |
126 | 123 | toXml(x('y', {a: 'b'}), {quote: "'", quoteSmart: true}),
|
127 | 124 | "<y a='b'></y>",
|
128 | 125 | "should quote attribute values with primary quotes by default (`'`)"
|
129 | 126 | )
|
130 | 127 |
|
131 |
| - st.deepEqual( |
| 128 | + assert.deepEqual( |
132 | 129 | toXml(x('y', {a: "'b'"}), {quoteSmart: true}),
|
133 | 130 | '<y a="\'b\'"></y>',
|
134 | 131 | 'should quote attribute values with primary quotes if the alternative occurs'
|
135 | 132 | )
|
136 | 133 |
|
137 |
| - st.deepEqual( |
| 134 | + assert.deepEqual( |
138 | 135 | toXml(x('y', {a: "'\"b'"}), {quoteSmart: true}),
|
139 | 136 | '<y a="\'"b\'"></y>',
|
140 | 137 | 'should quote attribute values with primary quotes if they occur less than the alternative'
|
141 | 138 | )
|
142 | 139 |
|
143 |
| - st.deepEqual( |
| 140 | + assert.deepEqual( |
144 | 141 | toXml(x('y', {a: '"b\''}), {quoteSmart: true}),
|
145 | 142 | '<y a=""b\'"></y>',
|
146 | 143 | 'should quote attribute values with primary quotes if they occur as much as alternatives (#1)'
|
147 | 144 | )
|
148 | 145 |
|
149 |
| - st.deepEqual( |
| 146 | + assert.deepEqual( |
150 | 147 | toXml(x('y', {a: '"\'b\'"'}), {quoteSmart: true}),
|
151 | 148 | '<y a=""\'b\'""></y>',
|
152 | 149 | 'should quote attribute values with primary quotes if they occur as much as alternatives (#2)'
|
153 | 150 | )
|
154 | 151 |
|
155 |
| - st.deepEqual( |
| 152 | + assert.deepEqual( |
156 | 153 | toXml(x('y', {a: '"b"'}), {quoteSmart: true}),
|
157 | 154 | '<y a=\'"b"\'></y>',
|
158 | 155 | 'should quote attribute values with alternative quotes if the primary occurs'
|
159 | 156 | )
|
160 | 157 |
|
161 |
| - st.deepEqual( |
| 158 | + assert.deepEqual( |
162 | 159 | toXml(x('y', {a: '"\'b"'}), {quoteSmart: true}),
|
163 | 160 | '<y a=\'"'b"\'></y>',
|
164 | 161 | 'should quote attribute values with alternative quotes if they occur less than the primary'
|
165 | 162 | )
|
166 |
| - |
167 |
| - st.end() |
168 | 163 | })
|
169 | 164 |
|
170 |
| - t.test('entities in attributes', (st) => { |
171 |
| - st.deepEqual( |
| 165 | + await t.test('entities in attributes', () => { |
| 166 | + assert.deepEqual( |
172 | 167 | toXml(x('y', {'3<5': 'a'})),
|
173 | 168 | '<y 3<5="a"></y>',
|
174 | 169 | 'should encode entities in attribute names'
|
175 | 170 | )
|
176 | 171 |
|
177 |
| - st.deepEqual( |
| 172 | + assert.deepEqual( |
178 | 173 | toXml(x('y', {'a\0b': 'c'})),
|
179 | 174 | '<y ab="c"></y>',
|
180 | 175 | 'should strip illegal characters in attribute names'
|
181 | 176 | )
|
182 | 177 |
|
183 |
| - st.deepEqual( |
| 178 | + assert.deepEqual( |
184 | 179 | toXml(x('y', {a: '3<5'})),
|
185 | 180 | '<y a="3<5"></y>',
|
186 | 181 | 'should encode entities in attribute values'
|
187 | 182 | )
|
188 | 183 |
|
189 |
| - st.deepEqual( |
| 184 | + assert.deepEqual( |
190 | 185 | toXml(x('y', {a: 'b\0c'})),
|
191 | 186 | '<y a="bc"></y>',
|
192 | 187 | 'should strip illegal characters in attribute values'
|
193 | 188 | )
|
194 | 189 |
|
195 |
| - st.deepEqual( |
| 190 | + assert.deepEqual( |
196 | 191 | toXml(x('y', {'3=5': 'a'})),
|
197 | 192 | '<y 3=5="a"></y>',
|
198 | 193 | 'should encode `=` in attribute names'
|
199 | 194 | )
|
200 | 195 |
|
201 |
| - st.deepEqual( |
| 196 | + assert.deepEqual( |
202 | 197 | toXml(x('y', {a: '3=5'})),
|
203 | 198 | '<y a="3=5"></y>',
|
204 | 199 | 'should not encode `=` in attribute values'
|
205 | 200 | )
|
206 |
| - |
207 |
| - st.end() |
208 | 201 | })
|
209 | 202 | })
|
210 | 203 |
|
|
0 commit comments