~alcinnz/haskell-stylist

227d7ca1f8dafec0c42275f6c07c6d142c79eb46 — Adrian Cochrane 1 year, 6 months ago 5dc32d0
Parse longhand list-style property.
3 files changed, 37 insertions(+), 3 deletions(-)

M src/Data/CSS/Preprocessor/Text.hs
A src/Data/CSS/ShorthandUtil.hs
M stylist.cabal
M src/Data/CSS/Preprocessor/Text.hs => src/Data/CSS/Preprocessor/Text.hs +3 -2
@@ 4,6 4,7 @@ module Data.CSS.Preprocessor.Text(TextStyle, resolve, CounterStore'(..)) where

import Data.CSS.Syntax.Tokens (Token(..), NumericValue(..))
import Data.CSS.Style (PropertyParser(..))
import Data.CSS.ShorthandUtil (parseUnorderedShorthand)
import Data.CSS.StyleTree
import qualified Data.Text as Txt
import Data.Text (Text)


@@ 71,8 72,8 @@ instance PropertyParser p => PropertyParser (TextStyle p) where
    shorthand self "white-space" [Ident val]
        | val `elem` ["normal", "pre", "pre-wrap", "pre-line"] = [("white-space", [Ident val])]
        | otherwise = shorthand (inner self) "white-space" [Ident val]
    -- FIXME Handle "list-style" shorthand for -type, -image, & -position.
    -- Needs helper util...
    shorthand self "list-style" toks = parseUnorderedShorthand self subprops toks
      where subprops = ["list-style-image", "list-style-type", "list-style-position"]
    shorthand TextStyle { inner = s } k v
        | Just _ <- longhand s s k $ removeCounters v = [(k, v)]
        | otherwise = shorthand s k v

A src/Data/CSS/ShorthandUtil.hs => src/Data/CSS/ShorthandUtil.hs +33 -0
@@ 0,0 1,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 [] = []

M stylist.cabal => stylist.cabal +1 -1
@@ 54,7 54,7 @@ source-repository head
library
  -- Modules exported by the library.
  exposed-modules:     Data.CSS.Syntax.StyleSheet, Data.CSS.Syntax.Selector,
                       Data.CSS.Style, Data.CSS.StyleTree,
                       Data.CSS.Style, Data.CSS.StyleTree, Data.CSS.ShorthandUtil,
                       Data.CSS.Preprocessor.Conditions, Data.CSS.Preprocessor.Conditions.Expr,
                       Data.CSS.Preprocessor.Assets, Data.CSS.Preprocessor.PsuedoClasses,
                            Data.CSS.Preprocessor.Text, Data.CSS.Preprocessor.Text.CounterStyle