From 66a90395fc1174d05254876d11698f4de5a895c2 Mon Sep 17 00:00:00 2001 From: Nicole Rauch Date: Sat, 13 Aug 2016 22:11:56 +0200 Subject: Added some unit tests for CompressCss --- hakyll.cabal | 1 + tests/Hakyll/Web/CompressCss/Tests.hs | 43 +++++++++++++++++++++++++++++++++++ tests/TestSuite.hs | 2 ++ 3 files changed, 46 insertions(+) create mode 100644 tests/Hakyll/Web/CompressCss/Tests.hs diff --git a/hakyll.cabal b/hakyll.cabal index 4966d72..ba99d5f 100644 --- a/hakyll.cabal +++ b/hakyll.cabal @@ -223,6 +223,7 @@ Test-suite hakyll-tests Hakyll.Core.Store.Tests Hakyll.Core.UnixFilter.Tests Hakyll.Core.Util.String.Tests + Hakyll.Web.CompressCss.Tests Hakyll.Web.Html.RelativizeUrls.Tests Hakyll.Web.Html.Tests Hakyll.Web.Pandoc.FileType.Tests diff --git a/tests/Hakyll/Web/CompressCss/Tests.hs b/tests/Hakyll/Web/CompressCss/Tests.hs new file mode 100644 index 0000000..1430abf --- /dev/null +++ b/tests/Hakyll/Web/CompressCss/Tests.hs @@ -0,0 +1,43 @@ +-------------------------------------------------------------------------------- +module Hakyll.Web.CompressCss.Tests + ( tests + ) where + + +-------------------------------------------------------------------------------- +import Data.Char (toUpper) +import Test.Framework (Test, testGroup) +import Test.HUnit (assert, (@=?)) + + +-------------------------------------------------------------------------------- +import Hakyll.Web.CompressCss +import TestSuite.Util + + +-------------------------------------------------------------------------------- +tests :: Test +tests = testGroup "Hakyll.Web.CompressCss.Tests" $ concat + [ fromAssertions "compressCss" + [ + -- compress whitespace + " something something " @=? + compressCss " something \n\t\r something " + + -- strip comments + , "" @=? + compressCss "/* abc { } ;; \n\t\r */" + + -- compress separators + , "}" @=? + compressCss "; }" + , "{};" @=? + compressCss " { } ; " + , ";" @=? + compressCss ";;;;;;;" + + -- some real-life css + , "a:after{content: \" (\" attr(href) \")\"}" @=? + compressCss "a:after { content: \" (\" attr(href) \")\"; }" + ] + ] diff --git a/tests/TestSuite.hs b/tests/TestSuite.hs index 3622301..79eb314 100644 --- a/tests/TestSuite.hs +++ b/tests/TestSuite.hs @@ -19,6 +19,7 @@ import qualified Hakyll.Core.Runtime.Tests import qualified Hakyll.Core.Store.Tests import qualified Hakyll.Core.UnixFilter.Tests import qualified Hakyll.Core.Util.String.Tests +import qualified Hakyll.Web.CompressCss.Tests import qualified Hakyll.Web.Html.RelativizeUrls.Tests import qualified Hakyll.Web.Html.Tests import qualified Hakyll.Web.Pandoc.FileType.Tests @@ -39,6 +40,7 @@ main = defaultMain , Hakyll.Core.Store.Tests.tests , Hakyll.Core.UnixFilter.Tests.tests , Hakyll.Core.Util.String.Tests.tests + , Hakyll.Web.CompressCss.Tests.tests , Hakyll.Web.Html.RelativizeUrls.Tests.tests , Hakyll.Web.Html.Tests.tests , Hakyll.Web.Pandoc.FileType.Tests.tests -- cgit v1.2.3 From eaf21c273d572e7300b6378f1279fb4695ab691d Mon Sep 17 00:00:00 2001 From: Nicole Rauch Date: Sat, 13 Aug 2016 22:51:06 +0200 Subject: Whitespace in certain kinds of string constants is no longer eliminated. --- src/Hakyll/Web/CompressCss.hs | 28 ++++++++++++++++++++++------ 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 ";;;;;;;" -- cgit v1.2.3 From 84818cb94aef6f7e3dc1a4b7abcd0e703508ea67 Mon Sep 17 00:00:00 2001 From: Nicole Rauch Date: Sun, 14 Aug 2016 17:02:01 +0200 Subject: Adding tests that make sure that the constant terminators don't get mixed up. --- tests/Hakyll/Web/CompressCss/Tests.hs | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/tests/Hakyll/Web/CompressCss/Tests.hs b/tests/Hakyll/Web/CompressCss/Tests.hs index 08c646d..cf90a34 100644 --- a/tests/Hakyll/Web/CompressCss/Tests.hs +++ b/tests/Hakyll/Web/CompressCss/Tests.hs @@ -36,6 +36,11 @@ tests = testGroup "Hakyll.Web.CompressCss.Tests" $ concat -- but do not compress separators inside of constants , "\" { } ; \"" @=? compressCss "\" { } ; \"" + -- don't get irritated by the wrong constant terminator + , "\" ' \"" @=? + compressCss "\" ' \"" + , "' \" '" @=? + compressCss "' \" '" , ";" @=? compressCss ";;;;;;;" -- cgit v1.2.3 From c0a86b3d7e759fa060e091df0a3ae7d0df6354ce Mon Sep 17 00:00:00 2001 From: Nicole Rauch Date: Sun, 14 Aug 2016 17:02:49 +0200 Subject: Adding tests that make sure that whitespace at the start or end of constants does not get eaten. --- tests/Hakyll/Web/CompressCss/Tests.hs | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/tests/Hakyll/Web/CompressCss/Tests.hs b/tests/Hakyll/Web/CompressCss/Tests.hs index cf90a34..c8db116 100644 --- a/tests/Hakyll/Web/CompressCss/Tests.hs +++ b/tests/Hakyll/Web/CompressCss/Tests.hs @@ -36,6 +36,11 @@ tests = testGroup "Hakyll.Web.CompressCss.Tests" $ concat -- but do not compress separators inside of constants , "\" { } ; \"" @=? compressCss "\" { } ; \"" + -- don't compress separators at the start or end of constants + , "\" }\"" @=? + compressCss "\" }\"" + , "\"{ \"" @=? + compressCss "\"{ \"" -- don't get irritated by the wrong constant terminator , "\" ' \"" @=? compressCss "\" ' \"" -- cgit v1.2.3 From 0404185e835fb11bec70882b695df7b53c3dd451 Mon Sep 17 00:00:00 2001 From: Nicole Rauch Date: Sun, 14 Aug 2016 19:23:10 +0200 Subject: We need to constantly check whether we are at the start of a constant, not just once. --- src/Hakyll/Web/CompressCss.hs | 16 +++++++++++----- tests/Hakyll/Web/CompressCss/Tests.hs | 10 ++++++---- 2 files changed, 17 insertions(+), 9 deletions(-) diff --git a/src/Hakyll/Web/CompressCss.hs b/src/Hakyll/Web/CompressCss.hs index ce7239f..c00b5ee 100644 --- a/src/Hakyll/Web/CompressCss.hs +++ b/src/Hakyll/Web/CompressCss.hs @@ -37,11 +37,17 @@ 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 + | isPrefixOf " " str = compressSeparators (drop 1 str) + | isPrefixOf " {" str = compressSeparators (drop 1 str) + | isPrefixOf " }" str = compressSeparators (drop 1 str) + | isPrefixOf " ;" str = compressSeparators (drop 1 str) + | isPrefixOf ";;" str = compressSeparators (drop 1 str) + | isPrefixOf "{ " str = compressSeparators (head str : (drop 2 str)) + | isPrefixOf "} " str = compressSeparators (head str : (drop 2 str)) + | isPrefixOf "; " str = compressSeparators (head str : (drop 2 str)) + | isPrefixOf ";}" str = '}' : compressSeparators (drop 2 str) + | otherwise = head str : compressSeparators (drop 1 str) + -------------------------------------------------------------------------------- -- | Compresses all whitespace. diff --git a/tests/Hakyll/Web/CompressCss/Tests.hs b/tests/Hakyll/Web/CompressCss/Tests.hs index c8db116..44dfc6c 100644 --- a/tests/Hakyll/Web/CompressCss/Tests.hs +++ b/tests/Hakyll/Web/CompressCss/Tests.hs @@ -46,11 +46,13 @@ tests = testGroup "Hakyll.Web.CompressCss.Tests" $ concat compressCss "\" ' \"" , "' \" '" @=? compressCss "' \" '" + -- don't compress whitespace in constants in the middle of a string + , "abc '{ '" @=? + compressCss "abc '{ '" + , "abc \"{ \"" @=? + compressCss "abc \"{ \"" + -- compress multiple semicolons , ";" @=? compressCss ";;;;;;;" - - -- some real-life css - , "a:after{content: \" (\" attr(href) \")\"}" @=? - compressCss "a:after { content: \" (\" attr(href) \")\"; }" ] ] -- cgit v1.2.3 From c6fdffc0a06c9185a062b1e4f9fb1088417754b5 Mon Sep 17 00:00:00 2001 From: Nicole Rauch Date: Sun, 14 Aug 2016 19:34:52 +0200 Subject: Compacted the tests and made them more readable. --- tests/Hakyll/Web/CompressCss/Tests.hs | 33 +++++++++++---------------------- 1 file changed, 11 insertions(+), 22 deletions(-) diff --git a/tests/Hakyll/Web/CompressCss/Tests.hs b/tests/Hakyll/Web/CompressCss/Tests.hs index 44dfc6c..a7b94ab 100644 --- a/tests/Hakyll/Web/CompressCss/Tests.hs +++ b/tests/Hakyll/Web/CompressCss/Tests.hs @@ -25,34 +25,23 @@ tests = testGroup "Hakyll.Web.CompressCss.Tests" $ concat compressCss " something \n\t\r something " -- strip comments - , "" @=? - compressCss "/* abc { } ;; \n\t\r */" + , "" @=? compressCss "/* abc { } ;; \n\t\r */" -- compress separators - , "}" @=? - compressCss "; }" - , "{};" @=? - compressCss " { } ; " + , "}" @=? compressCss "; }" + , "{};" @=? compressCss " { } ; " -- but do not compress separators inside of constants - , "\" { } ; \"" @=? - compressCss "\" { } ; \"" + , "\" { } ; \"" @=? compressCss "\" { } ; \"" -- don't compress separators at the start or end of constants - , "\" }\"" @=? - compressCss "\" }\"" - , "\"{ \"" @=? - compressCss "\"{ \"" + , "\" }\"" @=? compressCss "\" }\"" + , "\"{ \"" @=? compressCss "\"{ \"" -- don't get irritated by the wrong constant terminator - , "\" ' \"" @=? - compressCss "\" ' \"" - , "' \" '" @=? - compressCss "' \" '" + , "\" ' \"" @=? compressCss "\" ' \"" + , "' \" '" @=? compressCss "' \" '" -- don't compress whitespace in constants in the middle of a string - , "abc '{ '" @=? - compressCss "abc '{ '" - , "abc \"{ \"" @=? - compressCss "abc \"{ \"" + , "abc '{ '" @=? compressCss "abc '{ '" + , "abc \"{ \"" @=? compressCss "abc \"{ \"" -- compress multiple semicolons - , ";" @=? - compressCss ";;;;;;;" + , ";" @=? compressCss ";;;;;;;" ] ] -- cgit v1.2.3 From ec1486c4ba18452da4a5948477c1f8fe02c46d8f Mon Sep 17 00:00:00 2001 From: Nicole Rauch Date: Sun, 14 Aug 2016 19:35:43 +0200 Subject: Compacted the code by unifying all the similar prefix options. --- src/Hakyll/Web/CompressCss.hs | 15 ++++++--------- 1 file changed, 6 insertions(+), 9 deletions(-) diff --git a/src/Hakyll/Web/CompressCss.hs b/src/Hakyll/Web/CompressCss.hs index c00b5ee..aac48b1 100644 --- a/src/Hakyll/Web/CompressCss.hs +++ b/src/Hakyll/Web/CompressCss.hs @@ -37,17 +37,14 @@ compressSeparators [] = [] compressSeparators str | isPrefixOf "\"" str = head str : retainConstants compressSeparators "\"" (drop 1 str) | isPrefixOf "'" str = head str : retainConstants compressSeparators "'" (drop 1 str) - | isPrefixOf " " str = compressSeparators (drop 1 str) - | isPrefixOf " {" str = compressSeparators (drop 1 str) - | isPrefixOf " }" str = compressSeparators (drop 1 str) - | isPrefixOf " ;" str = compressSeparators (drop 1 str) - | isPrefixOf ";;" str = compressSeparators (drop 1 str) - | isPrefixOf "{ " str = compressSeparators (head str : (drop 2 str)) - | isPrefixOf "} " str = compressSeparators (head str : (drop 2 str)) - | isPrefixOf "; " str = compressSeparators (head str : (drop 2 str)) + | stripFirst = compressSeparators (drop 1 str) + | stripSecond = compressSeparators (head str : (drop 2 str)) | isPrefixOf ";}" str = '}' : compressSeparators (drop 2 str) | otherwise = head str : compressSeparators (drop 1 str) - + where + prefix p = isPrefixOf p str + stripFirst = or $ map prefix [" ", " {", " }", ";;"] + stripSecond = or $ map prefix ["{ ", "} ", "; "] -------------------------------------------------------------------------------- -- | Compresses all whitespace. -- cgit v1.2.3 From 8f11bbd1d7cd81572ddc433aa1706bc2d2db4c8d Mon Sep 17 00:00:00 2001 From: Nicole Rauch Date: Sun, 14 Aug 2016 19:37:29 +0200 Subject: Improved the compaction a bit more. --- src/Hakyll/Web/CompressCss.hs | 3 +-- tests/Hakyll/Web/CompressCss/Tests.hs | 2 ++ 2 files changed, 3 insertions(+), 2 deletions(-) diff --git a/src/Hakyll/Web/CompressCss.hs b/src/Hakyll/Web/CompressCss.hs index aac48b1..23adce2 100644 --- a/src/Hakyll/Web/CompressCss.hs +++ b/src/Hakyll/Web/CompressCss.hs @@ -39,11 +39,10 @@ compressSeparators str | isPrefixOf "'" str = head str : retainConstants compressSeparators "'" (drop 1 str) | stripFirst = compressSeparators (drop 1 str) | stripSecond = compressSeparators (head str : (drop 2 str)) - | isPrefixOf ";}" str = '}' : compressSeparators (drop 2 str) | otherwise = head str : compressSeparators (drop 1 str) where prefix p = isPrefixOf p str - stripFirst = or $ map prefix [" ", " {", " }", ";;"] + stripFirst = or $ map prefix [" ", " {", " }", ";;", ";}"] stripSecond = or $ map prefix ["{ ", "} ", "; "] -------------------------------------------------------------------------------- diff --git a/tests/Hakyll/Web/CompressCss/Tests.hs b/tests/Hakyll/Web/CompressCss/Tests.hs index a7b94ab..221aa82 100644 --- a/tests/Hakyll/Web/CompressCss/Tests.hs +++ b/tests/Hakyll/Web/CompressCss/Tests.hs @@ -30,6 +30,8 @@ tests = testGroup "Hakyll.Web.CompressCss.Tests" $ concat -- compress separators , "}" @=? compressCss "; }" , "{};" @=? compressCss " { } ; " + -- compress whitespace even after this curly brace + , "}" @=? compressCss "; } " -- but do not compress separators inside of constants , "\" { } ; \"" @=? compressCss "\" { } ; \"" -- don't compress separators at the start or end of constants -- cgit v1.2.3 From e70605dfe681dbc0f79e0a8f426ac6c9fc9820a9 Mon Sep 17 00:00:00 2001 From: Nicole Rauch Date: Sun, 14 Aug 2016 19:51:45 +0200 Subject: We must avoid the compression of whitespace in constants by handling it in the same way. --- src/Hakyll/Web/CompressCss.hs | 10 +++++++++- tests/Hakyll/Web/CompressCss/Tests.hs | 7 ++++++- 2 files changed, 15 insertions(+), 2 deletions(-) diff --git a/src/Hakyll/Web/CompressCss.hs b/src/Hakyll/Web/CompressCss.hs index 23adce2..6667842 100644 --- a/src/Hakyll/Web/CompressCss.hs +++ b/src/Hakyll/Web/CompressCss.hs @@ -52,7 +52,15 @@ 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 + | isPrefixOf "\t" str = compressWhitespace (' ' : (drop 1 str)) + | isPrefixOf "\n" str = compressWhitespace (' ' : (drop 1 str)) + | isPrefixOf "\r" str = compressWhitespace (' ' : (drop 1 str)) + + | isPrefixOf " \t" str = compressWhitespace (' ' : (drop 2 str)) + | isPrefixOf " \n" str = compressWhitespace (' ' : (drop 2 str)) + | isPrefixOf " \r" str = compressWhitespace (' ' : (drop 2 str)) + | isPrefixOf " " str = compressWhitespace (' ' : (drop 2 str)) + | otherwise = head str : compressWhitespace (drop 1 str) -------------------------------------------------------------------------------- diff --git a/tests/Hakyll/Web/CompressCss/Tests.hs b/tests/Hakyll/Web/CompressCss/Tests.hs index 221aa82..065a588 100644 --- a/tests/Hakyll/Web/CompressCss/Tests.hs +++ b/tests/Hakyll/Web/CompressCss/Tests.hs @@ -23,6 +23,11 @@ tests = testGroup "Hakyll.Web.CompressCss.Tests" $ concat -- compress whitespace " something something " @=? compressCss " something \n\t\r something " + -- do not compress whitespace in constants + , "abc \" \t\n\r \" xyz" @=? + compressCss "abc \" \t\n\r \" xyz" + , "abc ' \t\n\r ' xyz" @=? + compressCss "abc ' \t\n\r ' xyz" -- strip comments , "" @=? compressCss "/* abc { } ;; \n\t\r */" @@ -40,7 +45,7 @@ tests = testGroup "Hakyll.Web.CompressCss.Tests" $ concat -- don't get irritated by the wrong constant terminator , "\" ' \"" @=? compressCss "\" ' \"" , "' \" '" @=? compressCss "' \" '" - -- don't compress whitespace in constants in the middle of a string + -- don't compress whitespace around separators in constants in the middle of a string , "abc '{ '" @=? compressCss "abc '{ '" , "abc \"{ \"" @=? compressCss "abc \"{ \"" -- compress multiple semicolons -- cgit v1.2.3 From ce9730758e5d9317c3b0f060a7db995f52467115 Mon Sep 17 00:00:00 2001 From: Nicole Rauch Date: Sun, 14 Aug 2016 19:54:43 +0200 Subject: Compressed the code by grouping the guards together. --- src/Hakyll/Web/CompressCss.hs | 15 ++++++--------- 1 file changed, 6 insertions(+), 9 deletions(-) diff --git a/src/Hakyll/Web/CompressCss.hs b/src/Hakyll/Web/CompressCss.hs index 6667842..8f67c7c 100644 --- a/src/Hakyll/Web/CompressCss.hs +++ b/src/Hakyll/Web/CompressCss.hs @@ -52,16 +52,13 @@ compressWhitespace [] = [] compressWhitespace str | isPrefixOf "\"" str = head str : retainConstants compressWhitespace "\"" (drop 1 str) | isPrefixOf "'" str = head str : retainConstants compressWhitespace "'" (drop 1 str) - | isPrefixOf "\t" str = compressWhitespace (' ' : (drop 1 str)) - | isPrefixOf "\n" str = compressWhitespace (' ' : (drop 1 str)) - | isPrefixOf "\r" str = compressWhitespace (' ' : (drop 1 str)) - - | isPrefixOf " \t" str = compressWhitespace (' ' : (drop 2 str)) - | isPrefixOf " \n" str = compressWhitespace (' ' : (drop 2 str)) - | isPrefixOf " \r" str = compressWhitespace (' ' : (drop 2 str)) - | isPrefixOf " " str = compressWhitespace (' ' : (drop 2 str)) + | replaceOne = compressWhitespace (' ' : (drop 1 str)) + | replaceTwo = compressWhitespace (' ' : (drop 2 str)) | otherwise = head str : compressWhitespace (drop 1 str) - + where + prefix p = isPrefixOf p str + replaceOne = or $ map prefix ["\t", "\n", "\r"] + replaceTwo = or $ map prefix [" \t", " \n", " \r", " "] -------------------------------------------------------------------------------- -- | Function that strips CSS comments away. -- cgit v1.2.3 From e4c7cb547d8c4754b4b54b82b4315740b7bff37c Mon Sep 17 00:00:00 2001 From: Nicole Rauch Date: Sun, 14 Aug 2016 20:00:16 +0200 Subject: Do not strip comments that are inside constants. --- src/Hakyll/Web/CompressCss.hs | 2 ++ tests/Hakyll/Web/CompressCss/Tests.hs | 5 ++++- 2 files changed, 6 insertions(+), 1 deletion(-) diff --git a/src/Hakyll/Web/CompressCss.hs b/src/Hakyll/Web/CompressCss.hs index 8f67c7c..a032b10 100644 --- a/src/Hakyll/Web/CompressCss.hs +++ b/src/Hakyll/Web/CompressCss.hs @@ -65,6 +65,8 @@ compressWhitespace str stripComments :: String -> String stripComments [] = [] stripComments str + | isPrefixOf "\"" str = head str : retainConstants stripComments "\"" (drop 1 str) + | isPrefixOf "'" str = head str : retainConstants stripComments "'" (drop 1 str) | isPrefixOf "/*" str = stripComments $ eatComments $ drop 2 str | otherwise = head str : stripComments (drop 1 str) where diff --git a/tests/Hakyll/Web/CompressCss/Tests.hs b/tests/Hakyll/Web/CompressCss/Tests.hs index 065a588..c3fc840 100644 --- a/tests/Hakyll/Web/CompressCss/Tests.hs +++ b/tests/Hakyll/Web/CompressCss/Tests.hs @@ -30,7 +30,10 @@ tests = testGroup "Hakyll.Web.CompressCss.Tests" $ concat compressCss "abc ' \t\n\r ' xyz" -- strip comments - , "" @=? compressCss "/* abc { } ;; \n\t\r */" + , "before after" @=? compressCss "before /* abc { } ;; \n\t\r */ after" + -- don't strip comments inside constants + , "before \"/* abc { } ;; \n\t\r */\" after" + @=? compressCss "before \"/* abc { } ;; \n\t\r */\" after" -- compress separators , "}" @=? compressCss "; }" -- cgit v1.2.3 From c32e55447793af259f8483f14c4a93bec882dc65 Mon Sep 17 00:00:00 2001 From: Nicole Rauch Date: Sun, 14 Aug 2016 20:08:47 +0200 Subject: Extracted a helper method because it was defined locally twice. --- src/Hakyll/Web/CompressCss.hs | 19 +++++++++++-------- 1 file changed, 11 insertions(+), 8 deletions(-) diff --git a/src/Hakyll/Web/CompressCss.hs b/src/Hakyll/Web/CompressCss.hs index a032b10..21f3087 100644 --- a/src/Hakyll/Web/CompressCss.hs +++ b/src/Hakyll/Web/CompressCss.hs @@ -37,13 +37,12 @@ compressSeparators [] = [] compressSeparators str | isPrefixOf "\"" str = head str : retainConstants compressSeparators "\"" (drop 1 str) | isPrefixOf "'" str = head str : retainConstants compressSeparators "'" (drop 1 str) - | stripFirst = compressSeparators (drop 1 str) + | stripFirst = compressSeparators (drop 1 str) | stripSecond = compressSeparators (head str : (drop 2 str)) - | otherwise = head str : compressSeparators (drop 1 str) + | otherwise = head str : compressSeparators (drop 1 str) where - prefix p = isPrefixOf p str - stripFirst = or $ map prefix [" ", " {", " }", ";;", ";}"] - stripSecond = or $ map prefix ["{ ", "} ", "; "] + stripFirst = or $ map (isOfPrefix str) [" ", " {", " }", ";;", ";}"] + stripSecond = or $ map (isOfPrefix str) ["{ ", "} ", "; "] -------------------------------------------------------------------------------- -- | Compresses all whitespace. @@ -56,9 +55,8 @@ compressWhitespace str | replaceTwo = compressWhitespace (' ' : (drop 2 str)) | otherwise = head str : compressWhitespace (drop 1 str) where - prefix p = isPrefixOf p str - replaceOne = or $ map prefix ["\t", "\n", "\r"] - replaceTwo = or $ map prefix [" \t", " \n", " \r", " "] + replaceOne = or $ map (isOfPrefix str) ["\t", "\n", "\r"] + replaceTwo = or $ map (isOfPrefix str) [" \t", " \n", " \r", " "] -------------------------------------------------------------------------------- -- | Function that strips CSS comments away. @@ -82,3 +80,8 @@ retainConstants f delim str | null str = [] | isPrefixOf delim str = head str : f (drop 1 str) | otherwise = head str : retainConstants f delim (drop 1 str) + +-------------------------------------------------------------------------------- +-- | Helper function to determine whether a string is a substring. +isOfPrefix :: String -> String -> Bool +isOfPrefix = flip isPrefixOf -- cgit v1.2.3 From 82301c508aeae5749ec5889d95feefcbcd0a2ae4 Mon Sep 17 00:00:00 2001 From: Nicole Rauch Date: Sun, 14 Aug 2016 20:16:09 +0200 Subject: Unified the constant cases in all three functions. --- src/Hakyll/Web/CompressCss.hs | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) diff --git a/src/Hakyll/Web/CompressCss.hs b/src/Hakyll/Web/CompressCss.hs index 21f3087..8e4b268 100644 --- a/src/Hakyll/Web/CompressCss.hs +++ b/src/Hakyll/Web/CompressCss.hs @@ -35,12 +35,12 @@ compressCss = compressSeparators . stripComments . compressWhitespace compressSeparators :: String -> String compressSeparators [] = [] compressSeparators str - | isPrefixOf "\"" str = head str : retainConstants compressSeparators "\"" (drop 1 str) - | isPrefixOf "'" str = head str : retainConstants compressSeparators "'" (drop 1 str) + | isConstant = head str : retainConstants compressSeparators (head str) (drop 1 str) | stripFirst = compressSeparators (drop 1 str) | stripSecond = compressSeparators (head str : (drop 2 str)) | otherwise = head str : compressSeparators (drop 1 str) where + isConstant = or $ map (isOfPrefix str) ["\"", "'"] stripFirst = or $ map (isOfPrefix str) [" ", " {", " }", ";;", ";}"] stripSecond = or $ map (isOfPrefix str) ["{ ", "} ", "; "] @@ -49,12 +49,12 @@ compressSeparators str compressWhitespace :: String -> String compressWhitespace [] = [] compressWhitespace str - | isPrefixOf "\"" str = head str : retainConstants compressWhitespace "\"" (drop 1 str) - | isPrefixOf "'" str = head str : retainConstants compressWhitespace "'" (drop 1 str) + | isConstant = head str : retainConstants compressWhitespace (head str) (drop 1 str) | replaceOne = compressWhitespace (' ' : (drop 1 str)) | replaceTwo = compressWhitespace (' ' : (drop 2 str)) | otherwise = head str : compressWhitespace (drop 1 str) where + isConstant = or $ map (isOfPrefix str) ["\"", "'"] replaceOne = or $ map (isOfPrefix str) ["\t", "\n", "\r"] replaceTwo = or $ map (isOfPrefix str) [" \t", " \n", " \r", " "] @@ -63,11 +63,11 @@ compressWhitespace str stripComments :: String -> String stripComments [] = [] stripComments str - | isPrefixOf "\"" str = head str : retainConstants stripComments "\"" (drop 1 str) - | isPrefixOf "'" str = head str : retainConstants stripComments "'" (drop 1 str) + | isConstant = head str : retainConstants stripComments (head str) (drop 1 str) | isPrefixOf "/*" str = stripComments $ eatComments $ drop 2 str | otherwise = head str : stripComments (drop 1 str) where + isConstant = or $ map (isOfPrefix str) ["\"", "'"] eatComments str' | null str' = [] | isPrefixOf "*/" str' = drop 2 str' @@ -75,10 +75,10 @@ stripComments str -------------------------------------------------------------------------------- -- | Helper function to handle string constants correctly. -retainConstants :: (String -> String) -> String -> String -> String +retainConstants :: (String -> String) -> Char -> String -> String retainConstants f delim str | null str = [] - | isPrefixOf delim str = head str : f (drop 1 str) + | isPrefixOf [delim] str = head str : f (drop 1 str) | otherwise = head str : retainConstants f delim (drop 1 str) -------------------------------------------------------------------------------- -- cgit v1.2.3 From ea0a8d5226f653dd37b6801f52dc5ad58f3a5809 Mon Sep 17 00:00:00 2001 From: Nicole Rauch Date: Sun, 14 Aug 2016 22:04:43 +0200 Subject: Removed two compiler warnings (unnecessary imports). --- src/Hakyll/Web/CompressCss.hs | 2 -- 1 file changed, 2 deletions(-) diff --git a/src/Hakyll/Web/CompressCss.hs b/src/Hakyll/Web/CompressCss.hs index 8e4b268..239c383 100644 --- a/src/Hakyll/Web/CompressCss.hs +++ b/src/Hakyll/Web/CompressCss.hs @@ -8,14 +8,12 @@ module Hakyll.Web.CompressCss -------------------------------------------------------------------------------- -import Data.Char (isSpace) import Data.List (isPrefixOf) -------------------------------------------------------------------------------- import Hakyll.Core.Compiler import Hakyll.Core.Item -import Hakyll.Core.Util.String -------------------------------------------------------------------------------- -- cgit v1.2.3 From 9f850a9035c29de568fb61b9646f657f5935ab89 Mon Sep 17 00:00:00 2001 From: Nicole Rauch Date: Mon, 15 Aug 2016 00:05:19 +0200 Subject: Also trim whitespace around colons. --- src/Hakyll/Web/CompressCss.hs | 4 ++-- tests/Hakyll/Web/CompressCss/Tests.hs | 2 ++ 2 files changed, 4 insertions(+), 2 deletions(-) diff --git a/src/Hakyll/Web/CompressCss.hs b/src/Hakyll/Web/CompressCss.hs index 239c383..0371d8b 100644 --- a/src/Hakyll/Web/CompressCss.hs +++ b/src/Hakyll/Web/CompressCss.hs @@ -39,8 +39,8 @@ compressSeparators str | otherwise = head str : compressSeparators (drop 1 str) where isConstant = or $ map (isOfPrefix str) ["\"", "'"] - stripFirst = or $ map (isOfPrefix str) [" ", " {", " }", ";;", ";}"] - stripSecond = or $ map (isOfPrefix str) ["{ ", "} ", "; "] + stripFirst = or $ map (isOfPrefix str) [" ", " {", " }", " :", ";;", ";}"] + stripSecond = or $ map (isOfPrefix str) ["{ ", "} ", ": ", "; "] -------------------------------------------------------------------------------- -- | Compresses all whitespace. diff --git a/tests/Hakyll/Web/CompressCss/Tests.hs b/tests/Hakyll/Web/CompressCss/Tests.hs index c3fc840..b356388 100644 --- a/tests/Hakyll/Web/CompressCss/Tests.hs +++ b/tests/Hakyll/Web/CompressCss/Tests.hs @@ -51,6 +51,8 @@ tests = testGroup "Hakyll.Web.CompressCss.Tests" $ concat -- don't compress whitespace around separators in constants in the middle of a string , "abc '{ '" @=? compressCss "abc '{ '" , "abc \"{ \"" @=? compressCss "abc \"{ \"" + -- compress whitespace after colons + , "abc:xyz" @=? compressCss "abc : xyz" -- compress multiple semicolons , ";" @=? compressCss ";;;;;;;" ] -- cgit v1.2.3