From 0b37dedff7149662870aba77fb424bf16aa1602d Mon Sep 17 00:00:00 2001 From: Adrian Cochrane Date: Tue, 9 Aug 2022 15:59:58 +1200 Subject: [PATCH] Tweak priority to be a list for the sake of cascadeLayers. --- src/Data/CSS/Style.hs | 7 ++++--- src/Data/CSS/Style/Common.hs | 6 +++--- src/Data/CSS/Style/Importance.hs | 2 +- stylist.cabal | 1 - test/Test.hs | 16 ++++++++-------- 5 files changed, 16 insertions(+), 16 deletions(-) diff --git a/src/Data/CSS/Style.hs b/src/Data/CSS/Style.hs index 654f4da..a703d40 100644 --- a/src/Data/CSS/Style.hs +++ b/src/Data/CSS/Style.hs @@ -38,15 +38,16 @@ data QueryableStyleSheet' store parser = QueryableStyleSheet' { -- | The "PropertyParser" to use for property syntax validation. parser :: parser, -- | Whether author, useragent, or user styles are currently being parsed. - priority :: Int -- author vs user agent vs user styles + -- The tail of this list indicates which Cascade layer is active. + priority :: [Int] -- author vs user agent vs user styles } -- | Constructs an empty QueryableStyleSheet'. queryableStyleSheet :: PropertyParser p => QueryableStyleSheet p -queryableStyleSheet = QueryableStyleSheet' {store = new, parser = temp, priority = 0} +queryableStyleSheet = QueryableStyleSheet' {store = new, parser = temp, priority = [0]} instance (RuleStore s, PropertyParser p) => StyleSheet (QueryableStyleSheet' s p) where - setPriority v self = self {priority = v} + setPriority v self = self {priority = [v]} addRule self@(QueryableStyleSheet' store' _ priority') rule = self { store = addStyleRule store' priority' $ styleRule' rule } diff --git a/src/Data/CSS/Style/Common.hs b/src/Data/CSS/Style/Common.hs index ec230d3..8c7ed79 100644 --- a/src/Data/CSS/Style/Common.hs +++ b/src/Data/CSS/Style/Common.hs @@ -15,20 +15,20 @@ import Stylist (Element(..), Attribute(..)) class RuleStore a where new :: a - addStyleRule :: a -> Int -> StyleRule' -> a + addStyleRule :: a -> [Int] -> StyleRule' -> a lookupRules :: a -> Element -> [StyleRule'] type SelectorFunc = Element -> Bool data StyleRule' = StyleRule' { inner :: StyleRule, compiledSelector :: SelectorFunc, - rank :: (Int, (Int, Int, Int), Int) -- This reads ugly, but oh well. + rank :: ([Int], (Int, Int, Int), Int) -- This reads ugly, but oh well. } styleRule' :: StyleRule -> StyleRule' styleRule' rule = StyleRule' { inner = rule, compiledSelector = \_ -> True, - rank = (0, (0, 0, 0), 0) + rank = ([0], (0, 0, 0), 0) } instance Eq StyleRule' where diff --git a/src/Data/CSS/Style/Importance.hs b/src/Data/CSS/Style/Importance.hs index 5683cc2..feb114c 100644 --- a/src/Data/CSS/Style/Importance.hs +++ b/src/Data/CSS/Style/Importance.hs @@ -26,7 +26,7 @@ instance RuleStore inner => RuleStore (ImportanceSplitter inner) where new = ImportanceSplitter new addStyleRule (ImportanceSplitter self) priority rule = ImportanceSplitter $ addStyleRule ( - addStyleRule self (negate priority) $ buildRule unimportant + addStyleRule self (map negate priority) $ buildRule unimportant ) priority $ buildRule important where (unimportant, important) = splitProperties props diff --git a/stylist.cabal b/stylist.cabal index 27e5dba..c48216c 100644 --- a/stylist.cabal +++ b/stylist.cabal @@ -91,4 +91,3 @@ test-suite test-stylist network-uri >= 2.6 && <2.7, async >= 2.1 && <2.3, regex-tdfa >= 1.3, hspec, QuickCheck, scientific >= 0.3 && <1.0, regex-tdfa >= 1.3, stylist-traits >= 0.1 && < 0.2 - ghc-options: -Wall diff --git a/test/Test.hs b/test/Test.hs index d935c03..3bff696 100644 --- a/test/Test.hs +++ b/test/Test.hs @@ -98,7 +98,7 @@ spec = do ] describe "Style Index" $ do it "Retrieves appropriate styles" $ do - let index = addStyleRule styleIndex 0 $ styleRule' sampleRule + let index = addStyleRule styleIndex [0] $ styleRule' sampleRule let element = ElementNode { name = "a", namespace = "", @@ -121,17 +121,17 @@ spec = do rulesForElement index element2 `shouldBe` [] let rule1 = StyleRule (Element [Class "external"]) [("color", [Ident "green"])] "" - let index1 = addStyleRule styleIndex 0 $ styleRule' rule1 + let index1 = addStyleRule styleIndex [0] $ styleRule' rule1 rulesForElement index1 element `shouldBe` [rule1] rulesForElement index1 element2 `shouldBe` [] let rule2 = StyleRule (Element [Id "mysite"]) [("color", [Ident "green"])] "" - let index2 = addStyleRule styleIndex 0 $ styleRule' rule2 + let index2 = addStyleRule styleIndex [0] $ styleRule' rule2 rulesForElement index2 element `shouldBe` [rule2] rulesForElement index2 element2 `shouldBe` [] let rule3 = StyleRule (Element [Property Nothing "href" $ Prefix "https://"]) [("color", [Ident "green"])] "" - let index3 = addStyleRule styleIndex 0 $ styleRule' rule3 + let index3 = addStyleRule styleIndex [0] $ styleRule' rule3 rulesForElement index3 element `shouldBe` [rule3] rulesForElement index3 element2 `shouldBe` [] describe "Selector Compiler" $ do @@ -274,8 +274,8 @@ spec = do previous = Nothing, attributes = [Attribute "class" "" "link"] } - let rules = parse (queryable {priority = 1}) "a {color: green}" - let rules2 = parse (rules {priority = 2}) "a {color: red}" :: QueryableStyleSheet (VarParser TrivialPropertyParser) + let rules = parse (queryable {priority = [1]}) "a {color: green}" + let rules2 = parse (rules {priority = [2]}) "a {color: red}" :: QueryableStyleSheet (VarParser TrivialPropertyParser) let VarParser _ (TrivialPropertyParser style) = cascade rules2 el [] temp::(VarParser TrivialPropertyParser) style ! "color" `shouldBe` [Ident "green"] @@ -286,8 +286,8 @@ spec = do previous = Nothing, attributes = [Attribute "class" "" "link"] } - let rules' = parse (queryable {priority = 1}) "a {color: red}" - let rules2' = parse (rules' {priority = 2}) "a {color: green !important}" :: QueryableStyleSheet (VarParser TrivialPropertyParser) + let rules' = parse (queryable {priority = [1]}) "a {color: red}" + let rules2' = parse (rules' {priority = [2]}) "a {color: green !important}" :: QueryableStyleSheet (VarParser TrivialPropertyParser) let VarParser _ (TrivialPropertyParser style') = cascade rules2' el' [] temp::(VarParser TrivialPropertyParser) style' ! "color" `shouldBe` [Ident "green"] it "respects overrides" $ do -- 2.30.2