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 --- tests/Hakyll/Web/CompressCss/Tests.hs | 43 +++++++++++++++++++++++++++++++++++ 1 file changed, 43 insertions(+) create mode 100644 tests/Hakyll/Web/CompressCss/Tests.hs (limited to 'tests/Hakyll/Web') 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) \")\"; }" + ] + ] -- 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(-) (limited to 'tests/Hakyll/Web') 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(+) (limited to 'tests/Hakyll/Web') 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(+) (limited to 'tests/Hakyll/Web') 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(-) (limited to 'tests/Hakyll/Web') 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(-) (limited to 'tests/Hakyll/Web') 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 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(-) (limited to 'tests/Hakyll/Web') 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(-) (limited to 'tests/Hakyll/Web') 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 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(-) (limited to 'tests/Hakyll/Web') 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 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(-) (limited to 'tests/Hakyll/Web') 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