~alcinnz/haskell-stylist

d47030fdf3eb4d295f139b3b161a82370b25f721 — Adrian Cochrane 5 years ago 7d540c4
Handle !important CSS declarations.
M src/Stylish/Style/Selector.hs => src/Stylish/Style/Selector.hs +5 -4
@@ 5,13 5,14 @@ module Stylish.Style.Selector(
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

ruleStore = OrderedRuleStore (InterpretedRuleStore styleIndex) 0
ruleStore = ImportanceSplitter $ OrderedRuleStore (InterpretedRuleStore styleIndex) 0

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

queryableStyleSheet = QueryableStyleSheet {store = ruleStore, proirity = 0}
queryableStyleSheet = QueryableStyleSheet {store = ruleStore, priority = 0}

A src/Stylish/Style/Selector/Importance.hs => src/Stylish/Style/Selector/Importance.hs +29 -0
@@ 0,0 1,29 @@
{-# 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
    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

M src/Stylish/Style/Selector/Specificity.hs => src/Stylish/Style/Selector/Specificity.hs +1 -1
@@ 1,5 1,5 @@
module Stylish.Style.Selector.Specificity(
        OrderedRuleStore
        OrderedRuleStore(..)
    ) where

import Stylish.Parse.Selector

M test/Test.hs => test/Test.hs +1 -1
@@ 9,8 9,8 @@ import Stylish.Parse
import Stylish.Style.Selector.Index
import Stylish.Element
import Stylish.Style.Selector.Interpret
import Stylish.Style.Selector.Specificity
import Stylish.Style.Selector.Common
import Stylish.Style.Selector

main = hspec spec