~alcinnz/haskell-stylist

ref: 7d540c43481903333757c1232c6fdd6d7626b623 haskell-stylist/src/Stylish/Style/Selector/Common.hs -rw-r--r-- 924 bytes
7d540c43 — Adrian Cochrane Compose together rule stores. 5 years 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
module Stylish.Style.Selector.Common(
        RuleStore(..), StyleRule'(..), selector, properties, styleRule'
    ) where

import Stylish.Element
import Stylish.Parse

class RuleStore a where
    addStyleRule :: a -> Int -> StyleRule' -> a
    lookupRules :: a -> Element -> [StyleRule']

type SelectorFunc = Element -> Bool
data StyleRule' = StyleRule' {
    inner :: StyleRule,
    compiledSelector :: SelectorFunc,
    rank :: (Int, (Int, Int, Int), Int) -- This reads ugly, but oh well.
}
styleRule' rule = StyleRule' {
    inner = rule,
    compiledSelector = \_ -> True,
    rank = (0, (0, 0, 0), 0)
}

instance Eq StyleRule' where
    a == b = inner a == inner b
instance Show StyleRule' where show a = show $ inner a
instance Ord StyleRule' where compare x y = rank x `compare` rank y

selector rule | StyleRule selector _ <- inner rule = selector
properties rule | StyleRule _ properties <- inner rule = properties