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) tagTrue
:: a typeCtrl KGHC 9.10.3 · lts/ghc-9.10.x · c74966e · 2026-09-27
Moduletagsoup-0.14.8Haskell2010
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.
Match an opening tag
Matching an opening <a> tag with a "btn" class:
let tag = TagOpen "a" [("class", "btn")]tagOpen (== "a") (\attrs -> any (== ("class", "btn")) attrs) tagTrue
Match a closing tag
Matching a closing </a> tag:
tagClose (== "a") (TagClose "a")True
tagClose (== "a") (TagOpen "a" [])False
Match text tags
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"]
Match comment tags
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)) tagsTrue
Match an opening tag's name literally
Matching <a> tags with the id "foo":
let tag = TagOpen "a" [("id", "foo")]TagOpen "a" [("id","foo")]tagOpenLit "a" (\attrs -> any (== ("id", "foo")) attrs) tagTrue
Match a closing tag's name literally
Match a closing <a> tag:
tagCloseLit "a" (TagClose "a")True
tagCloseLit "a" (TagClose "em")False
Match an opening tag's name literally, and at least one of its attributes
Matching a <div> tag with the id "foo":
tagOpenAttrLit "div" ("id", "foo") (TagOpen "div" [("id", "foo")])True
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.
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) commentTagTrue
Check if the 'Tag str' is TagOpen and matches the given name
Matching an <a> tag:
tagOpenNameLit "a" (TagOpen "a" [])True
tagOpenNameLit "a" (TagOpen "div" [])False
Check if the 'Tag str' is TagClose and matches the given name
Matching a closing </a> tag:
tagCloseNameLit "a" (TagClose "a")True
tagCloseNameLit "a" (TagClose "div")False
Does any attribute name/value match the predicate.
Does any attribute name match the predicate.
Does any attribute value match the predicate.
Does any attribute name/value match.
Does any attribute name match.
Does any attribute value match.
Get the tags under tags with a given name where the attributes match some predicate.