Knowledge baseExam Center
ENEnglish
Choose language13

CKTIS / HTML

HTML concept: architecture

HTML processing architecture runs from bytes and encoding through the parser and DOM to scripts, styles, and presentation of the current document.

Version: WHATWG HTML Living Standard; index snapshot date recorded separately

Syntax

bytes → decoded code points → tokens → DOM → style/layout; scripts may mutate the DOM

A text/html resource begins as bytes that, after an encoding is selected, commonly UTF-8, are decoded to code points. The tokenizer and tree builder create a Document; malformed bytes, tentative encoding, and parser repair belong to this input boundary.

The parser builds the DOM incrementally, and an encountered script can pause parsing or mutate the tree, including through document.write(). After parsing, source is not a live model: scripts operate on nodes, and serialization can differ from the original bytes.

DOMParser and innerHTML already receive character strings, so they do not perform network encoding sniffing; they also create nodes in a defined context. Rendering consumes the connected tree and styles, while getComputedStyle() describes the styled result rather than source text.

The example uses TextEncoder and TextDecoder before passing text to DOMParser. After the paragraph is adopted, the current document reports display: block, but the script created by the detached document parser remains inert, giving scriptRan=false; the test does not reproduce network encoding sniffing.

Examples

<!doctype html>
<meta charset="utf-8">
<output id="result"></output>
<script>
const source = "<p id=decoded>✓</p><script>document.body.dataset.parserScript=\"ran\"<\/script>";
const bytes = new TextEncoder().encode(source);
const decoded = new TextDecoder("utf-8").decode(bytes);
const parsed = new DOMParser().parseFromString(decoded, "text/html");
const node = parsed.querySelector("#decoded");
const inertScript = parsed.querySelector("script");
document.body.append(node, inertScript);
result.textContent = `markBytes=${new TextEncoder().encode(node.textContent).length}; adopted=${node.ownerDocument === document}; display=${getComputedStyle(node).display}; scriptRan=${document.body.dataset.parserScript ?? "false"}`;
</script>

Result

markBytes=3; adopted=true; display=block; scriptRan=false

The executed example verifies decoding, DOM-adoption, and presentation boundaries, and that merely moving a parser-created script from the detached document is not ordinary code execution.

Sources