~alcinnz/haskell-stylist

feb616e86c9a448322a5637e985f17582e476ed6 — Adrian Cochrane 5 years ago 61ab1bc
Expose hooks for implementing var().

The tricky part here is around CSS inheritance, it works naturally in the existing cascade implementation.
1 files changed, 10 insertions(+), 4 deletions(-)

M src/Data/CSS/Style/Cascade.hs
M src/Data/CSS/Style/Cascade.hs => src/Data/CSS/Style/Cascade.hs +10 -4
@@ 23,6 23,11 @@ class PropertyParser a where
    -- longhand parent self name value
    longhand :: a -> a -> Text -> [Token] -> Maybe a

    getVars :: a -> Props
    getVars _ = []
    setVars :: Props -> a -> a
    setVars _ = id

data TrivialPropertyParser = TrivialPropertyParser (HashMap String [Token])
instance PropertyParser TrivialPropertyParser where
    temp = TrivialPropertyParser empty


@@ 45,18 50,19 @@ query self el = Prelude.foldr yield empty $ lookupRules self el

cascade :: PropertyParser p => [StyleRule'] -> Props -> p -> p
cascade styles overrides base =
    dispatch base (inherit base) $ toList $ cascadeRules overrides styles
    dispatch base (inherit base) $ toList $ cascadeRules (getVars base ++ overrides) styles

cascadeRules :: Props -> [StyleRule'] -> HashMap Text [Token]
cascadeRules overrides rules = cascadeProperties overrides $ concat $ Prelude.map properties rules
cascadeProperties :: Props -> Props -> HashMap Text [Token]
cascadeProperties overrides props = fromList (props ++ overrides)

dispatch :: PropertyParser p => p -> p -> Props -> p
dispatch base child ((key, value):props)
dispatch, dispatch' :: PropertyParser p => p -> p -> Props -> p
dispatch base child props = dispatch' base (setVars props child) props
dispatch' base child ((key, value):props)
    | Just child' <- longhand base child key value = dispatch base child' props
    | otherwise = dispatch base child props
dispatch _ child [] = child
dispatch' _ child [] = child

--------
---- attr()