HAST Utility: Is Event Handler
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.
Common errors
-
Error [ERR_REQUIRE_ESM]: require() of ES Module ... hast-util-is-event-handler/index.js from ... not supported.
cause Attempting to use `require()` to import `hast-util-is-event-handler`, which is an ES Module (ESM) only package.fixChange 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. -
TypeError: (0 , hast_util_is_event_handler__WEBPACK_IMPORTED_MODULE_0__.isEventHandler) is not a function
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.fixVerify 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. -
TS2305: Module '"hast-util-is-event-handler"' has no exported member 'IsEventHandler'.
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.fixThe 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.
Warnings
- breaking 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).
- breaking 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.
- breaking The broader `rehypejs` monorepo v7 release introduced changes to package `exports`, affecting how modules are resolved and preventing access to 'private' internal APIs.
- gotcha 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).
- gotcha 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.
Install
-
npm install hast-util-is-event-handler -
yarn add hast-util-is-event-handler -
pnpm add hast-util-is-event-handler
Imports
- isEventHandler
const isEventHandler = require('hast-util-is-event-handler')import { isEventHandler } from 'hast-util-is-event-handler' - isEventHandler (TypeScript)
import isEventHandler from 'hast-util-is-event-handler'
import { isEventHandler } from 'hast-util-is-event-handler' import type { Handler } from 'hast-util-is-event-handler/lib'
Quickstart
import { isEventHandler } from 'hast-util-is-event-handler';
// Basic usage to check common event handler names
console.log("Is 'onclick' an event handler?", isEventHandler('onclick'));
//=> true
console.log("Is 'onmouseover' an event handler?", isEventHandler('onmouseover'));
//=> true
// Checks for 'on' prefix and length >= 5
console.log("Is 'ones' an event handler?", isEventHandler('ones'));
//=> false (length < 5)
console.log("Is 'onfoo' an event handler?", isEventHandler('onfoo'));
//=> true (matches 'on' prefix and length criteria, even if 'onfoo' is not a real event)
console.log("Is 'class' an event handler?", isEventHandler('class'));
//=> false
// This package requires Node.js 16+ due to its ESM-only nature.
// Ensure your environment supports ES Modules for direct import usage.