~alcinnz/haskell-stylist

ref: 620c530c2e1346c26068eaa6e7d6997d74a2ac4e haskell-stylist/src/Data/CSS/ShorthandUtil.hs -rw-r--r-- 1.5 KiB
620c530c — Adrian Cochrane Add new support APIs to Stylist Traits, releasing v0.1.2! 1 year, 3 days ago
                                                                                
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
{-# 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 [] = []