hast-util-whitespace: Inter-Element Whitespace Check

3.0.0 · active · verified Tue Apr 21

This package provides a highly specialized utility within the HAST (HTML Abstract Syntax Tree) ecosystem, designed to determine if a given node or string constitutes "inter-element whitespace" as defined by HTML specifications. Currently at stable version 3.0.0, the package adheres to the unified collective's release cadence, which typically involves major version bumps for breaking changes and Node.js compatibility updates. Its key differentiator is its precise adherence to HTML's definition of inter-element whitespace, making it crucial for tools that need to correctly manipulate or process HTML ASTs while preserving semantic meaning. It is ESM-only and ships with TypeScript types.

Common errors

Warnings

Install

Imports

Quickstart

Demonstrates how to use the `whitespace` function to identify inter-element whitespace in both HAST text nodes and raw strings, showcasing various input types.

import { whitespace } from 'hast-util-whitespace';
import type { Text, Element } from 'hast';

// Example 1: An element node is not inter-element whitespace
const elementNode: Element = {
  type: 'element',
  tagName: 'div',
  properties: {},
  children: []
};
console.log('Is elementNode whitespace?', whitespace(elementNode)); // => false

// Example 2: A text node containing only inter-element whitespace characters
const whitespaceTextNode: Text = {
  type: 'text',
  value: '\t  \n'
};
console.log('Is whitespaceTextNode whitespace?', whitespace(whitespaceTextNode)); // => true

// Example 3: A text node containing both whitespace and non-whitespace
const mixedTextNode: Text = {
  type: 'text',
  value: '  text\f'
};
console.log('Is mixedTextNode whitespace?', whitespace(mixedTextNode)); // => false

// Example 4: A string containing only inter-element whitespace characters
console.log('Is "\t  \n" whitespace?', whitespace('\t  \n')); // => true

// Example 5: A string containing non-whitespace characters
console.log('Is "hello" whitespace?', whitespace('hello')); // => false

view raw JSON →