Foundation DOM classes including documents, nodes, elements, and collections. Provides the core tree structure and manipulation methods that form the basis of all DOM operations.
Main document interface for DOM tree management.
/**
* Main document interface for DOM tree management
*/
class Document extends Node {
/** Document element (html element) */
readonly documentElement: Element | null;
/** Document head element */
readonly head: HTMLHeadElement | null;
/** Document body element */
readonly body: HTMLBodyElement | null;
/** Document title */
title: string;
/** Create new element with specified tag name */
createElement(qualifiedName: string, options?: { is?: string }): HTMLElement;
/** Create new text node */
createTextNode(data: string): Text;
/** Create new comment node */
createComment(data: string): Comment;
/** Create new document fragment */
createDocumentFragment(): DocumentFragment;
/** Create new attribute */
createAttribute(name: string): Attr;
/** Get element by ID */
getElementById(id: string): Element | null;
/** Get elements by tag name */
getElementsByTagName(tagName: string): HTMLCollection;
/** Get elements by class name */
getElementsByClassName(className: string): HTMLCollection;
/** Query selector (returns first match) */
querySelector(selector: string): Element | null;
/** Query selector all (returns all matches) */
querySelectorAll(selector: string): NodeList;
/** Import node from another document */
importNode(node: Node, deep?: boolean): Node;
/** Adopt node from another document */
adoptNode(node: Node): Node;
}HTML-specific document implementation.
/**
* HTML-specific document implementation
*/
class HTMLDocument extends Document {
/** Document cookie string */
cookie: string;
/** Document ready state */
readonly readyState: DocumentReadyState;
/** Document character set */
readonly characterSet: string;
/** Document URL */
readonly URL: string;
/** Document domain */
domain: string;
/** Document referrer */
readonly referrer: string;
/** Write content to document */
write(content: string): void;
/** Write line to document */
writeln(content: string): void;
/** Open document for writing */
open(): void;
/** Close document */
close(): void;
}XML-specific document implementation.
/**
* XML-specific document implementation
*/
class XMLDocument extends Document {
/** XML version */
readonly xmlVersion: string | null;
/** XML encoding */
readonly xmlEncoding: string | null;
/** XML standalone flag */
readonly xmlStandalone: boolean;
}Base class for all DOM nodes.
/**
* Base class for all DOM nodes
*/
class Node extends EventTarget {
/** Node type constant */
readonly nodeType: number;
/** Node name */
readonly nodeName: string;
/** Node value */
nodeValue: string | null;
/** Text content of node and descendants */
textContent: string | null;
/** Parent node */
readonly parentNode: Node | null;
/** Parent element */
readonly parentElement: Element | null;
/** Child nodes */
readonly childNodes: NodeList;
/** First child node */
readonly firstChild: Node | null;
/** Last child node */
readonly lastChild: Node | null;
/** Previous sibling node */
readonly previousSibling: Node | null;
/** Next sibling node */
readonly nextSibling: Node | null;
/** Owner document */
readonly ownerDocument: Document | null;
/** Check if node has child nodes */
hasChildNodes(): boolean;
/** Append child node */
appendChild(node: Node): Node;
/** Insert node before reference node */
insertBefore(node: Node, referenceNode: Node | null): Node;
/** Replace child node */
replaceChild(newNode: Node, oldNode: Node): Node;
/** Remove child node */
removeChild(node: Node): Node;
/** Clone node */
cloneNode(deep?: boolean): Node;
/** Check if node contains another node */
contains(node: Node | null): boolean;
/** Compare document position */
compareDocumentPosition(node: Node): number;
/** Normalize node */
normalize(): void;
}
// Node type constants
const enum NodeType {
ELEMENT_NODE = 1,
ATTRIBUTE_NODE = 2,
TEXT_NODE = 3,
CDATA_SECTION_NODE = 4,
PROCESSING_INSTRUCTION_NODE = 7,
COMMENT_NODE = 8,
DOCUMENT_NODE = 9,
DOCUMENT_TYPE_NODE = 10,
DOCUMENT_FRAGMENT_NODE = 11
}Generic DOM element class.
/**
* Generic DOM element class
*/
class Element extends Node {
/** Element tag name */
readonly tagName: string;
/** Element ID */
id: string;
/** Element class name */
className: string;
/** Element class list */
readonly classList: DOMTokenList;
/** Element attributes */
readonly attributes: NamedNodeMap;
/** Inner HTML content */
innerHTML: string;
/** Outer HTML content */
outerHTML: string;
/** Element children */
readonly children: HTMLCollection;
/** First element child */
readonly firstElementChild: Element | null;
/** Last element child */
readonly lastElementChild: Element | null;
/** Previous element sibling */
readonly previousElementSibling: Element | null;
/** Next element sibling */
readonly nextElementSibling: Element | null;
/** Child element count */
readonly childElementCount: number;
/** Get attribute value */
getAttribute(name: string): string | null;
/** Set attribute value */
setAttribute(name: string, value: string): void;
/** Remove attribute */
removeAttribute(name: string): void;
/** Check if attribute exists */
hasAttribute(name: string): boolean;
/** Get attribute node */
getAttributeNode(name: string): Attr | null;
/** Set attribute node */
setAttributeNode(attr: Attr): Attr | null;
/** Remove attribute node */
removeAttributeNode(attr: Attr): Attr;
/** Get elements by tag name */
getElementsByTagName(tagName: string): HTMLCollection;
/** Get elements by class name */
getElementsByClassName(className: string): HTMLCollection;
/** Query selector (first match) */
querySelector(selector: string): Element | null;
/** Query selector all (all matches) */
querySelectorAll(selector: string): NodeList;
/** Check if element matches selector */
matches(selector: string): boolean;
/** Get closest ancestor matching selector */
closest(selector: string): Element | null;
/** Insert adjacent element */
insertAdjacentElement(position: InsertPosition, element: Element): Element | null;
/** Insert adjacent HTML */
insertAdjacentHTML(position: InsertPosition, html: string): void;
/** Insert adjacent text */
insertAdjacentText(position: InsertPosition, text: string): void;
/** Attach shadow root */
attachShadow(init: ShadowRootInit): ShadowRoot;
/** Shadow root if attached */
readonly shadowRoot: ShadowRoot | null;
}
type InsertPosition = 'beforebegin' | 'afterbegin' | 'beforeend' | 'afterend';
interface ShadowRootInit {
mode: ShadowRootMode;
delegatesFocus?: boolean;
}
type ShadowRootMode = 'open' | 'closed';Text node implementation.
/**
* Text node implementation
*/
class Text extends Node {
/** Text data */
data: string;
/** Text length */
readonly length: number;
/** Text content (same as data) */
textContent: string;
/** Whole text content */
readonly wholeText: string;
/** Split text at offset */
splitText(offset: number): Text;
/** Substring data */
substringData(offset: number, count: number): string;
/** Append data */
appendData(data: string): void;
/** Insert data */
insertData(offset: number, data: string): void;
/** Delete data */
deleteData(offset: number, count: number): void;
/** Replace data */
replaceData(offset: number, count: number, data: string): void;
}Comment node implementation.
/**
* Comment node implementation
*/
class Comment extends Node {
/** Comment data */
data: string;
/** Comment length */
readonly length: number;
/** Substring data */
substringData(offset: number, count: number): string;
/** Append data */
appendData(data: string): void;
/** Insert data */
insertData(offset: number, data: string): void;
/** Delete data */
deleteData(offset: number, count: number): void;
/** Replace data */
replaceData(offset: number, count: number, data: string): void;
}Document fragment for efficient DOM manipulation.
/**
* Document fragment for efficient DOM manipulation
*/
class DocumentFragment extends Node {
/** Fragment children */
readonly children: HTMLCollection;
/** First element child */
readonly firstElementChild: Element | null;
/** Last element child */
readonly lastElementChild: Element | null;
/** Child element count */
readonly childElementCount: number;
/** Get element by ID */
getElementById(id: string): Element | null;
/** Query selector */
querySelector(selector: string): Element | null;
/** Query selector all */
querySelectorAll(selector: string): NodeList;
}Document type declaration node.
/**
* Document type declaration node
*/
class DocumentType extends Node {
/** Document type name */
readonly name: string;
/** Public ID */
readonly publicId: string;
/** System ID */
readonly systemId: string;
}Processing instruction node.
/**
* Processing instruction node
*/
class ProcessingInstruction extends Node {
/** Processing instruction target */
readonly target: string;
/** Processing instruction data */
data: string;
}Attribute node implementation.
/**
* Attribute node implementation
*/
class Attr extends Node {
/** Attribute name */
readonly name: string;
/** Attribute value */
value: string;
/** Owner element */
readonly ownerElement: Element | null;
/** Specified flag */
readonly specified: boolean;
}Shadow DOM root implementation.
/**
* Shadow DOM root implementation
*/
class ShadowRoot extends DocumentFragment {
/** Shadow root mode */
readonly mode: ShadrowRootMode;
/** Host element */
readonly host: Element;
/** Delegates focus flag */
readonly delegatesFocus: boolean;
/** Inner HTML */
innerHTML: string;
}Live collection of HTML elements.
/**
* Live collection of HTML elements
*/
class HTMLCollection {
/** Collection length */
readonly length: number;
/** Get item by index */
item(index: number): Element | null;
/** Get named item */
namedItem(name: string): Element | null;
/** Array-like access */
[index: number]: Element;
}List of nodes (may be live or static).
/**
* List of nodes (may be live or static)
*/
interface NodeList {
/** List length */
readonly length: number;
/** Get item by index */
item(index: number): Node | null;
/** For each iteration */
forEach(callback: (node: Node, index: number, list: NodeList) => void): void;
/** Array-like access */
[index: number]: Node;
}import { Document } from "happy-dom";
const document = new Document();
// Create elements
const div = document.createElement('div');
div.id = 'container';
div.className = 'main-content';
// Add content
const text = document.createTextNode('Hello World');
div.appendChild(text);
// Query elements
const container = document.getElementById('container');
const divs = document.getElementsByTagName('div');import { Window } from "happy-dom";
const window = new Window();
const document = window.document;
// Complex DOM structure
document.body.innerHTML = `
<div class="container">
<p class="text">Paragraph 1</p>
<p class="text">Paragraph 2</p>
</div>
`;
// Query operations
const container = document.querySelector('.container');
const paragraphs = document.querySelectorAll('.text');
// DOM traversal
paragraphs.forEach(p => {
console.log(p.textContent);
console.log(p.parentElement === container); // true
});