HAST Utility: Is Event Handler

3.0.1 · active · verified Sun Apr 19

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

Warnings

Install

Imports

Quickstart

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.

view raw JSON →