From e5c4866d9d3e3295b2e4fe1c45c4e226f691d1b0 Mon Sep 17 00:00:00 2001 From: David Fox Date: Sat, 11 Nov 2023 02:23:43 -0800 Subject: [PATCH 1/7] Do not use the the Show instance of Clay.Property.Number for rendering. Instead add an instance of Pretty that uses the showFixed function of the underlying Fixed value. Adds a test case to spec/Clay/MediaSpec.hs that demonstrates the resulting output of the keyframes function. --- clay.cabal | 4 +++- spec/Clay/MediaSpec.hs | 49 +++++++++++++++++++++++++++++++++++++++--- src/Clay/Property.hs | 4 ++++ src/Clay/Render.hs | 3 ++- 4 files changed, 55 insertions(+), 5 deletions(-) diff --git a/clay.cabal b/clay.cabal index 731baa2d..2a3cceaf 100644 --- a/clay.cabal +++ b/clay.cabal @@ -79,6 +79,7 @@ Library Build-Depends: base >= 4.11 && < 4.18, mtl >= 1, + pretty >= 1, text >= 0.11 GHC-Options: -Wall -Wcompat Default-Language: Haskell2010 @@ -93,6 +94,7 @@ Test-Suite Test-Clay mtl >= 1, text >= 0.11, hspec >= 2.2.0, - hspec-discover >= 2.2.0 + hspec-discover >= 2.2.0, + pretty >= 1 GHC-Options: -Wall -Wcompat Default-Language: Haskell2010 diff --git a/spec/Clay/MediaSpec.hs b/spec/Clay/MediaSpec.hs index ecffbe40..800c5f0f 100644 --- a/spec/Clay/MediaSpec.hs +++ b/spec/Clay/MediaSpec.hs @@ -2,11 +2,12 @@ module Clay.MediaSpec (spec) where -import Clay (Css, em, query) +import Clay (Css, em, hidden, pretty, query, renderWith, visible, visibility) import Clay.Media -import Clay.Stylesheet (Feature, MediaType) +import Clay.Render (Config(..)) +import Clay.Stylesheet (Feature, keyframes, MediaType) import Common -import Data.Text.Lazy (Text, unpack) +import Data.Text.Lazy as Text (Text, toStrict, unlines, unpack) import Test.Hspec import Prelude hiding (all, print) @@ -73,6 +74,48 @@ spec = do "prefers-color-scheme: light" `shouldRenderFromFeature` prefersColorScheme light "prefers-color-scheme: dark" `shouldRenderFromFeature` prefersColorScheme dark + describe "keyframes tests" $ do + it "keyframes test 1" $ + (renderWith custom [] $ keyframes "blink-animation" [(0, visibility visible), (100, visibility hidden)]) + `shouldBe` (Text.unlines + [ "" + , "@-webkit-keyframes blink-animation", "{" + -- without fix for the rendering of the Clay.Property.Number type: + -- , "Number {unNumber = 0.00000}% ", "{", "visibility:visible;", "}", "" + -- , "Number {unNumber = 100.00000}% ", "{", "visibility:hidden;", "}", "" + , "0% ", "{", "visibility:visible;", "}", "" + , "100% ", "{", "visibility:hidden;", "}", "", "}" + , "" + , "@-moz-keyframes blink-animation", "{" + , "0% ", "{", "visibility:visible;", "}", "" + , "100% ", "{", "visibility:hidden;", "}", "", "}" + , "" + , "@-ms-keyframes blink-animation", "{" + , "0% ", "{", "visibility:visible;", "}", "" + , "100% ", "{", "visibility:hidden;", "}", "", "}" + , "" + , "@-o-keyframes blink-animation", "{" + , "0% ", "{", "visibility:visible;", "}", "" + , "100% ", "{", "visibility:hidden;", "}", "", "}" + , "" + , "@keyframes blink-animation", "{" + , "0% ", "{", "visibility:visible;", "}", "" + , "100% ", "{", "visibility:hidden;", "}", "", "}" + , ""]) + +custom = + pretty + { + indentation = "" + -- newline = "\n" + , sep = "" + -- , finalSemicolon = True -- False + , warn = False + , align = False + , banner = False + , comments = False + } + -- | Empty CSS for when CSS is needed but we don't care about it. emptyStyle :: Css emptyStyle = pure () diff --git a/src/Clay/Property.hs b/src/Clay/Property.hs index 12166e24..7c9cdb0d 100644 --- a/src/Clay/Property.hs +++ b/src/Clay/Property.hs @@ -8,6 +8,7 @@ import Data.List.NonEmpty (NonEmpty, toList) import Data.Maybe import Data.String import Data.Text (Text, replace) +import Text.PrettyPrint.HughesPJClass (Pretty(pPrint), text) data Prefixed = Prefixed { unPrefixed :: [(Text, Text)] } | Plain { unPlain :: Text } deriving (Show, Eq) @@ -102,6 +103,9 @@ intercalate s (x:xs) = foldl (\a b -> a `mappend` s `mappend` b) x xs newtype Number = Number { unNumber :: Fixed E5 } deriving (Enum, Eq, Fractional, Num, Ord, Read, Real, RealFrac, Show) +instance Pretty Number where + pPrint = text . showFixed True . unNumber + instance Val Number where value = Value . Plain . cssNumberText diff --git a/src/Clay/Render.hs b/src/Clay/Render.hs index a8552870..f7f0de7c 100644 --- a/src/Clay/Render.hs +++ b/src/Clay/Render.hs @@ -19,6 +19,7 @@ import Data.Maybe import Data.Text (Text, pack) import Data.Text.Lazy.Builder import Prelude hiding ((**)) +import Text.PrettyPrint.HughesPJClass (pPrint) import qualified Data.Text as Text import qualified Data.Text.Lazy as Lazy @@ -150,7 +151,7 @@ kframe cfg (Keyframes ident xs) = frame :: Config -> (Number, [Rule]) -> Builder frame cfg (p, rs) = mconcat - [ fromText (pack (show p)) + [ fromText (pack (show (pPrint p))) , "% " , rules cfg [] rs ] From 70f2947baf7fe2de79490f266d4e556090bac1cb Mon Sep 17 00:00:00 2001 From: David Fox Date: Sat, 11 Nov 2023 06:46:30 -0800 Subject: [PATCH 2/7] Use compact renderer for test --- spec/Clay/MediaSpec.hs | 47 +++++++----------------------------------- 1 file changed, 7 insertions(+), 40 deletions(-) diff --git a/spec/Clay/MediaSpec.hs b/spec/Clay/MediaSpec.hs index 800c5f0f..05944025 100644 --- a/spec/Clay/MediaSpec.hs +++ b/spec/Clay/MediaSpec.hs @@ -2,7 +2,7 @@ module Clay.MediaSpec (spec) where -import Clay (Css, em, hidden, pretty, query, renderWith, visible, visibility) +import Clay (compact, Css, em, hidden, opacity, query, renderWith) import Clay.Media import Clay.Render (Config(..)) import Clay.Stylesheet (Feature, keyframes, MediaType) @@ -76,45 +76,12 @@ spec = do describe "keyframes tests" $ do it "keyframes test 1" $ - (renderWith custom [] $ keyframes "blink-animation" [(0, visibility visible), (100, visibility hidden)]) - `shouldBe` (Text.unlines - [ "" - , "@-webkit-keyframes blink-animation", "{" - -- without fix for the rendering of the Clay.Property.Number type: - -- , "Number {unNumber = 0.00000}% ", "{", "visibility:visible;", "}", "" - -- , "Number {unNumber = 100.00000}% ", "{", "visibility:hidden;", "}", "" - , "0% ", "{", "visibility:visible;", "}", "" - , "100% ", "{", "visibility:hidden;", "}", "", "}" - , "" - , "@-moz-keyframes blink-animation", "{" - , "0% ", "{", "visibility:visible;", "}", "" - , "100% ", "{", "visibility:hidden;", "}", "", "}" - , "" - , "@-ms-keyframes blink-animation", "{" - , "0% ", "{", "visibility:visible;", "}", "" - , "100% ", "{", "visibility:hidden;", "}", "", "}" - , "" - , "@-o-keyframes blink-animation", "{" - , "0% ", "{", "visibility:visible;", "}", "" - , "100% ", "{", "visibility:hidden;", "}", "", "}" - , "" - , "@keyframes blink-animation", "{" - , "0% ", "{", "visibility:visible;", "}", "" - , "100% ", "{", "visibility:hidden;", "}", "", "}" - , ""]) - -custom = - pretty - { - indentation = "" - -- newline = "\n" - , sep = "" - -- , finalSemicolon = True -- False - , warn = False - , align = False - , banner = False - , comments = False - } + (renderWith compact [] $ keyframes "blink-animation" [(0, opacity 0), (100, opacity 1)]) + `shouldBe` ("@-webkit-keyframes blink-animation{0% {opacity:0}100% {opacity:1}}" <> + "@-moz-keyframes blink-animation{0% {opacity:0}100% {opacity:1}}" <> + "@-ms-keyframes blink-animation{0% {opacity:0}100% {opacity:1}}" <> + "@-o-keyframes blink-animation{0% {opacity:0}100% {opacity:1}}" <> + "@keyframes blink-animation{0% {opacity:0}100% {opacity:1}}") -- | Empty CSS for when CSS is needed but we don't care about it. emptyStyle :: Css From 20545ccef27ec2b53d0ae87076ba9d1130730b06 Mon Sep 17 00:00:00 2001 From: David Fox Date: Mon, 13 Nov 2023 11:29:22 -0800 Subject: [PATCH 3/7] Remove pretty dependency, use cssNumberText --- clay.cabal | 4 +--- src/Clay/Property.hs | 4 ---- src/Clay/Render.hs | 3 +-- 3 files changed, 2 insertions(+), 9 deletions(-) diff --git a/clay.cabal b/clay.cabal index 2a3cceaf..731baa2d 100644 --- a/clay.cabal +++ b/clay.cabal @@ -79,7 +79,6 @@ Library Build-Depends: base >= 4.11 && < 4.18, mtl >= 1, - pretty >= 1, text >= 0.11 GHC-Options: -Wall -Wcompat Default-Language: Haskell2010 @@ -94,7 +93,6 @@ Test-Suite Test-Clay mtl >= 1, text >= 0.11, hspec >= 2.2.0, - hspec-discover >= 2.2.0, - pretty >= 1 + hspec-discover >= 2.2.0 GHC-Options: -Wall -Wcompat Default-Language: Haskell2010 diff --git a/src/Clay/Property.hs b/src/Clay/Property.hs index 7c9cdb0d..12166e24 100644 --- a/src/Clay/Property.hs +++ b/src/Clay/Property.hs @@ -8,7 +8,6 @@ import Data.List.NonEmpty (NonEmpty, toList) import Data.Maybe import Data.String import Data.Text (Text, replace) -import Text.PrettyPrint.HughesPJClass (Pretty(pPrint), text) data Prefixed = Prefixed { unPrefixed :: [(Text, Text)] } | Plain { unPlain :: Text } deriving (Show, Eq) @@ -103,9 +102,6 @@ intercalate s (x:xs) = foldl (\a b -> a `mappend` s `mappend` b) x xs newtype Number = Number { unNumber :: Fixed E5 } deriving (Enum, Eq, Fractional, Num, Ord, Read, Real, RealFrac, Show) -instance Pretty Number where - pPrint = text . showFixed True . unNumber - instance Val Number where value = Value . Plain . cssNumberText diff --git a/src/Clay/Render.hs b/src/Clay/Render.hs index f7f0de7c..ff34ca19 100644 --- a/src/Clay/Render.hs +++ b/src/Clay/Render.hs @@ -19,7 +19,6 @@ import Data.Maybe import Data.Text (Text, pack) import Data.Text.Lazy.Builder import Prelude hiding ((**)) -import Text.PrettyPrint.HughesPJClass (pPrint) import qualified Data.Text as Text import qualified Data.Text.Lazy as Lazy @@ -151,7 +150,7 @@ kframe cfg (Keyframes ident xs) = frame :: Config -> (Number, [Rule]) -> Builder frame cfg (p, rs) = mconcat - [ fromText (pack (show (pPrint p))) + [ fromText (cssNumberText p) , "% " , rules cfg [] rs ] From d07a2656ba5ebf7d067adec7d02116fd5e8512a1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Manuel=20B=C3=A4renz?= Date: Tue, 14 Nov 2023 09:35:40 +0100 Subject: [PATCH 4/7] Update spec/Clay/MediaSpec.hs --- spec/Clay/MediaSpec.hs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/spec/Clay/MediaSpec.hs b/spec/Clay/MediaSpec.hs index 05944025..1aac60f7 100644 --- a/spec/Clay/MediaSpec.hs +++ b/spec/Clay/MediaSpec.hs @@ -2,7 +2,7 @@ module Clay.MediaSpec (spec) where -import Clay (compact, Css, em, hidden, opacity, query, renderWith) +import Clay (compact, Css, em, opacity, query, renderWith) import Clay.Media import Clay.Render (Config(..)) import Clay.Stylesheet (Feature, keyframes, MediaType) From ee14754947e5444290da635fbbbd4dd2d092b0f8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Manuel=20B=C3=A4renz?= Date: Tue, 14 Nov 2023 09:35:46 +0100 Subject: [PATCH 5/7] Update spec/Clay/MediaSpec.hs --- spec/Clay/MediaSpec.hs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/spec/Clay/MediaSpec.hs b/spec/Clay/MediaSpec.hs index 1aac60f7..0440a3a7 100644 --- a/spec/Clay/MediaSpec.hs +++ b/spec/Clay/MediaSpec.hs @@ -7,7 +7,7 @@ import Clay.Media import Clay.Render (Config(..)) import Clay.Stylesheet (Feature, keyframes, MediaType) import Common -import Data.Text.Lazy as Text (Text, toStrict, unlines, unpack) +import Data.Text.Lazy (Text, unpack) import Test.Hspec import Prelude hiding (all, print) From d76af9b41e96a90ae1717c20af62df588828ae9c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Manuel=20B=C3=A4renz?= Date: Tue, 14 Nov 2023 09:35:52 +0100 Subject: [PATCH 6/7] Update spec/Clay/MediaSpec.hs --- spec/Clay/MediaSpec.hs | 1 - 1 file changed, 1 deletion(-) diff --git a/spec/Clay/MediaSpec.hs b/spec/Clay/MediaSpec.hs index 0440a3a7..0cc353bf 100644 --- a/spec/Clay/MediaSpec.hs +++ b/spec/Clay/MediaSpec.hs @@ -4,7 +4,6 @@ module Clay.MediaSpec (spec) where import Clay (compact, Css, em, opacity, query, renderWith) import Clay.Media -import Clay.Render (Config(..)) import Clay.Stylesheet (Feature, keyframes, MediaType) import Common import Data.Text.Lazy (Text, unpack) From bb37ff7e03eccdfb26c161f8679b8605249c84a9 Mon Sep 17 00:00:00 2001 From: David Fox Date: Tue, 14 Nov 2023 18:12:49 -0800 Subject: [PATCH 7/7] Remove now-unused pack import --- src/Clay/Render.hs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Clay/Render.hs b/src/Clay/Render.hs index ff34ca19..7adc1a8d 100644 --- a/src/Clay/Render.hs +++ b/src/Clay/Render.hs @@ -16,7 +16,7 @@ import Control.Applicative import Control.Monad.Writer import Data.List (sort) import Data.Maybe -import Data.Text (Text, pack) +import Data.Text (Text) import Data.Text.Lazy.Builder import Prelude hiding ((**))