Registry / serialization / hast-util-is-event-handler

hast-util-is-event-handler

JSON →
library3.0.1jsnpmunverified

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.

npm install hast-util-is-event-handler
INSTALL
IMPORT
SIG · HAST-UTIL-IS-EVENT
H
hast-util-is-event-handler
serializationjavascriptv3.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.

isEventHandler
import { isEventHandler } from 'hast-util-is-event-handler'
const isEventHandler = require('hast-util-is-event-handler')
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.
isEventHandler (TypeScript)
import { isEventHandler } from 'hast-util-is-event-handler' import type { Handler } from 'hast-util-is-event-handler/lib'
import isEventHandler from 'hast-util-is-event-handler'
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.

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.

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.
Debug
Known issues
breakingThe `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).
fix
Upgrade your Node.js environment to version 16 or newer. Use `nvm install 16 && nvm use 16` or update your CI/CD configuration.
affects: >=3.0.0
breakingThe 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.
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`.
affects: >=3.0.0
breakingThe broader `rehypejs` monorepo v7 release introduced changes to package `exports`, affecting how modules are resolved and preventing access to 'private' internal APIs.
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.
affects: >=3.0.0
gotchaThe `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).
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.
affects: >=1.0.0
gotchaWhen 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.
fix
Always use `rehype-sanitize` or a similar sanitization plugin when processing untrusted HTML input to prevent XSS attacks in your `rehype` pipeline.
affects: >=1.0.0
Errors
Common errors & fixes
Error [ERR_REQUIRE_ESM]: require() of ES Module ... hast-util-is-event-handler/index.js from ... not supported.
Attempting to use `require()` to import `hast-util-is-event-handler`, which is an ES Module (ESM) only package.
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.
TypeError: (0 , hast_util_is_event_handler__WEBPACK_IMPORTED_MODULE_0__.isEventHandler) is not a function
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.
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.
TS2305: Module '"hast-util-is-event-handler"' has no exported member 'IsEventHandler'.
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.
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.
Upgrade
Version history
3.0.1latest on npm
Audit
Dependencies

No dependency data recorded yet.

Agent activity
8 hits · last 30 days
node
8
Resources
hast-util-is-event-handler — npm install hast-util-is-event-handler · libregistry