~alcinnz/haskell-stylist

c66e930343f9b255bbb10f3f4d2a06656d82bd03 — Adrian Cochrane 5 years ago e73cd8f
Test rules capturing of ConditionalStyleSheet.
M src/Data/CSS/Preprocessor/Conditions.hs => src/Data/CSS/Preprocessor/Conditions.hs +6 -2
@@ 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

M src/Data/CSS/Style/Cascade.hs => src/Data/CSS/Style/Cascade.hs +1 -1
@@ 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)

M src/Data/CSS/Syntax/StyleSheet.hs => src/Data/CSS/Syntax/StyleSheet.hs +3 -3
@@ 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

M test/Test.hs => test/Test.hs +26 -0
@@ 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