diff --git a/src/Text/Taggy/Combinators.hs b/src/Text/Taggy/Combinators.hs index 9df054a..28ba166 100644 --- a/src/Text/Taggy/Combinators.hs +++ b/src/Text/Taggy/Combinators.hs @@ -1,6 +1,6 @@ {-# LANGUAGE LambdaCase #-} -module Text.Taggy.Combinators (hasName, hasAttr, getAttr, innerText, (//), (/&), subtrees) where +module Text.Taggy.Combinators (hasName, hasAttr, getAttr, innerText, (//), (/&), trees, subtrees) where import Prelude hiding (lookup) import Data.Monoid (mconcat) @@ -29,6 +29,9 @@ innerText = mconcat . map getContent . eltChildren (/&) element [] = [element] (/&) element (x:xs) = (/& xs) <=< filter x . catElements $ eltChildren element +trees :: Element -> [Element] +trees = ap (:) subtrees + subtrees :: Element -> [Element] subtrees = ap (:) subtrees <=< catElements . eltChildren diff --git a/tests/Text/Taggy/CombinatorsSpec.hs b/tests/Text/Taggy/CombinatorsSpec.hs index 71bfd3d..396be0a 100644 --- a/tests/Text/Taggy/CombinatorsSpec.hs +++ b/tests/Text/Taggy/CombinatorsSpec.hs @@ -37,6 +37,9 @@ spec = do element /& [] `shouldBe` [element] element /& [const False] `shouldBe` [] element /& [flip hasAttr "class", flip hasName "quux"] `shouldBe` [Element "quux" mempty mempty] + describe "trees" $ do + it "Extracts all subtrees of its target, including the target (parent node)." $ do + trees element `shouldBe` element:subtrees element describe "subtrees" $ do - it "Extracts all subtrees of it's target." $ do + it "Extracts all subtrees of its target, excluding the target (parent node)." $ do length (subtrees element) `shouldBe` 3