mirror of
https://github.com/alpmestan/taggy.git
synced 2024-09-11 15:05:40 +03:00
Combinators: trees extracts subtrees, including the target parent.
This commit is contained in:
parent
349f7b7d50
commit
e7487d337c
@ -1,6 +1,6 @@
|
|||||||
{-# LANGUAGE LambdaCase #-}
|
{-# 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 Prelude hiding (lookup)
|
||||||
import Data.Monoid (mconcat)
|
import Data.Monoid (mconcat)
|
||||||
@ -29,6 +29,9 @@ innerText = mconcat . map getContent . eltChildren
|
|||||||
(/&) element [] = [element]
|
(/&) element [] = [element]
|
||||||
(/&) element (x:xs) = (/& xs) <=< filter x . catElements $ eltChildren element
|
(/&) element (x:xs) = (/& xs) <=< filter x . catElements $ eltChildren element
|
||||||
|
|
||||||
|
trees :: Element -> [Element]
|
||||||
|
trees = ap (:) subtrees
|
||||||
|
|
||||||
subtrees :: Element -> [Element]
|
subtrees :: Element -> [Element]
|
||||||
subtrees = ap (:) subtrees <=< catElements . eltChildren
|
subtrees = ap (:) subtrees <=< catElements . eltChildren
|
||||||
|
|
||||||
|
@ -37,6 +37,9 @@ spec = do
|
|||||||
element /& [] `shouldBe` [element]
|
element /& [] `shouldBe` [element]
|
||||||
element /& [const False] `shouldBe` []
|
element /& [const False] `shouldBe` []
|
||||||
element /& [flip hasAttr "class", flip hasName "quux"] `shouldBe` [Element "quux" mempty mempty]
|
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
|
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
|
length (subtrees element) `shouldBe` 3
|
||||||
|
Loading…
Reference in New Issue
Block a user