
Security News
Meet Socket at Black Hat and DEF CON 2025 in Las Vegas
Meet Socket at Black Hat & DEF CON 2025 for 1:1s, insider security talks at Allegiant Stadium, and a private dinner with top minds in software supply chain security.
@types/htmlparser2
Advanced tools
Stub TypeScript definitions entry for htmlparser2, which provides its own types definitions
@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.
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();
Cheerio is a fast, flexible, and lean implementation of core jQuery designed specifically for the server. It parses HTML and XML documents and provides a jQuery-like API for manipulating the resulting DOM. Compared to htmlparser2, Cheerio offers a higher-level API that is easier to use for DOM manipulation.
jsdom is a JavaScript implementation of the WHATWG DOM and HTML standards, primarily intended for use with Node.js. It provides a full-featured DOM environment, including support for HTML, XML, and CSS. Compared to htmlparser2, jsdom offers a more comprehensive and standards-compliant environment but is heavier and slower.
xml2js is a simple XML to JavaScript object converter. It is designed to be easy to use and provides a straightforward way to parse XML into JavaScript objects. Compared to htmlparser2, xml2js is more focused on XML parsing and conversion rather than handling HTML.
This is a stub types definition for @types/htmlparser2 (https://github.com/fb55/htmlparser2#readme).
htmlparser2 provides its own type definitions, so you don't need @types/htmlparser2 installed!
FAQs
Stub TypeScript definitions entry for htmlparser2, which provides its own types definitions
The npm package @types/htmlparser2 receives a total of 162,649 weekly downloads. As such, @types/htmlparser2 popularity was classified as popular.
We found that @types/htmlparser2 demonstrated a healthy version release cadence and project activity because the last version was released less than a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?
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.
Security News
Meet Socket at Black Hat & DEF CON 2025 for 1:1s, insider security talks at Allegiant Stadium, and a private dinner with top minds in software supply chain security.
Security News
CAI is a new open source AI framework that automates penetration testing tasks like scanning and exploitation up to 3,600× faster than humans.
Security News
Deno 2.4 brings back bundling, improves dependency updates and telemetry, and makes the runtime more practical for real-world JavaScript projects.