on master help sign in
Some small CSS compression improvements

* We now also compress whitespace around commas. * Remove whitespace around some other operators as well. * Clarified the code around the separator compression and even improved the code (one case slipped through unnoticed).

commit40945cd717b044d6d13c8b46050447d0fca61974 authorNicole Rauch <nicole.rauch@me.com> dateSat 10 Jun 2017, 16:32 (9 years ago) parentae6e241
2 files changed+14−9
src/Hakyll/Web/CompressCss.hs+7−6 tests/Hakyll/Web/CompressCss/Tests.hs+7−3
src/Hakyll/Web/CompressCss.hs+7−6
@@ -33,14 +33,15 @@ compressCss = compressSeparators . stripComments . compressWhitespace
3333compressSeparators :: String -> String
3434compressSeparators [] = []
3535compressSeparators str
36− | isConstant = head str : retainConstants compressSeparators (head str) (drop 1 str)
36+ | isConstant = head str : retainConstants compressSeparators (head str) (drop 1 str)
3737 | stripFirst = compressSeparators (drop 1 str)
3838 | stripSecond = compressSeparators (head str : (drop 2 str))
3939 | otherwise = head str : compressSeparators (drop 1 str)
4040 where
4141 isConstant = or $ map (isOfPrefix str) ["\"", "'"]
42− stripFirst = or $ map (isOfPrefix str) [" ", " {", " }", " :", ";;", ";}"]
43− stripSecond = or $ map (isOfPrefix str) ["{ ", "} ", ": ", "; "]
42+ stripFirst = or $ map (isOfPrefix str) $ [";;", ";}"] ++ (map (\c -> " " ++ c) separators)
43+ stripSecond = or $ map (isOfPrefix str) $ map (\c -> c ++ " ") separators
44+ separators = [" ", "{", "}", ":", ";", ",", ">", "+", "!"]
4445
4546--------------------------------------------------------------------------------
4647-- | Compresses all whitespace.
@@ -50,7 +51,7 @@ compressWhitespace str
5051 | isConstant = head str : retainConstants compressWhitespace (head str) (drop 1 str)
5152 | replaceOne = compressWhitespace (' ' : (drop 1 str))
5253 | replaceTwo = compressWhitespace (' ' : (drop 2 str))
53− | otherwise = head str : compressWhitespace (drop 1 str)
54+ | otherwise = head str : compressWhitespace (drop 1 str)
5455 where
5556 isConstant = or $ map (isOfPrefix str) ["\"", "'"]
5657 replaceOne = or $ map (isOfPrefix str) ["\t", "\n", "\r"]
@@ -61,9 +62,9 @@ compressWhitespace str
6162stripComments :: String -> String
6263stripComments [] = []
6364stripComments str
64− | isConstant = head str : retainConstants stripComments (head str) (drop 1 str)
65+ | isConstant = head str : retainConstants stripComments (head str) (drop 1 str)
6566 | isPrefixOf "/*" str = stripComments $ eatComments $ drop 2 str
66− | otherwise = head str : stripComments (drop 1 str)
67+ | otherwise = head str : stripComments (drop 1 str)
6768 where
6869 isConstant = or $ map (isOfPrefix str) ["\"", "'"]
6970 eatComments str'
tests/Hakyll/Web/CompressCss/Tests.hs+7−3
@@ -37,11 +37,15 @@ tests = testGroup "Hakyll.Web.CompressCss.Tests" $ concat
3737
3838 -- compress separators
3939 , "}" @=? compressCss "; }"
40− , "{};" @=? compressCss " { } ; "
40+ , ";{};" @=? compressCss " ; { } ; "
41+ , "text," @=? compressCss "text , "
42+ , "a>b" @=? compressCss "a > b"
43+ , "a+b" @=? compressCss "a + b"
44+ , "a!b" @=? compressCss "a ! b"
4145 -- compress whitespace even after this curly brace
4246 , "}" @=? compressCss "; } "
4347 -- but do not compress separators inside of constants
44− , "\" { } ; \"" @=? compressCss "\" { } ; \""
48+ , "\" { } ; , \"" @=? compressCss "\" { } ; , \""
4549 -- don't compress separators at the start or end of constants
4650 , "\" }\"" @=? compressCss "\" }\""
4751 , "\"{ \"" @=? compressCss "\"{ \""
@@ -51,7 +55,7 @@ tests = testGroup "Hakyll.Web.CompressCss.Tests" $ concat
5155 -- don't compress whitespace around separators in constants in the middle of a string
5256 , "abc '{ '" @=? compressCss "abc '{ '"
5357 , "abc \"{ \"" @=? compressCss "abc \"{ \""
54− -- compress whitespace after colons
58+ -- compress whitespace around colons
5559 , "abc:xyz" @=? compressCss "abc : xyz"
5660 -- compress multiple semicolons
5761 , ";" @=? compressCss ";;;;;;;"