Manipulation
Accessing Element Properties
tag(elem)— get the tag of an element as a symbolattrs(elem)— return the attributes dictgetattr(elem, name[, default])— get attribute value ornothing/defaultsetattr!(elem, name, value)— set attribute (validated)hasattr(elem, name)— check whether an attribute exists
Children and Text
children(elem)— return the children arraytext(elem)— recursively extract all text content
Finding Elements
findfirst(f, elem)/findfirst(f, doc)— find the first element (pre-order DFS) matching predicatefgetbyid(elem, id)/getbyid(doc, id)— find element byidattributeapplyif!(condition, f!, elem)— apply a mutating function to all matching elements
Examples
# Find the first <a> tag
findfirst(x -> tag(x) == :a, doc.root)
# Find element by id
getbyid(doc, "main-content")
# Apply a function to all matching elements
applyif!(x -> tag(x) == :div, x -> setattr!(x, "class", "wide"), doc)CSS Class Helpers
hasclass(elem, cls)— check if element has a CSS classaddclass!(elem, cls)— add a CSS class (no-op if already present)removeclass!(elem, cls)— remove a CSS classreplaceclass!(elem, old, new)— replace one class with another; passnothingto remove
Examples
el = HTMLElement(:div)
addclass!(el, "active")
addclass!(el, "highlight")
hasclass(el, "active") # true
replaceclass!(el, "active", "inactive")
removeclass!(el, "highlight")Validation
Attribute names and CSS class values are validated automatically when using setattr!, addclass!, replaceclass!, and bracket assignment.
Invalid attribute names raise InvalidAttributeException. Invalid class values raise ArgumentError or InvalidAttributeException.
Use the @validate macro directly:
@validate :attr "data-value" # validate attribute name
@validate :class "my-class" # validate class name
@validate :attr ["id", "data-value"] # validate multiple names
@validate :class ["cls1", "cls2"] # validate multiple classesPretty Printing
prettyprint(elem) / prettyprint(io, elem) — output nicely indented HTML:
prettyprint(doc) # print document to stdout
prettyprint(io, doc) # print to IO stream
prettyprint(elem) # print element