~alcinnz/haskell-stylist

ref: 4b5d0346d4c296003a3d5b5680216357c5939aee haskell-stylist/test/Test.hs -rw-r--r-- 38.3 KiB
4b5d0346 — Adrian Cochrane Test & fix rendering of Ethiopian 1. 1 year, 1 day ago
                                                                                
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
{-# LANGUAGE OverloadedStrings #-}
module Main where

import Test.Hspec
import Data.HashMap.Strict
import qualified Data.HashMap.Lazy as L
import Data.Maybe (fromJust)
import Network.URI
import Data.Scientific (toRealFloat)

import Data.CSS.Syntax.Tokens
import Data.CSS.Syntax.StyleSheet (parse, StyleSheet(..), TrivialStyleSheet(..), scanAtRule, scanValue)
import Data.CSS.Syntax.Selector

import Data.CSS.Syntax.AtLayer

import Data.CSS.Style.Common
import Data.CSS.Style.Selector.Index
import Data.CSS.Style.Selector.Interpret
import Data.CSS.Style
import Data.CSS.StyleTree

import Data.CSS.Preprocessor.Conditions
import Data.CSS.Preprocessor.Conditions.Expr (Datum(..), Op(..), parse', eval)
import qualified Data.CSS.Preprocessor.Text as Txt

import qualified Data.CSS.Preprocessor.Text.CounterStyle as Ctr

main :: IO ()
main = hspec spec

spec :: Spec
spec = do
    describe "Canary" $ do
        it "Test framework works" $ do
            True `shouldBe` True
    describe "Parsing" $ do
        it "Can scan @rules" $ do
            scanAtRule [Ident "utf-8", Semicolon, Ident "a"] `shouldBe` ([Ident "utf-8", Semicolon], [Ident "a"])
            scanAtRule [Ident "before", LeftCurlyBracket, Ident "inside", RightCurlyBracket, Ident "after"] `shouldBe` (
                [Ident "before", LeftCurlyBracket, Ident "inside", RightCurlyBracket],
                [Ident "after"])
        it "Ignores @rules" $ do
            parse emptyStyle "@encoding 'utf-8';" `shouldBe` emptyStyle
            parse emptyStyle "  @encoding 'utf-8';" `shouldBe` emptyStyle
            parse emptyStyle "@encoding 'utf-8';  " `shouldBe` emptyStyle
            parse emptyStyle "@media print { a:link {color: green;} }" `shouldBe` emptyStyle
            parse emptyStyle "  @media print { a:link {color: green;} }" `shouldBe` emptyStyle
            parse emptyStyle "@media print { a:link {color: green;} }  " `shouldBe` emptyStyle

            parse emptyStyle "@encoding 'utf-8'; a {color:green}" `shouldBe` linkStyle
            parse emptyStyle "a {color:green}@encoding 'utf-8';" `shouldBe` linkStyle
            parse emptyStyle "@media print{a{color:black;}}a {color:green}" `shouldBe` linkStyle
            parse emptyStyle "a {color:green} @media print {a{color:black;}}" `shouldBe` linkStyle
        it "Parses style rules" $ do
            -- Syntax examples from "Head First HTML & CSS with XHTML"
            parse emptyStyle "bedroom { drapes: blue; carpet: wool shag; }" `shouldBe` TrivialStyleSheet [
                StyleRule (Element [Tag "bedroom"]) [
                    ("drapes", [Ident "blue"]),
                    ("carpet", [Ident "wool", Ident "shag"])
                ] ""]
            parse emptyStyle "  bathroom{tile :1in white;drapes :pink}" `shouldBe` TrivialStyleSheet [
                StyleRule (Element [Tag "bathroom"]) [
                    ("tile", [Dimension "1" (NVInteger 1) "in", Ident "white"]),
                    ("drapes", [Ident "pink"])
                ] ""]
        it "Parses selectors" $ do
            parse emptyStyle ".class {}" `shouldBe` TrivialStyleSheet [
                    StyleRule (Element [Class "class"]) [] ""
                ]
            parse emptyStyle "*.class {}" `shouldBe` TrivialStyleSheet [
                    StyleRule (Element [Class "class"]) [] ""
                ]
            parse emptyStyle "#id {}" `shouldBe` TrivialStyleSheet [
                    StyleRule (Element [Id "id"]) [] ""
                ]
            parse emptyStyle "[attr] {}" `shouldBe` TrivialStyleSheet [
                    StyleRule (Element [Property Nothing "attr" Exists]) [] ""
                ]
            parse emptyStyle "a , b {}" `shouldBe` TrivialStyleSheet [
                    StyleRule (Element [Tag "b"]) [] "",
                    StyleRule (Element [Tag "a"]) [] ""
                ]
            parse emptyStyle "a b {}" `shouldBe` TrivialStyleSheet [
                    StyleRule (Descendant (Element [Tag "a"]) [Tag "b"]) [] ""
                ]
            parse emptyStyle "a > b {}" `shouldBe` TrivialStyleSheet [
                    StyleRule (Child (Element [Tag "a"]) [Tag "b"]) [] ""
                ]
            parse emptyStyle "a ~ b {}" `shouldBe` TrivialStyleSheet [
                    StyleRule (Sibling (Element [Tag "a"]) [Tag "b"]) [] ""
                ]
            parse emptyStyle "a + b {}" `shouldBe` TrivialStyleSheet [
                    StyleRule (Adjacent (Element [Tag "a"]) [Tag "b"]) [] ""
                ]
            parse emptyStyle "a::before {}"
                `shouldBe` TrivialStyleSheet [
                    StyleRule (Element [Tag "a"]) [] "before"
                ]
            parse emptyStyle "a:before {}"
                `shouldBe` TrivialStyleSheet [
                    StyleRule (Element [Tag "a", Psuedoclass "before" []]) [] ""
                ]
    describe "Style Index" $ do
        it "Retrieves appropriate styles" $ do
            let index = addStyleRule styleIndex [0] $ styleRule' sampleRule
            let element = ElementNode {
                name = "a",
                namespace = "",
                parent = Nothing,
                previous = Nothing,
                attributes = [
                    Attribute "class" "" "external",
                    Attribute "href" "" "https://adrian.geek.nz/",
                    Attribute "id" "" "mysite"
                ]
            }
            let element2 = ElementNode {
                name = "b",
                namespace = "",
                parent = Just element,
                previous = Just element, -- Invalid tree, oh well.
                attributes = []
            }
            rulesForElement index element `shouldBe` [sampleRule]
            rulesForElement index element2 `shouldBe` []

            let rule1 = StyleRule (Element [Class "external"]) [("color", [Ident "green"])] ""
            let index1 = addStyleRule styleIndex [0] $ styleRule' rule1
            rulesForElement index1 element `shouldBe` [rule1]
            rulesForElement index1 element2 `shouldBe` []

            let rule2 = StyleRule (Element [Id "mysite"]) [("color", [Ident "green"])] ""
            let index2 = addStyleRule styleIndex [0] $ styleRule' rule2
            rulesForElement index2 element `shouldBe` [rule2]
            rulesForElement index2 element2 `shouldBe` []

            let rule3 = StyleRule (Element [Property Nothing "href" $ Prefix "https://"]) [("color", [Ident "green"])] ""
            let index3 = addStyleRule styleIndex [0] $ styleRule' rule3
            rulesForElement index3 element `shouldBe` [rule3]
            rulesForElement index3 element2 `shouldBe` []
    describe "Selector Compiler" $ do
        it "Correctly evaluates selectors" $ do
            let parentEl = ElementNode {
                name = "a",
                namespace = "",
                parent = Nothing,
                previous = Nothing,
                attributes = [
                    Attribute "class" "" "external secure link",
                    Attribute "href" "" "https://adrian.geek.nz/index.html",
                    Attribute "id" "" "mysite",
                    Attribute "lang" "" "en-US"
                ]
            }
            let sibling = ElementNode {
                name = "img",
                namespace = "",
                parent = Just parentEl,
                previous = Nothing,
                attributes = []
            }
            let child = ElementNode {
                name = "b",
                namespace = "",
                parent = Just parentEl,
                previous = Just sibling,
                attributes = []
            }

            let selector1 = compile (Element [Tag "a"])
            selector1 parentEl `shouldBe` True
            selector1 sibling `shouldBe` False
            selector1 child `shouldBe` False

            let selector2 = compile (Element [Class "external"])
            selector2 parentEl `shouldBe` True
            selector2 sibling `shouldBe` False
            selector2 child `shouldBe` False

            let selector3 = compile (Element [Id "mysite"])
            selector3 parentEl `shouldBe` True
            selector3 sibling `shouldBe` False
            selector3 child `shouldBe` False

            let selector4 = compile (Element [Property Nothing "lang" Exists])
            selector4 parentEl `shouldBe` True
            selector4 sibling `shouldBe` False
            selector4 child `shouldBe` False

            let selector5 = compile (Element [Property Nothing "class" $ Include "secure"])
            selector5 parentEl `shouldBe` True
            selector5 sibling `shouldBe` False
            selector5 child `shouldBe` False

            let selector6 = compile (Element [Property Nothing "href" $ Prefix "https://"])
            selector6 parentEl `shouldBe` True
            selector6 sibling `shouldBe` False
            selector6 child `shouldBe` False

            let selector7 = compile (Element [Property Nothing "href" $ Suffix ".html"])
            selector7 parentEl `shouldBe` True
            selector7 sibling `shouldBe` False
            selector7 child `shouldBe` False

            let selector8 = compile (Element [Property Nothing "href" $ Substring ".geek.nz"])
            selector8 parentEl `shouldBe` True
            selector8 sibling `shouldBe` False
            selector8 child `shouldBe` False

            let selector9 = compile (Element [Property Nothing "lang" $ Dash "en"])
            selector9 parentEl `shouldBe` True
            selector9 sibling `shouldBe` False
            selector9 child `shouldBe` False

            let selectorA = compile (Element [Property Nothing "lang" $ Dash "en-US"])
            selectorA parentEl `shouldBe` True
            selectorA sibling `shouldBe` False
            selectorA child `shouldBe` False

            let selectorB = compile (Element [Property Nothing "lang" $ Dash "en-UK"])
            selectorB parentEl `shouldBe` False
            selectorB sibling `shouldBe` False
            selectorB child `shouldBe` False

            -- TODO These could be tested better.
            let selectorC = compile $ Child (Element [Tag "a"]) [Tag "b"]
            selectorC parentEl `shouldBe` False
            selectorC sibling `shouldBe` False
            selectorC child `shouldBe` True

            let selectorD = compile $ Descendant (Element [Tag "a"]) [Tag "b"]
            selectorD parentEl `shouldBe` False
            selectorD sibling `shouldBe` False
            selectorD child `shouldBe` True

            let selectorE = compile $ Sibling (Element [Tag "img"]) [Tag "b"]
            selectorE parentEl `shouldBe` False
            selectorE sibling `shouldBe` False
            selectorE child `shouldBe` True

            let selectorF = compile $ Adjacent (Element [Tag "img"]) [Tag "b"]
            selectorF parentEl `shouldBe` False
            selectorF sibling `shouldBe` False
            selectorF child `shouldBe` True

    describe "Style resolution" $ do
        it "respects selector specificity" $ do
            let el = ElementNode {
                name = "a",
                namespace = "",
                parent = Nothing,
                previous = Nothing,
                attributes = [Attribute "class" "" "link"]
            }
            let rules = parse queryable "a.link {color: green} a {color: red}"
            let VarParser _ (TrivialPropertyParser style) = cascade rules el [] temp::(VarParser TrivialPropertyParser)
            style ! "color" `shouldBe` [Ident "green"]
        it "respects syntax order" $ do
            let el = ElementNode {
                name = "a",
                namespace = "",
                parent = Nothing,
                previous = Nothing,
                attributes = [Attribute "class" "" "link"]
            }
            let rules = parse queryable "a {color: red; color: green}"
            let VarParser _ (TrivialPropertyParser style) = cascade rules el [] temp::(VarParser TrivialPropertyParser)
            style ! "color" `shouldBe` [Ident "green"]

            let rules2 = parse queryable "a {color: red} a {color: green}"
            let VarParser _ (TrivialPropertyParser style2) = cascade rules2 el [] temp::(VarParser TrivialPropertyParser)
            style2 ! "color" `shouldBe` [Ident "green"]
        it "respects stylesheet precedence" $ do
            let el = ElementNode {
                name = "a",
                namespace = "",
                parent = Nothing,
                previous = Nothing,
                attributes = [Attribute "class" "" "link"]
            }
            let rules = parse (queryable {priority = [1]}) "a {color: green}"
            let rules2 = parse (rules {priority = [2]}) "a {color: red}" :: QueryableStyleSheet (VarParser TrivialPropertyParser)
            let VarParser _ (TrivialPropertyParser style) = cascade rules2 el [] temp::(VarParser TrivialPropertyParser)
            style ! "color" `shouldBe` [Ident "green"]

            let el' = ElementNode {
                name = "a",
                namespace = "",
                parent = Nothing,
                previous = Nothing,
                attributes = [Attribute "class" "" "link"]
            }
            let rules' = parse (queryable {priority = [1]}) "a {color: red}"
            let rules2' = parse (rules' {priority = [2]}) "a {color: green !important}" :: QueryableStyleSheet (VarParser TrivialPropertyParser)
            let VarParser _ (TrivialPropertyParser style') = cascade rules2' el' [] temp::(VarParser TrivialPropertyParser)
            style' ! "color" `shouldBe` [Ident "green"]
        it "respects overrides" $ do
            let el = ElementNode {
                name = "a",
                namespace = "",
                parent = Nothing,
                previous = Nothing,
                attributes = [Attribute "class" "" "link"]
            }
            let rules = parse queryable "a {color: red;}"
            let VarParser _ (TrivialPropertyParser style) = cascade rules el [("color", [Ident "green"])] temp::(VarParser TrivialPropertyParser)
            style ! "color" `shouldBe` [Ident "green"]
    describe "Parser freezes" $ do
        it "does not regress" $ do
            parse emptyStyle "output: {content: 'Output'; pitch: high}"
                `shouldBe` TrivialStyleSheet [
                    StyleRule (Element [Tag "output"]) [] ""
                ] -- Turned out to just be incorrect parsing
            parse emptyStyle "input, output {content: attr(value)}"
                `shouldBe` TrivialStyleSheet [
                    StyleRule (Element [Tag "output"]) [
                        ("content", [Function "attr", Ident "value", RightParen])
                    ] "",
                    StyleRule (Element [Tag "input"]) [
                        ("content", [Function "attr", Ident "value", RightParen])
                    ] ""
                ]
        it "paren balancing" $ do
            scanValue [RightParen] `shouldBe` ([], [RightParen])
            scanValue [LeftParen] `shouldBe` ([LeftParen], [])
            scanValue [Function "fn", LeftParen] `shouldBe` ([Function "fn", LeftParen], [])
            scanValue [Function "fn", Ident "arg", LeftParen] `shouldBe`
                ([Function "fn", Ident "arg", LeftParen], [])

    describe "CSS Variables" $ do
        it "are captured" $ do
            let parser = temp :: VarParser TrivialPropertyParser
            vars parser `shouldBe` []
            let parser1 = setVars [("--var", [Ident "value"])] parser
            vars parser1 `shouldBe` [("--var", [Ident "value"])]
            let parser2 = fromJust $ longhand parser parser1 "property" [Function "var", Ident "--var", RightParen]
            vars parser2 `shouldBe` [("--var", [Ident "value"])]
            let VarParser _ (TrivialPropertyParser style) = parser2
            style ! "property" `shouldBe` [Ident "value"]

            let el = ElementNode {
                name = "a",
                namespace = "",
                parent = Nothing,
                previous = Nothing,
                attributes = []
            }
            let rules = parse queryable "a {--var: value}"
            let VarParser v _ = cascade rules el [] temp
            v `shouldBe` [("--var", [Ident "value"])]
        it "applies within element" $ do
            let el = ElementNode {
                name = "a",
                namespace = "",
                parent = Nothing,
                previous = Nothing,
                attributes = []
            }
            let rules = parse queryable "a {--link: #f00; color: var(--link)}"
            let VarParser vars (TrivialPropertyParser style) = cascade rules el [] temp
            style ! "color" `shouldBe` [Hash HId "f00"]
            style ! "--link" `shouldBe` [Hash HId "f00"]
            vars `shouldBe` [("--link", [Hash HId "f00"])]
        it "inherits" $ do
            let parent = ElementNode {
                name = "a",
                namespace = "",
                parent = Nothing,
                previous = Nothing,
                attributes = []
            }
            let el = ElementNode {
                name = "b",
                namespace = "",
                parent = Just parent,
                previous = Nothing,
                attributes = []
            }
            let rules = parse queryable "a {--link: #f00} b {color: var(--link)}"
            let VarParser vars (TrivialPropertyParser style) = cascade rules el [] $ cascade rules parent [] temp
            vars `shouldBe` [("--link", [Hash HId "f00"])]
            style ! "color" `shouldBe` [Hash HId "f00"]
    describe "Conditional @rules" $ do
        it "handles normal rules" $ do
            let TrivialStyleSheet styles = resolve' $ parse conditional "a {color: green}"
            styles `shouldBe` [StyleRule (Element [Tag "a"]) [("color", [Ident "green"])] ""]

            let TrivialStyleSheet styles = resolve' $ parse conditional "@rule; a {color: green}"
            styles `shouldBe` [StyleRule (Element [Tag "a"]) [("color", [Ident "green"])] ""]

            let TrivialStyleSheet styles = resolve' $ parse conditional "a {color: green} @rule;"
            styles `shouldBe` [StyleRule (Element [Tag "a"]) [("color", [Ident "green"])] ""]

            let TrivialStyleSheet styles = resolve' $ parse conditional "a {color: green} @font {}"
            styles `shouldBe` [StyleRule (Element [Tag "a"]) [("color", [Ident "green"])] ""]

            let TrivialStyleSheet styles = resolve' $ parse conditional "@font {} a {color: green}"
            styles `shouldBe` [StyleRule (Element [Tag "a"]) [("color", [Ident "green"])] ""]
        it "handles @document" $ do
            let TrivialStyleSheet styles = resolve' $ parse conditional "@document url(about:blank) { a {color: green} }"
            styles `shouldBe` [StyleRule (Element []) [] "", StyleRule (Element [Tag "a"]) [("color", [Ident "green"])] ""]
            let TrivialStyleSheet styles = resolve' $ parse conditional "@document url(about:credits) { a {color: red} }"
            styles `shouldBe` []
            let TrivialStyleSheet styles = resolve' $ parse conditional "@document url-prefix('about:') { a {color: green} }"
            styles `shouldBe` [StyleRule (Element []) [] "", StyleRule (Element [Tag "a"]) [("color", [Ident "green"])] ""]
            let TrivialStyleSheet styles = resolve' $ parse conditional "@document url-prefix('https:') { a {color: red} }"
            styles `shouldBe` []
            let TrivialStyleSheet styles = resolve' $ parse conditional "@document media-document('test') { a {color: green} }"
            styles `shouldBe` [StyleRule (Element []) [] "", StyleRule (Element [Tag "a"]) [("color", [Ident "green"])] ""]
            let TrivialStyleSheet styles = resolve' $ parse conditional "@document media-document('other') { a {color: red} }"
            styles `shouldBe` []
        it "handles @media" $ do
            let TrivialStyleSheet styles = resolve' $ parse conditional "@media test { a {color: green} }"
            styles `shouldBe` [StyleRule (Element []) [] "", StyleRule (Element [Tag "a"]) [("color", [Ident "green"])] ""]
            let TrivialStyleSheet styles = resolve' $ parse conditional "@media screen { a {color: red} }"
            styles `shouldBe` []

            let TrivialStyleSheet styles = resolve' $ parse conditional "@media test or screen { a {color: green} }"
            styles `shouldBe` [StyleRule (Element []) [] "", StyleRule (Element [Tag "a"]) [("color", [Ident "green"])] ""]
            let TrivialStyleSheet styles = resolve' $ parse conditional "@media test or test {a {color: green} }"
            styles `shouldBe` [StyleRule (Element []) [] "", StyleRule (Element [Tag "a"]) [("color", [Ident "green"])] ""]
            let TrivialStyleSheet styles = resolve' $ parse conditional "@media screen or screen {a {color: red} }"
            styles `shouldBe` []
            let TrivialStyleSheet styles = resolve' $ parse conditional "@media screen or test {a {color: green} }"
            styles `shouldBe` [StyleRule (Element []) [] "", StyleRule (Element [Tag "a"]) [("color", [Ident "green"])] ""]

            let TrivialStyleSheet styles = resolve' $ parse conditional "@media test and screen { a {color: red} }"
            styles `shouldBe` []
            let TrivialStyleSheet styles = resolve' $ parse conditional "@media test and test { a {color: green} }"
            styles `shouldBe` [StyleRule (Element []) [] "", StyleRule (Element [Tag "a"]) [("color", [Ident "green"])] ""]
            let TrivialStyleSheet styles = resolve' $ parse conditional "@media screen and screen { a {color: red} }"
            styles `shouldBe` []
            let TrivialStyleSheet styles = resolve' $ parse conditional "@media screen and test {a {color: red} }"
            styles `shouldBe` []

            let TrivialStyleSheet styles = resolve' $ parse conditional "@media 2 < 3 { a {color: green} }"
            styles `shouldBe` [StyleRule (Element []) [] "", StyleRule (Element [Tag "a"]) [("color", [Ident "green"])] ""]
            let TrivialStyleSheet styles = resolve' $ parse conditional "@media 2 < 2 { a {color: red} }"
            styles `shouldBe` []
            let TrivialStyleSheet styles = resolve' $ parse conditional "@media 2 < 1 { a {color: red} }"
            styles `shouldBe` []
        it "handles @import" $ do
            let styles = parse conditional "@import url(about:style.css);"
            extractImports' styles `shouldBe` [URI "about:" Nothing "style.css" "" ""]
            let styles = parse conditional "@import 'about:style.css';"
            extractImports' styles `shouldBe` [URI "about:" Nothing "style.css" "" ""]

            let styles = parse conditional "@import url(about:style.css) test;"
            extractImports' styles `shouldBe` [URI "about:" Nothing "style.css" "" ""]
            let styles = parse conditional "@import url(about:style.css) screen;"
            extractImports' styles `shouldBe` []
        -- TODO @supports is harder to test

    describe "CSS Counters" $ do
        it "Propagates other properties" $ do
            let textStyle = temp :: Txt.TextStyle TrivialPropertyParser
            let textStyle1 = fromJust $ longhand temp textStyle "foo" [Ident "bar"]
            style (Txt.resolve $ StyleTree textStyle1 []) `shouldBe`
                TrivialPropertyParser (fromList [("foo", [Ident "bar"])])

            let textStyle2 = fromJust $ longhand temp textStyle1 "counter-reset" [Ident "heading"]
            style (Txt.resolve $ StyleTree textStyle2 []) `shouldBe`
                TrivialPropertyParser (fromList [("foo", [Ident "bar"])])

            let textStyle3 = fromJust $ longhand temp textStyle2 "counter-set" [Ident "heading"]
            style (Txt.resolve $ StyleTree textStyle3 []) `shouldBe`
                TrivialPropertyParser (fromList [("foo", [Ident "bar"])])

            let textStyle4 = fromJust $ longhand temp textStyle3 "counter-increment" [Ident "heading"]
            style (Txt.resolve $ StyleTree textStyle4 []) `shouldBe`
                TrivialPropertyParser (fromList [("foo", [Ident "bar"])])

            let textStyle5 = fromJust $ longhand temp textStyle4 "white-space" [Ident "normal"]
            style (Txt.resolve $ StyleTree textStyle5 []) `shouldBe`
                TrivialPropertyParser (fromList [("foo", [Ident "bar"]), ("white-space", [Ident "normal"])])

            let textStyle6 = fromJust $ longhand temp textStyle4 "white-space" [Ident "pre"]
            style (Txt.resolve $ StyleTree textStyle6 []) `shouldBe`
                TrivialPropertyParser (fromList [("foo", [Ident "bar"]), ("white-space", [Ident "nowrap"])])

            let textStyle7 = fromJust $ longhand temp textStyle4 "white-space" [Ident "nowrap"]
            style (Txt.resolve $ StyleTree textStyle7 []) `shouldBe`
                TrivialPropertyParser (fromList [("foo", [Ident "bar"]), ("white-space", [Ident "nowrap"])])

            let textStyle8 = fromJust $ longhand temp textStyle4 "white-space" [Ident "pre-wrap"]
            style (Txt.resolve $ StyleTree textStyle8 []) `shouldBe`
                TrivialPropertyParser (fromList [("foo", [Ident "bar"]), ("white-space", [Ident "normal"])])

            let textStyle9 = fromJust $ longhand temp textStyle4 "white-space" [Ident "pre-line"]
            style (Txt.resolve $ StyleTree textStyle9 []) `shouldBe`
                TrivialPropertyParser (fromList [("foo", [Ident "bar"]), ("white-space", [Ident "normal"])])

        it "Inserts counters" $ do
            let textStyle = temp :: Txt.TextStyle TrivialPropertyParser
            shorthand textStyle "content" [Function "counter", Ident "-rhaps-ol", RightParen] `shouldBe`
                [("content", [Function "counter", Ident "-rhaps-ol", RightParen])]
            shorthand textStyle "content" [Function "counters", Ident "-rhaps-ol", Comma, String ".", RightParen] `shouldBe`
                [("content", [Function "counters", Ident "-rhaps-ol", Comma, String ".", RightParen])]

            let textStyle1 = fromJust $ longhand temp textStyle "content" [Function "counter", Ident "-rhaps-ol", RightParen]
            style (Txt.resolve $ StyleTree textStyle1 []) `shouldBe` TrivialPropertyParser (fromList [("content", [])])

            let textStyle2 = fromJust $ longhand temp textStyle1 "counter-reset" [Ident "-rhaps-ol"]
            style (Txt.resolve $ StyleTree textStyle2 []) `shouldBe` TrivialPropertyParser (fromList [("content", [String "0"])])

            let textStyle3 = fromJust $ longhand temp textStyle1 "counter-set" [Ident "-rhaps-ol"]
            style (Txt.resolve $ StyleTree textStyle3 []) `shouldBe` TrivialPropertyParser (fromList [("content", [String "0"])])

            let textStyle4 = fromJust $ longhand temp textStyle1 "counter-increment" [Ident "-rhaps-ol"]
            style (Txt.resolve $ StyleTree textStyle4 []) `shouldBe` TrivialPropertyParser (fromList [("content", [String "1"])])
    describe "@layer" $ do
        it "Deduplicates names" $ do
            let init = Tree L.empty
            let tree2 = registerLayer ["LeagueOfGentlemenAdventurers", "The Stranger"] init
            let tree3 = registerLayer ["JusticeUnion", "TomTomorrow"] tree2
            let tree4 = registerLayer ["HomeTeam", "DocRocket"] tree3
            let tree5 = registerLayer ["JusticeUnion", "TheOgre"] tree4

            layerPath ["JusticeUnion", "TheOgre"] tree5 `shouldBe` [2, 2]
            layerPath ["HomeTeam"] tree5 `shouldBe` [3]
            layerPath ["LeagueOfGentlemenAdventurers"] tree5 `shouldBe` [1]
            uniqueName ["HomeTeam"] tree5 `shouldBe` ["HomeTeam", "1"]

    describe "@counter-style" $ do
        -- NOTE: Thes are mostly based on examples given in the spec
        -- https://w3c.github.io/csswg-drafts/css-counter-styles-3/#counter-style-system
        it "Handles system: cyclic" $ do
            let counter = Ctr.defaultCounter {
                Ctr.system = Ctr.Cyclic, Ctr.symbols = ["!", "@", "#"]
              }
            Ctr.counterRenderMarker counter 2 `shouldBe` "@. "
            Ctr.counterRenderMarker counter 4 `shouldBe` "!. "
        it "Handles system: fixed" $ do
            let counter = Ctr.defaultCounter {
                Ctr.system = Ctr.Fixed 1, Ctr.symbols = ["◰", "◳", "◲", "◱"],
                Ctr.suffix = ": "
              }
            Ctr.counterRenderMarker counter 1 `shouldBe` "◰: "
            Ctr.counterRenderMarker counter 2 `shouldBe` "◳: "
            Ctr.counterRenderMarker counter 3 `shouldBe` "◲: "
            Ctr.counterRenderMarker counter 4 `shouldBe` "◱: "
            Ctr.counterRenderMarker counter 5 `shouldBe` "5: "
            Ctr.counterRenderMarker counter 6 `shouldBe` "6: "
        it "Handles system: symbolic" $ do
            let counter = Ctr.defaultCounter {
                Ctr.system = Ctr.Symbolic, Ctr.symbols = ["*", "⁑", "†", "‡"],
                Ctr.suffix = " "
            }
            Ctr.counterRenderMarker counter 1 `shouldBe` "* "
            Ctr.counterRenderMarker counter 2 `shouldBe` "⁑ "
            Ctr.counterRenderMarker counter 3 `shouldBe` "† "
            Ctr.counterRenderMarker counter 4 `shouldBe` "‡ "
            Ctr.counterRenderMarker counter 5 `shouldBe` "** "
            Ctr.counterRenderMarker counter 6 `shouldBe` "⁑⁑ "
        it "Handles system: alphabetic" $ do
            let counter = Ctr.defaultCounter {
                Ctr.system = Ctr.Alphabetic, Ctr.symbols = ["◦", "•"],
                Ctr.suffix = " "
            }
            Ctr.counterRenderMarker counter 1 `shouldBe` "◦ "
            Ctr.counterRenderMarker counter 2 `shouldBe` "• "
            Ctr.counterRenderMarker counter 3 `shouldBe` "◦◦ "
            Ctr.counterRenderMarker counter 4 `shouldBe` "◦• "
            Ctr.counterRenderMarker counter 5 `shouldBe` "•◦ "
            Ctr.counterRenderMarker counter 6 `shouldBe` "•• "
            Ctr.counterRenderMarker counter 7 `shouldBe` "◦◦◦ "
        it "Handles system: numeric" $ do
            let counter = Ctr.defaultCounter {
                Ctr.system = Ctr.Numeric, Ctr.symbols = ["0", "1", "2"]
            }
            Ctr.counterRenderMarker counter 1 `shouldBe` "1. "
            Ctr.counterRenderMarker counter 2 `shouldBe` "2. "
            Ctr.counterRenderMarker counter 3 `shouldBe` "10. "
            Ctr.counterRenderMarker counter 4 `shouldBe` "11. "
            Ctr.counterRenderMarker counter 5 `shouldBe` "12. "
            Ctr.counterRenderMarker counter 6 `shouldBe` "20. "
        it "Handles system: additive" $ do
            let counter = Ctr.defaultCounter {
                Ctr.system = Ctr.Additive, Ctr.suffix = " ",
                Ctr.additiveSymbols = [(6, "⚅"), (5, "⚄"), (4, "⚃"),
                                        (3, "⚂"), (2, "⚁"), (1, "⚀")]
            }
            Ctr.counterRenderMarker counter 1 `shouldBe` "⚀ "
            Ctr.counterRenderMarker counter 2 `shouldBe` "⚁ "
            Ctr.counterRenderMarker counter 3 `shouldBe` "⚂ "
            Ctr.counterRenderMarker counter 11 `shouldBe` "⚅⚄ "
            Ctr.counterRenderMarker counter 12 `shouldBe` "⚅⚅ "
            Ctr.counterRenderMarker counter 13 `shouldBe` "⚅⚅⚀ "
        it "Handles Chinese-specific numbering systems" $ do
            let counterRender = Ctr.counterRender Ctr.simpChineseInformal
            counterRender 1 `shouldBe` "一"
            counterRender 2 `shouldBe` "二"
            counterRender 3 `shouldBe` "三"
            counterRender 4 `shouldBe` "四"
            counterRender 5 `shouldBe` "五"
            counterRender 6 `shouldBe` "六"
            counterRender 7 `shouldBe` "七"
            counterRender 8 `shouldBe` "八"
            counterRender 9 `shouldBe` "九"
            counterRender 10 `shouldBe` "十"
            counterRender 11 `shouldBe` "十一"
            counterRender 12 `shouldBe` "十二"
            counterRender 13 `shouldBe` "十三"
            counterRender 14 `shouldBe` "十四"
            counterRender 15 `shouldBe` "十五"
            counterRender 16 `shouldBe` "十六"
            counterRender 17 `shouldBe` "十七"
            counterRender 18 `shouldBe` "十八"
            counterRender 19 `shouldBe` "十九"
            counterRender 20 `shouldBe` "二十"
            counterRender 21 `shouldBe` "二十一"
            counterRender 22 `shouldBe` "二十二"
            counterRender 23 `shouldBe` "二十三"
            counterRender 24 `shouldBe` "二十四"
            counterRender 25 `shouldBe` "二十五"
            counterRender 26 `shouldBe` "二十六"
            counterRender 27 `shouldBe` "二十七"
            counterRender 28 `shouldBe` "二十八"
            counterRender 29 `shouldBe` "二十九"
            counterRender 30 `shouldBe` "三十"
            counterRender 31 `shouldBe` "三十一"
            counterRender 32 `shouldBe` "三十二"
            counterRender 33 `shouldBe` "三十三"
            counterRender 34 `shouldBe` "三十四"
            counterRender 35 `shouldBe` "三十五"
            counterRender 36 `shouldBe` "三十六"
            counterRender 37 `shouldBe` "三十七"
            counterRender 38 `shouldBe` "三十八"
            counterRender 39 `shouldBe` "三十九"
            counterRender 40 `shouldBe` "四十"
            counterRender 41 `shouldBe` "四十一"
            counterRender 42 `shouldBe` "四十二"
            counterRender 43 `shouldBe` "四十三"
            counterRender 44 `shouldBe` "四十四"
            counterRender 45 `shouldBe` "四十五"
            counterRender 46 `shouldBe` "四十六"
            counterRender 47 `shouldBe` "四十七"
            counterRender 48 `shouldBe` "四十八"
            counterRender 49 `shouldBe` "四十九"
            counterRender 50 `shouldBe` "五十"
            counterRender 51 `shouldBe` "五十一"
            counterRender 52 `shouldBe` "五十二"
            counterRender 53 `shouldBe` "五十三"
            counterRender 54 `shouldBe` "五十四"
            counterRender 55 `shouldBe` "五十五"
            counterRender 56 `shouldBe` "五十六"
            counterRender 57 `shouldBe` "五十七"
            counterRender 58 `shouldBe` "五十八"
            counterRender 59 `shouldBe` "五十九"
            counterRender 60 `shouldBe` "六十"
            counterRender 61 `shouldBe` "六十一"
            counterRender 62 `shouldBe` "六十二"
            counterRender 63 `shouldBe` "六十三"
            counterRender 64 `shouldBe` "六十四"
            counterRender 65 `shouldBe` "六十五"
            counterRender 66 `shouldBe` "六十六"
            counterRender 67 `shouldBe` "六十七"
            counterRender 68 `shouldBe` "六十八"
            counterRender 69 `shouldBe` "六十九"
            counterRender 70 `shouldBe` "七十"
            counterRender 71 `shouldBe` "七十一"
            counterRender 72 `shouldBe` "七十二"
            counterRender 73 `shouldBe` "七十三"
            counterRender 74 `shouldBe` "七十四"
            counterRender 75 `shouldBe` "七十五"
            counterRender 76 `shouldBe` "七十六"
            counterRender 77 `shouldBe` "七十七"
            counterRender 78 `shouldBe` "七十八"
            counterRender 79 `shouldBe` "七十九"
            counterRender 80 `shouldBe` "八十"
            counterRender 81 `shouldBe` "八十一"
            counterRender 82 `shouldBe` "八十二"
            counterRender 83 `shouldBe` "八十三"
            counterRender 84 `shouldBe` "八十四"
            counterRender 85 `shouldBe` "八十五"
            counterRender 86 `shouldBe` "八十六"
            counterRender 87 `shouldBe` "八十七"
            counterRender 88 `shouldBe` "八十八"
            counterRender 89 `shouldBe` "八十九"
            counterRender 90 `shouldBe` "九十"
            counterRender 91 `shouldBe` "九十一"
            counterRender 92 `shouldBe` "九十二"
            counterRender 93 `shouldBe` "九十三"
            counterRender 94 `shouldBe` "九十四"
            counterRender 95 `shouldBe` "九十五"
            counterRender 96 `shouldBe` "九十六"
            counterRender 97 `shouldBe` "九十七"
            counterRender 98 `shouldBe` "九十八"
            counterRender 99 `shouldBe` "九十九"
            counterRender 100 `shouldBe` "一百"
            counterRender 101 `shouldBe` "一百零一"
            counterRender 102 `shouldBe` "一百零二"
            counterRender 103 `shouldBe` "一百零三"
            counterRender 104 `shouldBe` "一百零四"
            counterRender 105 `shouldBe` "一百零五"
            counterRender 106 `shouldBe` "一百零六"
            counterRender 107 `shouldBe` "一百零七"
            counterRender 108 `shouldBe` "一百零八"
            counterRender 109 `shouldBe` "一百零九"
            counterRender 110 `shouldBe` "一百一十"
            counterRender 111 `shouldBe` "一百一十一"
            counterRender 112 `shouldBe` "一百一十二"
            counterRender 113 `shouldBe` "一百一十三"
            counterRender 114 `shouldBe` "一百一十四"
            counterRender 115 `shouldBe` "一百一十五"
            counterRender 116 `shouldBe` "一百一十六"
            counterRender 117 `shouldBe` "一百一十七"
            counterRender 118 `shouldBe` "一百一十八"
            counterRender 119 `shouldBe` "一百一十九"
            counterRender 120 `shouldBe` "一百二十"
        it "Handles ethiopian numbering system" $ do
            Ctr.counterRender Ctr.ethiopic 1 `shouldBe` "፩"
            Ctr.counterRender Ctr.ethiopic 100 `shouldBe` "፻"
            Ctr.counterRender Ctr.ethiopic 78010092 `shouldBe` "፸፰፻፩፼፺፪"
            Ctr.counterRender Ctr.ethiopic 780100000092 `shouldBe` "፸፰፻፩፼፼፺፪"

styleIndex :: StyleIndex
styleIndex = new
queryable :: QueryableStyleSheet (VarParser TrivialPropertyParser)
queryable = queryableStyleSheet
emptyStyle :: TrivialStyleSheet
emptyStyle = TrivialStyleSheet []
conditional :: ConditionalStyles TrivialPropertyParser
conditional = conditionalStyles (fromJust $ parseURI "about:blank") "test"
linkStyle :: TrivialStyleSheet
linkStyle = TrivialStyleSheet [sampleRule]
sampleRule :: StyleRule
sampleRule = StyleRule (Element [Tag "a"]) [("color", [Ident "green"])] ""
resolve' = resolve (\var -> B (var == "test")) evalToken emptyStyle
    where
        evalToken (Number _ (NVInteger x)) = N $ fromInteger x
        evalToken (Number _ (NVNumber x)) = N $ toRealFloat x
        evalToken _ = B False
extractImports' = extractImports (\var -> B (var == "test")) evalToken
    where
        evalToken (Number _ (NVInteger x)) = N $ fromInteger x
        evalToken (Number _ (NVNumber x)) = N $ toRealFloat x
        evalToken _ = B False