From 773419b9a00e36e0af08d0cb633b418ea60e1487 Mon Sep 17 00:00:00 2001 From: vi Date: Mon, 23 Jun 2014 14:39:19 +0800 Subject: [PATCH] Two simple combinators on Element: getAttr and hasAttr. --- src/Text/Taggy/Combinators.hs | 11 +++++++++++ tests/Text/Taggy/CombinatorsSpec.hs | 21 +++++++++++++++++++++ 2 files changed, 32 insertions(+) create mode 100644 src/Text/Taggy/Combinators.hs create mode 100644 tests/Text/Taggy/CombinatorsSpec.hs diff --git a/src/Text/Taggy/Combinators.hs b/src/Text/Taggy/Combinators.hs new file mode 100644 index 0000000..9b13fe1 --- /dev/null +++ b/src/Text/Taggy/Combinators.hs @@ -0,0 +1,11 @@ +module Text.Taggy.Combinators (hasAttr, getAttr) where + +import Prelude hiding (lookup) +import Text.Taggy.DOM (Element(..), AttrName, AttrValue) +import Data.HashMap.Strict (lookup, keys) + +hasAttr :: Element -> AttrName -> Bool +hasAttr = flip elem . keys . eltAttrs + +getAttr :: Element -> AttrName -> Maybe AttrValue +getAttr = flip lookup . eltAttrs diff --git a/tests/Text/Taggy/CombinatorsSpec.hs b/tests/Text/Taggy/CombinatorsSpec.hs new file mode 100644 index 0000000..c714dc1 --- /dev/null +++ b/tests/Text/Taggy/CombinatorsSpec.hs @@ -0,0 +1,21 @@ +{-# LANGUAGE OverloadedStrings #-} + +module Text.Taggy.CombinatorsSpec where + +import Text.Taggy.Combinators +import Test.Hspec +import Text.Taggy.DOM +import Data.HashMap.Strict + +spec :: Spec +spec = do + let element = Element "html" (fromList [("xmlns", "http://www.w3.org/1999/xhtml")]) [] + describe "hasAttr" $ do + it "Tests whether an attribute is present." $ do + (element `hasAttr` "xmlns") `shouldBe` True + (element `hasAttr` "href") `shouldBe` False + describe "getAttr" $ do + it "Retrieves present attributes." $ + (element `getAttr` "xmlns") `shouldBe` Just "http://www.w3.org/1999/xhtml" + it "Nothing's missing attributes." $ + (element `getAttr` "style") `shouldBe` Nothing