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

Safe Haskell | Safe-Inferred
Language | Haskell2010

Text.HTML.TagSoup.Match

Contents

- Matching Tags
- Matching attributes

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

- tagOpen :: (str -> Bool) -> ([Attribute str] -> Bool) -> Tag str -> Bool
- tagClose :: (str -> Bool) -> Tag str -> Bool
- tagText :: (str -> Bool) -> Tag str -> Bool
- tagComment :: (str -> Bool) -> Tag str -> Bool
- tagOpenLit :: Eq str => str -> ([Attribute str] -> Bool) -> Tag str -> Bool
- tagCloseLit :: Eq str => str -> Tag str -> Bool
- tagOpenAttrLit :: Eq str => str -> Attribute str -> Tag str -> Bool
- tagOpenAttrNameLit :: Eq str => str -> str -> (str -> Bool) -> Tag str -> Bool
- tagOpenNameLit :: Eq str => str -> Tag str -> Bool
- tagCloseNameLit :: Eq str => str -> Tag str -> Bool
- anyAttr :: ((str, str) -> Bool) -> [Attribute str] -> Bool
- anyAttrName :: (str -> Bool) -> [Attribute str] -> Bool
- anyAttrValue :: (str -> Bool) -> [Attribute str] -> Bool
- anyAttrLit :: Eq str => (str, str) -> [Attribute str] -> Bool
- anyAttrNameLit :: Eq str => str -> [Attribute str] -> Bool
- anyAttrValueLit :: Eq str => str -> [Attribute str] -> Bool
- getTagContent :: Eq str => str -> ([Attribute str] -> Bool) -> [Tag str] -> [Tag str]

# Matching Tags

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

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 #

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 #

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 #

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 #

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 #

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 #

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 #

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 #

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 #

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 #

Does any attribute name/value match the predicate.

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

Does any attribute name match the predicate.

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

Does any attribute value match the predicate.

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

Does any attribute name/value match.

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

Does any attribute name match.

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

Does any attribute value match.

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

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