html - build and render an HTML tree
Import with import "html.j" as html;. Assembles an HTML element tree and renders it to a correctly-escaped HTML5 string. Pure Jennifer over strings and lists, so it runs on either binary; a tolerant parser reads HTML back into the same tree. It is the shared output layer for HTML-emitting code (a Markdown renderer, a documentation generator, a view layer).
use io;
import "html.j" as html;
def kids as list of html.Node init [];
$kids[] = html.text("hi & bye");
def p as html.Node init html.element("p", [], $kids);
io.printf("%s\n", html.render($p)); # <p>hi & bye</p>Runnable: examples/modules/html_demo.j.
The node model
An HTML tree is Node values. A node is one of three kinds, tagged by its kind field (a NodeKind enum), and is built with a constructor rather than a literal:
| Kind | Built with | Renders as |
|---|---|---|
NodeKind.Element | element(tag, attrs, children) | <tag ...>children</tag> (or a void tag) |
NodeKind.Text | text(s) | s, HTML-escaped |
NodeKind.Raw | raw(s) | s, verbatim (already-trusted markup only) |
# html.NodeKind / html.Node / html.Attr, as html.j declares (and exports) them
def enum NodeKind { Element, Text, Raw };
def struct Node {
kind as NodeKind, tag as string,
attrs as list of Attr, children as list of Node, text as string
};
def struct Attr { name as string, value as string, boolean as bool };render dispatches over NodeKind with an exhaustive match, so a new node kind cannot be added without handling it in the renderer.
Children are supplied to element as a list of Node you build first (the append sugar does not chain into a struct field, so build the list in a variable, then pass it). Attributes are a list of Attr built with attr (a normal name="value" attribute) or boolAttr (a valueless boolean attribute).
Surface
| Call | Returns | Notes |
|---|---|---|
html.element(tag, attrs, children) | Node | An element node. Pass [] for no attributes or no children. |
html.text(s) | Node | A text node; s is HTML-escaped on render. |
html.raw(s) | Node | A verbatim node; s is not escaped. Trusted markup only. |
html.attr(name, value) | Attr | One attribute; value is escaped in attribute context on render. |
html.boolAttr(name) | Attr | A boolean (valueless) attribute; renders as the bare name (disabled). |
html.render(node) | string | Serialize a node and its subtree to HTML5. |
html.renderAll(nodes) | string | Serialize a list of Node fragment in order. |
html.renderXhtml(node) | string | Like render, but well-formed XHTML: void elements self-close (<hr />) and boolean attributes expand (disabled="disabled") - an XHTML / EPUB content document. |
html.renderAllXhtml(nodes) | string | renderAll in XHTML mode (see renderXhtml). |
html.escape(s) | string | HTML-escape a bare string: & < > " '. Safe in text and inside a quoted attribute value; not a URL check (use safeUrl). |
html.unescape(s) | string | The inverse of escape: decode < > " ' ' & (& last, so &lt; stays <). Only the metacharacter entities, not / numeric references. |
html.safeUrl(url) | string | The URL if its scheme is http / https / mailto, else "#" (anti-XSS href/src gate). |
Escaping
Escaping is automatic and context-aware; a value is escaped exactly once:
- Text nodes escape
&,<,>(with&first, so an existing entity is not double-escaped). - Attribute values additionally escape
", since they render inside double quotes. html.escape(for strings you splice into markup by hand) escapes both quotes as well, so its result is safe in text and in a single- or double-quoted attribute value.renderdoes not use it: text nodes keep their quotes readable.rawnodes are emitted verbatim - the escape hatch for markup you have already produced (an SVG blob, a rendered sub-tree). Only pass trusted content.
use io;
import "html.j" as html;
def a as list of html.Attr init [];
$a[] = html.attr("title", "a \"b\" <c>");
io.printf("%s\n", html.render(html.element("span", $a, [])));
# <span title="a "b" <c>"></span>html.escape(s) escapes a string without building a node. It also escapes both quotes, so it is safe wherever a hand-built template puts the result:
import "html.j" as html;
def query as string init 'say "hi" <b>';
def page as string init '<input value="' + html.escape($query) + '">';Boolean attributes
HTML boolean attributes - disabled, checked, selected, required, readonly, multiple, autofocus, and the like - carry no value; presence means true. html.boolAttr(name) renders as the bare name, not name="". Normal and boolean attributes mix freely on one element, in the order you append them; a boolean-attribute name is validated exactly like an attr name.
use io;
import "html.j" as html;
def a as list of html.Attr init [];
$a[] = html.attr("type", "checkbox");
$a[] = html.boolAttr("checked");
$a[] = html.boolAttr("required");
io.printf("%s\n", html.render(html.element("input", $a, [])));
# <input type="checkbox" checked required>Safe tag / attribute names
html.element and html.attr validate the tag / attribute name against [A-Za-z][A-Za-z0-9-]* and throw an Error of kind "html" on anything else. A name is markup structure, not data, so unlike a value it cannot be escaped: html.attr("x onclick=alert(1)", "y") or a tag built from an untrusted string would otherwise inject a live attribute or element. Legal names ("data-id", "h1", "aria-label") build normally; a name carrying a space, >, =, /, or a quote is rejected at construction.
render checks names again, since a Node or Attr can also be built as a struct literal or come from parse: it accepts any name without whitespace, a quote, <, >, /, = or a control character (so a parsed @click or v-bind:x round-trips) and throws an "html" error on anything else. parse drops an attribute whose name holds a quote or < - always a parse error in HTML. Both errors repeat the refused name quoted the way Go's %q does (" and \ escaped, CR / LF / tab as \r / \n / \t, other control and format characters as \xNN bytes, cut after 64 characters), so a name from a document or a request cannot forge lines in a log.
Safe URLs in links
html.safeUrl(url) returns the URL when its scheme is http, https, or mailto, and "#" otherwise, so a javascript: (or data:) URL built from untrusted input cannot become a live href / src. Whitespace and control characters are ignored while reading the scheme, so "java\tscript:..." is still caught; a relative reference (no scheme) is returned unchanged.
import "html.j" as html;
def userUrl as string init "javascript:alert(1)";
def a as list of html.Attr init [];
$a[] = html.attr("href", html.safeUrl($userUrl)); # "#" if $userUrl is javascript:...Void elements
The HTML5 void elements - area base br col embed hr img input link meta param source track wbr - render with no closing tag, and any children passed to them are dropped (they cannot have content). The tag is matched case-insensitively.
use io;
import "html.j" as html;
def a as list of html.Attr init [];
$a[] = html.attr("src", "logo.png");
io.printf("%s\n", html.render(html.element("img", $a, []))); # <img src="logo.png">Fragments
render serializes a single node; renderAll serializes a list of sibling nodes with no wrapping element - a document fragment:
use io;
import "html.j" as html;
def heading as list of html.Node init [html.text("Title")];
def parts as list of html.Node init [];
$parts[] = html.element("h1", [], $heading);
$parts[] = html.element("hr", [], []);
io.printf("%s\n", html.renderAll($parts));Parsing
html.parse(src) reads an HTML string into the same Node tree the builders produce, so a document can be walked, edited, and re-rendered with render. The result is a synthetic #root element whose children are the document's top-level nodes:
use io;
import "html.j" as html;
def doc as html.Node init html.parse("<ul class=fruit><li>apples</li><li>figs</li></ul>");
io.printf("%s\n", html.attrOf(html.get($doc, "ul"), "class")); # fruit
for (def li in html.findAll($doc, "ul/li")) {
io.printf("%s\n", $li.children[0].text); # apples, figs
}Because Node is a plain struct, read .tag / .attrs / .children / .text directly; the query helpers add attribute lookup and tree search:
| Call | Returns |
|---|---|
html.attrOf(node, name) | an attribute's value, or "" |
html.hasAttr(node, name) | whether the attribute is present |
html.get(node, sel) | the first element matching sel (an empty node if none) |
html.findAll(node, sel) | every element matching sel |
html.has(node, sel) | whether any element matches sel |
A selector is an XPath-ish /-path; each step is a tag name, * (any element), or name[k] (the k-th such child, 1-based). Steps match direct element children (text nodes are skipped), so "article/ul/li[2]" is the second li under the ul under the article.
The parser is tolerant of real-world HTML: void elements (<br> / <img>), self-closing tags, unquoted (id=x) and boolean (disabled) attributes, mismatched nesting (<b>x</i> closes the <b>), comments, <!DOCTYPE>, and script / style raw text are handled, not rejected; the common entities (& < > " ' ') decode. A nesting depth over 256 or more than 200,000 nodes is a catchable Error{kind: "limit"}, so a malicious document cannot force an unbounded parse. On jennifer-tiny the interpreter's own nesting cap (64 value levels, two per element) throws "limit" first, near depth 31.
Out of scope
The parser handles the common real-world syntax listed under Parsing, not the whole HTML5 tree-construction algorithm. There is no pretty-printing / indentation pass; output is compact, which round-trips and diffs predictably. A <!DOCTYPE html> prologue is not emitted; prepend html.raw("<!DOCTYPE html>") (or a literal string) when you need a full document.
See also
- strings.md -
replace/lower, which the escaping and void-element check build on. - lists.md -
contains, used for the void-element lookup. - modules/index.md - the module catalog and import rules.
Academy talks
| Talk | What it shows |
|---|---|
| Your own small blog with Jennifer | building escaped HTML in code |
API reference
Every exported function, struct and constant of html, with its signature, generated from the module's doc comments: the html API reference.