Registry / http-networking / context-parser

context-parser

JSON →
library2.0.1jsnpmunverified

context-parser is a robust and compact HTML5 context parser library designed to analyze web pages and report the execution context of each character, primarily for security purposes like preventing Cross-Site Scripting (XSS). The current stable version is 2.0.1. It differentiates itself by adhering strictly to the HTML5 WHATWG specification for accurate context determination, offering two distinct parser classes: `FastParser` for performance-critical scenarios and `Parser` for feature-rich analysis. The library is event-driven and focuses on the tokenization process to provide precise context reporting without unnecessary overhead, making it suitable for server-side HTML analysis. Its design emphasizes security, simplicity, and straightforward code for easier review and smaller footprint.

npm install context-parser
INSTALL
IMPORT
SIG · CONTEXT-PARSER
C
context-parser
http-networkingjavascriptv2.0.1
Install
Import
Disk
Pass rate
0/ 6
Env Coverage0 / 6
glibc
1822
musl
1822
Install & Compatibility
Where this runs
tested against v? · npm install
Install × environment matrix
Each cell = how many times install + import succeeded across repeated harness runs. Partial = flaky.
glibc = Debian/Ubuntu slim · musl = Alpine Linux
musl
node 18226 runs
build_error
glibc
node 18226 runs
build_error
Code
Verified usage

Verified import paths — ran on the pinned version, not inferred.

Parser
const { Parser } = require('context-parser');
import { Parser } from 'context-parser';
The library primarily uses CommonJS `require` syntax as shown in official documentation. Direct ESM `import` may not be supported or requires specific Node.js configuration (e.g., `type: 'module'` in package.json) if the package doesn't ship ESM exports.
FastParser
const { FastParser } = require('context-parser');
import FastParser from 'context-parser';
Introduced in v2.0.0, `FastParser` is exported alongside `Parser` for performance-critical applications. Like `Parser`, it follows the CommonJS named export pattern.
contextualize
parserInstance.contextualize(htmlString);
contextualize(htmlString);
The `contextualize` method is an instance method of `Parser` (and likely `FastParser`) and must be called on an instantiated parser object, not as a standalone function. This API name was standardized in v2.0.0.

This quickstart demonstrates how to instantiate the `Parser` class, read an HTML file, and use the `contextualize` method to analyze its execution context in a Node.js environment.

const fs = require('fs'); const { Parser } = require('context-parser'); // Create a dummy HTML file for parsing const dummyHtmlContent = ` <!DOCTYPE html> <html> <head> <title>Test Page</title> <style>body { font-family: sans-serif; }</style> </head> <body> <h1>Hello World</h1> <script>console.log('Parsed!');</script> <!--[if IE]> <p>IE specific content</p> <![endif]--> </body> </html> `; const filePath = './test.html'; fs.writeFileSync(filePath, dummyHtmlContent, 'utf-8'); // Create the context parser const parser = new Parser(); // Read the HTML web page const data = fs.readFileSync(filePath, 'utf-8'); // Analyze the execution context console.log('Starting HTML context analysis...'); parser.contextualize(data); console.log('Analysis complete. Check parser events for details.'); // Clean up the dummy file fs.unlinkSync(filePath);
Debug
Known issues
breakingVersion 2.0.0 introduced significant breaking changes by separating the codebase into two distinct classes, `FastParser` and `Parser`, for different design objectives. The primary API for context analysis was standardized to `contextualize`.
fix
Review your code for direct usage of v1.x parsing methods. Migrate to instantiating either `Parser` or `FastParser` and calling the `contextualize()` method on the instance for context analysis.
affects: >=2.0.0
gotchaIn version 2.0.1, the initialization value for attribute values was changed from an empty string (`''`) to `null`. This might affect logic that expects attribute values to always be strings, even when not explicitly set.
fix
Ensure your code handles `null` values when accessing attribute values, especially in scenarios where attributes might be present but without an explicit value (e.g., boolean attributes). Add `null` checks where appropriate.
affects: >=2.0.1
Errors
Common errors & fixes
TypeError: context-parser is not a constructor
Attempting to `new require('context-parser')` or directly `new Parser()` without destructuring.
fix
Use object destructuring for CommonJS imports: `const { Parser } = require('context-parser');`
Error: Cannot find module 'context-parser'
The package is not installed or the Node.js module resolution path is incorrect.
fix
Run `npm install context-parser` in your project directory to install the package.
SyntaxError: Named export 'Parser' not found. The requested module 'context-parser' does not provide an export named 'Parser'
Attempting to use ES module `import { Parser } from 'context-parser'` syntax in a project that primarily uses CommonJS exports.
fix
If your project is CommonJS, switch to `const { Parser } = require('context-parser');`. If your project is ESM, verify if the package has a `type: 'module'` in its `package.json` or provides an `exports` field for ESM compatibility.
Upgrade
Version history
2.0.1latest on npm
Audit
Dependencies

No dependency data recorded yet.

Agent activity
19 hits · last 30 days
node
18
OpenAI (training)
1
Resources
context-parser — npm install context-parser · libregistry