What is domutils?
The domutils package is a utility library for working with DOM elements in Node.js. It provides a variety of functions to manipulate and traverse the DOM, extract information, and convert between different formats.
What are domutils's main functionalities?
Manipulating the DOM
This feature allows you to manipulate DOM elements by appending children or removing elements.
const { append, removeElement } = require('domutils');
const dom = [{ type: 'tag', name: 'div' }];
const child = { type: 'tag', name: 'span' };
append(dom[0], child);
removeElement(child);
Traversing the DOM
This feature provides functions to traverse the DOM and find elements based on a predicate function.
const { findOne, findAll } = require('domutils');
const dom = [{ type: 'tag', name: 'div', children: [{ type: 'tag', name: 'span' }] }];
const span = findOne(elem => elem.name === 'span', dom);
const allDivs = findAll(elem => elem.name === 'div', dom);
Extracting information
This feature allows you to extract information such as text content from DOM elements.
const { getText } = require('domutils');
const dom = [{ type: 'text', data: 'Hello World' }];
const text = getText(dom);
Converting between formats
This feature enables you to convert DOM elements to other formats, such as HTML strings.
const { getOuterHTML } = require('domutils');
const dom = [{ type: 'tag', name: 'div', children: [{ type: 'text', data: 'Hello World' }] }];
const html = getOuterHTML(dom);
Other packages similar to domutils
cheerio
Cheerio is a fast, flexible, and lean implementation of core jQuery designed specifically for the server. It provides a simpler API for manipulating the DOM compared to domutils and is often used for web scraping and server-side DOM manipulation.
jsdom
jsdom is a pure-JavaScript implementation of many web standards, notably the WHATWG DOM and HTML Standards. It is more comprehensive than domutils, creating a whole web page environment, and is often used for testing web pages and running scripts as if they were in a browser.
parse5
parse5 is an HTML parsing/serialization toolset for Node.js that adheres to the HTML5 specification. Unlike domutils, which provides utilities for manipulating a DOM structure, parse5 focuses on parsing and serializing HTML documents.
domutils
Utilities for working with htmlparser2's DOM.
All functions are exported as a single module. Look through the docs to see what is available.
Ecosystem
Name | Description |
---|
htmlparser2 | Fast & forgiving HTML/XML parser |
domhandler | Handler for htmlparser2 that turns documents into a DOM |
domutils | Utilities for working with domhandler's DOM |
css-select | CSS selector engine, compatible with domhandler's DOM |
cheerio | The jQuery API for domhandler's DOM |
dom-serializer | Serializer for domhandler's DOM |
License: BSD-2-Clause
Security contact information
To report a security vulnerability, please use the Tidelift security contact.
Tidelift will coordinate the fix and disclosure.
domutils
for enterprise
Available as part of the Tidelift Subscription
The maintainers of domutils
and thousands of other packages are working with Tidelift to deliver commercial support and maintenance for the open source dependencies you use to build your applications. Save time, reduce risk, and improve code health, while paying the maintainers of the exact dependencies you use. Learn more.