Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

node-html-parser

Package Overview
Dependencies
Maintainers
1
Versions
119
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

node-html-parser

A very fast HTML parser, generating a simplified DOM, with basic element query support.

  • 3.3.2
  • Source
  • npm
  • Socket score

Version published
Weekly downloads
2.9M
increased by3.49%
Maintainers
1
Weekly downloads
 
Created

What is node-html-parser?

The node-html-parser package is a fast HTML parser designed for Node.js, which allows users to parse HTML documents and manipulate the resulting DOM tree. It provides an API to navigate and modify the DOM, extract data, and serialize DOM back to HTML.

What are node-html-parser's main functionalities?

Parsing HTML string to DOM

This feature allows you to parse a string containing HTML and creates a DOM tree that can be manipulated. The example code demonstrates parsing an HTML string and logging the structure of the first child element.

const { parse } = require('node-html-parser');
const root = parse('<ul id="list"><li>Hello World</li></ul>');
console.log(root.firstChild.structure);

Querying the DOM

This feature enables querying the DOM tree for elements using selectors. The code sample shows how to select the first 'li' element and log its text content.

const { parse } = require('node-html-parser');
const root = parse('<ul id="list"><li>Hello World</li></ul>');
const listItem = root.querySelector('li');
console.log(listItem.text);

Modifying the DOM

This feature allows you to modify the DOM tree by changing the content of elements. In the example, the content of the 'li' element is changed from 'Hello World' to 'Hello Universe', and the updated HTML is logged.

const { parse } = require('node-html-parser');
const root = parse('<ul id="list"><li>Hello World</li></ul>');
const listItem = root.querySelector('li');
listItem.set_content('Hello Universe');
console.log(root.toString());

Serializing DOM back to HTML

After manipulating the DOM, you can serialize it back to an HTML string. The code sample demonstrates how to convert the DOM tree back into an HTML string and log it.

const { parse } = require('node-html-parser');
const root = parse('<div><p>Hello World</p></div>');
const html = root.toString();
console.log(html);

Other packages similar to node-html-parser

Keywords

FAQs

Package last updated on 22 May 2021

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc