From c58641a1700a928dca9e94e7426904f315649c0a Mon Sep 17 00:00:00 2001 From: Krzysztof Jurewicz Date: Fri, 17 Nov 2017 15:56:47 +0100 Subject: Fix compression of calc() in CSS MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit According to Mozilla Developer Network, “The + and - operators must always be surrounded by whitespace.”. --- lib/Hakyll/Web/CompressCss.hs | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) (limited to 'lib') 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 -- cgit v1.2.3