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

@types/htmlparser2

Package Overview
Dependencies
Maintainers
1
Versions
21
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

@types/htmlparser2

TypeScript definitions for htmlparser2

  • 3.10.7
  • ts4.5
  • ts4.6
  • ts4.7
  • ts4.8
  • ts4.9
  • ts5.0
  • ts5.1
  • ts5.2
  • ts5.3
  • ts5.4
  • ts5.5
  • ts5.6
  • ts5.7
  • latest
  • Source
  • npm
  • Socket score

Version published
Weekly downloads
172K
decreased by-5.82%
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 07 Nov 2023

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