css-parser 0.0.1 → 0.0.2
raw patch · API changes only · 13 files changed · hide whitespace changes · browse css-parser-0.0.2
+151/−45 lines, 13 filesPVP: major bump suggested
API changes (from Hackage documentation)
0 removed, 5 added, 2 changed
API removals or changes: PVP suggests a major version bump
+ CssParser.Rule.Show: instance CssParser.Show.ShowSpaceBetween CssParser.Ident.Ident CssParser.Ident.Ident
+ CssParser.Rule.Value: AppCe0 :: PropertyName -> CalcExpr
+ CssParser.Rule.Value: CosFn :: CalcFns
+ CssParser.Rule.Value: SinFn :: CalcFns
+ CssParser.Utils: encodeFirstChar :: Char -> Text
- CssParser: DefineMixin :: Ident -> [CssRuleBodyItem] -> AtRule
+ CssParser: DefineMixin :: Ident -> [Ident] -> [CssRuleBodyItem] -> AtRule
- CssParser.Rule: DefineMixin :: Ident -> [CssRuleBodyItem] -> AtRule
+ CssParser.Rule: DefineMixin :: Ident -> [Ident] -> [CssRuleBodyItem] -> AtRule
Files
- CHANGELOG.md +7/−0
- css-parser.cabal +1/−1
- src/CssParser/Lexer.x +8/−20
- src/CssParser/Lexer/Token.hs +4/−1
- src/CssParser/Parser.y +33/−9
- src/CssParser/Rule.hs +1/−1
- src/CssParser/Rule/Show.hs +8/−2
- src/CssParser/Rule/Value.hs +16/−1
- src/CssParser/Utils.hs +16/−2
- test/CssParser/Test/Arbitrary.hs +10/−5 · folded
- test/CssParser/Test/Arbitrary/At.hs +1/−1 · folded
- test/CssParser/Test/Arbitrary/Value.hs +40/−2 · folded
- test/Spec.hs +6/−0 · folded
CHANGELOG.md view
@@ -1,5 +1,12 @@ # `css-parser` Changelog +## Version 0.0.2 2026-05-18++* define-mixin arguments+* identifier can start with dollar char+* extension calc syntax after function or parethesis and arithmetic operator (`*`, `-`, `+`)+* full support of CSS from orbit library+ ## Version 0.0.1 2026-04-18 * init
css-parser.cabal view
@@ -1,6 +1,6 @@ cabal-version: 3.0 name: css-parser-version: 0.0.1+version: 0.0.2 synopsis: pure CSS parser/printer description: Modern CSS parser in pure Haskell built on Alex+Happy stack homepage: https://github.com/yaitskov/css-parser
src/CssParser/Lexer.x view
@@ -24,22 +24,21 @@ $nonascii = [^\0-\x9f] $w = [\ \t\r\n\f] $tl = [\~]-$pm = [\-\+] @nl = \r|\n|\r\n|\f @unicode = \\[0-9a-fA-F]{1,6}(\r\n|[ \n\r\t\f])? @escape = @unicode | \\[^\n\r\f0-9a-fA-F] @wo = $w* @nonaesc = $nonascii | @escape-@nmstart = [_a-zA-Z] | @nonaesc-@nmchar = [_\-a-zA-Z0-9] | @nonaesc+@nmstart = [_a-zA-Z\$] | @nonaesc+@nmaschar = [_\-a-zA-Z0-9]+@nmchar = @nmaschar | @nonaesc -@name = @nmchar++@name = [_\-a-zA-Z0-9\$] [_\-a-zA-Z0-9]* @dec = [0-9]-@int = @dec+ @uint = @dec+-@hexdig = [0-9a-fA-F]+ @updig = [0-9a-fA-F\?]-@hexdigs = @hexdig++ @string1 = \'([^\n\r\f\\\'] | \\@nl | @nonaesc )*\' -- strings with single quote @string2 = \"([^\n\r\f\\\"] | \\@nl | @nonaesc )*\" -- strings with double quotes @string = @string1 | @string2@@ -84,17 +83,6 @@ @anum = [\-\+]? ( @dec+ ([\.]@dec+)? (@e [\-\+]? @dec+)? | [\.]@dec+ ) @var = [\-][\-] @selector = @s@e@l@e@c@t@o@r-@keyframes = @k@e@y@f@r@a@m@e@s-@charset = @c@h@a@r@s@e@t-@import = @i@m@p@o@r@t-@style = @s@t@y@l@e-@counter = @c@o@u@n@t@e@r-@namespace = @n@a@m@e@s@p@a@c@e-@property = @p@r@o@p@e@r@t@y-@not = @n@o@t-@and = @a@n@d-@or = @o@r-@only = @o@n@l@y @url = @u@r@l @cmo = \/\* @cmc = \*\/@@ -152,14 +140,14 @@ @wo "{" @wo { constoken COpen } @wo "}" @wo { constoken CClose } - @pse [a-zA-Z0-9_\-]+ { tokenize (tokenizePseudoElement) }+ @pse @name { tokenize (tokenizePseudoElement) } @psc @l@a@n@g "(" { constAndBegin TLang lang_state } @psc @h@e@a@d@i@n@g { constoken THeading } @psc @h@o@s@t { constoken THost } @psc @s@t@a@t@e "(" { constoken TState } - @psc [a-zA-Z0-9_\-]+ { tokenize (tokenizePseudoClass) }+ @psc @name { tokenize (tokenizePseudoClass) } @wo ")" { constoken TClose } "(" { constoken TOpen }
src/CssParser/Lexer/Token.hs view
@@ -12,7 +12,7 @@ import CssParser.Rule.Type ( AtomicCssType ) import CssParser.Rule.TypedNum ( NumberStr ) import CssParser.Rule.Value- ( CalcFns(CalcFn, MinFn, MaxFn, ClampFn), Ratio )+ ( CalcFns(CalcFn, MinFn, MaxFn, SinFn, CosFn, ClampFn), Ratio ) import CssParser.Show ( mkDecodingMap', smartLookup ) import CssParser.Utils ( readIdentifier ) import Data.Text (pack)@@ -259,6 +259,9 @@ , ("supports" , SupportsT) , ("min" , CalcFunT MinFn) , ("max" , CalcFunT MaxFn)+ , ("max" , CalcFunT MaxFn)+ , ("sin" , CalcFunT SinFn)+ , ("cos" , CalcFunT CosFn) , ("clamp" , CalcFunT ClampFn) , ("calc" , CalcFunT CalcFn) , ("to" , ToT)
src/CssParser/Parser.y view
@@ -233,6 +233,10 @@ CssRule :: { CssRule } : SelectorList ERB { CssRule $1 $2 } | '@' AtRule { AtRule $1 $2 }+MixinArgs :: { [ R.Ident ] }+ : { [] }+ | IdKwd { [$1] }+ | IdKwd ' ' MixinArgs { $1 : $3 } AtRule :: { AtRule } : media Os '{' OsCssRuleBody '}' { MediaRule (MediaQueryList []) $4 } | media Os MediaQueryList ERB { MediaRule (MediaQueryList $3) $4 }@@ -240,7 +244,7 @@ | namespace Os IdKwdMb Os Source ';' { Namespace $3 $5 } | import Os Import ';' { ImportStmt $3 } | mixin Os IdKwd ';' { Mixin $3 }- | defineMixin Os IdKwd ERB { DefineMixin $3 $4 }+ | defineMixin Os IdKwd Os MixinArgs Os ERB { DefineMixin $3 $5 $7 } | customSelector Os pseudc Os SelectorList ';' {% mkCustomSelector $3 $5 } | charset Os Str ';' { CharsetStmt (Charset $3) }@@ -551,16 +555,33 @@ | hash { HexColor (HC (pack $1)) } | PropN { propRef $1 } | Str { StrVal $1 }- | PropVal '/' Os PropVal { Div $1 $4 }- | PropN Op PropParValsList ')' { mkAppFun $1 $3 }- | PropN Op ')' { AppConst $1 }+ | PropParVal '/' Os PropParVal { Div $1 $4 }+ | PropN Op PropParValsList ')' Os { mkAppFun $1 $3 }+ | PropN Op PropParValsList ')' Os CalcASP Os CalcExpr+ {% massageExpr+ CalcFun+ (BinOpCe+ (AppCe $1 (PropValsList $3))+ $6+ $8)+ }+ | PropN Op ')' Os { AppConst $1 }+ | PropN Op ')' Os CalcASP Os CalcExpr {% massageExpr CalcFun (BinOpCe (AppCe0 $1) $5 $7) } | '.' { DotVal } | Url { UrlVal $1 } | Bool { BoolVal $1 } | 'attr(' Os AttrName Os Maybe(AttrType) AttrDefVal Os ')' { AttrFun $3 $5 $6 }- | calcFns Op CalcExprList Os ')' {% massageExpr CalcFun (CalcCe $1 $3) }- | Op Os CalcExprList Os ')' {% massageExpr CalcFun (CalcCe NoFn $3) }+ | calcFns Op CalcExprList Os ')' Os {% massageExpr CalcFun (CalcCe $1 $3) }+ | calcFns Op CalcExprList Os ')' Os CalcASP Os CalcExpr+ {% massageExpr CalcFun+ (BinOpCe (CalcCe $1 $3) $7 $9) }+ | Op Os CalcExprList Os ')' Os {% massageExpr CalcFun (CalcCe NoFn $3) }+ | Op Os CalcExprList Os ')' Os CalcASP Os CalcExpr+ {% massageExpr+ CalcFun+ (BinOpCe (CalcCe NoFn $3) $7 $9)+ } | 'ratio' { RatioVal $1 } | UnicodeRange { Vl.UnicodeRangeVal $1 } | alpha Op Ident Os '=' Os Unsigned Os ')' { AlphaF $7 }@@ -576,17 +597,20 @@ AttrDefVal :: { Maybe PropVal } : { Nothing } | ',' Os PropParVal { Just $3 }-CalcOp :: { CalcOp }+CalcASP :: { CalcOp } : '+' { PlusCe } | '-' { MinusCe }- | '/' { DivCe } | '*' { ProdCe }+CalcOp :: { CalcOp }+ : CalcASP { $1 }+ | '/' { DivCe } CalcExpr :: { CalcExpr } : Op CalcExprList ')' Os { CalcCe NoFn $2 } | '-' Os CalcExpr { CalcNeg $3 } | CalcExpr CalcOp Os CalcExpr { BinOpCe $1 $2 $4 } | CalcExpr CalcExpr {% recoverCalcBinOp $1 $2 } | PropN Op PropParValsList Os ')' Os { AppCe $1 (PropValsList $3) }+ | PropN Op ')' Os { AppCe0 $1 } | PropN Os { VarCe $1 } | TypedNum Os { ValCe $1 } | calcFns Op CalcExprList ')' Os { CalcCe $1 $3 }@@ -604,7 +628,7 @@ | Desc Os { [] } | CssRule OsCssRuleBody { CssNestedRule $1 : $2 } PropParValsList :: { NonEmpty PropVals }- : NonEmpty(',', PropParVals) { $1 }+ : NonEmpty(',', PropParVals) { $1 } PropValsList :: { NonEmpty PropVals } : NonEmpty(',', PropVals) { $1 } PropParVals :: { PropVals }
src/CssParser/Rule.hs view
@@ -55,7 +55,7 @@ | MediaRule MediaQueryList [CssRuleBodyItem] | Mixin Ident | CustomSelector CustomSelectorName SelectorList- | DefineMixin Ident [CssRuleBodyItem]+ | DefineMixin Ident [Ident] [CssRuleBodyItem] | Namespace (Maybe Ident) Source | Page PageSelectorList [CssRuleBodyItem] | PageMarginBlock PageMargin [CssRuleBodyItem]
src/CssParser/Rule/Show.hs view
@@ -18,6 +18,9 @@ AtRule pb ar -> "@" <> toCssText pb <> toCssText ar +instance ShowSpaceBetween Ident Ident where+ cssSpace _ _ = " "+ instance CssShow AtRule where toCssText = \case MediaRule mql body ->@@ -25,8 +28,11 @@ Mixin l -> "mixin " <> toCssText l <> ";" CustomSelector sn sel -> "custom-selector " <> toCssText sn <> " " <> toCssText sel <> ";"- DefineMixin m body ->- "define-mixin " <> toCssText m <> " {" <> toCssText body <> "}"+ DefineMixin m args body ->+ "define-mixin "+ <> toCssText m+ <> case args of [] -> ""; _ -> " " <> toCssText args+ <> " {" <> toCssText body <> "}" CustomMedia v body -> "custom-media " <> toCssText v <> " " <> toCssText body <> ";" LayerBlock mbn body ->
src/CssParser/Rule/Value.hs view
@@ -12,6 +12,12 @@ import CssParser.Show ( CssShow(..), ShowSpaceBetween(..), encodeStringLiteral, mayCss ) import Expression.Reorder+ ( reorder,+ SyntaxTree(..),+ Validation(..),+ Assoc(AssocLeft),+ Fixity(Fixity),+ Node(NodeLeaf, NodeInfix, NodePrefix) ) newtype Unsigned = Unsigned RawNum deriving newtype (Eq, Show, Ord, CssShow)@@ -62,7 +68,8 @@ VarProp v -> VarRef v -data CalcOp = PlusCe | MinusCe | DivCe | ProdCe deriving (Eq, Ord, Show, Enum, Bounded, Generic)+data CalcOp = PlusCe | MinusCe | DivCe | ProdCe+ deriving (Eq, Ord, Show, Enum, Bounded, Generic) instance CssShow CalcOp where toCssText = \case@@ -76,6 +83,8 @@ | MinFn | NoFn | MaxFn+ | SinFn+ | CosFn | ClampFn deriving (Eq, Ord, Show, Bounded, Enum, Generic) @@ -85,6 +94,8 @@ MinFn -> "min" NoFn -> "" MaxFn -> "max"+ CosFn -> "cos"+ SinFn -> "sin" ClampFn -> "clamp" data NthFormula@@ -103,6 +114,7 @@ | ValCe TypedNum | VarCe PropertyName | AppCe PropertyName PropValsList+ | AppCe0 PropertyName | CalcCe CalcFns CalcExprList | CalcNeg CalcExpr deriving (Eq, Ord, Show, Generic)@@ -116,6 +128,7 @@ o@VarCe {} -> o o@ValCe {} -> o o@AppCe {} -> o+ o@AppCe0 {} -> o newtype CalcExprList = CalcExprList (NonEmpty CalcExpr) deriving newtype (Eq, Ord)@@ -162,11 +175,13 @@ ValCe v -> toCssText v VarCe v -> toCssText v AppCe f a -> toCssText f <> "(" <> toCssText a <> ")"+ AppCe0 f -> toCssText f <> "()" CalcCe f a -> toCssText f <> "(" <> toCssText a <> ")" CalcNeg x@CalcNeg {} -> "-(" <> toCssText x <> ")" CalcNeg x@BinOpCe {} -> "-(" <> toCssText x <> ")" CalcNeg x@(VarCe _) -> "- " <> toCssText x CalcNeg x@(AppCe _ _) -> "- " <> toCssText x+ CalcNeg x@(AppCe0 _) -> "- " <> toCssText x CalcNeg x -> "-" <> toCssText x data AttrType
src/CssParser/Utils.hs view
@@ -45,7 +45,12 @@ readIdentifier = _readCssString '\\' _notEncode :: Char -> Bool-_notEncode x = isAsciiLower x || isAsciiUpper x || x == '-' || x == '_' || isDigit x+_notEncode x =+ isAsciiLower x ||+ isAsciiUpper x ||+ x == '-' ||+ x == '_' ||+ isDigit x -- | Convert a string to a css selector string literal. This is done by putting -- quotes around the content, and escaping certain characters.@@ -68,6 +73,11 @@ | _notEncode c = LT.singleton c | otherwise = LT.cons '\\' (LT.pack (_showHex (ord c) "")) +encodeFirstChar :: Char -> LT.Text+encodeFirstChar c+ | c == '$' || _notEncode c = LT.singleton c+ | otherwise = LT.cons '\\' (LT.pack (_showHex (ord c) ""))+ _encodeCharacter :: Char -> Text _encodeCharacter c | _notEncode c = singleton c@@ -80,7 +90,11 @@ Text -> -- | The encoded identifier. LT.Text-encodeIdentifier = LT.concatMap encodeCharacter . LT.fromStrict+encodeIdentifier s =+ case LT.uncons (LT.fromStrict s) of+ Nothing -> ""+ Just (fl, ols) ->+ encodeFirstChar fl <> LT.concatMap encodeCharacter ols _showHex :: Int -> ShowS _showHex = go (6 :: Int)
test/CssParser/Test/Arbitrary.hs view
@@ -39,16 +39,21 @@ T.words "not of or and only src false true from to url x y d r" <> T.words "cx cy rx ry is dir odd even open min max clamp calc"+ <>+ T.words "sin cos var past part" arbitraryIdent :: Gen Text arbitraryIdent = do i <- arbitraryText fl nl- if i `member` keywords- then pure $ i <> "_"- else pure i+ if i == "$"+ then pure "$a"+ else+ if i `member` keywords+ then pure $ i <> "_"+ else pure i where- fl = ['a' .. 'z'] <> "_"- nl = fl <> ['-', '0' .. '9']+ fl = "$_" <> ['a' .. 'z']+ nl = drop 1 fl <> ['-', '0' .. '9'] arbitraryText :: String -> String -> Gen Text arbitraryText fl nl = do
test/CssParser/Test/Arbitrary/At.hs view
@@ -16,7 +16,7 @@ import Data.Text qualified as T browserPrefixes :: [Text]-browserPrefixes = T.words "-moz- -ms- -webkit- -apple- -o-"+browserPrefixes = T.words "-moz- -ms- -webkit- -apple- -o- $" instance Arbitrary BrowserSpecificIdent where arbitrary =
test/CssParser/Test/Arbitrary/Value.hs view
@@ -72,8 +72,46 @@ deriving via Ident instance Arbitrary LiteralString deriving via (GenericArbitrary Ratio) instance Arbitrary Ratio-deriving via (GenericArbitrary PropVals) instance Arbitrary PropVals-deriving via (GenericArbitrary PropValsList) instance Arbitrary PropValsList++instance Norm PropVals where+ normalize (PropVals l i) = PropVals (mapInitNe embraceBinOp embraceDivOp l) i++instance Arbitrary PropVals where+ arbitrary = normalize <$> genericArbitrary+ shrink x = normalize <$> genericShrink x++mapInit :: (a -> a) -> (a -> a) -> [a] -> [a]+mapInit _ _ [] = []+mapInit _ g [a] = [g a]+mapInit f g (h:nh:t) = f h : mapInit f g (nh : t)++mapInitNe :: (a -> a) -> (a -> a) -> NonEmpty a -> NonEmpty a+mapInitNe f g = \case+ h :| [] -> g h :| []+ h :| l -> f h :| mapInit f g l++embraceBinOp :: PropVal -> PropVal+embraceBinOp = \case+ CalcFun bop@BinOpCe {} -> CalcFun (CalcCe NoFn (CalcExprList $ bop :| []))+ o -> o++embraceDivOp :: PropVal -> PropVal+embraceDivOp = \case+ CalcFun bop@(BinOpCe _ DivCe _) -> CalcFun (CalcCe NoFn (CalcExprList $ bop :| []))+ o -> o++stripImportant :: PropVals -> PropVals+stripImportant = \case+ PropVals l (Just _) -> PropVals l Nothing+ o -> o++instance Norm PropValsList where+ normalize (PropValsList l) = PropValsList (stripImportant <$> l)++instance Arbitrary PropValsList where+ arbitrary = normalize <$> genericArbitrary+ shrink x = normalize <$> genericShrink x+ deriving via (GenericArbitrary PropValType) instance Arbitrary PropValType deriving via (GenericArbitrary CalcFns) instance Arbitrary CalcFns
test/Spec.hs view
@@ -147,6 +147,7 @@ [ "@position-try --x {x: 1px;}" , "@starting-style {}" , "@mixin input;"+ , "@define-mixin is-resizable $direction { resize: $direction; }" , "@define-mixin m1 { @mixin input; display: block; }" , "@custom-selector :--ti input[type='dt'], input[type='tm'];" , "[data-bs-theme=light]{--bs-btn-close-filter: }"@@ -197,7 +198,12 @@ properties :: [String] properties = [ "margin: 20px;"+ , "resize: $direction;" , "margin: 2.0px;"+ , "--o: var(--o-a) * var(--o-o) * var(--o-d, 1);"+ , "x: cos(var(--a) + var(--o ))"+ , "x: (1px + 2px) * 3mm;"+ , "x: (1px + 2px) / (3mm + 2);" , "font-size: clamp(1mm / 2, 2mm * 2mm, 3mm + 1cm);" , "grid-template-columns: repeat(12, [col-start] 1fr);" , "padding-block: max(2 / 2 - 0.06rem);"