html API reference
Build an HTML element tree and render it to a correctly escaped HTML5 string. Pure Jennifer over strings and lists - a writer, not a parser, so it has no dependency on an XML parser: serialization is a handful of string operations. The shared output layer any HTML-emitting consumer reuses (a Markdown renderer, a documentation generator, a view layer). Text nodes are escaped on render (& < >); attribute values also escape "; a raw node passes through verbatim for already-trusted markup. Void elements (br, img, ...) render without a closing tag and drop children.
Import with import "html.j" as html;. See the html guide for prose and examples.
Contents
| Name | What it is |
|---|---|
attr | Build one name/value attribute for an element. |
attrOf | The value of an element's attribute by name, or "" if it has no such attribute. |
boolAttr | Build a boolean (valueless) HTML attribute - disabled, checked, selected, required, readonly,... |
element | Build an element node from a tag, its attributes, and its children. |
escape | Return s with every HTML metacharacter replaced by an entity: & (first, so an escaped entity is not re-es... |
findAll | Every element matching an XPath-ish selector relative to node: /-separated steps, each a tag name,... |
get | The first element matching selector (see findAll), or an empty element node (.tag == "") if there is ... |
has | Whether any element matches selector (see findAll). |
hasAttr | Whether an element has an attribute by name (including a valueless boolean one). |
parse | Parse an HTML string into a Node tree. |
raw | Build a node whose content is emitted verbatim - for already-trusted markup only, since it is not escaped. |
render | Serialize a node and its subtree to an HTML5 string. |
renderAll | Serialize a list of sibling nodes (a fragment) in order. |
renderAllXhtml | Serialize a fragment as well-formed XHTML (see renderXhtml). |
renderXhtml | Serialize a node and its subtree to a well-formed XHTML string: void elements self-close (<hr />) and boo... |
safeUrl | Return a URL safe to place in an href / src attribute, or "#" if its scheme is not one of http /... |
text | Build a text node; its content is HTML-escaped on render. |
unescape | Decode the common HTML entities in a string - < / > / " / ' / ' / & to ... |
Attr | struct: One HTML attribute. |
Node | struct: A node is one of three kinds, tagged by kind: "element" (tag + attrs + children), "text" (escaped content... |
NodeKind | enum: The kind of an HTML node: Element (a tag with attributes and children), Text (escaped text content), or... |
Functions
attr
html.attr(name as string, value as string)Build one name/value attribute for an element.
name- the attribute name
value- the attribute value
- Returns
Attr - the attribute
- Throws
Error - kind "html" when the attribute name does not match
[A-Za-z][A-Za-z0-9-]*; the message quotes it with control characters escaped
attrOf
html.attrOf(node as Node, name as string)The value of an element's attribute by name, or "" if it has no such attribute. (Read .attrs directly for the full list.)
node- the element node
name- the attribute name
- Returns
string - the attribute value, or ""
boolAttr
html.boolAttr(name as string)Build a boolean (valueless) HTML attribute - disabled, checked, selected, required, readonly, multiple, autofocus, and the like. It renders as the bare name (<input disabled>), not name="". The name is validated exactly as attr validates it.
name- the attribute name
- Returns
Attr - the boolean attribute
- Throws
Error - kind "html" when the attribute name does not match
[A-Za-z][A-Za-z0-9-]*; the message quotes it with control characters escaped
element
html.element(tag as string, attrs as list of Attr, children as list of Node)Build an element node from a tag, its attributes, and its children. Pass [] for either when there are none.
tag- the element tag name
attrs- the element's attributes
children- the element's child nodes
- Returns
Node - the element node
- Throws
Error - kind "html" when the tag name does not match
[A-Za-z][A-Za-z0-9-]*; the message quotes it with control characters escaped
escape
html.escape(s as string)Return s with every HTML metacharacter replaced by an entity: & (first, so an escaped entity is not re-escaped), <, >, " and '. The result is safe in text content and inside a single- or double-quoted attribute value, so a hand-built '<input value="' + html.escape($q) + '">' cannot break out of the quotes. It does not make a URL safe: gate an href / src with safeUrl.
s- the string to escape
- Returns
string - the escaped string
findAll
html.findAll(node as Node, selector as string)Every element matching an XPath-ish selector relative to node: /-separated steps, each a tag name, * (any element), or name[k] (the k-th such child, 1-based). Steps match direct element children; text nodes are skipped.
node- the node to search under
selector- the selector path (e.g. "body/ul/li")
- Returns
list of Node - the matching element nodes (empty if none)
get
html.get(node as Node, selector as string)The first element matching selector (see findAll), or an empty element node (.tag == "") if there is no match.
node- the node to search under
selector- the selector path
- Returns
Node - the first match, or an empty element node
has
html.has(node as Node, selector as string)Whether any element matches selector (see findAll).
node- the node to search under
selector- the selector path
- Returns
bool - true if at least one element matches
hasAttr
html.hasAttr(node as Node, name as string)Whether an element has an attribute by name (including a valueless boolean one).
node- the element node
name- the attribute name
- Returns
bool - true if present
parse
html.parse(src as string)Parse an HTML string into a Node tree. The result is a synthetic #root element whose children are the document's top-level nodes; walk it with .children / get / findAll, and re-serialize any node with render. Tolerant of real-world HTML (void / self-closing tags, unquoted attributes, mismatched nesting, comments, DOCTYPE, script / style raw text).
src- the HTML source
- Returns
Node - the
#rootelement containing the parsed top-level nodes - Throws
Error - kind "limit" if the document exceeds the depth or node budget
raw
html.raw(s as string)Build a node whose content is emitted verbatim - for already-trusted markup only, since it is not escaped.
s- the verbatim markup
- Returns
Node - the raw node
render
html.render(node as Node)Serialize a node and its subtree to an HTML5 string.
node- the node to render
- Returns
string - the rendered HTML
- Throws
Error - kind "html" for a tag or attribute name that would break out of the tag; the message quotes it with control characters escaped
renderAll
html.renderAll(nodes as list of Node)Serialize a list of sibling nodes (a fragment) in order.
nodes- the sibling nodes
- Returns
string - the rendered HTML fragment
- Throws
Error - kind "html" for a tag or attribute name that would break out of the tag; the message quotes it with control characters escaped
renderAllXhtml
html.renderAllXhtml(nodes as list of Node)Serialize a fragment as well-formed XHTML (see renderXhtml).
nodes- the sibling nodes
- Returns
string - the rendered XHTML fragment
- Throws
Error - kind "html" for a tag or attribute name that would break out of the tag; the message quotes it with control characters escaped
renderXhtml
html.renderXhtml(node as Node)Serialize a node and its subtree to a well-formed XHTML string: void elements self-close (<hr />) and boolean attributes expand (disabled="disabled"), so the output parses as XML - an XHTML / EPUB content document. Identical to render otherwise (text and attribute escaping is XML-clean already).
node- the node to render
- Returns
string - the rendered XHTML
- Throws
Error - kind "html" for a tag or attribute name that would break out of the tag; the message quotes it with control characters escaped
safeUrl
html.safeUrl(url as string)Return a URL safe to place in an href / src attribute, or "#" if its scheme is not one of http / https / mailto. Whitespace and control characters are ignored while reading the scheme (so "java\tscript:..." is still caught), and a relative reference (no scheme) is returned unchanged. This is the anti-XSS gate for building links from untrusted input.
url- the URL to check
- Returns
string - the URL if its scheme is allowed, else
"#"
text
html.text(s as string)Build a text node; its content is HTML-escaped on render.
s- the text content
- Returns
Node - the text node
unescape
html.unescape(s as string)Decode the common HTML entities in a string - < / > / " / ' / ' / & to their characters - the inverse of escape. & is decoded last, so &lt; yields the literal <, not <. A string with no & is returned unchanged.
Scope is deliberately the metacharacter entities only: the ones any standard text-context escaper (including this module's escape / escapeAttr) emits, so a round-trip is exact. It is not a general HTML5 entity decoder - the ~2000 named references for authoring ( / © / — / ...) and numeric refs (—) are out of scope, since decoding those means shipping the full named table plus numeric parsing, not the metacharacter round-trip this serves.
s- the text to decode
- Returns
string - the decoded text
Structs
Attr
One HTML attribute. A normal attribute has a name and a value and renders as name="value"; a boolean attribute (boolean: true, built by boolAttr) carries no value and renders as the bare name (disabled, checked, ...).
def struct Attr {
name as string,
value as string,
boolean as bool
};name- the attribute name
value- the attribute value (escaped on render; empty for a boolean attribute)
boolean- true for a valueless boolean attribute (rendered as the bare name)
Node
A node is one of three kinds, tagged by kind: "element" (tag + attrs + children), "text" (escaped content), or "raw" (verbatim content). The constructors below are the intended way to build one.
def struct Node {
kind as NodeKind,
tag as string,
attrs as list of Attr,
children as list of Node,
text as string
};kind- the node kind (
Element,Text, orRaw) tag- the element tag name (element nodes only)
attrs- the element's attributes (element nodes only)
children- the element's child nodes (element nodes only)
text- the content of a text or raw node
Enums
NodeKind
The kind of an HTML node: Element (a tag with attributes and children), Text (escaped text content), or Raw (verbatim, already-trusted markup).