diff options
author | Krzysztof Jurewicz <krzysztof.jurewicz@gmail.com> | 2017-11-17 15:56:47 +0100 |
---|---|---|
committer | Jasper Van der Jeugt <jaspervdj@gmail.com> | 2017-11-17 15:56:47 +0100 |
commit | c58641a1700a928dca9e94e7426904f315649c0a (patch) | |
tree | ad392b2d8ffbf458829f3db97105ecb781e7a4c8 /lib/Hakyll | |
parent | 7ee4e6134cc447c8b053129e7557d9883c7e8e01 (diff) | |
download | hakyll-c58641a1700a928dca9e94e7426904f315649c0a.tar.gz |
Fix compression of calc() in CSS
According to Mozilla Developer Network, “The + and - operators must
always be surrounded by whitespace.”.
Diffstat (limited to 'lib/Hakyll')
-rw-r--r-- | lib/Hakyll/Web/CompressCss.hs | 18 |
1 files changed, 18 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 |