diff options
-rw-r--r-- | lib/Hakyll/Web/CompressCss.hs | 18 | ||||
-rw-r--r-- | tests/Hakyll/Web/CompressCss/Tests.hs | 2 |
2 files changed, 20 insertions, 0 deletions
diff --git a/lib/Hakyll/Web/CompressCss.hs b/lib/Hakyll/Web/CompressCss.hs index 9f61534..af6e18a 100644 --- a/lib/Hakyll/Web/CompressCss.hs +++ b/lib/Hakyll/Web/CompressCss.hs @@ -1,6 +1,7 @@ -------------------------------------------------------------------------------- -- | Module used for CSS compression. The compression is currently in a simple -- state, but would typically reduce the number of bytes by about 25%. +{-# LANGUAGE PatternGuards #-} module Hakyll.Web.CompressCss ( compressCssCompiler , compressCss @@ -34,6 +35,8 @@ compressSeparators :: String -> String compressSeparators [] = [] compressSeparators str | isConstant = head str : retainConstants compressSeparators (head str) (drop 1 str) + | isPrefixOf "calc( " str = "calc(" ++ compressCalcSeparators 1 (drop 6 str) + | isPrefixOf "calc(" str = "calc(" ++ compressCalcSeparators 1 (drop 5 str) | stripFirst = compressSeparators (drop 1 str) | stripSecond = compressSeparators (head str : (drop 2 str)) | otherwise = head str : compressSeparators (drop 1 str) @@ -43,6 +46,21 @@ compressSeparators str stripSecond = or $ map (isOfPrefix str) $ map (\c -> c ++ " ") separators separators = [" ", "{", "}", ":", ";", ",", ">", "+", "!"] +-- | Compresses separators when starting inside calc(). +compressCalcSeparators :: Int -> String -> String +compressCalcSeparators 0 str = compressSeparators str +compressCalcSeparators depth str + | stripFirst = compressCalcSeparators depth (tail str) + | stripSecond = compressCalcSeparators depth (head str : (drop 2 str)) + | ('(' : xs) <- str = '(' : compressCalcSeparators (depth + 1) xs + | isPrefixOf "calc( " str = compressCalcSeparators depth ("calc(" ++ (drop 6 str)) + | isPrefixOf "calc(" str = '(' : compressCalcSeparators (depth + 1) (drop 5 str) + | (')' : xs) <- str = ')' : compressCalcSeparators (depth - 1) xs + | otherwise = head str : compressCalcSeparators depth (tail str) + where + stripFirst = or $ map (isOfPrefix str) $ map (\c -> " " ++ c) ["*", "/", ")"] + stripSecond = or $ map (isOfPrefix str) $ map (\c -> c ++ " ") ["*", "/", "("] + -------------------------------------------------------------------------------- -- | Compresses all whitespace. compressWhitespace :: String -> String diff --git a/tests/Hakyll/Web/CompressCss/Tests.hs b/tests/Hakyll/Web/CompressCss/Tests.hs index 7420e4c..bf51ee2 100644 --- a/tests/Hakyll/Web/CompressCss/Tests.hs +++ b/tests/Hakyll/Web/CompressCss/Tests.hs @@ -42,6 +42,8 @@ tests = testGroup "Hakyll.Web.CompressCss.Tests" $ concat , "a>b" @=? compressCss "a > b" , "a+b" @=? compressCss "a + b" , "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 whitespace even after this curly brace , "}" @=? compressCss "; } " -- but do not compress separators inside of constants |