on master help sign in
Some small CSS compression improvements
40945cdNicole Rauch
src/Hakyll/Web/CompressCss.hs+7−6
@@ -33,14 +33,15 @@ compressCss = compressSeparators . stripComments . compressWhitespace
33compressSeparators :: String -> String
33compressSeparators :: String -> String
34compressSeparators [] = []
34compressSeparators [] = []
35compressSeparators str
35compressSeparators str
36− | isConstant = head str : retainConstants compressSeparators (head str) (drop 1 str)
36+ | isConstant = head str : retainConstants compressSeparators (head str) (drop 1 str)
37 | stripFirst = compressSeparators (drop 1 str)
37 | stripFirst = compressSeparators (drop 1 str)
38 | stripSecond = compressSeparators (head str : (drop 2 str))
38 | stripSecond = compressSeparators (head str : (drop 2 str))
39 | otherwise = head str : compressSeparators (drop 1 str)
39 | otherwise = head str : compressSeparators (drop 1 str)
40 where
40 where
41 isConstant = or $ map (isOfPrefix str) ["\"", "'"]
41 isConstant = or $ map (isOfPrefix str) ["\"", "'"]
42− stripFirst = or $ map (isOfPrefix str) [" ", " {", " }", " :", ";;", ";}"]
42+ stripFirst = or $ map (isOfPrefix str) $ [";;", ";}"] ++ (map (\c -> " " ++ c) separators)
43− stripSecond = or $ map (isOfPrefix str) ["{ ", "} ", ": ", "; "]
43+ stripSecond = or $ map (isOfPrefix str) $ map (\c -> c ++ " ") separators
44+ separators = [" ", "{", "}", ":", ";", ",", ">", "+", "!"]
44
45
45--------------------------------------------------------------------------------
46--------------------------------------------------------------------------------
46-- | Compresses all whitespace.
47-- | Compresses all whitespace.
@@ -50,7 +51,7 @@ compressWhitespace str
50 | isConstant = head str : retainConstants compressWhitespace (head str) (drop 1 str)
51 | isConstant = head str : retainConstants compressWhitespace (head str) (drop 1 str)
51 | replaceOne = compressWhitespace (' ' : (drop 1 str))
52 | replaceOne = compressWhitespace (' ' : (drop 1 str))
52 | replaceTwo = compressWhitespace (' ' : (drop 2 str))
53 | replaceTwo = compressWhitespace (' ' : (drop 2 str))
53− | otherwise = head str : compressWhitespace (drop 1 str)
54+ | otherwise = head str : compressWhitespace (drop 1 str)
54 where
55 where
55 isConstant = or $ map (isOfPrefix str) ["\"", "'"]
56 isConstant = or $ map (isOfPrefix str) ["\"", "'"]
56 replaceOne = or $ map (isOfPrefix str) ["\t", "\n", "\r"]
57 replaceOne = or $ map (isOfPrefix str) ["\t", "\n", "\r"]
@@ -61,9 +62,9 @@ compressWhitespace str
61stripComments :: String -> String
62stripComments :: String -> String
62stripComments [] = []
63stripComments [] = []
63stripComments str
64stripComments str
64− | isConstant = head str : retainConstants stripComments (head str) (drop 1 str)
65+ | isConstant = head str : retainConstants stripComments (head str) (drop 1 str)
65 | isPrefixOf "/*" str = stripComments $ eatComments $ drop 2 str
66 | isPrefixOf "/*" str = stripComments $ eatComments $ drop 2 str
66− | otherwise = head str : stripComments (drop 1 str)
67+ | otherwise = head str : stripComments (drop 1 str)
67 where
68 where
68 isConstant = or $ map (isOfPrefix str) ["\"", "'"]
69 isConstant = or $ map (isOfPrefix str) ["\"", "'"]
69 eatComments str'
70 eatComments str'
tests/Hakyll/Web/CompressCss/Tests.hs+7−3
@@ -37,11 +37,15 @@ tests = testGroup "Hakyll.Web.CompressCss.Tests" $ concat
37
37
38 -- compress separators
38 -- compress separators
39 , "}" @=? compressCss "; }"
39 , "}" @=? 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"
41 -- compress whitespace even after this curly brace
45 -- compress whitespace even after this curly brace
42 , "}" @=? compressCss "; } "
46 , "}" @=? compressCss "; } "
43 -- but do not compress separators inside of constants
47 -- but do not compress separators inside of constants
44− , "\" { } ; \"" @=? compressCss "\" { } ; \""
48+ , "\" { } ; , \"" @=? compressCss "\" { } ; , \""
45 -- don't compress separators at the start or end of constants
49 -- don't compress separators at the start or end of constants
46 , "\" }\"" @=? compressCss "\" }\""
50 , "\" }\"" @=? compressCss "\" }\""
47 , "\"{ \"" @=? compressCss "\"{ \""
51 , "\"{ \"" @=? compressCss "\"{ \""
@@ -51,7 +55,7 @@ tests = testGroup "Hakyll.Web.CompressCss.Tests" $ concat
51 -- don't compress whitespace around separators in constants in the middle of a string
55 -- don't compress whitespace around separators in constants in the middle of a string
52 , "abc '{ '" @=? compressCss "abc '{ '"
56 , "abc '{ '" @=? compressCss "abc '{ '"
53 , "abc \"{ \"" @=? compressCss "abc \"{ \""
57 , "abc \"{ \"" @=? compressCss "abc \"{ \""
54− -- compress whitespace after colons
58+ -- compress whitespace around colons
55 , "abc:xyz" @=? compressCss "abc : xyz"
59 , "abc:xyz" @=? compressCss "abc : xyz"
56 -- compress multiple semicolons
60 -- compress multiple semicolons
57 , ";" @=? compressCss ";;;;;;;"
61 , ";" @=? compressCss ";;;;;;;"