From ee89bcfdbf10f2759cf3b618a5fa3f2b0bbafa6c Mon Sep 17 00:00:00 2001 From: Adrian Cochrane Date: Tue, 18 Jun 2019 09:37:07 +1200 Subject: [PATCH] Abstract away RuleStore construction. --- src/Stylish/Style/Selector.hs | 20 +++++++++++++------- src/Stylish/Style/Selector/Common.hs | 1 + src/Stylish/Style/Selector/Importance.hs | 1 + src/Stylish/Style/Selector/Index.hs | 5 ++--- src/Stylish/Style/Selector/Interpret.hs | 1 + src/Stylish/Style/Selector/Specificity.hs | 1 + test/Test.hs | 3 +++ 7 files changed, 22 insertions(+), 10 deletions(-) diff --git a/src/Stylish/Style/Selector.hs b/src/Stylish/Style/Selector.hs index ddc3eb4..5efa0e5 100644 --- a/src/Stylish/Style/Selector.hs +++ b/src/Stylish/Style/Selector.hs @@ -11,24 +11,30 @@ import Stylish.Style.Selector.Common import Stylish.Parse (StyleSheet(..)) --- TODO do performance tests to decide beside between strict/lazy. +-- 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 -ruleStore = ImportanceSplitter $ OrderedRuleStore (InterpretedRuleStore styleIndex) 0 +type QueryableStyleSheet = QueryableStyleSheet' (ImportanceSplitter ( + OrderedRuleStore (InterpretedRuleStore StyleIndex) + )) -data QueryableStyleSheet store = QueryableStyleSheet { +data QueryableStyleSheet' store = QueryableStyleSheet' { store :: store, priority :: Int -- author vs user agent vs user styles } -queryableStyleSheet = QueryableStyleSheet {store = ruleStore, priority = 0} +queryableStyleSheet :: QueryableStyleSheet +queryableStyleSheet = QueryableStyleSheet' {store = new, priority = 0} -instance RuleStore s => StyleSheet (QueryableStyleSheet s) where - addRule self@(QueryableStyleSheet store priority) rule = self { +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 +queryRules (QueryableStyleSheet' store _) el = lookupRules store el -------- ---- Cascade diff --git a/src/Stylish/Style/Selector/Common.hs b/src/Stylish/Style/Selector/Common.hs index 89c4125..8995523 100644 --- a/src/Stylish/Style/Selector/Common.hs +++ b/src/Stylish/Style/Selector/Common.hs @@ -6,6 +6,7 @@ import Stylish.Element import Stylish.Parse class RuleStore a where + new :: a addStyleRule :: a -> Int -> StyleRule' -> a lookupRules :: a -> Element -> [StyleRule'] diff --git a/src/Stylish/Style/Selector/Importance.hs b/src/Stylish/Style/Selector/Importance.hs index 35c88e2..bd49e21 100644 --- a/src/Stylish/Style/Selector/Importance.hs +++ b/src/Stylish/Style/Selector/Importance.hs @@ -18,6 +18,7 @@ splitProperties (prop@(name, value):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 diff --git a/src/Stylish/Style/Selector/Index.hs b/src/Stylish/Style/Selector/Index.hs index 250cde6..9b04d7b 100644 --- a/src/Stylish/Style/Selector/Index.hs +++ b/src/Stylish/Style/Selector/Index.hs @@ -1,6 +1,6 @@ {-# LANGUAGE OverloadedStrings #-} module Stylish.Style.Selector.Index ( - StyleIndex(..), styleIndex, + StyleIndex(..), rulesForElement ) where @@ -20,12 +20,11 @@ data StyleIndex = StyleIndex { unindexed :: [StyleRule'] } -styleIndex = StyleIndex {indexed = empty, unindexed = []} - lookup' :: SimpleSelector -> HashMap SimpleSelector [a] -> [a] lookup' = lookupDefault [] instance RuleStore StyleIndex where + new = StyleIndex {indexed = empty, unindexed = []} addStyleRule self _ rule | [] == properties rule = self | otherwise = addRuleForSelector self rule $ simpleSelector $ selector rule lookupRules self element = nub $ Prelude.foldr (++) [] rules diff --git a/src/Stylish/Style/Selector/Interpret.hs b/src/Stylish/Style/Selector/Interpret.hs index c700d88..5b57c93 100644 --- a/src/Stylish/Style/Selector/Interpret.hs +++ b/src/Stylish/Style/Selector/Interpret.hs @@ -83,6 +83,7 @@ hasLang expected value = expected == value || isPrefixOf (expected ++ "-") value -------- data InterpretedRuleStore inner = InterpretedRuleStore inner instance RuleStore inner => RuleStore (InterpretedRuleStore inner) where + new = InterpretedRuleStore new addStyleRule (InterpretedRuleStore self) priority rule = InterpretedRuleStore $ addStyleRule self priority $ rule { compiledSelector = compile $ selector rule diff --git a/src/Stylish/Style/Selector/Specificity.hs b/src/Stylish/Style/Selector/Specificity.hs index 6b809d8..9d93bae 100644 --- a/src/Stylish/Style/Selector/Specificity.hs +++ b/src/Stylish/Style/Selector/Specificity.hs @@ -27,6 +27,7 @@ add (a, b, c) (x, y, z) = (a + x, b + y, c + z) data OrderedRuleStore inner = OrderedRuleStore inner Int instance RuleStore inner => RuleStore (OrderedRuleStore inner) where + new = OrderedRuleStore new 0 addStyleRule (OrderedRuleStore self count) priority rule = OrderedRuleStore ( addStyleRule self priority $ rule { rank = (priority, computeSpecificity $ selector rule, count) diff --git a/test/Test.hs b/test/Test.hs index d60ddbd..ca6fa46 100644 --- a/test/Test.hs +++ b/test/Test.hs @@ -12,6 +12,7 @@ import Stylish.Style.Selector.Interpret import Stylish.Style.Selector.Common import Stylish.Style.Selector +main :: IO () main = hspec spec spec = do @@ -210,6 +211,8 @@ spec = do selector sibling `shouldBe` False selector child `shouldBe` True +styleIndex :: StyleIndex +styleIndex = new emptyStyle = TrivialStyleSheet [] linkStyle = TrivialStyleSheet [sampleRule] sampleRule = StyleRule (Element [Tag "a"]) [("color", [Ident "green"])] -- 2.30.2