From 086da474d2729c627d0472e9f2e64e48a3968d0a Mon Sep 17 00:00:00 2001 From: Adrian Cochrane Date: Wed, 22 Apr 2020 19:42:17 +1200 Subject: [PATCH] Rewrite trivial cases of :where & :is. This is mostly for the psuedoselector rewrites performed in Data.CSS.Preprocessor.PsuedoClasses --- src/Data/CSS/Style.hs | 5 +++- src/Data/CSS/Style/Selector/LowerWhere.hs | 32 +++++++++++++++++++++++ stylist.cabal | 3 ++- 3 files changed, 38 insertions(+), 2 deletions(-) create mode 100644 src/Data/CSS/Style/Selector/LowerWhere.hs diff --git a/src/Data/CSS/Style.hs b/src/Data/CSS/Style.hs index 30381e7..654f4da 100644 --- a/src/Data/CSS/Style.hs +++ b/src/Data/CSS/Style.hs @@ -12,6 +12,7 @@ module Data.CSS.Style( import Data.CSS.Style.Selector.Index import Data.CSS.Style.Selector.Interpret import Data.CSS.Style.Selector.Specificity +import Data.CSS.Style.Selector.LowerWhere import Data.CSS.Style.Importance import Data.CSS.Style.Common import qualified Data.CSS.Style.Cascade as Cascade @@ -25,7 +26,9 @@ import Data.List (elemIndex) -- | A parsed CSS stylesheet from which you can query styles to match an element. type QueryableStyleSheet parser = QueryableStyleSheet' (ImportanceSplitter ( - PropertyExpander parser (OrderedRuleStore (InterpretedRuleStore StyleIndex)) + PropertyExpander parser ( + OrderedRuleStore (WhereLowerer (InterpretedRuleStore StyleIndex)) + ) )) parser -- | More generic version of `QueryableStyleSheet`. diff --git a/src/Data/CSS/Style/Selector/LowerWhere.hs b/src/Data/CSS/Style/Selector/LowerWhere.hs new file mode 100644 index 0000000..fc3e024 --- /dev/null +++ b/src/Data/CSS/Style/Selector/LowerWhere.hs @@ -0,0 +1,32 @@ +{-# LANGUAGE OverloadedStrings #-} +module Data.CSS.Style.Selector.LowerWhere( + WhereLowerer(..) + ) where + +import Data.CSS.Syntax.Selector +import Data.CSS.Style.Common + +lowerSelector :: Selector -> Selector +lowerSelector (Element sel) = Element $ lowerSelector' sel +lowerSelector (Child sel x) = Child (lowerSelector sel) x +lowerSelector (Descendant sel x) = Descendant (lowerSelector sel) x +lowerSelector (Adjacent sel x) = Adjacent (lowerSelector sel) x +lowerSelector (Sibling sel x) = Sibling (lowerSelector sel) x + +lowerSelector' :: [SimpleSelector] -> [SimpleSelector] +lowerSelector' (Psuedoclass c args:sel) + | c `elem` ["is", "where"], ([Element arg'], []) <- parseSelectors args = + arg' ++ lowerSelector' sel +lowerSelector' (test:tests) = test : lowerSelector' tests +lowerSelector' [] = [] + +data WhereLowerer s = WhereLowerer s + +instance RuleStore s => RuleStore (WhereLowerer s) where + new = WhereLowerer new + addStyleRule (WhereLowerer self) priority rule = + WhereLowerer $ addStyleRule self priority $ rule { + inner = StyleRule (lowerSelector sel) props psuedo + } + where StyleRule sel props psuedo = inner rule + lookupRules (WhereLowerer self) el = lookupRules self el diff --git a/stylist.cabal b/stylist.cabal index 1b759e0..6ac2109 100644 --- a/stylist.cabal +++ b/stylist.cabal @@ -61,7 +61,8 @@ library -- Modules included in this library but not exported. other-modules: Data.CSS.Syntax.StylishUtil, Data.CSS.Style.Importance, Data.CSS.Style.Common, Data.CSS.Style.Cascade, - Data.CSS.Style.Selector.Index, Data.CSS.Style.Selector.Interpret, Data.CSS.Style.Selector.Specificity + Data.CSS.Style.Selector.Index, Data.CSS.Style.Selector.Interpret, + Data.CSS.Style.Selector.Specificity, Data.CSS.Style.Selector.LowerWhere -- LANGUAGE extensions used by modules in this package. -- other-extensions: -- 2.30.2