{"id":11006,"library":"hast-util-is-css-style","title":"hast-util-is-css-style","description":"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.","status":"active","version":"3.0.1","language":"javascript","source_language":"en","source_url":"https://github.com/rehypejs/rehype-minify#main","tags":["javascript","css","hast","hast-util","html","style","unist","util","utility","typescript"],"install":[{"cmd":"npm install hast-util-is-css-style","lang":"bash","label":"npm"},{"cmd":"yarn add hast-util-is-css-style","lang":"bash","label":"yarn"},{"cmd":"pnpm add hast-util-is-css-style","lang":"bash","label":"pnpm"}],"dependencies":[],"imports":[{"note":"This package is ESM-only since version 3. CommonJS `require()` is not supported.","wrong":"const {isCssStyle} = require('hast-util-is-css-style')","symbol":"isCssStyle","correct":"import {isCssStyle} from 'hast-util-is-css-style'"},{"note":"While `isCssStyle` operates on `Node` from `unist` directly, `hast` provides a more specific type definition for HTML AST nodes which is commonly used in this ecosystem.","symbol":"Node","correct":"import type {Node} from 'hast'"},{"note":"Used for creating HAST nodes in examples and tests; `hastscript` uses named exports.","wrong":"import h from 'hastscript'","symbol":"h","correct":"import {h} from 'hastscript'"}],"quickstart":{"code":"import {h} from 'hastscript';\nimport {isCssStyle} from 'hast-util-is-css-style';\n\n// Create various HAST nodes to test\nconst styleNodeWithoutType = h('style', 'body { color: red; }');\nconst styleNodeWithCssType = h('style', {type: 'text/css'}, 'p { font-size: 16px; }');\nconst styleNodeWithMixedCaseCssType = h('style', {type: ' TEXT/CSS '}, 'a { text-decoration: none; }');\nconst styleNodeWithNonCssType = h('style', {type: 'text/foo'}, '.foo { display: block; }');\nconst scriptNode = h('script', 'console.log(\"hello\");');\nconst divNode = h('div', 'Some content');\n\n// Check if each node is considered a CSS style element\nconsole.log('Is <style> (no type) CSS style?', isCssStyle(styleNodeWithoutType)); // Expected: true\nconsole.log('Is <style type=\"text/css\"> CSS style?', isCssStyle(styleNodeWithCssType)); // Expected: true\nconsole.log('Is <style type=\" TEXT/CSS \"> CSS style?', isCssStyle(styleNodeWithMixedCaseCssType)); // Expected: true\nconsole.log('Is <style type=\"text/foo\"> CSS style?', isCssStyle(styleNodeWithNonCssType)); // Expected: false\nconsole.log('Is <script> CSS style?', isCssStyle(scriptNode)); // Expected: false\nconsole.log('Is <div> CSS style?', isCssStyle(divNode)); // Expected: false\n","lang":"typescript","description":"This example demonstrates how to use `isCssStyle` to identify HTML `<style>` elements containing CSS, handling various `type` attribute scenarios and non-style elements."},"warnings":[{"fix":"Ensure your project is configured for ESM, or use dynamic `import()` if you must load it from a CommonJS module. Upgrade to Node.js 16+.","message":"Version 3 of `hast-util-is-css-style` is an ECMAScript Module (ESM) only package. Attempting to `require()` it in CommonJS environments will result in an error.","severity":"breaking","affected_versions":">=3.0.0"},{"fix":"Upgrade your Node.js runtime to version 16 or higher.","message":"This package, and the broader `unified` ecosystem, requires Node.js version 16 or newer for compatibility. Older Node.js versions are not supported.","severity":"breaking","affected_versions":">=3.0.0"},{"fix":"Only use public import paths like `hast-util-is-css-style` for module imports.","message":"Due to the adoption of the `exports` field in `package.json` across the `rehypejs` monorepo (e.g., in `7.0.0` for related packages), direct imports to internal paths (e.g., `hast-util-is-css-style/lib/foo.js`) are no longer supported. Only documented public APIs should be imported.","severity":"breaking","affected_versions":">=3.0.0"},{"fix":"When processing untrusted HTML, always use a sanitizer like `rehype-sanitize` in your processing pipeline to ensure the generated output is safe.","message":"Working with HAST and HTML can introduce security vulnerabilities, specifically Cross-Site Scripting (XSS), if not handled carefully. `hast-util-is-css-style` itself is a utility for inspection and does not inherently mitigate these risks.","severity":"gotcha","affected_versions":"*"}],"env_vars":null,"last_verified":"2026-04-19T00:00:00.000Z","next_check":"2026-07-18T00:00:00.000Z","problems":[{"fix":"Change `const {isCssStyle} = require('hast-util-is-css-style')` to `import {isCssStyle} from 'hast-util-is-css-style'` and ensure your Node.js environment supports ESM (e.g., by using `\"type\": \"module\"` in `package.json` or by using `.mjs` file extensions).","cause":"Attempting to import `hast-util-is-css-style` using CommonJS `require()` in a Node.js environment where it is exclusively an ESM package.","error":"ERR_REQUIRE_ESM"},{"fix":"As `hast-util-is-css-style` is ESM-only, ensure you are using `import {isCssStyle} from 'hast-util-is-css-style'` within an ESM context.","cause":"This typically happens in a CommonJS context when `require()` is used on an ESM-only package that exports named exports, or when `require()` is used on a mixed module that's interpreted incorrectly.","error":"TypeError: isCssStyle is not a function"},{"fix":"Refactor your code to use `import` statements instead of `require` for all module dependencies. If you need to load CommonJS modules from ESM, consider `import pkg from 'some-cjs-package';` or `const cjsModule = await import('some-cjs-package');`.","cause":"You are trying to use `require` in an ESM module context (e.g., in a file with `\"type\": \"module\"` or `.mjs` extension) where `require` is not globally available.","error":"ReferenceError: require is not defined"}],"ecosystem":"npm"}