hast-util-is-css-style

3.0.1 · active · verified Sun Apr 19

hast-util-is-css-style is a focused utility from the unified ecosystem, specifically designed to inspect HAST (HTML Abstract Syntax Tree) nodes. Its primary function is to determine whether a given HAST node represents an HTML <style> element that is intended to contain CSS. This utility is currently stable at version 3.0.1 and is part of a larger monorepo (rehypejs/rehype-minify) which tends to release updates synchronously across related packages, especially for major ecosystem changes (e.g., Node.js version support, ESM-only shifts). Its key differentiator lies in its deep integration with the hast specification, ensuring accurate identification of CSS style elements based on their type attribute, including default cases and various text/css variations, which is crucial for tools that parse, transform, or optimize HTML content.

Common errors

Warnings

Install

Imports

Quickstart

This example demonstrates how to use `isCssStyle` to identify HTML `<style>` elements containing CSS, handling various `type` attribute scenarios and non-style elements.

import {h} from 'hastscript';
import {isCssStyle} from 'hast-util-is-css-style';

// Create various HAST nodes to test
const styleNodeWithoutType = h('style', 'body { color: red; }');
const styleNodeWithCssType = h('style', {type: 'text/css'}, 'p { font-size: 16px; }');
const styleNodeWithMixedCaseCssType = h('style', {type: ' TEXT/CSS '}, 'a { text-decoration: none; }');
const styleNodeWithNonCssType = h('style', {type: 'text/foo'}, '.foo { display: block; }');
const scriptNode = h('script', 'console.log("hello");');
const divNode = h('div', 'Some content');

// Check if each node is considered a CSS style element
console.log('Is <style> (no type) CSS style?', isCssStyle(styleNodeWithoutType)); // Expected: true
console.log('Is <style type="text/css"> CSS style?', isCssStyle(styleNodeWithCssType)); // Expected: true
console.log('Is <style type=" TEXT/CSS "> CSS style?', isCssStyle(styleNodeWithMixedCaseCssType)); // Expected: true
console.log('Is <style type="text/foo"> CSS style?', isCssStyle(styleNodeWithNonCssType)); // Expected: false
console.log('Is <script> CSS style?', isCssStyle(scriptNode)); // Expected: false
console.log('Is <div> CSS style?', isCssStyle(divNode)); // Expected: false

view raw JSON →