Serialise a DOM-like tree to HTML
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 &, < with < and > with > — quotes are left alone in text. Attribute values are escaped the same way plus " becoming ".
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&b=2" title="He said "hi"">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
trueas a bare boolean attribute and omitfalse,nullandundefinedvalues. - Emit void elements as
<tag ... />and ignore any children they have. - Add no whitespace of your own, and tolerate a missing
attrsorchildren.
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.
Stuck? The javascript course covers everything this challenge needs.
- 1.serialises a simple element with text
- 2.escapes attribute values
- 3.emits boolean attributes bare and omits falsy ones
- 4.nests children and escapes text
- 5.handles a void element with no attrs key
These assertions run in your browser the moment you hit Run. Pass all of them to claim the XP.
Discussion
Sign in to postNo comments yet. Be the first to say something.