tagsoup-0.13.10: Parsing and extracting information from (possibly malformed) HTML/XML documents

Safe HaskellSafe
LanguageHaskell98

Text.HTML.TagSoup.Match

Contents

Description

Combinators to match tags. Some people prefer to use (~==) from Text.HTML.TagSoup, others prefer these more structured combinators. Which you use is personal preference.

The functions below offer maximum flexibility for matching tags. Using tagOpen, for example, you can match all links or buttons that have the "btn" class.

For simple uses cases—like matching all comment tags, or matching opening <a> tags, use the tag identification functions in Text.HTML.TagSoup.

Synopsis

Matching Tags

tagOpen :: (str -> Bool) -> ([Attribute str] -> Bool) -> Tag str -> Bool Source

Match an opening tag

Examples

Matching an opening <a> tag with a "btn" class:

>>> let tag = TagOpen "a" [("class", "btn")]
>>> tagOpen (== "a") (\attrs -> any (== ("class", "btn")) attrs) tag
True

tagClose :: (str -> Bool) -> Tag str -> Bool Source

Match a closing tag

Examples

Matching a closing </a> tag:

>>> tagClose (== "a") (TagClose "a")
True
>>> tagClose (== "a") (TagOpen "a" [])
False

tagText :: (str -> Bool) -> Tag str -> Bool Source

Match text tags

Examples

Match all text tags:

>>> let tags = parseTags "<p>This is a paragraph</p>"
[TagOpen "p" [],TagText "This is a paragraph",TagClose "p"]
>>> filter (tagText (const True)) tags
[TagText "This is a paragraph"]

tagComment :: (str -> Bool) -> Tag str -> Bool Source

Match comment tags

Examples

Matching comment tags that include an exclamation mark:

>>> let tags = parseTags "<!--This is a comment-->"
[TagComment "This is a comment!"]
>>> all (tagComment (\s -> '!' `elem` s)) tags
True

tagOpenLit :: Eq str => str -> ([Attribute str] -> Bool) -> Tag str -> Bool Source

Match an opening tag's name literally

Examples

Matching <a> tags with the id "foo":

>>> let tag = TagOpen "a" [("id", "foo")]
TagOpen "a" [("id","foo")]
>>> tagOpenLit "a" (\attrs -> any (== ("id", "foo")) attrs) tag
True

tagCloseLit :: Eq str => str -> Tag str -> Bool Source

Match a closing tag's name literally

Examples

Match a closing <a> tag:

>>> tagCloseLit "a" (TagClose "a")
True
>>> tagCloseLit "a" (TagClose "em")
False

tagOpenAttrLit :: Eq str => str -> Attribute str -> Tag str -> Bool Source

Match an opening tag's name literally, and at least one of its attributes

Examples

Matching a <div> tag with the id "foo":

>>> tagOpenAttrLit "div" ("id", "foo") (TagOpen "div" [("id", "foo")])
True

tagOpenAttrNameLit :: Eq str => str -> str -> (str -> Bool) -> Tag str -> Bool Source

Match a tag with given name, that contains an attribute with given name, that satisfies a predicate. If an attribute occurs multiple times, all occurrences are checked.

Examples

Matching an <a> tag with an ID that starts with "comment-":

>>> let commentTag = TagOpen "a" [("id", "comment-45678")]
>>> tagOpenAttrNameLit "a" "id" (\idValue -> "comment-" `Data.List.isPrefixOf` idValue) commentTag
True

tagOpenNameLit :: Eq str => str -> Tag str -> Bool Source

Check if the 'Tag str' is TagOpen and matches the given name

Examples

Matching an <a> tag:

>>> tagOpenNameLit "a" (TagOpen "a" [])
True
>>> tagOpenNameLit "a" (TagOpen "div" [])
False

tagCloseNameLit :: Eq str => str -> Tag str -> Bool Source

Check if the 'Tag str' is TagClose and matches the given name

Examples

Matching a closing </a> tag:

>>> tagCloseNameLit "a" (TagClose "a")
True
>>> tagCloseNameLit "a" (TagClose "div")
False

Matching attributes

anyAttr :: ((str, str) -> Bool) -> [Attribute str] -> Bool Source

anyAttrName :: (str -> Bool) -> [Attribute str] -> Bool Source

anyAttrValue :: (str -> Bool) -> [Attribute str] -> Bool Source

anyAttrLit :: Eq str => (str, str) -> [Attribute str] -> Bool Source

anyAttrNameLit :: Eq str => str -> [Attribute str] -> Bool Source

anyAttrValueLit :: Eq str => str -> [Attribute str] -> Bool Source

getTagContent :: Eq str => str -> ([Attribute str] -> Bool) -> [Tag str] -> [Tag str] Source