What is html-dom-parser?
The html-dom-parser npm package is designed to parse HTML strings into DOM nodes and vice versa, making it easier to manipulate, traverse, and work with HTML content programmatically in JavaScript environments. It is particularly useful for server-side rendering, web scraping, and building web crawlers or SEO tools.
What are html-dom-parser's main functionalities?
Parsing HTML string to DOM nodes
This feature allows you to convert an HTML string into DOM nodes, enabling programmatic manipulation of the resulting structure. It's useful for extracting information from HTML content or preparing it for further processing.
const parse = require('html-dom-parser');
const domNodes = parse('<div><p>Hello World</p></div>');
Converting DOM nodes back to HTML string
This functionality allows you to take DOM nodes (possibly after manipulation) and convert them back into an HTML string. This is particularly useful for generating HTML content dynamically or modifying existing HTML content programmatically.
const domToHtml = require('html-dom-parser').domToHtml;
const htmlString = domToHtml([{ type: 'tag', name: 'div', children: [{ type: 'tag', name: 'p', children: [{ type: 'text', data: 'Hello World' }] }] }]);
Other packages similar to html-dom-parser
cheerio
Cheerio is a fast, flexible, and lean implementation of core jQuery designed specifically for the server. It provides a simpler API for parsing, manipulating, and rendering DOM structures. Compared to html-dom-parser, Cheerio offers a more jQuery-like syntax and additional manipulation capabilities, making it more suitable for complex DOM manipulation tasks.
jsdom
jsdom is a pure-JavaScript implementation of many web standards, notably the WHATWG DOM and HTML Standards, for use with Node.js. It simulates a web browser's environment, allowing you to interact with the DOM as if you were in the browser. jsdom is more comprehensive than html-dom-parser, providing a complete simulated browser environment, making it ideal for testing web pages and running web pages or applications in a Node.js environment.
html-dom-parser
HTML to DOM parser that works on both the server (Node.js) and the client (browser):
HTMLDOMParser(string[, options])
It converts an HTML string to a JavaScript object that describes the DOM tree.
Example:
var parse = require('html-dom-parser');
parse('<div>text</div>');
Output:
[ { type: 'tag',
name: 'div',
attribs: {},
children:
[ { data: 'text',
type: 'text',
next: null,
prev: null,
parent: [Circular] } ],
next: null,
prev: null,
parent: null } ]
Repl.it | JSFiddle | Examples
Installation
NPM:
$ npm install html-dom-parser --save
Yarn:
$ yarn add html-dom-parser
CDN:
<script src="https://unpkg.com/html-dom-parser@latest/dist/html-dom-parser.js"></script>
<script>
window.HTMLDOMParser();
</script>
Usage
Import the module:
var parse = require('html-dom-parser');
import parse from 'html-dom-parser';
Parse markup:
parse('<p class="primary" style="color: skyblue;">Hello world</p>');
Output:
[ { type: 'tag',
name: 'p',
attribs: { class: 'primary', style: 'color: skyblue;' },
children:
[ { data: 'Hello world',
type: 'text',
next: null,
prev: null,
parent: [Circular] } ],
next: null,
prev: null,
parent: null } ]
The server parser is a wrapper of htmlparser2's parseDOM
; the client parser mimics the server parser by using the DOM API.
Testing
Run server and client tests:
$ npm test
Generate HTML coverage report for server tests:
$ npx nyc report --reporter=html
Lint files:
$ npm run lint
$ npm run lint:fix
Test TypeScript declaration file for style and correctness:
$ npm run lint:dts
Release
Only collaborators with credentials can release and publish:
$ npm run release
$ git push --follow-tags && npm publish
Special Thanks
License
MIT