From 227d7ca1f8dafec0c42275f6c07c6d142c79eb46 Mon Sep 17 00:00:00 2001 From: Adrian Cochrane Date: Fri, 28 Apr 2023 16:49:27 +1200 Subject: [PATCH] Parse longhand list-style property. --- src/Data/CSS/Preprocessor/Text.hs | 5 +++-- src/Data/CSS/ShorthandUtil.hs | 33 +++++++++++++++++++++++++++++++ stylist.cabal | 2 +- 3 files changed, 37 insertions(+), 3 deletions(-) create mode 100644 src/Data/CSS/ShorthandUtil.hs diff --git a/src/Data/CSS/Preprocessor/Text.hs b/src/Data/CSS/Preprocessor/Text.hs index 9148698..e732fab 100644 --- a/src/Data/CSS/Preprocessor/Text.hs +++ b/src/Data/CSS/Preprocessor/Text.hs @@ -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 diff --git a/src/Data/CSS/ShorthandUtil.hs b/src/Data/CSS/ShorthandUtil.hs new file mode 100644 index 0000000..7602c18 --- /dev/null +++ b/src/Data/CSS/ShorthandUtil.hs @@ -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 [] = [] diff --git a/stylist.cabal b/stylist.cabal index 1cf3328..dca4b0c 100644 --- a/stylist.cabal +++ b/stylist.cabal @@ -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 -- 2.30.2