A super fast Node.js addon for HTML parsing and manipulation, written in Rust. It provides a standard-compliant DOM API for Node.js, mirroring the browser's built-in DOMParser.
- Standard Compliant: Type definitions aligned to
lib.dom.d.ts— usesNode,Element,Document,Text,Comment, and other standard DOM interfaces. - High-performance DOM parsing and manipulation
- Exposes a simple JavaScript API via NAPI-RS
- Designed for both server-side and CLI HTML processing
- Written in Rust for speed and safety
yarn add domparser-rs
# or
npm install domparser-rsconst { DOMParser } = require('domparser-rs');
const parser = new DOMParser();
const doc = parser.parseFromString('<div id="foo" class="bar">hello <span>world</span></div>', 'text/html');
const div = doc.querySelector('div');
console.log(div.getAttribute('id')); // "foo"
console.log(div.textContent); // "hello world"
div.setAttribute('title', 'my-title');
console.log(div.outerHTML); // <div id="foo" class="bar" title="my-title">hello <span>world</span></div>class DOMParser {
parseFromString(string: string, type: DOMParserSupportedType): Document;
}Parses a string using the specified MIME type (e.g., "text/html") and returns a Document.
Extends Node. Represents the entire HTML document.
| Property | Type | Description |
|---|---|---|
doctype |
DocumentType | null |
The DTD associated with the document |
documentElement |
Element | null |
The root element (e.g., <html>) |
head |
Element | null |
The <head> element |
body |
Element | null |
The <body> element |
title |
string |
The document title |
children |
Element[] |
Child elements |
childElementCount |
number |
Number of child elements |
firstElementChild |
Element | null |
First child element |
lastElementChild |
Element | null |
Last child element |
createElement(tagName: string): ElementcreateTextNode(data: string): TextcreateComment(data: string): CommentcreateDocumentFragment(): DocumentFragmentcreateProcessingInstruction(target: string, data: string): ProcessingInstructionimportNode<T extends Node>(node: T, deep?: boolean): TadoptNode<T extends Node>(node: T): T
getElementById(elementId: string): Element | nullgetElementsByClassName(classNames: string): Element[]getElementsByTagName(qualifiedName: string): Element[]querySelector(selectors: string): Element | nullquerySelectorAll(selectors: string): Element[]append(...nodes: (Node | string)[]): voidprepend(...nodes: (Node | string)[]): void
Extends Node. Represents an HTML element.
| Property | Type | Description |
|---|---|---|
tagName |
string |
The tag name |
localName |
string |
The local part of the qualified name |
namespaceURI |
string | null |
The namespace URI |
prefix |
string | null |
The namespace prefix |
id |
string |
The id attribute |
className |
string |
The class attribute |
classList |
DOMTokenList |
Live token list of class names |
dataset |
Record<string, string> |
Data attributes |
innerHTML |
string |
Inner HTML content |
outerHTML |
string |
Outer HTML content |
children |
Element[] |
Child elements |
childElementCount |
number |
Number of child elements |
firstElementChild |
Element | null |
First child element |
lastElementChild |
Element | null |
Last child element |
previousElementSibling |
Element | null |
Previous sibling element |
nextElementSibling |
Element | null |
Next sibling element |
getAttribute(qualifiedName: string): string | nullsetAttribute(qualifiedName: string, value: string): voidremoveAttribute(qualifiedName: string): voidtoggleAttribute(qualifiedName: string, force?: boolean): booleanhasAttribute(qualifiedName: string): booleanhasAttributes(): booleangetAttributeNames(): string[]getAttributeNS(namespace: string | null, localName: string): string | nullsetAttributeNS(namespace: string | null, qualifiedName: string, value: string): voidremoveAttributeNS(namespace: string | null, localName: string): voidhasAttributeNS(namespace: string | null, localName: string): boolean
querySelector(selectors: string): Element | nullquerySelectorAll(selectors: string): Element[]getElementById(id: string): Element | nullgetElementsByClassName(classNames: string): Element[]getElementsByTagName(qualifiedName: string): Element[]closest(selectors: string): Element | nullmatches(selectors: string): boolean
append(...nodes: (Node | string)[]): voidprepend(...nodes: (Node | string)[]): voidbefore(...nodes: (Node | string)[]): voidafter(...nodes: (Node | string)[]): voidremove(): voidreplaceWith(...nodes: (Node | string)[]): voidinsertAdjacentHTML(position: InsertPosition, html: string): voidinsertAdjacentText(position: InsertPosition, text: string): voidinsertAdjacentElement(position: InsertPosition, element: Element): Element | null
Base interface for all DOM nodes.
| Property | Type | Description |
|---|---|---|
nodeType |
number |
The type of the node |
nodeName |
string |
The name of the node |
nodeValue |
string | null |
The value of the node |
textContent |
string | null |
The text content |
parentNode |
Node | null |
The parent node |
parentElement |
Element | null |
The parent element |
firstChild |
Node | null |
The first child |
lastChild |
Node | null |
The last child |
previousSibling |
Node | null |
The previous sibling |
nextSibling |
Node | null |
The next sibling |
childNodes |
Node[] |
All child nodes |
ownerDocument |
Document | null |
The owner document |
appendChild<T extends Node>(node: T): TremoveChild<T extends Node>(child: T): TinsertBefore<T extends Node>(node: T, child: Node | null): TreplaceChild<T extends Node>(node: Node, child: T): TcloneNode(deep?: boolean): Nodecontains(other: Node | null): booleanhasChildNodes(): booleangetRootNode(): Nodenormalize(): voidisSameNode(otherNode: Node | null): booleanisEqualNode(otherNode: Node | null): booleancompareDocumentPosition(other: Node): numberlookupNamespaceURI(prefix: string | null): string | nulllookupPrefix(namespace: string | null): string | nullisDefaultNamespace(namespace: string | null): boolean
Extends Node. Base interface for Text, Comment, and ProcessingInstruction.
data: stringreadonly length: numbersubstringData(offset: number, count: number): stringappendData(data: string): voidinsertData(offset: number, data: string): voiddeleteData(offset: number, count: number): voidreplaceData(offset: number, count: number, data: string): void
splitText(offset: number): Text
readonly target: string
readonly name: stringreadonly publicId: stringreadonly systemId: string
getElementById(elementId: string): Element | nullquerySelector(selectors: string): Element | nullquerySelectorAll(selectors: string): Element[]
npm install
npm run build
npm testnpm run benchmarkFor more usage examples and advanced API, see the source code and tests in the repository.