From a47a96e04144af21b425d29c1ce6f57593abea5e Mon Sep 17 00:00:00 2001 From: Phil Curzon Date: Wed, 1 Feb 2023 23:56:53 +0000 Subject: [PATCH] Treat null values as falsey in inverted sections --- CHANGELOG.md | 4 ++++ package.yaml | 2 +- src/Text/Mustache/Render.hs | 1 + test/unit/Spec.hs | 6 ++++++ 4 files changed, 12 insertions(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index e6c726f..5994b32 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,5 +1,9 @@ # Mustache library changelog +## v2.4.1 + +- Also treat Null as falsey in inverted sections + ## v2.4.0 - Support for aeson 2 diff --git a/package.yaml b/package.yaml index c876834..d8e2f48 100644 --- a/package.yaml +++ b/package.yaml @@ -1,5 +1,5 @@ name: mustache -version: '2.4.0' +version: '2.4.1' synopsis: A mustache template parser library. description: ! 'Allows parsing and rendering template files with mustache markup. See the diff --git a/src/Text/Mustache/Render.hs b/src/Text/Mustache/Render.hs index 13c6458..7a0ad89 100644 --- a/src/Text/Mustache/Render.hs +++ b/src/Text/Mustache/Render.hs @@ -154,6 +154,7 @@ substituteNode (InvertedSection (NamedData secName) invSecSTree) = search secName >>= \case Just (Bool False) -> contents Just (Array a) | V.null a -> contents + Just Null -> contents Nothing -> contents _ -> return () where diff --git a/test/unit/Spec.hs b/test/unit/Spec.hs index 04555ed..33263a2 100644 --- a/test/unit/Spec.hs +++ b/test/unit/Spec.hs @@ -161,6 +161,12 @@ substituteSpec = ]) `shouldBe` "var1var2" + it "substitutes an inverse section when the key is present (and null)" $ + substitute + (toTemplate [InvertedSection (NamedData ["section"]) [TextBlock "t"]]) + (object ["section" ~> Null]) + `shouldBe` "t" + it "does not substitute a section when the key is not present" $ substitute (toTemplate [Section (NamedData ["section"]) [TextBlock "t"]])