Skip to content

Commit

Permalink
Fixed an issue where compressing CSS expressions involving clamp wo…
Browse files Browse the repository at this point in the history
…uld return invalid CSS (fixes #1021)
  • Loading branch information
LaurentRDC committed Oct 24, 2024
1 parent 8042b77 commit 8059cb2
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 8 deletions.
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,11 @@ title: Releases

# Releases

## Unreleased

- Fixed an issue where compressing CSS with `clamp` expressions would
result in invalid CSS (#1021) (contribution by Laurent P. René de Cotret)

## Hakyll 4.16.3.0 (2024-10-24)

- Bump `pandoc` upper bound to include up to version 3.5
Expand Down
20 changes: 12 additions & 8 deletions lib/Hakyll/Web/CompressCss.hs
Original file line number Diff line number Diff line change
Expand Up @@ -42,14 +42,17 @@ compressSeparators =
replaceAll " *[{};,>+~!] *" (take 1 . dropWhile isSpace) .
replaceAll ": *" (take 1) -- not destroying pseudo selectors (#323)

-- | Uses `compressCalcExpression` on all parenthesised calc expressions
-- and applies `transform` to all parts outside of them
-- | Uses `compressExpression` on all parenthesised calc and
-- clamp expressions, and applies `transform` to all parts
-- outside of them
handleCalcExpressions :: (String -> String) -> String -> String
handleCalcExpressions transform = top transform
where
top f "" = f ""
top f str | "calc(" `isPrefixOf` str = f "calc" ++ nested 0 compressCalcExpression (drop 4 str)
top f (x:xs) = top (f . (x:)) xs
top f "" = f ""
top f str | "calc(" `isPrefixOf` str = f "calc" ++ nested 0 compressExpression (drop 4 str)
-- See issue #1021
| "clamp(" `isPrefixOf` str = f "clamp" ++ nested 0 compressExpression (drop 5 str)
top f (x:xs) = top (f . (x:)) xs

-- when called with depth=0, the first character must be a '('
nested :: Int -> (String -> String) -> String -> String
Expand All @@ -62,9 +65,10 @@ handleCalcExpressions transform = top transform
_ -> depth
) (f . (x:)) xs

-- | does not remove whitespace around + and -, which is important in calc() expressions
compressCalcExpression :: String -> String
compressCalcExpression =
-- | does not remove whitespace around + and -, which is important
-- in calc() and clamp() expressions
compressExpression :: String -> String
compressExpression =
replaceAll " *[*/] *| *\\)|\\( *" (take 1 . dropWhile isSpace)

--------------------------------------------------------------------------------
Expand Down
2 changes: 2 additions & 0 deletions tests/Hakyll/Web/CompressCss/Tests.hs
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,8 @@ tests = testGroup "Hakyll.Web.CompressCss.Tests" $ concat
, "a!b" @=? compressCss "a ! b"
-- compress calc()
, "calc(1px + 100%/(5 + 3) - (3px + 2px)*5)" @=? compressCss "calc( 1px + 100% / ( 5 + 3) - calc( 3px + 2px ) * 5 )"
-- compress clamp() (issue #1021)
, "clamp(2.25rem, 2vw + 1.5rem, 3.25rem)" @=? compressCss "clamp(2.25rem, 2vw + 1.5rem, 3.25rem)"
-- compress whitespace even after this curly brace
, "}" @=? compressCss "; } "
-- but do not compress separators inside string tokens
Expand Down

0 comments on commit 8059cb2

Please sign in to comment.