~alcinnz/haskell-stylist

ref: 5c97dd0e1094c7f2e405f30a14133000be310cd5 haskell-stylist/src/Data/CSS/Style/Importance.hs -rw-r--r-- 1.5 KiB
5c97dd0e — Adrian Cochrane Fix build failure against new enough versions of 'text'. 2 years ago
                                                                                
c1fca3d5 Adrian Cochrane
186cbffa Adrian Cochrane
c1fca3d5 Adrian Cochrane
b6808a67 Adrian Cochrane
c1fca3d5 Adrian Cochrane
00ed62a1 Adrian Cochrane
c1fca3d5 Adrian Cochrane
00ed62a1 Adrian Cochrane
c1fca3d5 Adrian Cochrane
3bd6ae52 Adrian Cochrane
c1fca3d5 Adrian Cochrane
b6808a67 Adrian Cochrane
186cbffa Adrian Cochrane
c1fca3d5 Adrian Cochrane
b6808a67 Adrian Cochrane
c1fca3d5 Adrian Cochrane
00ed62a1 Adrian Cochrane
6344dc8e Adrian Cochrane
c1fca3d5 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
{-# LANGUAGE OverloadedStrings #-}
-- | Evaluates !important.
-- INTERNAL MODULE.
module Data.CSS.Style.Importance (
        ImportanceSplitter(..)
    ) where

import Data.CSS.Syntax.Tokens
import Data.CSS.Style.Common

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

--- NOTE: Prorities are defined with lower numbers being more important,
---     so negate to be consistant with other priority sources.
--- This API decision started out being accidental, but I find it more intuitive.
-- | Evaluates "!important" by splitting all `StyleRule'` in two.
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 unimportant
            ) priority $ buildRule important
        where
            (unimportant, important) = splitProperties props
            (StyleRule sel props psuedo) = inner rule
            buildRule x = rule {inner = StyleRule sel x psuedo}
    lookupRules (ImportanceSplitter self) el = lookupRules self el