hast-util-whitespace: Inter-Element Whitespace Check
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
-
TypeError [ERR_UNKNOWN_FILE_EXTENSION]: Unknown file extension ".ts" for .../hast-util-whitespace/index.ts
cause Trying to import an ESM-only TypeScript package directly in a CommonJS context or an incorrectly configured Node.js environment.fixEnsure your project is configured for ESM by setting `"type": "module"` in your `package.json` or by using `.mjs` file extensions. Also, make sure your Node.js version meets the package requirements (16+ for v3). -
Error [ERR_REQUIRE_ESM]: require() of ES Module .../hast-util-whitespace/index.js from ... not supported.
cause Attempting to use `require()` to import `hast-util-whitespace`, which is an ESM-only package.fixSwitch to using `import` statements for `hast-util-whitespace`: `import { whitespace } from 'hast-util-whitespace';`. Ensure your project supports ESM. -
TypeError: The 'thing' argument must be a string or a hast Node
cause An invalid type (e.g., `null`, `undefined`, a number, or an element node that isn't a `Text` node) was passed to the `whitespace` function.fixVerify that the value you are passing to `whitespace` is either a string or a HAST `Text` node. Version 3.0.0 introduced stricter validation. -
SyntaxError: Named export 'whitespace' not found. The requested module 'hast-util-whitespace' does not provide an export named 'whitespace'
cause This error can occur if you're using an older Node.js version that doesn't fully support the `exports` field in `package.json` correctly or if you're trying to import a non-existent named export.fixEnsure you are running Node.js 16+ for `hast-util-whitespace@3`. Also, confirm that you are using the correct named import `{ whitespace }` as there is no default export.
Warnings
- breaking Version 3.0.0 of `hast-util-whitespace` requires Node.js version 16 or higher. Older Node.js versions are no longer supported.
- breaking Version 3.0.0 changed to use the `exports` field in `package.json`, which implies that internal/private APIs should not be accessed directly. Only documented public exports should be used.
- breaking As of version 3.0.0, the `whitespace` function strictly validates its input, only accepting valid HAST `Node` objects (specifically `Text` nodes) or strings. Passing other types will result in a `TypeError`.
- breaking Version 2.0.0 transitioned to being an ESM-only package. It no longer supports CommonJS `require()` syntax.
- gotcha This utility is highly niche, specifically checking for 'inter-element whitespace' as defined by HTML specifications, not general whitespace. Misunderstanding this specific definition can lead to unexpected results when trying to identify all whitespace in an HTML tree.
Install
-
npm install hast-util-whitespace -
yarn add hast-util-whitespace -
pnpm add hast-util-whitespace
Imports
- whitespace
const { whitespace } = require('hast-util-whitespace')import { whitespace } from 'hast-util-whitespace'
Quickstart
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