~alcinnz/haskell-stylist

ref: b0fa6994e37d78effeb6866615fdf0be30727241 haskell-stylist/src/Stylish/Style/Selector/Importance.hs -rw-r--r-- 1.3 KiB
b0fa6994 — Adrian Cochrane Draft example property definitions. 5 years ago
                                                                                
d47030fd Adrian Cochrane
ee89bcfd Adrian Cochrane
d47030fd 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
{-# LANGUAGE OverloadedStrings #-}
module Stylish.Style.Selector.Importance (
        splitProperties, ImportanceSplitter(..)
    ) where

import Data.CSS.Syntax.Tokens
import Data.Text.Internal (Text(..))
import Stylish.Parse (StyleRule(..))
import Stylish.Style.Selector.Common

type Property = (Text, [Token])
splitProperties :: [Property] -> ([Property], [Property])
splitProperties (prop@(name, value):rest)
        | (Ident "important":Delim '!':value') <- reverse value =
            (unimportant, (name, reverse value'):important)
        | otherwise = (prop:unimportant, important)
    where (unimportant, important) = splitProperties rest

data ImportanceSplitter a = ImportanceSplitter a
instance RuleStore inner => RuleStore (ImportanceSplitter inner) where
    new = ImportanceSplitter new
    addStyleRule (ImportanceSplitter self) priority rule =
            ImportanceSplitter $ addStyleRule (
                addStyleRule self (negate priority) $ buildRule important
            ) priority $ buildRule unimportant
        where
            (important, unimportant) = splitProperties properties
            (StyleRule selector properties) = inner rule
            buildRule properties = rule {inner = StyleRule selector properties}
    lookupRules (ImportanceSplitter self) el = lookupRules self el