{"id":11008,"library":"hast-util-is-event-handler","title":"HAST Utility: Is Event Handler","description":"This package provides a utility function, `isEventHandler`, to check if a given string represents a potential HTML event handler attribute name. It primarily identifies strings starting with 'on' and having a length of five or more characters (e.g., 'onclick', 'onmouseover'), without performing validation on the actual event handler's existence or validity. As part of the `unified` and `hast` ecosystem, it is designed for processing HTML Abstract Syntax Trees. The current stable version is `3.0.1`, maintained within the `rehypejs` monorepo, which generally coordinates major releases across its packages. It is ESM-only and requires Node.js 16 or newer. This tool differentiates itself by its focused scope within the `hast` ecosystem, offering a lightweight and specific check for attribute names.","status":"active","version":"3.0.1","language":"javascript","source_language":"en","source_url":"https://github.com/rehypejs/rehype-minify#main","tags":["javascript","event","handler","hast","hast-util","html","property","unist","util","typescript"],"install":[{"cmd":"npm install hast-util-is-event-handler","lang":"bash","label":"npm"},{"cmd":"yarn add hast-util-is-event-handler","lang":"bash","label":"yarn"},{"cmd":"pnpm add hast-util-is-event-handler","lang":"bash","label":"pnpm"}],"dependencies":[],"imports":[{"note":"The package is ESM-only since its major version 3 release, aligning with the broader rehypejs monorepo's v7 update. CommonJS `require()` is not supported.","wrong":"const isEventHandler = require('hast-util-is-event-handler')","symbol":"isEventHandler","correct":"import { isEventHandler } from 'hast-util-is-event-handler'"},{"note":"This package ships with full TypeScript types. There is no default export; the function must be imported as a named export. While `Handler` is not directly exported from the root, types for internal structures may be available from subpaths if needed for advanced usage, otherwise, relying on the function's inferred types is common.","wrong":"import isEventHandler from 'hast-util-is-event-handler'","symbol":"isEventHandler (TypeScript)","correct":"import { isEventHandler } from 'hast-util-is-event-handler'\nimport type { Handler } from 'hast-util-is-event-handler/lib'"}],"quickstart":{"code":"import { isEventHandler } from 'hast-util-is-event-handler';\n\n// Basic usage to check common event handler names\nconsole.log(\"Is 'onclick' an event handler?\", isEventHandler('onclick'));\n//=> true\n\nconsole.log(\"Is 'onmouseover' an event handler?\", isEventHandler('onmouseover'));\n//=> true\n\n// Checks for 'on' prefix and length >= 5\nconsole.log(\"Is 'ones' an event handler?\", isEventHandler('ones'));\n//=> false (length < 5)\n\nconsole.log(\"Is 'onfoo' an event handler?\", isEventHandler('onfoo'));\n//=> true (matches 'on' prefix and length criteria, even if 'onfoo' is not a real event)\n\nconsole.log(\"Is 'class' an event handler?\", isEventHandler('class'));\n//=> false\n\n// This package requires Node.js 16+ due to its ESM-only nature.\n// Ensure your environment supports ES Modules for direct import usage.\n","lang":"typescript","description":"Demonstrates how to import and use the `isEventHandler` function to identify attribute names that conform to the basic pattern of an HTML event handler, emphasizing its `on` prefix and length criteria."},"warnings":[{"fix":"Upgrade your Node.js environment to version 16 or newer. Use `nvm install 16 && nvm use 16` or update your CI/CD configuration.","message":"The `rehypejs` ecosystem, including `hast-util-is-event-handler`, updated to require Node.js 16 or higher in its v7 monorepo release (which applies to `hast-util-is-event-handler` v3.x line).","severity":"breaking","affected_versions":">=3.0.0"},{"fix":"Update all imports to use ESM syntax: `import { isEventHandler } from 'hast-util-is-event-handler'`. Ensure your project is configured for ESM, potentially by setting `\"type\": \"module\"` in `package.json`.","message":"The package transitioned to ES Modules (ESM) exclusively, discontinuing support for CommonJS `require()`. This change was part of the broader `rehypejs` monorepo v7 update, impacting `hast-util-is-event-handler`'s v3.x.","severity":"breaking","affected_versions":">=3.0.0"},{"fix":"Always import symbols directly from the main package entry point (`'hast-util-is-event-handler'`) and avoid relying on deep imports to internal paths, as these are subject to change without major version bumps.","message":"The broader `rehypejs` monorepo v7 release introduced changes to package `exports`, affecting how modules are resolved and preventing access to 'private' internal APIs.","severity":"breaking","affected_versions":">=3.0.0"},{"fix":"Do not rely on `isEventHandler` alone for security or strict HTML validation. If you need to verify against a list of actual HTML events, combine this utility with a separate, comprehensive schema check.","message":"The `isEventHandler` utility only checks if a property name *looks like* an event handler (starts with 'on' and has 5+ characters). It does not validate if the event handler is a known or valid HTML event handler (e.g., 'onmadeupevent' will still return true).","severity":"gotcha","affected_versions":">=1.0.0"},{"fix":"Always use `rehype-sanitize` or a similar sanitization plugin when processing untrusted HTML input to prevent XSS attacks in your `rehype` pipeline.","message":"When working with `hast` trees, especially when accepting user input, there is a risk of Cross-Site Scripting (XSS) vulnerabilities if HTML is not properly sanitized. This utility itself doesn't introduce XSS but is part of an ecosystem where it's a concern.","severity":"gotcha","affected_versions":">=1.0.0"}],"env_vars":null,"last_verified":"2026-04-19T00:00:00.000Z","next_check":"2026-07-18T00:00:00.000Z","problems":[{"fix":"Change your import statement to use ESM syntax: `import { isEventHandler } from 'hast-util-is-event-handler';`. Ensure your `package.json` has `\"type\": \"module\"` or your file uses the `.mjs` extension for Node.js environments.","cause":"Attempting to use `require()` to import `hast-util-is-event-handler`, which is an ES Module (ESM) only package.","error":"Error [ERR_REQUIRE_ESM]: require() of ES Module ... hast-util-is-event-handler/index.js from ... not supported."},{"fix":"Verify that `isEventHandler` is imported as a named export from `hast-util-is-event-handler`: `import { isEventHandler } from 'hast-util-is-event-handler';`. Do not use `import isEventHandler from 'hast-util-is-event-handler';` as there is no default export.","cause":"This error often occurs in bundled or transpiled code when a named export is incorrectly imported, such as trying to destructure a default export or importing a non-existent named export.","error":"TypeError: (0 , hast_util_is_event_handler__WEBPACK_IMPORTED_MODULE_0__.isEventHandler) is not a function"},{"fix":"The function `isEventHandler` is fully typed, and its return type (`boolean`) is simple. You typically do not need to import a specific type for this utility. If you're looking for a type related to AST nodes, refer to `@types/hast` or `hast` itself. For custom type definitions, you might need to declare them locally or extract them from the function's signature if complex.","cause":"Attempting to import a type named `IsEventHandler` from the package, but the package does not explicitly export a type with that name from its root.","error":"TS2305: Module '\"hast-util-is-event-handler\"' has no exported member 'IsEventHandler'."}],"ecosystem":"npm"}