From c66e930343f9b255bbb10f3f4d2a06656d82bd03 Mon Sep 17 00:00:00 2001 From: Adrian Cochrane Date: Fri, 8 Nov 2019 20:00:45 +1300 Subject: [PATCH] Test rules capturing of ConditionalStyleSheet. --- src/Data/CSS/Preprocessor/Conditions.hs | 8 ++++++-- src/Data/CSS/Style/Cascade.hs | 2 +- src/Data/CSS/Syntax/StyleSheet.hs | 6 +++--- test/Test.hs | 26 +++++++++++++++++++++++++ 4 files changed, 36 insertions(+), 6 deletions(-) diff --git a/src/Data/CSS/Preprocessor/Conditions.hs b/src/Data/CSS/Preprocessor/Conditions.hs index 15a71e8..169428a 100644 --- a/src/Data/CSS/Preprocessor/Conditions.hs +++ b/src/Data/CSS/Preprocessor/Conditions.hs @@ -1,6 +1,7 @@ {-# LANGUAGE OverloadedStrings #-} module Data.CSS.Preprocessor.Conditions( - ConditionalStyles(..), extractImports, resolveImports, resolve + ConditionalStyles(..), conditionalStyles, + extractImports, resolveImports, resolve ) where import qualified Data.CSS.Preprocessor.Conditions.Expr as Query @@ -23,6 +24,9 @@ data ConditionalStyles p = ConditionalStyles { propertyParser :: p } +conditionalStyles :: PropertyParser p => URI -> String -> ConditionalStyles p +conditionalStyles uri mediaDocument = ConditionalStyles uri mediaDocument [] temp + data ConditionalRule p = Priority Int | StyleRule' StyleRule | AtRule Text [Token] | External Query.Expr URI | Internal Query.Expr (ConditionalStyles p) @@ -79,7 +83,7 @@ instance PropertyParser p => StyleSheet (ConditionalStyles p) where if evalSupports (propertyParser self) cond then parseAtBlock self toks' else (self, skipAtRule toks') - addAtRule self rule tokens = let (block, rest) = scanBlock tokens in + addAtRule self rule tokens = let (block, rest) = scanAtRule tokens in (addRule' self $ AtRule rule block, rest) -------- ---- @import/@media diff --git a/src/Data/CSS/Style/Cascade.hs b/src/Data/CSS/Style/Cascade.hs index 32f3ddc..79c7039 100644 --- a/src/Data/CSS/Style/Cascade.hs +++ b/src/Data/CSS/Style/Cascade.hs @@ -58,7 +58,7 @@ cascadeProperties :: Props -> Props -> HashMap Text [Token] cascadeProperties overrides props = fromList (props ++ overrides) construct :: PropertyParser p => p -> Props -> p -construct base props = dispatch' base child props +construct base props = dispatch base child props where child = setVars [item | item@(n, _) <- props, isPrefixOf "--" n] $ inherit base dispatch :: PropertyParser p => p -> p -> Props -> p dispatch base child ((key, value):props) diff --git a/src/Data/CSS/Syntax/StyleSheet.hs b/src/Data/CSS/Syntax/StyleSheet.hs index 08ac1e3..7a3e28b 100644 --- a/src/Data/CSS/Syntax/StyleSheet.hs +++ b/src/Data/CSS/Syntax/StyleSheet.hs @@ -1,7 +1,7 @@ {-# LANGUAGE OverloadedStrings #-} module Data.CSS.Syntax.StyleSheet ( parse, parse', parseForURL, TrivialStyleSheet(..), - StyleSheet(..), skipAtRule, scanBlock, skipSpace, + StyleSheet(..), skipAtRule, scanAtRule, scanBlock, skipSpace, StyleRule(..), -- For parsing at-rules, HTML "style" attribute, etc. parseProperties, parseProperties', @@ -97,8 +97,8 @@ parseProperties' tokens = parseProperties' (skipValue tokens) ---- Skipping/Scanning utilities -------- scanAtRule :: Parser [Token] -scanAtRule (Semicolon:tokens) = ([], tokens) -scanAtRule (LeftCurlyBracket:tokens) = scanInner tokens $ \rest -> ([], rest) +scanAtRule (Semicolon:tokens) = ([Semicolon], tokens) +scanAtRule tokens@(LeftCurlyBracket:_) = scanInner tokens $ \rest -> ([], rest) scanAtRule tokens@(LeftParen:_) = scanInner tokens scanValue scanAtRule tokens@(Function _:_) = scanInner tokens scanValue diff --git a/test/Test.hs b/test/Test.hs index 78270b0..4a197ad 100644 --- a/test/Test.hs +++ b/test/Test.hs @@ -4,6 +4,7 @@ module Main where import Test.Hspec import Data.HashMap.Strict import Data.Maybe (fromJust) +import Network.URI import Data.CSS.Syntax.Tokens import Data.CSS.Syntax.StyleSheet @@ -15,6 +16,7 @@ import Data.CSS.Style.Selector.Interpret import Data.CSS.Style import Data.CSS.Preprocessor.Conditions +import Data.CSS.Preprocessor.Conditions.Expr (Datum(..)) main :: IO () main = hspec spec @@ -25,6 +27,11 @@ spec = do it "Test framework works" $ do True `shouldBe` True describe "Parsing" $ do + it "Can scan @rules" $ do + scanAtRule [Ident "utf-8", Semicolon, Ident "a"] `shouldBe` ([Ident "utf-8", Semicolon], [Ident "a"]) + scanAtRule [Ident "before", LeftCurlyBracket, Ident "inside", RightCurlyBracket, Ident "after"] `shouldBe` ( + [Ident "before", LeftCurlyBracket, Ident "inside", RightCurlyBracket], + [Ident "after"]) it "Ignores @rules" $ do parse emptyStyle "@encoding 'utf-8';" `shouldBe` emptyStyle parse emptyStyle " @encoding 'utf-8';" `shouldBe` emptyStyle @@ -352,6 +359,22 @@ spec = do let VarParser vars (TrivialPropertyParser style) = cascade rules el [] $ cascade rules parent [] temp vars `shouldBe` [("--link", [Hash HId "f00"])] style ! "color" `shouldBe` [Hash HId "f00"] + describe "Conditional @rules" $ do + it "can handle normal rules" $ do + let TrivialStyleSheet styles = resolve' $ parse conditional "a {color: green}" + styles `shouldBe` [StyleRule (Element [Tag "a"]) [("color", [Ident "green"])] ""] + + let TrivialStyleSheet styles = resolve' $ parse conditional "@rule; a {color: green}" + styles `shouldBe` [StyleRule (Element [Tag "a"]) [("color", [Ident "green"])] ""] + + let TrivialStyleSheet styles = resolve' $ parse conditional "a {color: green} @rule;" + styles `shouldBe` [StyleRule (Element [Tag "a"]) [("color", [Ident "green"])] ""] + + let TrivialStyleSheet styles = resolve' $ parse conditional "a {color: green} @font {}" + styles `shouldBe` [StyleRule (Element [Tag "a"]) [("color", [Ident "green"])] ""] + + let TrivialStyleSheet styles = resolve' $ parse conditional "@font {} a {color: green}" + styles `shouldBe` [StyleRule (Element [Tag "a"]) [("color", [Ident "green"])] ""] styleIndex :: StyleIndex styleIndex = new @@ -359,7 +382,10 @@ queryable :: QueryableStyleSheet (VarParser TrivialPropertyParser) queryable = queryableStyleSheet emptyStyle :: TrivialStyleSheet emptyStyle = TrivialStyleSheet [] +conditional :: ConditionalStyles TrivialPropertyParser +conditional = conditionalStyles (fromJust $ parseURI "about:blank") "test" linkStyle :: TrivialStyleSheet linkStyle = TrivialStyleSheet [sampleRule] sampleRule :: StyleRule sampleRule = StyleRule (Element [Tag "a"]) [("color", [Ident "green"])] "" +resolve' = resolve (\_ -> B False) (\_ -> B False) emptyStyle -- 2.30.2