HAST Property Checker

3.0.0 · active · verified Sun Apr 19

hast-util-has-property is a focused utility within the unified (specifically HAST) ecosystem, designed to ascertain if a given HAST (Hypertext Abstract Syntax Tree) element node possesses a specified property. The current stable version is 3.0.0. This package is maintained by the syntax-tree collective, aligning its release cadence with Node.js LTS versions and the broader `unified` project's evolution, frequently releasing new major versions to adopt modern JavaScript features like ESM. Its primary differentiation lies in its tight integration with the HAST specification, providing a type-safe (TypeScript-enabled) and robust method for property checking, crucial for HTML processing pipelines. It is a highly specialized tool, typically used by developers working directly with HAST manipulation, offering a reliable primitive for conditional logic based on element properties in transform plugins. It strictly operates on valid HAST `Node` objects, particularly `Element` types, providing a precise `boolean` result.

Common errors

Warnings

Install

Imports

Quickstart

Demonstrates how to import and use the `hasProperty` function to check for the existence of properties on different HAST nodes.

import {hasProperty} from 'hast-util-has-property'

// Example 1: Text node has no properties, so 'bravo' is false.
hasProperty({type: 'text', value: 'alpha'}, 'bravo') // => false

// Example 2: Element node without 'className' property.
hasProperty(
  {
    type: 'element',
    tagName: 'div',
    properties: {id: 'bravo'},
    children: []
  },
  'className'
) // => false

// Example 3: Element node with 'id' property.
hasProperty(
  {
    type: 'element',
    tagName: 'div',
    properties: {id: 'charlie'},
    children: []
  },
  'id'
) // => true

console.log('Checked properties successfully.')

view raw JSON →