Build a tree from HTML tag tokens, list its tags depth-first and by level, and serialise it back
Problem
A tokenizer has already split an HTML document into tokens: one string per tag or text run.
["<html>", "<body>", "<div>", "<p>", "hello", "</p>", "</div>", "</body>", "</html>"]Unlike question 122, there is no string to scan: your job starts from these tokens. Build a tree from them, then walk it.
Nodes
- An element is
{ tag, children }, wheretagis the lower-case tag name andchildrenis an array of nodes in document order. - A text node is
{ text }, holding the token exactly as given.
Tokens
<name>opens an element and</name>closes one. A name is a letter followed by letters, digits or hyphens, so<h1>and<my-widget>are valid. Names are case-insensitive:<DIV>and</div>match.- Any other token that starts with
<is invalid. Tag tokens have no attributes. - A token that does not start with
<is text. Empty strings are skipped. Text made only of whitespace is ignored outside the root element and kept inside it.
1. buildTree(tokens) returns the root element. The tokens describe exactly one root element.
2. listTags(root) returns the tag names in depth-first order: an element comes before its children, and children are visited left to right. Text nodes are skipped.
3. tagsByLevel(root) returns one array of tag names per depth, from the root down, each left to right.
4. toHTML(node) turns an element or text node back into a string: <tag>, then the children, then </tag>. Text is written as it is, so toHTML(buildTree(tokens)) gives back tokens.join("") for lower-case tags.
const root = buildTree(["<html>", "<body>", "<div>", "<p>", "hello", "</p>", "</div>", "</body>", "</html>"]);
// { tag: "html", children: [{ tag: "body", children: [{ tag: "div", children: [
// { tag: "p", children: [{ text: "hello" }] } ] }] }] }
listTags(root); // ["html", "body", "div", "p"]
tagsByLevel(buildTree(["<ul>", "<li>", "a", "</li>", "<li>", "<b>", "b", "</b>", "</li>", "</ul>"]));
// [["ul"], ["li", "li"], ["b"]]
toHTML(root); // "<html><body><div><p>hello</p></div></body></html>"Invalid input (follow-up level). First solve it for valid HTML. Then make buildTree throw a SyntaxError whose message contains these words:
| Problem | Message contains |
|---|---|
A closing tag that is not the innermost open element, such as </div> while <p> is open |
mismatched </div> |
| A closing tag when no element is open | unexpected </p> |
| Tokens that end with elements still open (name the innermost one) | unclosed <p> |
| Text or a second element before or after the root element | outside the root |
| No root element at all | no root element |
A token that starts with < but is not a valid tag, such as <div class="a"> |
invalid tag |
Throw a TypeError if tokens is not an array or a token is not a string. All four functions must work on a tree 100,000 levels deep.
Ref. UR-1540 · © UIRound · uiround.com/questions/build-a-tree-from-html-tag-tokens-list-its-tags-depth-first-and-by-level-and-serialise-it-back
Unlock this question
Its tests and the tested solution are part of Pro.
- 2
- code listings
- 7
- edge cases
- 5
- follow-up questions
What's inside
- Tests that check your solution
- Step-by-step
- Approach
- Solution
- Complexity
- Edge cases
- Follow-up questions
- Card worldwide
- UPI and net banking in India
- One-time payment, nothing renews
Private, and saved in this browser as you type. All your notes are on My lists.