on master help sign in
Merge pull request #463 from NicoleRauch/master

Bugfix: CompressCss modified CSS string constants which it should not do

commit9d4640b76431be46847f28ea6d167bce83500e03 authorJasper Van der Jeugt <jaspervdj@gmail.com> dateSun 16 Oct 2016, 10:41 (10 years ago) parent32e34f49f850a9
4 files changed+97−9
hakyll.cabal+1 src/Hakyll/Web/CompressCss.hs+35−9 tests/Hakyll/Web/CompressCss/Tests.hs+59 tests/TestSuite.hs+2
hakyll.cabal+1
@@ -223,6 +223,7 @@ Test-suite hakyll-tests
223223 Hakyll.Core.Store.Tests
224224 Hakyll.Core.UnixFilter.Tests
225225 Hakyll.Core.Util.String.Tests
226+ Hakyll.Web.CompressCss.Tests
226227 Hakyll.Web.Html.RelativizeUrls.Tests
227228 Hakyll.Web.Html.Tests
228229 Hakyll.Web.Pandoc.FileType.Tests
src/Hakyll/Web/CompressCss.hs+35−9
@@ -8,14 +8,12 @@ module Hakyll.Web.CompressCss
88
99
1010--------------------------------------------------------------------------------
11−import Data.Char (isSpace)
1211import Data.List (isPrefixOf)
1312
1413
1514--------------------------------------------------------------------------------
1615import Hakyll.Core.Compiler
1716import Hakyll.Core.Item
18−import Hakyll.Core.Util.String
1917
2018
2119--------------------------------------------------------------------------------
@@ -33,27 +31,55 @@ compressCss = compressSeparators . stripComments . compressWhitespace
3331--------------------------------------------------------------------------------
3432-- | Compresses certain forms of separators.
3533compressSeparators :: String -> String
36−compressSeparators =
37− replaceAll "; *}" (const "}") .
38− replaceAll " *([{};]) *" (take 1 . dropWhile isSpace) .
39− replaceAll ";+" (const ";")
40−
34+compressSeparators [] = []
35+compressSeparators str
36+ | isConstant = head str : retainConstants compressSeparators (head str) (drop 1 str)
37+ | stripFirst = compressSeparators (drop 1 str)
38+ | stripSecond = compressSeparators (head str : (drop 2 str))
39+ | otherwise = head str : compressSeparators (drop 1 str)
40+ where
41+ isConstant = or $ map (isOfPrefix str) ["\"", "'"]
42+ stripFirst = or $ map (isOfPrefix str) [" ", " {", " }", " :", ";;", ";}"]
43+ stripSecond = or $ map (isOfPrefix str) ["{ ", "} ", ": ", "; "]
4144
4245--------------------------------------------------------------------------------
4346-- | Compresses all whitespace.
4447compressWhitespace :: String -> String
45−compressWhitespace = replaceAll "[ \t\n\r]+" (const " ")
46−
48+compressWhitespace [] = []
49+compressWhitespace str
50+ | isConstant = head str : retainConstants compressWhitespace (head str) (drop 1 str)
51+ | replaceOne = compressWhitespace (' ' : (drop 1 str))
52+ | replaceTwo = compressWhitespace (' ' : (drop 2 str))
53+ | otherwise = head str : compressWhitespace (drop 1 str)
54+ where
55+ isConstant = or $ map (isOfPrefix str) ["\"", "'"]
56+ replaceOne = or $ map (isOfPrefix str) ["\t", "\n", "\r"]
57+ replaceTwo = or $ map (isOfPrefix str) [" \t", " \n", " \r", " "]
4758
4859--------------------------------------------------------------------------------
4960-- | Function that strips CSS comments away.
5061stripComments :: String -> String
5162stripComments [] = []
5263stripComments str
64+ | isConstant = head str : retainConstants stripComments (head str) (drop 1 str)
5365 | isPrefixOf "/*" str = stripComments $ eatComments $ drop 2 str
5466 | otherwise = head str : stripComments (drop 1 str)
5567 where
68+ isConstant = or $ map (isOfPrefix str) ["\"", "'"]
5669 eatComments str'
5770 | null str' = []
5871 | isPrefixOf "*/" str' = drop 2 str'
5972 | otherwise = eatComments $ drop 1 str'
73+
74+--------------------------------------------------------------------------------
75+-- | Helper function to handle string constants correctly.
76+retainConstants :: (String -> String) -> Char -> String -> String
77+retainConstants f delim str
78+ | null str = []
79+ | isPrefixOf [delim] str = head str : f (drop 1 str)
80+ | otherwise = head str : retainConstants f delim (drop 1 str)
81+
82+--------------------------------------------------------------------------------
83+-- | Helper function to determine whether a string is a substring.
84+isOfPrefix :: String -> String -> Bool
85+isOfPrefix = flip isPrefixOf
tests/Hakyll/Web/CompressCss/Tests.hsnew file+59
@@ -0,0 +1,59 @@
1+--------------------------------------------------------------------------------
2+module Hakyll.Web.CompressCss.Tests
3+ ( tests
4+ ) where
5+
6+
7+--------------------------------------------------------------------------------
8+import Data.Char (toUpper)
9+import Test.Framework (Test, testGroup)
10+import Test.HUnit (assert, (@=?))
11+
12+
13+--------------------------------------------------------------------------------
14+import Hakyll.Web.CompressCss
15+import TestSuite.Util
16+
17+
18+--------------------------------------------------------------------------------
19+tests :: Test
20+tests = testGroup "Hakyll.Web.CompressCss.Tests" $ concat
21+ [ fromAssertions "compressCss"
22+ [
23+ -- compress whitespace
24+ " something something " @=?
25+ compressCss " something \n\t\r something "
26+ -- do not compress whitespace in constants
27+ , "abc \" \t\n\r \" xyz" @=?
28+ compressCss "abc \" \t\n\r \" xyz"
29+ , "abc ' \t\n\r ' xyz" @=?
30+ compressCss "abc ' \t\n\r ' xyz"
31+
32+ -- strip comments
33+ , "before after" @=? compressCss "before /* abc { } ;; \n\t\r */ after"
34+ -- don't strip comments inside constants
35+ , "before \"/* abc { } ;; \n\t\r */\" after"
36+ @=? compressCss "before \"/* abc { } ;; \n\t\r */\" after"
37+
38+ -- compress separators
39+ , "}" @=? compressCss "; }"
40+ , "{};" @=? compressCss " { } ; "
41+ -- compress whitespace even after this curly brace
42+ , "}" @=? compressCss "; } "
43+ -- but do not compress separators inside of constants
44+ , "\" { } ; \"" @=? compressCss "\" { } ; \""
45+ -- don't compress separators at the start or end of constants
46+ , "\" }\"" @=? compressCss "\" }\""
47+ , "\"{ \"" @=? compressCss "\"{ \""
48+ -- don't get irritated by the wrong constant terminator
49+ , "\" ' \"" @=? compressCss "\" ' \""
50+ , "' \" '" @=? compressCss "' \" '"
51+ -- don't compress whitespace around separators in constants in the middle of a string
52+ , "abc '{ '" @=? compressCss "abc '{ '"
53+ , "abc \"{ \"" @=? compressCss "abc \"{ \""
54+ -- compress whitespace after colons
55+ , "abc:xyz" @=? compressCss "abc : xyz"
56+ -- compress multiple semicolons
57+ , ";" @=? compressCss ";;;;;;;"
58+ ]
59+ ]
tests/TestSuite.hs+2
@@ -19,6 +19,7 @@ import qualified Hakyll.Core.Runtime.Tests
1919import qualified Hakyll.Core.Store.Tests
2020import qualified Hakyll.Core.UnixFilter.Tests
2121import qualified Hakyll.Core.Util.String.Tests
22+import qualified Hakyll.Web.CompressCss.Tests
2223import qualified Hakyll.Web.Html.RelativizeUrls.Tests
2324import qualified Hakyll.Web.Html.Tests
2425import qualified Hakyll.Web.Pandoc.FileType.Tests
@@ -39,6 +40,7 @@ main = defaultMain
3940 , Hakyll.Core.Store.Tests.tests
4041 , Hakyll.Core.UnixFilter.Tests.tests
4142 , Hakyll.Core.Util.String.Tests.tests
43+ , Hakyll.Web.CompressCss.Tests.tests
4244 , Hakyll.Web.Html.RelativizeUrls.Tests.tests
4345 , Hakyll.Web.Html.Tests.tests
4446 , Hakyll.Web.Pandoc.FileType.Tests.tests