{-# LANGUAGE OverloadedStrings #-}
-- NOTE: These would be better in Stylist Traits, but didn't want to release an update yet.
module Data.CSS.ShorthandUtil(parseUnorderedShorthand, parseOperands) where
import Data.CSS.Syntax.Tokens (Token(..))
import Data.CSS.Style (PropertyParser(..))
import Data.CSS.Syntax.StyleSheet (scanBlock)
import Data.Text (Text)
parseUnorderedShorthand :: PropertyParser a =>
a -> [Text] -> [Token] -> [(Text, [Token])]
parseUnorderedShorthand self properties toks
| Just _ <- lookup "" ret = [] -- Error recovery!
| otherwise = ret
where
ret = parseUnorderedShorthand' self properties $ parseOperands toks
parseUnorderedShorthand' :: PropertyParser a =>
a -> [Text] -> [[Token]] -> [(Text, [Token])]
parseUnorderedShorthand' self properties (arg:args) = inner properties []
where
inner (prop:props) props'
| Just _ <- longhand self self prop arg =
parseUnorderedShorthand' self (props' ++ props) args
| otherwise = inner props (prop:props')
inner [] _ = [("", [])] -- Error caught & handled by public API.
parseUnorderedShorthand' self (prop:props) [] = -- Shorthands have long effects!
(prop, [Ident "initial"]):parseUnorderedShorthand' self props []
parseUnorderedShorthand' _ [] [] = []
parseOperands :: [Token] -> [[Token]]
parseOperands (Function name:toks) = let (args, toks') = scanBlock toks
in (Function name:args):parseOperands toks'
parseOperands (tok:toks) = [tok]:parseOperands toks
parseOperands [] = []