CKTIS / HTML
HTML concept: html-parser
The HTML parser turns an encoded input stream into a DOM document through tokenization and stateful tree construction, with precisely defined recovery for many errors.
Version: WHATWG HTML Living Standard; index snapshot date recorded separately
Syntax
bytes → code points → tokens → tree construction → DOMFor text/html syntax, the browser first determines an encoding, decodes bytes, normalizes newlines, and passes code points to the tokenizer. Tokens feed the tree builder, which uses insertion modes, a stack of open elements, and a list of active formatting elements.
A parse error usually does not reject the document: the algorithm can ignore a token, close an element, or insert a node. A script element can pause the parser, and document.write() can supply more tokens and reenter tree construction; execution order therefore matters.
In a table context, disallowed content can be moved before the table by foster parenting, and the parser can insert tbody between table and tr; a td closes according to table-mode rules. Predictable repair does not make nonconforming source conforming, so a validator is still required.
The example parses misnested markup through DOMParser: the p element moves before the table, and a missing tbody is created. Its exact result exposes child order, the new parent, and the text that remains in the table.
Examples
<!doctype html>
<meta charset="utf-8">
<output id="result"></output>
<script>
const parsed = new DOMParser().parseFromString("<table id=t><p id=move>X</p><tr><td>Y</td></tr></table>", "text/html");
const table = parsed.querySelector("#t");
const moved = parsed.querySelector("#move");
const order = Array.from(parsed.body.children, node => node.tagName).join(",");
result.textContent = `order=${order}; movedBefore=${parsed.body.firstElementChild === moved}; tbody=${table.firstElementChild.tagName}; tableText=${table.textContent}`;
</script>
Result
order=P,TABLE; movedBefore=true; tbody=TBODY; tableText=Y
The executed example verifies foster parenting and an implied table container; it does not cover fragment parsing, encoding changes, or script execution during network loading.