summaryrefslogtreecommitdiff
path: root/lib
diff options
context:
space:
mode:
authorKrzysztof Jurewicz <krzysztof.jurewicz@gmail.com>2017-11-17 15:56:47 +0100
committerJasper Van der Jeugt <jaspervdj@gmail.com>2017-11-17 15:56:47 +0100
commitc58641a1700a928dca9e94e7426904f315649c0a (patch)
treead392b2d8ffbf458829f3db97105ecb781e7a4c8 /lib
parent7ee4e6134cc447c8b053129e7557d9883c7e8e01 (diff)
downloadhakyll-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')
-rw-r--r--lib/Hakyll/Web/CompressCss.hs18
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