# DOM Simulation API Reference NogginLessDom provides a complete, zero-dependency DOM simulation for testing web applications without a browser. Every class and function described here is available from the top-level package import. ```typescript import { Document, Element, Node, TextNode, Comment, DocumentFragment, Event, createWindow, } from '@asymmetric-effort/nogginlessdom'; ``` --- ## Core Classes ### Node Base class for all DOM nodes. Provides tree manipulation, traversal, and comparison methods. #### Node Type Constants | Constant | Value | |---|---| | `Node.ELEMENT_NODE` | 1 | | `Node.TEXT_NODE` | 3 | | `Node.COMMENT_NODE` | 8 | | `Node.DOCUMENT_NODE` | 9 | | `Node.DOCUMENT_FRAGMENT_NODE` | 11 | #### Document Position Constants | Constant | Value | |---|---| | `Node.DOCUMENT_POSITION_DISCONNECTED` | 1 | | `Node.DOCUMENT_POSITION_PRECEDING` | 2 | | `Node.DOCUMENT_POSITION_FOLLOWING` | 4 | | `Node.DOCUMENT_POSITION_CONTAINS` | 8 | | `Node.DOCUMENT_POSITION_CONTAINED_BY` | 16 | #### Properties | Property | Type | Description | |---|---|---| | `nodeType` | `number` | The node type constant | | `nodeName` | `string` | The node name (`#text`, `#comment`, tag name, etc.) | | `childNodes` | `Node[]` | Direct child nodes | | `parentNode` | `Node \| null` | Parent node | | `parentElement` | `Element \| null` | Parent element (null if parent is not an Element) | | `ownerDocument` | `Document \| null` | The owning document | | `isConnected` | `boolean` | True if the node is in a document tree | | `nodeValue` | `string \| null` | Node value (null for non-text nodes) | | `textContent` | `string` | Text content of the node and all descendants | | `firstChild` | `Node \| null` | First child node | | `lastChild` | `Node \| null` | Last child node | | `nextSibling` | `Node \| null` | Next sibling node | | `previousSibling` | `Node \| null` | Previous sibling node | #### Methods **`appendChild(child: Node): Node`** -- Appends a child node. Removes the child from its current parent if it has one. Fires `connectedCallback` on custom elements. ```typescript const div = document.createElement('div'); const span = document.createElement('span'); div.appendChild(span); ``` **`removeChild(child: Node): Node`** -- Removes a child node. Throws if the node is not a child. Fires `disconnectedCallback` on custom elements. **`insertBefore(newChild: Node, referenceChild: Node | null): Node`** -- Inserts a node before a reference child. If `referenceChild` is null, appends to the end. **`replaceChild(newChild: Node, oldChild: Node): Node`** -- Replaces an existing child with a new node. **`contains(other: Node): boolean`** -- Returns true if `other` is a descendant of this node (or is this node itself). **`hasChildNodes(): boolean`** -- Returns true if the node has children. **`cloneNode(deep?: boolean): Node`** -- Creates a copy. If `deep` is true, clones all descendants. **`normalize(): void`** -- Merges adjacent text nodes and removes empty text nodes. **`isEqualNode(other: Node | null): boolean`** -- Deep-compares two nodes for structural equality. **`isSameNode(other: Node | null): boolean`** -- Returns true if both references point to the same node. **`compareDocumentPosition(other: Node): number`** -- Returns a bitmask describing the position of `other` relative to this node. --- ### Element Extends `Node`. Represents an HTML or SVG element with attributes, styling, events, and DOM manipulation capabilities. #### Constructor ```typescript new Element(tagName: string, namespaceURI?: string | null) ``` Tag names are uppercased for HTML, preserved for SVG. #### Properties | Property | Type | Description | |---|---|---| | `tagName` | `string` | Uppercase tag name (e.g. `'DIV'`) | | `id` | `string` | The element's ID | | `className` | `string` | Space-separated class names | | `namespaceURI` | `string \| null` | Namespace URI | | `innerHTML` | `string` | HTML content of the element | | `outerHTML` | `string` (read-only) | HTML including the element itself | | `classList` | `DOMTokenList` | Token list for class manipulation | | `style` | `CSSStyleDeclaration` | Inline style object | | `dataset` | `DOMStringMap` | Data attribute access via `data-*` | | `children` | `HTMLCollection` | Child elements (not text/comment nodes) | | `childElementCount` | `number` | Number of child elements | | `firstElementChild` | `Element \| null` | First child element | | `lastElementChild` | `Element \| null` | Last child element | | `nextElementSibling` | `Element \| null` | Next sibling element | | `previousElementSibling` | `Element \| null` | Previous sibling element | | `shadowRoot` | `ShadowRoot \| null` | Open shadow root, or null | | `slot` | `string` | Slot name for shadow DOM distribution | | `assignedSlot` | `Element \| null` | The slot element this is assigned to | | `tabIndex` | `number` | Tab order (0 for interactive elements, -1 otherwise) | | `contentEditable` | `string` | `'true'`, `'false'`, or `'inherit'` | | `isContentEditable` | `boolean` | Resolved editability | #### Attribute Methods ```typescript element.setAttribute('href', '/page'); element.getAttribute('href'); // '/page' element.hasAttribute('href'); // true element.removeAttribute('href'); element.toggleAttribute('disabled'); // toggles presence element.getAttributeNames(); // ['class', 'id', ...] element.hasAttributes(); // true if any attributes exist // Namespaced variants element.setAttributeNS(ns, name, value); element.getAttributeNS(ns, name); element.removeAttributeNS(ns, name); element.hasAttributeNS(ns, name); ``` #### Query Methods ```typescript element.querySelector('.active'); // first match or null element.querySelectorAll('li.item'); // NodeList of all matches element.getElementsByTagName('span'); // live HTMLCollection element.getElementsByClassName('highlight'); // live HTMLCollection element.closest('.wrapper'); // nearest ancestor matching selector element.matches('.active'); // true if this element matches ``` #### DOM Manipulation ```typescript element.before(nodeA, nodeB); // insert before this element element.after(nodeA); // insert after this element element.prepend(child); // insert as first child element.append(childA, childB); // append children element.remove(); // remove from parent element.replaceWith(newElement); // replace with another node element.click(); // dispatch a click event element.focus(); // dispatch focus event element.blur(); // dispatch blur event ``` #### Layout and Geometry (Test Helpers) All layout properties default to 0. Use `setBoundingClientRect` and `setLayoutMetrics` to configure values for testing. ```typescript element.setBoundingClientRect({ x: 10, y: 20, width: 100, height: 50 }); const rect = element.getBoundingClientRect(); // { x: 10, y: 20, width: 100, height: 50, top: 20, right: 110, bottom: 70, left: 10 } element.setLayoutMetrics({ offsetWidth: 200, offsetHeight: 100, clientWidth: 180, clientHeight: 80, scrollWidth: 500, scrollHeight: 300, }); element.scrollIntoView(); // no-op stub element.scroll(x, y); element.scrollTo(x, y); element.scrollBy(dx, dy); element.getClientRects(); // [getBoundingClientRect()] ``` #### Event Handler Properties All standard `on*` event handler properties are supported: `onclick`, `ondblclick`, `onmousedown`, `onmouseup`, `onmousemove`, `onmouseover`, `onmouseout`, `onmouseenter`, `onmouseleave`, `onkeydown`, `onkeyup`, `onkeypress`, `onfocus`, `onblur`, `onchange`, `oninput`, `onsubmit`, `onreset`, `onscroll`, `onwheel`, `ondrag`, `ondragstart`, `ondragend`, `ondragover`, `ondragenter`, `ondragleave`, `ondrop`, `onload`, `onerror`, `onresize` ```typescript element.onclick = (event) => { console.log('clicked'); }; ``` --- ### TextNode Extends `Node` with `nodeType` 3. Represents text content. ```typescript const text = new TextNode('Hello'); text.data; // 'Hello' text.data = 'World'; // updates text; fires characterData mutation text.textContent; // 'World' text.nodeValue; // 'World' text.cloneNode(); // new TextNode('World') ``` --- ### Comment Extends `Node` with `nodeType` 8. Represents an HTML comment. ```typescript const comment = new Comment('TODO: fix this'); comment.data; // 'TODO: fix this' comment.textContent; // 'TODO: fix this' ``` --- ### DocumentFragment Extends `Node` with `nodeType` 11. A lightweight container for nodes that can be inserted into the DOM as a group. ```typescript const fragment = new DocumentFragment(); fragment.appendChild(new Element('p')); fragment.querySelector('p'); // works fragment.querySelectorAll('*'); // works parent.appendChild(fragment); // transfers all children ``` --- ### Document Extends `Node` with `nodeType` 9. The root of a DOM tree. #### Properties | Property | Type | Description | |---|---|---| | `readyState` | `string` | Always `'complete'` | | `activeElement` | `Element \| null` | Currently focused element | | `contentType` | `string` | `'text/html'` | | `characterSet` | `string` | `'UTF-8'` | | `URL` | `string` | Document URL | | `domain` | `string` | Document domain | | `referrer` | `string` | Referrer URL | | `defaultView` | `unknown` | The parent Window | | `visibilityState` | `string` | `'visible'` or `'hidden'` | | `hidden` | `boolean` | True when `visibilityState` is `'hidden'` | | `cookie` | `string` | Cookie string (full cookie jar) | | `body` | `Element \| null` | The `
` element | | `head` | `Element \| null` | The `` element | | `title` | `string` | Content of the `