{"id":12997,"library":"context-parser","title":"HTML5 Context Parser","description":"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.","status":"active","version":"2.0.1","language":"javascript","source_language":"en","source_url":"ssh://git@github.com/yahoo/context-parser","tags":["javascript","context","parser","html5"],"install":[{"cmd":"npm install context-parser","lang":"bash","label":"npm"},{"cmd":"yarn add context-parser","lang":"bash","label":"yarn"},{"cmd":"pnpm add context-parser","lang":"bash","label":"pnpm"}],"dependencies":[],"imports":[{"note":"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.","wrong":"import { Parser } from 'context-parser';","symbol":"Parser","correct":"const { Parser } = require('context-parser');"},{"note":"Introduced in v2.0.0, `FastParser` is exported alongside `Parser` for performance-critical applications. Like `Parser`, it follows the CommonJS named export pattern.","wrong":"import FastParser from 'context-parser';","symbol":"FastParser","correct":"const { FastParser } = require('context-parser');"},{"note":"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.","wrong":"contextualize(htmlString);","symbol":"contextualize","correct":"parserInstance.contextualize(htmlString);"}],"quickstart":{"code":"const fs = require('fs');\nconst { Parser } = require('context-parser');\n\n// Create a dummy HTML file for parsing\nconst dummyHtmlContent = `\n  <!DOCTYPE html>\n  <html>\n  <head>\n    <title>Test Page</title>\n    <style>body { font-family: sans-serif; }</style>\n  </head>\n  <body>\n    <h1>Hello World</h1>\n    <script>console.log('Parsed!');</script>\n    <!--[if IE]>\n      <p>IE specific content</p>\n    <![endif]-->\n  </body>\n  </html>\n`;\nconst filePath = './test.html';\nfs.writeFileSync(filePath, dummyHtmlContent, 'utf-8');\n\n// Create the context parser\nconst parser = new Parser();\n\n// Read the HTML web page\nconst data = fs.readFileSync(filePath, 'utf-8');\n\n// Analyze the execution context\nconsole.log('Starting HTML context analysis...');\nparser.contextualize(data);\nconsole.log('Analysis complete. Check parser events for details.');\n\n// Clean up the dummy file\nfs.unlinkSync(filePath);\n","lang":"javascript","description":"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."},"warnings":[{"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.","message":"Version 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`.","severity":"breaking","affected_versions":">=2.0.0"},{"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.","message":"In 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.","severity":"gotcha","affected_versions":">=2.0.1"}],"env_vars":null,"last_verified":"2026-04-19T00:00:00.000Z","next_check":"2026-07-18T00:00:00.000Z","problems":[{"fix":"Use object destructuring for CommonJS imports: `const { Parser } = require('context-parser');`","cause":"Attempting to `new require('context-parser')` or directly `new Parser()` without destructuring.","error":"TypeError: context-parser is not a constructor"},{"fix":"Run `npm install context-parser` in your project directory to install the package.","cause":"The package is not installed or the Node.js module resolution path is incorrect.","error":"Error: Cannot find module 'context-parser'"},{"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.","cause":"Attempting to use ES module `import { Parser } from 'context-parser'` syntax in a project that primarily uses CommonJS exports.","error":"SyntaxError: Named export 'Parser' not found. The requested module 'context-parser' does not provide an export named 'Parser'"}],"ecosystem":"npm","meta_description":null,"install_score":null,"install_tag":null,"quickstart_score":null,"quickstart_tag":null,"pypi_latest":null,"cli_name":""}