CSSOM.js is a pure JavaScript CSS parser and a partial implementation of the W3C CSS Object Model (CSSOM). Currently at version 0.5.0, this library provides functionality to parse raw CSS strings into a structured object representation, allowing for basic programmatic inspection and manipulation of CSS rules. Its release cadence has been slow, with the last publish being 5 years ago. A key differentiator is its direct representation of CSS as a JavaScript object. However, it explicitly advises against its use for advanced CSS processing tasks such as minification, munging, or reformatting, particularly where the preservation of property order or fallback declarations (e.g., `background: gray; background: linear-gradient(...)`) is critical, as it applies CSS cascade rules and overwrites properties. It also has known limitations, including incomplete support for all CSS escape sequences and incompatibility with Internet Explorer versions prior to 9.
npm install cssomVerified import paths — ran on the pinned version, not inferred.
This quickstart demonstrates how to parse a CSS string using `CSSOM.parse()` in Node.js and iterate through the resulting CSS rules and their styles, highlighting the property overwriting behavior.
For CSS minification, munging, or reformatting where property order and all declarations must be preserved, consider using purpose-built tools like PostCSS, ReworkCSS, CSSTree, or CSSToJSON.
Preprocess CSS to normalize or resolve complex escape sequences before feeding them into CSSOM, or handle them manually in post-processing of the parsed output.
Ensure that the target browser environment is IE9+ if using CSSOM on the client-side. For broader compatibility, consider alternative client-side CSS parsers or polyfills for the necessary JavaScript features.
Evaluate the long-term viability for new projects. For ongoing development or critical applications, consider modern, actively maintained alternatives like PostCSS or the browser's native CSS Typed OM (where supported).
For Node.js, add `const CSSOM = require('cssom');` at the top of your file. For browser use, run `node build.js` in the project directory and then include the generated `build/CSSOM.js` file in your HTML using a `<script>` tag.Ensure you import the entire `CSSOM` object using `const CSSOM = require('cssom');` and then call `CSSOM.parse(...)`.If your use case requires preserving all declarations, including fallbacks or duplicate properties, CSSOM is not the right tool. Consider using alternative CSS parsers like PostCSS or reworkcss/css, which offer more granular control over parsing and AST manipulation.
No dependency data recorded yet.