diff options
-rw-r--r-- | src/Hakyll/Web/CompressCss.hs | 28 | ||||
-rw-r--r-- | tests/Hakyll/Web/CompressCss/Tests.hs | 3 |
2 files changed, 25 insertions, 6 deletions
diff --git a/src/Hakyll/Web/CompressCss.hs b/src/Hakyll/Web/CompressCss.hs index 58d52b4..ce7239f 100644 --- a/src/Hakyll/Web/CompressCss.hs +++ b/src/Hakyll/Web/CompressCss.hs @@ -33,16 +33,24 @@ compressCss = compressSeparators . stripComments . compressWhitespace -------------------------------------------------------------------------------- -- | Compresses certain forms of separators. compressSeparators :: String -> String -compressSeparators = - replaceAll "; *}" (const "}") . - replaceAll " *([{};]) *" (take 1 . dropWhile isSpace) . - replaceAll ";+" (const ";") - +compressSeparators [] = [] +compressSeparators str + | isPrefixOf "\"" str = head str : retainConstants compressSeparators "\"" (drop 1 str) + | isPrefixOf "'" str = head str : retainConstants compressSeparators "'" (drop 1 str) + | otherwise = + replaceAll "; *}" (const "}") $ + replaceAll " *([{};]) *" (take 1 . dropWhile isSpace) $ + replaceAll ";+" (const ";") str + where -------------------------------------------------------------------------------- -- | Compresses all whitespace. compressWhitespace :: String -> String -compressWhitespace = replaceAll "[ \t\n\r]+" (const " ") +compressWhitespace [] = [] +compressWhitespace str + | isPrefixOf "\"" str = head str : retainConstants compressWhitespace "\"" (drop 1 str) + | isPrefixOf "'" str = head str : retainConstants compressWhitespace "'" (drop 1 str) + | otherwise = replaceAll "[ \t\n\r]+" (const " ") str -------------------------------------------------------------------------------- @@ -57,3 +65,11 @@ stripComments str | null str' = [] | isPrefixOf "*/" str' = drop 2 str' | otherwise = eatComments $ drop 1 str' + +-------------------------------------------------------------------------------- +-- | Helper function to handle string constants correctly. +retainConstants :: (String -> String) -> String -> String -> String +retainConstants f delim str + | null str = [] + | isPrefixOf delim str = head str : f (drop 1 str) + | otherwise = head str : retainConstants f delim (drop 1 str) diff --git a/tests/Hakyll/Web/CompressCss/Tests.hs b/tests/Hakyll/Web/CompressCss/Tests.hs index 1430abf..08c646d 100644 --- a/tests/Hakyll/Web/CompressCss/Tests.hs +++ b/tests/Hakyll/Web/CompressCss/Tests.hs @@ -33,6 +33,9 @@ tests = testGroup "Hakyll.Web.CompressCss.Tests" $ concat compressCss "; }" , "{};" @=? compressCss " { } ; " + -- but do not compress separators inside of constants + , "\" { } ; \"" @=? + compressCss "\" { } ; \"" , ";" @=? compressCss ";;;;;;;" |