AniUI Academy

Serialise a DOM-like tree to HTML

hardDOM & Utilities+350 XPPractice

This is what a server renderer does at the end of its work, and what a static site generator does for every page. The rules are small but unforgiving: one missed escape is an injection bug, and one </br> is invalid markup.

A node is either a plain string, meaning text content, or a plain object { tag, attrs, children } where attrs and children may both be missing. Implement serialize(node) returning the HTML string, with no whitespace added anywhere.

Text nodes are escaped by replacing & with &amp;, < with &lt; and > with &gt; — quotes are left alone in text. Attribute values are escaped the same way plus " becoming &quot;.

Attributes are emitted in the attrs object's key order, each as a space then name="value". A value of true is a boolean attribute and is emitted as the bare name with no =; a value of false, null or undefined is omitted entirely; anything else is converted with String(value) and escaped.

The void elements area, base, br, col, embed, hr, img, input, link, meta, source, track, wbr are emitted as <tag ... /> — a single space before the slash — and their children are ignored. Every other tag is emitted as <tag ...> then its serialised children joined with nothing, then </tag>.

Arguments

nodestring | { tag: string; attrs?: object; children?: Array }
A text string (treated as text content) or a plain-object element. Child entries can themselves be strings or further element objects.

Returns

string — The serialised HTML, with no whitespace added anywhere beyond the single space before /> on void tags.

Examples

serialize({ tag: "p", attrs: {}, children: ["Hi"] });
// => "<p>Hi</p>"

A plain element with one text child.

serialize({
  tag: "a",
  attrs: { href: "/x?a=1&b=2", title: 'He said "hi"' },
  children: ["Link"]
});
// => '<a href="/x?a=1&amp;b=2" title="He said &quot;hi&quot;">Link</a>'

Attribute values escape & and ".

serialize({
  tag: "input",
  attrs: { type: "checkbox", checked: true, disabled: false, value: null },
  children: []
});
// => '<input type="checkbox" checked />'

true is emitted as a bare boolean attribute; false and null are omitted.

serialize({ tag: "br" });
// => "<br />"

Void elements self-close and ignore any children.

What it has to do

  • Serialise a string node as escaped text, escaping &, < and > only.
  • Escape attribute values for &, <, > and ", and emit attributes in key order.
  • Emit true as a bare boolean attribute and omit false, null and undefined values.
  • Emit void elements as <tag ... /> and ignore any children they have.
  • Add no whitespace of your own, and tolerate a missing attrs or children.

Notes

  • Text content does _not_ escape quotes because quotes have no special meaning outside an attribute; attribute content does, because unescaped " would close the attribute early and open an injection.
  • The <br /> form (with a space before the slash) is what React emits and what every HTML parser accepts; XHTML required it, HTML5 is lenient but keeping it maximises compatibility.
Asked atMetaGoogle

Stuck? The javascript course covers everything this challenge needs.

Try it yourself
Loading playground...
5 tests · +350 XP

Discussion

Sign in to post
Sign in to join the conversation.Sign in

No comments yet. Be the first to say something.