-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathparse.test.ts
More file actions
2743 lines (2233 loc) · 83.9 KB
/
parse.test.ts
File metadata and controls
2743 lines (2233 loc) · 83.9 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
import { describe, test, expect } from 'vitest'
import { parse } from './parse'
import {
STYLESHEET,
STYLE_RULE,
AT_RULE,
DECLARATION,
BLOCK,
SELECTOR_LIST,
SELECTOR,
PSEUDO_CLASS_SELECTOR,
TYPE_SELECTOR,
ATTRIBUTE_SELECTOR,
NESTING_SELECTOR,
URL,
RAW,
AT_RULE_PRELUDE,
} from './constants'
import { ATTR_OPERATOR_PIPE_EQUAL } from './arena'
describe('Core Nodes', () => {
describe('Locations', () => {
describe('STYLESHEET', () => {
test('offset and length for empty stylesheet', () => {
const ast = parse('')
expect(ast.start).toBe(0)
expect(ast.length).toBe(0)
expect(ast.end).toBe(0)
})
test('offset and length for stylesheet with rules', () => {
const css = 'body { color: red; }'
const ast = parse(css)
expect(ast.start).toBe(0)
expect(ast.length).toBe(css.length)
expect(ast.end).toBe(css.length)
})
test('line and column for stylesheet', () => {
const ast = parse('body { color: red; }')
expect(ast.line).toBe(1)
expect(ast.column).toBe(1)
})
})
describe('STYLE_RULE', () => {
test('offset and length for simple rule', () => {
const source = 'body { color: red; }'
const ast = parse(source)
const rule = ast.first_child!
expect(rule.start).toBe(0)
expect(rule.length).toBe(source.length)
expect(rule.end).toBe(source.length)
})
test('offset and length for multiple rules', () => {
const css = 'body { } div { }'
const ast = parse(css)
const [rule1, rule2] = ast.children
expect(rule1.start).toBe(0)
expect(rule1.length).toBe(8) // 'body { }'
expect(rule1.end).toBe(8)
expect(rule2.start).toBe(9)
expect(rule2.length).toBe(7) // 'div { }'
expect(rule2.end).toBe(16)
})
test('line and column for rules on single line', () => {
const css = 'body { color: red; }'
const ast = parse(css)
const rule = ast.first_child!
expect(rule.line).toBe(1)
expect(rule.column).toBe(1)
})
test('line and column for rules on multiple lines', () => {
const css = 'body { color: red; }\ndiv { margin: 0; }'
const ast = parse(css)
const [rule1, rule2] = ast.children
expect(rule1.line).toBe(1)
expect(rule1.column).toBe(1)
expect(rule2.line).toBe(2)
expect(rule2.column).toBe(1)
})
test('column for multiple rules on same line', () => {
const css = 'a { color: red; } b { color: blue; }'
const ast = parse(css)
const [rule1, rule2] = ast.children
expect(rule1.line).toBe(1)
expect(rule1.column).toBe(1)
expect(rule2.line).toBe(1)
expect(rule2.column).toBe(19)
})
test('column with leading whitespace', () => {
const css = ' body { color: red; }'
const ast = parse(css)
const rule = ast.first_child!
expect(rule.line).toBe(1)
expect(rule.column).toBe(5)
})
test('column for nested rule in at-rule', () => {
const css = '@media screen { body { color: blue; } }'
const ast = parse(css)
const atRule = ast.first_child!
const block = atRule.block!
const nestedRule = block.first_child!
expect(nestedRule.line).toBe(1)
expect(nestedRule.column).toBe(17)
})
test('line, column for rule with multiple selectors', () => {
const css = `
first#selector,
#second.selector {}
`
const ast = parse(css)
const rule = ast.first_child!
const selectorlist = rule.prelude!
const [first, second] = selectorlist.children
expect(first.line).toBe(2)
expect(first.column).toBe(6)
expect(first.start).toBe(6)
expect(second.line).toBe(3)
expect(second.column).toBe(6)
expect(second.start).toBe(27)
})
})
describe('AT_RULE', () => {
test('offset and length for @import', () => {
const source = '@import url("style.css");'
const ast = parse(source, { parse_atrule_preludes: false })
const atRule = ast.first_child!
expect(atRule.start).toBe(0)
expect(atRule.length).toBe(25)
expect(atRule.end).toBe(25)
})
test('offset and length for @media', () => {
const source = '@media (min-width: 768px) { body { color: red; } }'
const ast = parse(source, { parse_atrule_preludes: false })
const media = ast.first_child!
expect(media.start).toBe(0)
expect(media.length).toBe(50)
expect(media.end).toBe(50)
})
test('line and column for at-rule', () => {
const css = '@media screen { body { color: blue; } }'
const ast = parse(css)
const atRule = ast.first_child!
expect(atRule.line).toBe(1)
expect(atRule.column).toBe(1)
})
test('line for at-rule after rule', () => {
const css = 'body { color: red; }\n\n@media screen { }'
const ast = parse(css)
const [_rule1, atRule] = ast.children
expect(atRule.line).toBe(3)
})
})
describe('DECLARATION', () => {
test('offset and length for simple declaration', () => {
const css = 'body { color: red; }'
const ast = parse(css)
const rule = ast.first_child!
const block = rule.block!
const decl = block.first_child!
expect(decl.start).toBeGreaterThan(0)
expect(decl.length).toBeGreaterThan(0)
})
test('column for single-line declaration', () => {
const css = 'body { color: red; }'
const ast = parse(css)
const rule = ast.first_child!
const block = rule.block!
const decl = block.first_child!
expect(decl.line).toBe(1)
expect(decl.column).toBe(8)
})
test('column for multi-line declarations', () => {
const css = `body {
color: red;
font-size: 16px;
}`
const ast = parse(css)
const rule = ast.first_child!
const block = rule.block!
const [decl1, decl2] = block.children
expect(decl1.line).toBe(2)
expect(decl1.column).toBe(3)
expect(decl2.line).toBe(3)
expect(decl2.column).toBe(3)
})
test('offset ordering for multiple declarations', () => {
const css = 'body { color: red; margin: 0; }'
const ast = parse(css)
const rule = ast.first_child!
const block = rule.block!
const [decl1, decl2] = block.children
expect(decl1.start).toBeLessThan(decl2.start)
})
})
describe('BLOCK', () => {
test('offset and length for block', () => {
const css = 'body { color: red; }'
const ast = parse(css)
const rule = ast.first_child!
const block = rule.block!
expect(block.start).toBeGreaterThan(0)
expect(block.length).toBeGreaterThan(0)
})
})
describe('Column tracking after comments', () => {
test('column after comment', () => {
const css = '/* comment */ body { color: red; }'
const ast = parse(css)
const rule = ast.first_child!
expect(rule.line).toBe(1)
expect(rule.column).toBe(15)
})
})
})
describe('Types', () => {
test('STYLESHEET type constant', () => {
const ast = parse('body { }')
expect(ast.type).toBe(STYLESHEET)
})
test('STYLE_RULE type constant', () => {
const ast = parse('body { }')
const rule = ast.first_child!
expect(rule.type).toBe(STYLE_RULE)
})
test('AT_RULE type constant', () => {
const ast = parse('@media screen { }')
const atRule = ast.first_child!
expect(atRule.type).toBe(AT_RULE)
})
test('DECLARATION type constant', () => {
const ast = parse('body { color: red; }')
const rule = ast.first_child!
const block = rule.block!
const decl = block.first_child!
expect(decl.type).toBe(DECLARATION)
})
test('BLOCK type constant', () => {
const ast = parse('body { color: red; }')
const rule = ast.first_child!
const block = rule.block!
expect(block.type).toBe(BLOCK)
})
})
describe('Type Names', () => {
test('STYLESHEET type_name', () => {
const ast = parse('body { }')
expect(ast.type_name).toBe('StyleSheet')
})
test('STYLE_RULE type_name', () => {
const ast = parse('body { }')
const rule = ast.first_child!
expect(rule.type_name).toBe('Rule')
})
test('AT_RULE type_name', () => {
const ast = parse('@media screen { }')
const atRule = ast.first_child!
expect(atRule.type_name).toBe('Atrule')
})
test('DECLARATION type_name', () => {
const ast = parse('body { color: red; }')
const rule = ast.first_child!
const block = rule.block!
const decl = block.first_child!
expect(decl.type_name).toBe('Declaration')
})
test('BLOCK type_name', () => {
const ast = parse('body { color: red; }')
const rule = ast.first_child!
const block = rule.block!
expect(block.type_name).toBe('Block')
})
})
describe('Node Properties', () => {
describe('STYLESHEET', () => {
test('empty stylesheet has no children', () => {
const root = parse('')
expect(root.type).toBe(STYLESHEET)
expect(root.has_children).toBe(false)
})
test('stylesheet with only whitespace has no children', () => {
const root = parse(' \n\n ')
expect(root.type).toBe(STYLESHEET)
expect(root.has_children).toBe(false)
})
})
describe('STYLE_RULE', () => {
describe('Basic structure', () => {
test('should have selector list as first child', () => {
const ast = parse('body { color: red; margin: 0; }')
const rule = ast.first_child!
expect(rule.type).toBe(STYLE_RULE)
const firstChild = rule.first_child!
expect(firstChild.type).toBe(SELECTOR_LIST)
})
test('should have block as second child', () => {
const ast = parse('body { color: red; margin: 0; }')
const rule = ast.first_child!
const selectorList = rule.first_child!
const block = selectorList.next_sibling!
expect(block).not.toBeNull()
expect(block.type).toBe(BLOCK)
})
test('declarations should be inside the block', () => {
const ast = parse('body { color: red; margin: 0; }')
const rule = ast.first_child!
const selectorList = rule.first_child!
const block = selectorList.next_sibling!
const firstDecl = block.first_child!
expect(firstDecl.type).toBe(DECLARATION)
const secondDecl = firstDecl.next_sibling!
expect(secondDecl).not.toBeNull()
expect(secondDecl.type).toBe(DECLARATION)
expect(secondDecl.next_sibling).toBeNull()
})
test('selector list should be first child, never in middle or end', () => {
const testCases = [
'body { color: red; }',
'div { margin: 0; padding: 10px; }',
'h1 { color: blue; .nested { margin: 0; } }',
'p { font-size: 16px; @media print { display: none; } }',
]
testCases.forEach((source) => {
const ast = parse(source)
const rule = ast.first_child!
expect(rule.first_child!.type).toBe(SELECTOR_LIST)
// Walk through all children and verify no other selector lists
let child = rule.first_child!.next_sibling
while (child) {
expect(child.type).not.toBe(SELECTOR_LIST)
child = child.next_sibling
}
})
})
test('empty rule should still have selector list and block', () => {
const ast = parse('body { }')
const rule = ast.first_child!
expect(rule.type).toBe(STYLE_RULE)
expect(rule.first_child!.type).toBe(SELECTOR_LIST)
const block = rule.first_child!.next_sibling
expect(block).not.toBeNull()
expect(block!.is_empty).toBe(true)
})
})
describe('Selector list structure', () => {
test('selector list children should have next_sibling links', () => {
const ast = parse('h1, h2, h3 { color: red; }')
const rule = ast.first_child!
const selectorList = rule.first_child!
expect(selectorList.type).toBe(SELECTOR_LIST)
const children = []
let child = selectorList.first_child
while (child) {
children.push(child)
child = child.next_sibling
}
expect(children.length).toBe(3)
for (let i = 0; i < children.length - 1; i++) {
const nextSibling = children[i].next_sibling
expect(nextSibling).not.toBeNull()
expect(nextSibling!.start).toBe(children[i + 1].start)
}
expect(children[children.length - 1].next_sibling).toBeNull()
})
test('complex selectors should maintain component chains', () => {
const ast = parse('div.class, span#id { margin: 0; }')
const rule = ast.first_child!
const selectorList = rule.first_child!
expect(selectorList.type).toBe(SELECTOR_LIST)
const selectors = []
let selector = selectorList.first_child
while (selector) {
selectors.push(selector)
selector = selector.next_sibling
}
expect(selectors.length).toBe(2)
// First selector (div.class) should have 2 components
const components1 = []
let comp = selectors[0].first_child
while (comp) {
components1.push(comp)
comp = comp.next_sibling
}
expect(components1.length).toBe(2) // div, .class
// Second selector (span#id) should have 2 components
const components2 = []
comp = selectors[1].first_child
while (comp) {
components2.push(comp)
comp = comp.next_sibling
}
expect(components2.length).toBe(2) // span, #id
})
test('selector list with combinators should chain all components', () => {
const ast = parse('div > p, span + a { color: blue; }')
const rule = ast.first_child!
const selectorList = rule.first_child!
const selectors = []
let selector = selectorList.first_child
while (selector) {
selectors.push(selector)
selector = selector.next_sibling
}
expect(selectors.length).toBe(2)
// First selector (div > p) should have 3 components: div, >, p
const components1 = []
let comp = selectors[0].first_child
while (comp) {
components1.push(comp)
comp = comp.next_sibling
}
expect(components1.length).toBe(3)
// Second selector (span + a) should have 3 components: span, +, a
const components2 = []
comp = selectors[1].first_child
while (comp) {
components2.push(comp)
comp = comp.next_sibling
}
expect(components2.length).toBe(3)
})
})
describe('Block children structure', () => {
test('block children should be linked via next_sibling with declarations only', () => {
const ast = parse('body { color: red; margin: 0; padding: 10px; }')
const rule = ast.first_child!
const selectorList = rule.first_child!
const block = selectorList.next_sibling!
const children = []
let child = block.first_child
while (child) {
children.push(child)
child = child.next_sibling
}
expect(children.length).toBe(3)
for (let i = 0; i < children.length; i++) {
expect(children[i].type).toBe(DECLARATION)
}
for (let i = 0; i < children.length - 1; i++) {
expect(children[i].next_sibling).not.toBeNull()
expect(children[i].next_sibling!.start).toBe(children[i + 1].start)
}
expect(children[children.length - 1].next_sibling).toBeNull()
})
test('block children should be linked via next_sibling with mixed content', () => {
const ast = parse(`
.parent {
color: red;
.nested { margin: 0; }
padding: 10px;
@media print { display: none; }
font-size: 16px;
}
`)
const rule = ast.first_child!
const selectorList = rule.first_child!
const block = selectorList.next_sibling!
const children = []
let child = block.first_child
while (child) {
children.push(child)
child = child.next_sibling
}
expect(children.length).toBe(5)
expect(children[0].type).toBe(DECLARATION) // color: red
expect(children[1].type).toBe(STYLE_RULE) // .nested { margin: 0; }
expect(children[2].type).toBe(DECLARATION) // padding: 10px
expect(children[3].type).toBe(AT_RULE) // @media print { display: none; }
expect(children[4].type).toBe(DECLARATION) // font-size: 16px
for (let i = 0; i < children.length - 1; i++) {
const nextSibling = children[i].next_sibling
expect(nextSibling).not.toBeNull()
expect(nextSibling!.start).toBe(children[i + 1].start)
}
expect(children[children.length - 1].next_sibling).toBeNull()
})
test('block with only nested rules should have correct next_sibling chain', () => {
const ast = parse(`
.parent {
.child1 { color: red; }
.child2 { margin: 0; }
.child3 { padding: 10px; }
}
`)
const rule = ast.first_child!
const selectorList = rule.first_child!
const block = selectorList.next_sibling!
const children = []
let child = block.first_child
while (child) {
children.push(child)
child = child.next_sibling
}
expect(children.length).toBe(3)
for (const child of children) {
expect(child.type).toBe(STYLE_RULE)
}
for (let i = 0; i < children.length - 1; i++) {
expect(children[i].next_sibling).not.toBeNull()
expect(children[i].next_sibling!.start).toBe(children[i + 1].start)
}
expect(children[children.length - 1].next_sibling).toBeNull()
})
test('block with only at-rules should have correct next_sibling chain', () => {
const ast = parse(`
.parent {
@media screen { color: blue; }
@media print { display: none; }
@supports (display: flex) { display: flex; }
}
`)
const rule = ast.first_child!
const selectorList = rule.first_child!
const block = selectorList.next_sibling!
const children = []
let child = block.first_child
while (child) {
children.push(child)
child = child.next_sibling
}
expect(children.length).toBe(3)
for (const child of children) {
expect(child.type).toBe(AT_RULE)
}
for (let i = 0; i < children.length - 1; i++) {
expect(children[i].next_sibling).not.toBeNull()
expect(children[i].next_sibling!.start).toBe(children[i + 1].start)
}
expect(children[children.length - 1].next_sibling).toBeNull()
})
})
describe('Nested rules', () => {
test('nested style rules should have selector list as first child', () => {
const ast = parse('div { .nested { color: red; } }')
const outerRule = ast.first_child!
expect(outerRule.type).toBe(STYLE_RULE)
expect(outerRule.first_child!.type).toBe(SELECTOR_LIST)
const block = outerRule.first_child!.next_sibling!
const nestedRule = block.first_child!
expect(nestedRule.type).toBe(STYLE_RULE)
expect(nestedRule.first_child!.type).toBe(SELECTOR_LIST)
const nestedBlock = nestedRule.first_child!.next_sibling!
expect(nestedBlock.first_child!.type).toBe(DECLARATION)
})
test('& span should be parsed as ONE selector with 3 components', () => {
const ast = parse('.parent { & span { color: red; } }')
const outerRule = ast.first_child!
const block = outerRule.first_child!.next_sibling!
const nestedRule = block.first_child!
expect(nestedRule.type).toBe(STYLE_RULE)
const selectorList = nestedRule.first_child!
expect(selectorList.type).toBe(SELECTOR_LIST)
const selectors = []
let selector = selectorList.first_child
while (selector) {
selectors.push(selector)
selector = selector.next_sibling
}
expect(selectors.length).toBe(1)
if (selectors.length === 1) {
const components = []
let component = selectors[0].first_child
while (component) {
components.push(component)
component = component.next_sibling
}
expect(components.length).toBe(3)
}
})
})
describe('Selector parsing', () => {
test('should parse simple selector', () => {
const source = 'body { }'
const root = parse(source)
const rule = root.first_child!
expect(rule.has_children).toBe(true)
const selector = rule.first_child!
expect(selector.text).toBe('body')
expect(selector.line).toBe(1)
expect(selector.start).toBe(0)
expect(selector.length).toBe(4)
expect(selector.end).toBe(4)
})
test('should parse complex selector', () => {
const source = 'div.class > p#id { }'
const root = parse(source)
const rule = root.first_child!
const selectorlist = rule.first_child!
expect(selectorlist.start).toBe(0)
expect(selectorlist.length).toBe(16)
expect(selectorlist.end).toBe(16)
expect(selectorlist.text).toBe('div.class > p#id')
const selector = selectorlist.first_child!
expect(selector.children[0].text).toBe('div')
expect(selector.children[1].text).toBe('.class')
expect(selector.children[2].text).toBe('>')
expect(selector.children[3].text).toBe('p')
expect(selector.children[4].text).toBe('#id')
})
test('should parse pseudo class selector', () => {
const source = 'p:has(a) {}'
const root = parse(source)
const rule = root.first_child!
const selectorlist = rule.first_child!
const selector = selectorlist.first_child!
expect(selector.type).toBe(SELECTOR)
expect(selector.children[0].type).toBe(TYPE_SELECTOR)
expect(selector.children[1].type).toBe(PSEUDO_CLASS_SELECTOR)
expect(selector.children[2]).toBeUndefined()
const pseudo = selector.children[1]
expect(pseudo.text).toBe(':has(a)')
expect(pseudo.children).toHaveLength(1)
})
test('attribute selector should have name, value and operator', () => {
const source = '[root|="test"] {}'
const root = parse(source)
const rule = root.first_child!
const selectorlist = rule.first_child!
const selector = selectorlist.first_child!
expect(selector.type).toBe(SELECTOR)
const s = selector.children[0]
expect(s.type).toBe(ATTRIBUTE_SELECTOR)
expect(s.attr_operator).toEqual(ATTR_OPERATOR_PIPE_EQUAL)
expect(s.name).toBe('root')
expect(s.value).toBe('"test"')
})
})
describe('Multiple rules', () => {
test('should parse multiple style rules', () => {
const root = parse('body { } div { }')
const [rule1, rule2] = root.children
expect(rule1.type).toBe(STYLE_RULE)
expect(rule2.type).toBe(STYLE_RULE)
expect(rule2.next_sibling).toBe(null)
})
})
})
describe('AT_RULE', () => {
describe('Statement at-rules (no block)', () => {
test('@import', () => {
const source = '@import url("style.css");'
const root = parse(source, { parse_atrule_preludes: false })
const atRule = root.first_child!
expect(atRule.type).toBe(AT_RULE)
expect(atRule.name).toBe('import')
expect(atRule.has_children).toBe(true)
})
test('@namespace', () => {
const source = '@namespace url(http://www.w3.org/1999/xhtml);'
const root = parse(source)
const atRule = root.first_child!
expect(atRule.type).toBe(AT_RULE)
expect(atRule.name).toBe('namespace')
expect(atRule.length).toBe(45)
})
})
describe('Case-insensitive at-rule names', () => {
test('should parse @MEDIA (uppercase)', () => {
const source = '@MEDIA (min-width: 768px) { body { color: red; } }'
const root = parse(source, { parse_atrule_preludes: false })
const media = root.first_child!
expect(media.type).toBe(AT_RULE)
expect(media.name).toBe('MEDIA')
expect(media.has_children).toBe(true)
const block = media.block!
const nestedRule = block.first_child!
expect(nestedRule.type).toBe(STYLE_RULE)
})
test('should parse @Font-Face (mixed case)', () => {
const source = '@Font-Face { font-family: "MyFont"; src: url("font.woff"); }'
const root = parse(source)
const fontFace = root.first_child!
expect(fontFace.type).toBe(AT_RULE)
expect(fontFace.name).toBe('Font-Face')
expect(fontFace.length).toBe(60)
expect(fontFace.has_children).toBe(true)
const block = fontFace.block!
const decl = block.first_child!
expect(decl.type).toBe(DECLARATION)
})
test('should parse @SUPPORTS (uppercase)', () => {
const source = '@SUPPORTS (display: grid) { .grid { display: grid; } }'
const root = parse(source, { parse_atrule_preludes: false })
const supports = root.first_child!
expect(supports.type).toBe(AT_RULE)
expect(supports.name).toBe('SUPPORTS')
expect(supports.has_children).toBe(true)
})
})
describe('Block at-rules with nested rules', () => {
test('@media with nested rule', () => {
const source = '@media (min-width: 768px) { body { color: red; } }'
const root = parse(source, { parse_atrule_preludes: false })
const media = root.first_child!
expect(media.type).toBe(AT_RULE)
expect(media.name).toBe('media')
expect(media.has_children).toBe(true)
expect(media.length).toBe(50)
const block = media.block!
const nestedRule = block.first_child!
expect(nestedRule.type).toBe(STYLE_RULE)
expect(nestedRule.length).toBe(20)
})
test('@layer with name', () => {
const source = '@layer utilities { .text-center { text-align: center; } }'
const root = parse(source)
const layer = root.first_child!
expect(layer.type).toBe(AT_RULE)
expect(layer.name).toBe('layer')
expect(layer.has_children).toBe(true)
})
test('anonymous @layer', () => {
const source = '@layer { body { margin: 0; } }'
const root = parse(source)
const layer = root.first_child!
expect(layer.type).toBe(AT_RULE)
expect(layer.name).toBe('layer')
expect(layer.has_children).toBe(true)
})
test('@supports', () => {
const source = '@supports (display: grid) { .grid { display: grid; } }'
const root = parse(source)
const supports = root.first_child!
expect(supports.type).toBe(AT_RULE)
expect(supports.name).toBe('supports')
expect(supports.has_children).toBe(true)
})
test('@container', () => {
const source = '@container (min-width: 400px) { .card { padding: 2rem; } }'
const root = parse(source)
const container = root.first_child!
expect(container.type).toBe(AT_RULE)
expect(container.name).toBe('container')
expect(container.has_children).toBe(true)
})
})
describe('Descriptor at-rules (with declarations)', () => {
test('@font-face', () => {
const source = '@font-face { font-family: "Open Sans"; src: url(font.woff2); }'
const root = parse(source)
const fontFace = root.first_child!
expect(fontFace.type).toBe(AT_RULE)
expect(fontFace.name).toBe('font-face')
expect(fontFace.has_children).toBe(true)
const block = fontFace.block!
const [decl1, decl2] = block.children
expect(decl1.type).toBe(DECLARATION)
expect(decl2.type).toBe(DECLARATION)
})
test('@page', () => {
const source = '@page { margin: 1in; }'
const root = parse(source)
const page = root.first_child!
expect(page.type).toBe(AT_RULE)
expect(page.name).toBe('page')
const block = page.block!
const decl = block.first_child!
expect(decl.type).toBe(DECLARATION)
})
test('@counter-style', () => {
const source = '@counter-style thumbs { system: cyclic; symbols: "👍"; }'
const root = parse(source)
const counterStyle = root.first_child!
expect(counterStyle.type).toBe(AT_RULE)
expect(counterStyle.name).toBe('counter-style')
const block = counterStyle.block!
const decl = block.first_child!
expect(decl.type).toBe(DECLARATION)
})
})
describe('Nested at-rules', () => {
test('@media inside @supports', () => {
const source =
'@supports (display: grid) { @media (min-width: 768px) { body { color: red; } } }'
const root = parse(source, { parse_atrule_preludes: false })
const supports = root.first_child!
expect(supports.name).toBe('supports')
expect(supports.length).toBe(80)
const supports_block = supports.block!
const media = supports_block.first_child!
expect(media.type).toBe(AT_RULE)
expect(media.name).toBe('media')
expect(media.text).toBe('@media (min-width: 768px) { body { color: red; } }')
expect(media.length).toBe(50)
const media_block = media.block!
const rule = media_block.first_child!
expect(rule.type).toBe(STYLE_RULE)
expect(rule.length).toBe(20)
})
})
describe('Multiple at-rules', () => {
test('multiple at-rules at top level', () => {
const source =
'@import url("a.css"); @layer base { body { margin: 0; } } @media print { body { color: black; } }'
const root = parse(source)
const [import1, layer, media] = root.children
expect(import1.name).toBe('import')
expect(import1.length).toBe(21)
expect(layer.name).toBe('layer')
expect(layer.length).toBe(35)
expect(media.name).toBe('media')
expect(media.length).toBe(39)
})
})
describe('Special at-rules', () => {
test('@charset', () => {
let source = '@charset "UTF-8"; body { color: red; }'
let root = parse(source)
let [charset, _body] = root.children
expect(charset.type).toBe(AT_RULE)
expect(charset.name).toBe('charset')
})
test('@import with media query', () => {
let source = '@import url("print.css") print;'
let root = parse(source)
let import_rule = root.first_child!
expect(import_rule.type).toBe(AT_RULE)
expect(import_rule.name).toBe('import')
})
test('@font-face with multiple descriptors', () => {
let source = `
@font-face {
font-family: "Custom";
src: url("font.woff2") format("woff2"),
url("font.woff") format("woff");
font-weight: 400;
font-style: normal;
font-display: swap;
}
`
let root = parse(source)
let font_face = root.first_child!
expect(font_face.name).toBe('font-face')
let block = font_face.block!
expect(block.children.length).toBeGreaterThan(3)
})
test('@counter-style', () => {
let source = '@counter-style custom { system: cyclic; symbols: "⚫" "⚪"; suffix: " "; }'
let root = parse(source)
let counter = root.first_child!
expect(counter.name).toBe('counter-style')
let block = counter.block!
expect(block.children.length).toBeGreaterThan(1)
})
test('@property', () => {
let source =
'@property --my-color { syntax: "<color>"; inherits: false; initial-value: #c0ffee; }'
let root = parse(source)
let property = root.first_child!
expect(property.name).toBe('property')
})
})