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

Safe HaskellSafe
LanguageHaskell2010

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#tag-identification".

Synopsis

Matching Tags

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

Match an opening tag

Examples

Expand

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

Expand

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

Expand

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

Expand

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

Expand

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

Expand

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

Expand

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

Expand

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

Expand

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

Expand

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 #

Does any attribute name/value match the predicate.

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

Does any attribute name match the predicate.

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

Does any attribute value match the predicate.

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

Does any attribute name/value match.

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

Does any attribute name match.

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

Does any attribute value match.

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

Get the tags under tags with a given name where the attributes match some predicate.