What is W3C DOM Level 4?
The W3C DOM Level 4 (now continuously published as the WHATWG DOM Living Standard) is the modern official specification that standardizes how web browsers model HTML and XML documents in memory as an object-oriented node tree.
Historically, DOM Level 1 (1998) introduced basic document access, Level 2 (2000) added namespaces and event models, and Level 3 (2004) expanded serialization. DOM Level 4 unified all browser implementations into a single, standardized core that eliminates browser incompatibilities, replaces legacy vendor quirks, and establishes the modern Node, Element, TreeWalker, and MutationObserver interfaces found in every modern browser engine today (Chromium Blink, Firefox Gecko, Apple WebKit).
Standardized Node.nodeType
Official numeric enum constants defined in WHATWG DOM §4.4. tag-to-json uses strict constants (Node.ELEMENT_NODE === 1, Node.TEXT_NODE === 3, Node.COMMENT_NODE === 8) rather than unreliable string checking.
True Document-Order childNodes
Guarantees exact document order traversal. By using childNodes instead of children, tag-to-json preserves mixed content (e.g. inline SVG icons alongside sibling text labels) without dropping whitespace or inline copy.
Unified NamedNodeMap Attributes
Iterates native element.attributes directly from the browser's C++ representation. Standardizes HTML attributes, custom data-* attributes, ARIA accessibility semantics, and SVG namespaces (viewBox, xmlns).
C++ Native Browser Speed
Zero JavaScript virtual DOM parsing overhead. tag-to-json executes directly against the browser engine's high-speed C++ DOM tree, achieving microsecond extraction times with 0 dependencies.
The Crucial Difference: childNodes vs children
Most naive DOM-to-JSON tools fail on real-world websites because they loop over element.children instead of element.childNodes. Here is why the W3C DOM Level 4 specification makes all the difference:
Using element.children
element.children only returns element tags (nodeType === 1). Any text nodes placed directly next to child tags are completely deleted!
<i class="bi-star"></i>
Favorite <-- LOST SILENTLY!
</button>
Result: The text "Favorite" vanishes from the extracted JSON!
Using element.childNodes
Traverses every child node in document order, honoring official W3C nodeType specifications:
{ tag: "i", attributes: { class: "bi-star" } },
"Favorite" <-- Perfectly Preserved!
]
Result: 100% lossless mixed-content extraction.
W3C DOM Level 4 Node Type Specification Table
Official W3C constant mapping implemented in tag-to-json pipeline
| W3C Constant | Value | Standard Definition | tag-to-json v5 Handling |
|---|---|---|---|
Node.ELEMENT_NODE |
1 | An Element node such as <div>, <span>, <table>, or <svg>. |
Extracted recursively into a clean declarative JSON spec object with tag, attributes, and children. |
Node.ATTRIBUTE_NODE |
2 | An Attr node representing an element attribute. |
Serialized via Chapter 3 into key-value attribute dictionaries with extension noise filtering. |
Node.TEXT_NODE |
3 | The actual textual data inside an element or attribute. | Preserved in place: Leaf text is extracted to textContent; mixed sibling text is placed directly in the children array. |
Node.COMMENT_NODE |
8 | A Comment node (<!-- comment -->). |
Gracefully skipped by Chapter 5 to keep the JSON output clean and production-ready. |
Node.DOCUMENT_NODE |
9 | A Document node (the top root node of the DOM tree). |
Resolved to document.documentElement or document.body for full-page extraction. |
Node.DOCUMENT_FRAGMENT_NODE |
11 | A DocumentFragment node (lightweight DOM container). |
Supported: Extracts fragments and template elements (<template>) losslessly. |
The 5-Chapter W3C Serialization Pipeline
Rather than arbitrary string concatenation, tag-to-json translates W3C DOM Level 4 nodes through a predictable 5-chapter lifecycle:
flowchart TD
Start(["Live DOM Node / Element ID"]) --> Ch1["Chapter 1: Target Resolution & Type Guard
Resolve ID string & verify Node.ELEMENT_NODE"]
Ch1 --> Ch2["Chapter 2: Clean Gatekeeping
Filter <script>, <style>, extension artifacts"]
Ch2 --> Ch3["Chapter 3: Attribute Serialization
Extract class, href, id, data-*, SVG attributes"]
Ch3 --> Ch4{"Chapter 4: The Fork in the Road
Does element have child element tags?"}
Ch4 -- "No: Pure Leaf Tag" --> Leaf["Leaf Extraction
Extract clean textContent"]
Ch4 -- "Yes: Mixed Content / Container" --> Ch5["Chapter 5: The W3C Tree Walker
Iterate element.childNodes in exact document order"]
Ch5 --> N1["Node.ELEMENT_NODE (1)
Recursive domToSpec() call"]
Ch5 --> N2["Node.TEXT_NODE (3)
Trimmed text string in place"]
Ch5 --> N3["Node.COMMENT_NODE (8)
Gracefully skipped"]
N1 --> Mixed["Assemble 'children' Array
Preserves SVG icons + sibling text labels"]
N2 --> Mixed
Leaf --> Spec(["Declarative JSON Specification Tree"])
Mixed --> Spec
Spec ==> Rebuild(["json-to-tag: buildSpecElement()"])
Rebuild ==> DomOut(["Faithfully Reconstructed Live DOM Node"])
classDef chapter fill:#f0f7ff,stroke:#0d6efd,stroke-width:2px,color:#0b3c7b;
classDef success fill:#e8f9ee,stroke:#198754,stroke-width:2px,color:#0a522a;
classDef nodeTypes fill:#fff8e6,stroke:#ffc107,stroke-width:1px,color:#664d03;
class Ch1,Ch2,Ch3,Ch4,Ch5 chapter;
class Spec,Rebuild,DomOut success;
class N1,N2,N3 nodeTypes;
Live W3C DOM Level 4 Node Inspector
Test any HTML snippet and inspect how W3C nodeTypes are traversed and serialized live
W3C DOM Nodes Detected:
// Extracted JSON will appear here...
Explore Next
Learn why having zero external dependencies pairs with W3C DOM Level 4 to build an ultra-secure, featherweight browser tool.