Skip to content
Jennifer Programming Language

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

NameWhat it is
attrBuild one name/value attribute for an element.
attrOfThe value of an element's attribute by name, or "" if it has no such attribute.
boolAttrBuild a boolean (valueless) HTML attribute - disabled, checked, selected, required, readonly,...
elementBuild an element node from a tag, its attributes, and its children.
escapeReturn s with every HTML metacharacter replaced by an entity: & (first, so an escaped entity is not re-es...
findAllEvery element matching an XPath-ish selector relative to node: /-separated steps, each a tag name,...
getThe first element matching selector (see findAll), or an empty element node (.tag == "") if there is ...
hasWhether any element matches selector (see findAll).
hasAttrWhether an element has an attribute by name (including a valueless boolean one).
parseParse an HTML string into a Node tree.
rawBuild a node whose content is emitted verbatim - for already-trusted markup only, since it is not escaped.
renderSerialize a node and its subtree to an HTML5 string.
renderAllSerialize a list of sibling nodes (a fragment) in order.
renderAllXhtmlSerialize a fragment as well-formed XHTML (see renderXhtml).
renderXhtmlSerialize a node and its subtree to a well-formed XHTML string: void elements self-close (<hr />) and boo...
safeUrlReturn a URL safe to place in an href / src attribute, or "#" if its scheme is not one of http /...
textBuild a text node; its content is HTML-escaped on render.
unescapeDecode the common HTML entities in a string - &lt; / &gt; / &quot; / &#39; / &apos; / &amp; to ...
Attrstruct: One HTML attribute.
Nodestruct: A node is one of three kinds, tagged by kind: "element" (tag + attrs + children), "text" (escaped content...
NodeKindenum: The kind of an HTML node: Element (a tag with attributes and children), Text (escaped text content), or...

Functions

attr

jennifer
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

jennifer
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

jennifer
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

jennifer
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

jennifer
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

jennifer
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

jennifer
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

jennifer
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

jennifer
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

jennifer
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 #root element containing the parsed top-level nodes
Throws Error
kind "limit" if the document exceeds the depth or node budget

raw

jennifer
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

jennifer
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

jennifer
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

jennifer
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

jennifer
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

jennifer
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

jennifer
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

jennifer
html.unescape(s as string)

Decode the common HTML entities in a string - &lt; / &gt; / &quot; / &#39; / &apos; / &amp; to their characters - the inverse of escape. &amp; is decoded last, so &amp;lt; yields the literal &lt;, 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 (&nbsp; / &copy; / &mdash; / ...) and numeric refs (&#8212;) 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, ...).

jennifer
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.

jennifer
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, or Raw)
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).