~alcinnz/haskell-stylist

ref: ee89bcfdbf10f2759cf3b618a5fa3f2b0bbafa6c haskell-stylist/src/Stylish/Style/Selector.hs -rw-r--r-- 1.4 KiB
ee89bcfd — Adrian Cochrane Abstract away RuleStore construction. 5 years ago
                                                                                
7d540c43 Adrian Cochrane
2881aec4 Adrian Cochrane
7d540c43 Adrian Cochrane
d47030fd Adrian Cochrane
7d540c43 Adrian Cochrane
2881aec4 Adrian Cochrane
ee89bcfd Adrian Cochrane
768a480d Adrian Cochrane
ee89bcfd Adrian Cochrane
768a480d Adrian Cochrane
ee89bcfd Adrian Cochrane
7d540c43 Adrian Cochrane
ee89bcfd Adrian Cochrane
d47030fd Adrian Cochrane
7d540c43 Adrian Cochrane
ee89bcfd Adrian Cochrane
2881aec4 Adrian Cochrane
ee89bcfd Adrian Cochrane
2881aec4 Adrian Cochrane
ee89bcfd Adrian Cochrane
768a480d Adrian Cochrane
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
module Stylish.Style.Selector(
        QueryableStyleSheet(..), queryableStyleSheet,
        queryRules
    ) where

import Stylish.Style.Selector.Index
import Stylish.Style.Selector.Interpret
import Stylish.Style.Selector.Specificity
import Stylish.Style.Selector.Importance
import Stylish.Style.Selector.Common

import Stylish.Parse (StyleSheet(..))

-- TODO do performance tests to decide beside between strict/lazy,
--      or is another Map implementation better?
import Data.HashMap.Strict
import Data.Text.Internal (Text(..))
import Data.CSS.Syntax.Tokens

type QueryableStyleSheet = QueryableStyleSheet' (ImportanceSplitter (
        OrderedRuleStore (InterpretedRuleStore StyleIndex)
    ))

data QueryableStyleSheet' store = QueryableStyleSheet' {
    store :: store,
    priority :: Int -- author vs user agent vs user styles
}

queryableStyleSheet :: QueryableStyleSheet
queryableStyleSheet = QueryableStyleSheet' {store = new, priority = 0}

instance RuleStore s => StyleSheet (QueryableStyleSheet' s) where
    addRule self@(QueryableStyleSheet' store priority) rule = self {
            store = addStyleRule store priority $ styleRule' rule
        }

queryRules (QueryableStyleSheet' store _) el = lookupRules store el

--------
---- Cascade
--------

cascadeRules rules = cascadeProperties $ concat $ Prelude.map properties rules

cascadeProperties ((name, value):props) = insert name value $ cascadeProperties props