Skip to content

Commit

Permalink
Add null function, bump version
Browse files Browse the repository at this point in the history
  • Loading branch information
jaspervdj committed Apr 24, 2014
1 parent 0f96245 commit c0e2ff4
Show file tree
Hide file tree
Showing 4 changed files with 61 additions and 9 deletions.
3 changes: 3 additions & 0 deletions CHANGELOG
@@ -1,3 +1,6 @@
- 0.6.1.0
* Add the `null` query to Text.Blaze.Internal.

- 0.6.0.0
* Add the operator (!?) for nicely setting conditional attributes

Expand Down
4 changes: 2 additions & 2 deletions blaze-markup.cabal
@@ -1,5 +1,5 @@
Name: blaze-markup
Version: 0.6.0.0
Version: 0.6.1.0
Homepage: http://jaspervdj.be/blaze
Bug-Reports: http://github.com/jaspervdj/blaze-markup/issues
License: BSD3
Expand Down Expand Up @@ -51,7 +51,7 @@ Test-suite blaze-markup-tests

Build-depends:
HUnit >= 1.2 && < 1.3,
QuickCheck >= 2.4 && < 2.7,
QuickCheck >= 2.4 && < 2.8,
containers >= 0.3 && < 0.6,
test-framework >= 0.4 && < 0.9,
test-framework-hunit >= 0.3 && < 0.4,
Expand Down
31 changes: 31 additions & 0 deletions src/Text/Blaze/Internal.hs
Expand Up @@ -58,10 +58,15 @@ module Text.Blaze.Internal
-- * Modifying Markup elements
, contents
, external

-- * Querying Markup elements
, null
) where

import Prelude hiding (null)
import Data.Monoid (Monoid, mappend, mempty, mconcat)
import Unsafe.Coerce (unsafeCoerce)
import qualified Data.List as List

import Data.ByteString.Char8 (ByteString)
import Data.Text (Text)
Expand Down Expand Up @@ -463,3 +468,29 @@ contents (Append c1 c2) = Append (contents c1) (contents c2)
contents (AddAttribute _ _ _ c) = contents c
contents (AddCustomAttribute _ _ c) = contents c
contents _ = Empty

-- | Check if a 'Markup' value is completely empty (renders to the empty
-- string).
null :: MarkupM a -> Bool
null markup = case markup of
Parent _ _ _ _ -> False
CustomParent _ _ -> False
Leaf _ _ _ -> False
CustomLeaf _ _ -> False
Content c -> emptyChoiceString c
Append c1 c2 -> null c1 && null c2
AddAttribute _ _ _ c -> null c
AddCustomAttribute _ _ c -> null c
Empty -> True
where
emptyChoiceString cs = case cs of
Static ss -> emptyStaticString ss
String s -> List.null s
Text t -> T.null t
ByteString bs -> B.null bs
PreEscaped c -> emptyChoiceString c
External c -> emptyChoiceString c
AppendChoiceString c1 c2 -> emptyChoiceString c1 && emptyChoiceString c2
EmptyChoiceString -> True

emptyStaticString = B.null . getUtf8ByteString
32 changes: 25 additions & 7 deletions tests/Text/Blaze/Tests.hs
@@ -1,16 +1,17 @@
{-# LANGUAGE OverloadedStrings, FlexibleInstances, TypeSynonymInstances #-}
{-# OPTIONS_GHC -fno-warn-orphans #-}
{-# OPTIONS_GHC -fno-warn-orphans #-}
{-# OPTIONS_GHC -fno-warn-unused-do-bind #-}
module Text.Blaze.Tests
( tests
) where

import Prelude hiding (div, id)
import Data.Monoid (mempty)
import Prelude hiding (div, id, null)
import Data.Monoid (mempty, mappend)
import Control.Monad (replicateM)
import Control.Applicative ((<$>))
import Data.Word (Word8)
import Data.Char (ord)
import Data.List (isInfixOf)
import qualified Data.List as List

import Test.Framework (Test)
import Test.HUnit (Assertion, (@=?))
Expand All @@ -37,6 +38,8 @@ tests = [ testProperty "left identity Monoid law" monoidLeftIdentity

, testCase "conditional attributes" conditionalAttributes
, testCase "contents 1" contents1
, testCase "empty 1" empty1
, testCase "empty 2" empty2
]

-- | The left identity Monoid law.
Expand Down Expand Up @@ -101,7 +104,7 @@ unsafeByteStringId ws =
-- | Check if the "</" sequence does not appear in @<script>@ or @<style>@ tags.
--
externalEndSequence :: String -> Bool
externalEndSequence = not . isInfixOf "</" . LBC.unpack
externalEndSequence = not . List.isInfixOf "</" . LBC.unpack
. renderUsingUtf8 . external . string

-- | Check that the "<>" characters are well-nested.
Expand All @@ -121,7 +124,7 @@ conditionalAttributes =
where
html = do
p !? (4 > length [()], class_ "foo") $ "Hello"
p !? (null [()], class_ "bar") !? (True, id "2nd") $ "World"
p !? (List.null [()], class_ "bar") !? (True, id "2nd") $ "World"

contents1 :: Assertion
contents1 = "Hello World!" @=? renderUsingUtf8 (contents html)
Expand All @@ -132,6 +135,21 @@ contents1 = "Hello World!" @=? renderUsingUtf8 (contents html)
img ! name "An image"
p "World!"

empty1 :: Assertion
empty1 = True @=? null html
where
html :: Markup
html = do
""
""
mempty

empty2 :: Assertion
empty2 = False @=? null html
where
html :: Markup
html = "" `mappend` "" `mappend` p "a"

-- Show instance for the HTML type, so we can debug.
--
instance Show Markup where
Expand All @@ -157,7 +175,7 @@ instance Arbitrary Markup where
--
arbitraryMarkup :: Int -- ^ Maximum depth.
-> Gen Markup -- ^ Resulting arbitrary HTML snippet.
arbitraryMarkup depth = do
arbitraryMarkup depth = do
-- Choose the size (width) of this element.
size <- choose (0, 3)

Expand Down

0 comments on commit c0e2ff4

Please sign in to comment.