Debian Codegraph @

test/tests/unit/HCON.js

raw symbols SCIP
1 const HCON = htmx.__HCON;
2
3 describe('HCON unit tests', function() {
4
5 it('parses simple key-value pair', function () {
6 let parsed = HCON.parse('delay:100ms')
7 assert.equal(parsed.delay, '100ms')
8 })
9
10 it('parses boolean true', function () {
11 let parsed = HCON.parse('once')
12 assert.equal(parsed.once, true)
13 })
14
15 it('parses boolean false', function () {
16 let parsed = HCON.parse('once:false')
17 assert.equal(parsed.once, false)
18 })
19
20 it('parses integer value', function () {
21 let parsed = HCON.parse('count:42')
22 assert.equal(parsed.count, 42)
23 })
24
25 it('parses multiple key-value pairs', function () {
26 let parsed = HCON.parse('delay:100ms throttle:200ms')
27 assert.equal(parsed.delay, '100ms')
28 assert.equal(parsed.throttle, '200ms')
29 })
30
31 it('parses comma-separated pairs', function () {
32 let parsed = HCON.parse('delay:100ms, throttle:200ms')
33 assert.equal(parsed.delay, '100ms')
34 assert.equal(parsed.throttle, '200ms')
35 })
36
37 it('parses double-quoted string', function () {
38 let parsed = HCON.parse('target:"#foo .bar"')
39 assert.equal(parsed.target, '#foo .bar')
40 })
41
42 it('parses single-quoted string', function () {
43 let parsed = HCON.parse("target:'#foo .bar'")
44 assert.equal(parsed.target, '#foo .bar')
45 })
46
47 it('parses nested object with dot notation', function () {
48 let parsed = HCON.parse('sse.mode:once')
49 assert.equal(parsed.sse.mode, 'once')
50 })
51
52 it('parses multiple nested properties', function () {
53 let parsed = HCON.parse('sse.mode:once sse.maxRetries:5')
54 assert.equal(parsed.sse.mode, 'once')
55 assert.equal(parsed.sse.maxRetries, 5)
56 })
57
58 it('parses JSON object', function () {
59 let parsed = HCON.parse('{"delay":"100ms","throttle":"200ms"}')
60 assert.equal(parsed.delay, '100ms')
61 assert.equal(parsed.throttle, '200ms')
62 })
63
64 it('parses JSON with nested object', function () {
65 let parsed = HCON.parse('{"sse":{"mode":"once","maxRetries":5}}')
66 assert.equal(parsed.sse.mode, 'once')
67 assert.equal(parsed.sse.maxRetries, 5)
68 })
69
70 it('parses JSON with boolean', function () {
71 let parsed = HCON.parse('{"once":true,"changed":false}')
72 assert.equal(parsed.once, true)
73 assert.equal(parsed.changed, false)
74 })
75
76 it('handles empty string', function () {
77 let parsed = HCON.parse('')
78 assert.deepEqual(parsed, {})
79 })
80
81 it('handles whitespace', function () {
82 let parsed = HCON.parse(' delay:100ms ')
83 assert.equal(parsed.delay, '100ms')
84 })
85
86 it('blocks __proto__ pollution via dotted keys', function () {
87 let before = ({}).polluted;
88 HCON.parse('__proto__.polluted: true');
89 assert.equal(({}).polluted, before);
90 })
91
92 it('blocks constructor pollution via dotted keys', function () {
93 let before = ({}).hacked;
94 HCON.parse('constructor.prototype.hacked: true');
95 assert.equal(({}).hacked, before);
96 })
97
98 it('parses unparseable value as string', function () {
99 let parsed = HCON.parse('foo:bar')
100 assert.equal(parsed.foo, 'bar')
101 })
102
103 it('parses quoted dotted key literally', function () {
104 let parsed = HCON.parse('"a.b":1')
105 assert.equal(parsed['a.b'], 1)
106 assert.isUndefined(parsed.a)
107 })
108
109 it('parses single-quoted dotted key literally', function () {
110 let parsed = HCON.parse("'a.b':1")
111 assert.equal(parsed['a.b'], 1)
112 assert.isUndefined(parsed.a)
113 })
114
115 it('parses quoted value with comma', function () {
116 let parsed = HCON.parse('x:"hello, world"')
117 assert.equal(parsed.x, 'hello, world')
118 })
119
120 it('parses JSON array inside quoted value', function () {
121 let parsed = HCON.parse('x:"[1, 2, 3]"')
122 assert.deepEqual(parsed.x, [1, 2, 3])
123 })
124
125 // hyperscript value form (CSS selectors of every shape)
126
127 it('parses hyperscript value: simple selector', function () {
128 let parsed = HCON.parse('from:<.foo/>')
129 assert.equal(parsed.from, '.foo')
130 })
131
132 it('parses hyperscript value: descendant selector', function () {
133 let parsed = HCON.parse('from:<div p/>')
134 assert.equal(parsed.from, 'div p')
135 })
136
137 it('parses hyperscript value: child combinator (>)', function () {
138 let parsed = HCON.parse('from:<ul > li/>')
139 assert.equal(parsed.from, 'ul > li')
140 })
141
142 it('parses hyperscript value: adjacent sibling (+)', function () {
143 let parsed = HCON.parse('from:<h1 + p/>')
144 assert.equal(parsed.from, 'h1 + p')
145 })
146
147 it('parses hyperscript value: general sibling (~)', function () {
148 let parsed = HCON.parse('from:<h1 ~ p/>')
149 assert.equal(parsed.from, 'h1 ~ p')
150 })
151
152 it('parses hyperscript value: class chain', function () {
153 let parsed = HCON.parse('from:<.a.b.c/>')
154 assert.equal(parsed.from, '.a.b.c')
155 })
156
157 it('parses hyperscript value: id + class', function () {
158 let parsed = HCON.parse('from:<#id.class/>')
159 assert.equal(parsed.from, '#id.class')
160 })
161
162 it('parses hyperscript value: attribute selector', function () {
163 let parsed = HCON.parse('from:<[data-x="y"]/>')
164 assert.equal(parsed.from, '[data-x="y"]')
165 })
166
167 it('parses hyperscript value: attribute selector with comma', function () {
168 let parsed = HCON.parse('from:<[data-list="a, b"]/>')
169 assert.equal(parsed.from, '[data-list="a, b"]')
170 })
171
172 it('parses hyperscript value: :not() with comma', function () {
173 let parsed = HCON.parse('from:<:not(.a, .b)/>')
174 assert.equal(parsed.from, ':not(.a, .b)')
175 })
176
177 it('parses hyperscript value: :has() with child combinator', function () {
178 let parsed = HCON.parse('from:<div:has(> .child)/>')
179 assert.equal(parsed.from, 'div:has(> .child)')
180 })
181
182 it('parses hyperscript value: :nth-child', function () {
183 let parsed = HCON.parse('from:<li:nth-child(2n+1)/>')
184 assert.equal(parsed.from, 'li:nth-child(2n+1)')
185 })
186
187 it('parses hyperscript value: selector group (comma-separated)', function () {
188 let parsed = HCON.parse('from:<.a, .b, .c/>')
189 assert.equal(parsed.from, '.a, .b, .c')
190 })
191
192 it('parses hyperscript value: complex multi-combinator selector', function () {
193 let parsed = HCON.parse('from:<.list > li:not(.disabled, .hidden) + .item/>')
194 assert.equal(parsed.from, '.list > li:not(.disabled, .hidden) + .item')
195 })
196
197 it('parses hyperscript value followed by another modifier', function () {
198 let parsed = HCON.parse('from:<ul > li/> throttle:50ms')
199 assert.equal(parsed.from, 'ul > li')
200 assert.equal(parsed.throttle, '50ms')
201 })
202
203 });
204
205 describe('HCON.split unit tests', function() {
206
207 // trivial
208
209 it('splits empty string into one empty part', function () {
210 assert.deepEqual(HCON.split(''), [''])
211 })
212
213 it('returns a single value untouched', function () {
214 assert.deepEqual(HCON.split('foo'), ['foo'])
215 })
216
217 it('splits two values', function () {
218 assert.deepEqual(HCON.split('foo,bar'), ['foo', 'bar'])
219 })
220
221 it('splits three values', function () {
222 assert.deepEqual(HCON.split('a,b,c'), ['a', 'b', 'c'])
223 })
224
225 it('preserves whitespace around commas', function () {
226 assert.deepEqual(HCON.split('foo , bar'), ['foo ', ' bar'])
227 })
228
229 // edges with empties
230
231 it('produces empty part for trailing comma', function () {
232 assert.deepEqual(HCON.split('foo,'), ['foo', ''])
233 })
234
235 it('produces empty part for leading comma', function () {
236 assert.deepEqual(HCON.split(',foo'), ['', 'foo'])
237 })
238
239 it('produces empty parts for consecutive commas', function () {
240 assert.deepEqual(HCON.split('a,,b'), ['a', '', 'b'])
241 })
242
243 it('produces only empties for only-commas input', function () {
244 assert.deepEqual(HCON.split(',,'), ['', '', ''])
245 })
246
247 // bracket protection
248
249 it('does not split commas inside []', function () {
250 assert.deepEqual(HCON.split('click[fn(a,b)],keyup'), ['click[fn(a,b)]', 'keyup'])
251 })
252
253 it('does not split commas inside attribute selector value', function () {
254 assert.deepEqual(HCON.split('[data-x="a,b"],.c'), ['[data-x="a,b"]', '.c'])
255 })
256
257 // paren protection
258
259 it('does not split commas inside ()', function () {
260 assert.deepEqual(HCON.split('.x:not(.a, .b),.c'), ['.x:not(.a, .b)', '.c'])
261 })
262
263 it('does not split commas inside nested pseudo-classes', function () {
264 assert.deepEqual(HCON.split(':has(.a, .b),.c'), [':has(.a, .b)', '.c'])
265 })
266
267 // hyperscript protection
268
269 it('does not split commas inside <.../>', function () {
270 assert.deepEqual(HCON.split('<doX(a, b)/>,change'), ['<doX(a, b)/>', 'change'])
271 })
272
273 it('does not split commas inside <.../> with > combinator', function () {
274 assert.deepEqual(HCON.split('<ul > li:not(.a, .b)/>,next'), ['<ul > li:not(.a, .b)/>', 'next'])
275 })
276
277 // quote protection
278
279 it('does not split commas inside double-quoted values', function () {
280 assert.deepEqual(HCON.split('from:".a, .b",intersect'), ['from:".a, .b"', 'intersect'])
281 })
282
283 it('does not split commas inside single-quoted values', function () {
284 assert.deepEqual(HCON.split("from:'.a, .b',intersect"), ["from:'.a, .b'", 'intersect'])
285 })
286
287 // combined protections
288
289 it('protects nested protections (bracket containing paren)', function () {
290 assert.deepEqual(HCON.split('[fn(a, b)],x'), ['[fn(a, b)]', 'x'])
291 })
292
293 it('handles all four protections in one input', function () {
294 let input = '[a,b],(c,d),<e,f/>,"g,h",i'
295 assert.deepEqual(HCON.split(input), ['[a,b]', '(c,d)', '<e,f/>', '"g,h"', 'i'])
296 })
297
298 // realistic inputs
299
300 it('splits realistic trigger spec', function () {
301 let result = HCON.split('click from:".a, .b", change from:<ul > li/>')
302 assert.equal(result.length, 2)
303 assert.equal(result[0], 'click from:".a, .b"')
304 assert.equal(result[1], ' change from:<ul > li/>')
305 })
306
307 it('splits realistic selector list', function () {
308 let result = HCON.split('closest .parent, find :not(.a, .b), .other')
309 assert.equal(result.length, 3)
310 assert.equal(result[0], 'closest .parent')
311 assert.equal(result[1], ' find :not(.a, .b)')
312 assert.equal(result[2], ' .other')
313 })
314
315 // unbalanced (best-effort recovery)
316
317 it('still splits after unbalanced bracket (best-effort)', function () {
318 let result = HCON.split('foo[bar,baz')
319 assert.equal(result.length, 2)
320 })
321
322 });
323
324 describe('HCON.merge unit tests', function() {
325
326 // trivial
327
328 it('returns target unchanged when source is empty object', function () {
329 let target = { a: 1 }
330 let result = HCON.merge({}, target)
331 assert.deepEqual(result, { a: 1 })
332 assert.strictEqual(result, target)
333 })
334
335 it('copies source key into empty target', function () {
336 let result = HCON.merge({ a: 1 }, {})
337 assert.deepEqual(result, { a: 1 })
338 })
339
340 it('mutates target in place and returns it', function () {
341 let target = {}
342 let result = HCON.merge({ a: 1 }, target)
343 assert.strictEqual(result, target)
344 assert.equal(target.a, 1)
345 })
346
347 // shallow merge
348
349 it('merges non-overlapping keys', function () {
350 let result = HCON.merge({ b: 2 }, { a: 1 })
351 assert.deepEqual(result, { a: 1, b: 2 })
352 })
353
354 it('overwrites scalar values', function () {
355 let result = HCON.merge({ a: 2 }, { a: 1 })
356 assert.equal(result.a, 2)
357 })
358
359 // deep merge
360
361 it('deep-merges 2-level nested objects preserving siblings', function () {
362 let result = HCON.merge({ sse: { mode: 'once' } }, { sse: { maxRetries: 5 } })
363 assert.deepEqual(result, { sse: { mode: 'once', maxRetries: 5 } })
364 })
365
366 it('deep-merges 3-level nested objects preserving siblings (the bug fix)', function () {
367 let result = HCON.merge(
368 { sse: { connection: { retries: 3 } } },
369 { sse: { connection: { timeout: 5000 } } }
370 )
371 assert.deepEqual(result, { sse: { connection: { timeout: 5000, retries: 3 } } })
372 })
373
374 it('deep-merges 4-level nested objects', function () {
375 let result = HCON.merge(
376 { a: { b: { c: { d: 1 } } } },
377 { a: { b: { c: { e: 2 } } } }
378 )
379 assert.deepEqual(result, { a: { b: { c: { d: 1, e: 2 } } } })
380 })
381
382 // type clashes
383
384 it('overwrites object target with scalar source', function () {
385 let result = HCON.merge({ a: 'x' }, { a: { b: 1 } })
386 assert.equal(result.a, 'x')
387 })
388
389 it('overwrites scalar target with object source', function () {
390 let result = HCON.merge({ a: { b: 1 } }, { a: 'x' })
391 assert.deepEqual(result.a, { b: 1 })
392 })
393
394 it('does not deep-merge arrays (replaces them)', function () {
395 let result = HCON.merge({ list: [3, 4] }, { list: [1, 2] })
396 assert.deepEqual(result.list, [3, 4])
397 })
398
399 // string source (auto-parse)
400
401 it('parses an HCON string source', function () {
402 let result = HCON.merge('a:1 b:2', {})
403 assert.deepEqual(result, { a: 1, b: 2 })
404 })
405
406 it('parses a JSON-format string source', function () {
407 let result = HCON.merge('{"a":1,"b":2}', {})
408 assert.deepEqual(result, { a: 1, b: 2 })
409 })
410
411 it('parses dotted HCON keys into nested merge', function () {
412 let result = HCON.merge('sse.mode:once', { sse: { existing: true } })
413 assert.deepEqual(result.sse, { existing: true, mode: 'once' })
414 })
415
416 // pollution guard
417
418 it('skips __proto__ key from source', function () {
419 let target = {}
420 HCON.merge({ __proto__: { polluted: true } }, target)
421 assert.isUndefined(({}).polluted)
422 })
423
424 it('skips constructor key from source', function () {
425 let target = {}
426 HCON.merge({ constructor: { hacked: true } }, target)
427 assert.isUndefined(({}).hacked)
428 })
429
430 it('skips prototype key from source', function () {
431 let target = {}
432 HCON.merge({ prototype: { hacked: true } }, target)
433 assert.equal(target.prototype, undefined)
434 })
435
436 });
437