mirror of
https://github.com/alpmestan/taggy.git
synced 2024-09-11 15:05:40 +03:00
Two simple combinators on Element: getAttr and hasAttr.
This commit is contained in:
parent
11b57a4bad
commit
773419b9a0
11
src/Text/Taggy/Combinators.hs
Normal file
11
src/Text/Taggy/Combinators.hs
Normal file
@ -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
|
21
tests/Text/Taggy/CombinatorsSpec.hs
Normal file
21
tests/Text/Taggy/CombinatorsSpec.hs
Normal file
@ -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
|
Loading…
Reference in New Issue
Block a user