~alcinnz/haskell-stylist

ref: 227d7ca1f8dafec0c42275f6c07c6d142c79eb46 haskell-stylist/src/Data/CSS/ShorthandUtil.hs -rw-r--r-- 1.5 KiB
227d7ca1 — Adrian Cochrane Parse longhand list-style property. 1 year, 6 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 [] = []