🚀 Big News: Socket Acquires Coana to Bring Reachability Analysis to Every Appsec Team.Learn more
Socket
Book a DemoInstallSign in
Socket

@types/htmlparser2

Package Overview
Dependencies
Maintainers
1
Versions
22
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

@types/htmlparser2

Stub TypeScript definitions entry for htmlparser2, which provides its own types definitions

4.1.0
latest
npm
Version published
Weekly downloads
169K
1.61%
Maintainers
1
Weekly downloads
 
Created

What is @types/htmlparser2?

@types/htmlparser2 provides TypeScript type definitions for the htmlparser2 library, which is a fast and forgiving HTML/XML parser. It is widely used for web scraping, data extraction, and HTML manipulation.

What are @types/htmlparser2's main functionalities?

Parsing HTML

This feature allows you to parse HTML content. The parser emits events like 'onopentag', 'ontext', and 'onclosetag' to handle different parts of the HTML structure.

const htmlparser2 = require('htmlparser2');
const parser = new htmlparser2.Parser({
  onopentag(name, attribs) {
    console.log(`Tag opened: ${name}`);
  },
  ontext(text) {
    console.log(`Text: ${text}`);
  },
  onclosetag(tagname) {
    console.log(`Tag closed: ${tagname}`);
  }
}, { decodeEntities: true });
parser.write('<div>Hello <strong>world</strong></div>');
parser.end();

Parsing XML

This feature allows you to parse XML content. By setting the 'xmlMode' option to true, the parser will handle XML-specific parsing rules.

const htmlparser2 = require('htmlparser2');
const parser = new htmlparser2.Parser({
  onopentag(name, attribs) {
    console.log(`Tag opened: ${name}`);
  },
  ontext(text) {
    console.log(`Text: ${text}`);
  },
  onclosetag(tagname) {
    console.log(`Tag closed: ${tagname}`);
  }
}, { xmlMode: true });
parser.write('<note><to>User</to><from>Admin</from><message>Hello</message></note>');
parser.end();

Handling Errors

This feature allows you to handle errors that occur during parsing. The 'onerror' event is triggered when the parser encounters an error.

const htmlparser2 = require('htmlparser2');
const parser = new htmlparser2.Parser({
  onerror(error) {
    console.error(`Error: ${error.message}`);
  }
});
parser.write('<div><span>Unclosed tag</div>');
parser.end();

Other packages similar to @types/htmlparser2

FAQs

Package last updated on 03 Mar 2025

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