@acemir/cssom

中文文档
0.10.1发布于 2026/10/30MITCommonJS零依赖近 30 天下载 5052.5万

CSS Object Model implementation and CSS parser

原始使用文档
文档目录3 节
  1. Parser demo
  2. Don’t use it if...
  3. Tests

CSSOM

CSSOM.js is a CSS parser written in pure JavaScript. It is also a partial implementation of CSS Object Model.

CSSOM.parse("body {color: black}")
-> {
  cssRules: [
    {
      selectorText: "body",
      style: {
        0: "color",
        color: "black",
        length: 1
      }
    }
  ]
}

Parser demo

Works well in Google Chrome 6+, Safari 5+, Firefox 3.6+, Opera 10.63+. Doesn't work in IE < 9 because of unsupported getters/setters.

To use CSSOM.js in the browser you might want to build a one-file version that exposes a single CSSOM global variable:

➤ git clone https://github.com/acemir/CSSOM.git
➤ cd CSSOM
➤ node build.js
build/CSSOM.js is done

To use it with Node.js or any other CommonJS loader:

➤ npm install @acemir/cssom

You can inject a custom declaration implementation for parsed rule styles:

const cssom = require("@acemir/cssom");

cssom.setup({
  CSSStyleDeclaration: YourCSSStyleDeclaration
});

// or globally:
cssom.setCSSStyleDeclaration(YourCSSStyleDeclaration);

Don’t use it if...

You parse CSS to mungle, minify or reformat code like this:

div {
  background: gray;
  background: linear-gradient(to bottom, white 0%, black 100%);
}

This pattern is often used to give browsers that don’t understand linear gradients a fallback solution (e.g. gray color in the example). In CSSOM, background: gray gets overwritten. It does NOT get preserved.

If you do CSS mungling, minification, or image inlining, considere using one of the following:

Tests

To run tests locally:

➤ git submodule init
➤ git submodule update

版本与发布记录

@acemir/cssom 共发布 36 个版本,首次发布于 2025/07/12;过去 12 个月发布了 14 个版本。

版本发布日期距今
0.10.1latest2026/10/307 天前
0.10.02026/10/307 天前
0.9.322026/10/1126 天前
0.9.312026/01/129 个月前
0.9.302025/12/2310 个月前
0.9.292025/12/1110 个月前

关于 @acemir/cssom 的常见问题

@acemir/cssom 是什么?

CSS Object Model implementation and CSS parser

如何安装 @acemir/cssom?

在项目目录运行 npm install @acemir/cssom,或使用 pnpm add / yarn add / bun add。

@acemir/cssom 支持 TypeScript 吗?

该包的 package.json 没有声明类型入口。需要类型提示时,可以在 npm 上查找社区维护的 @types/acemir__cssom 是否存在。

@acemir/cssom 支持 ESM 还是 CommonJS?

以 CommonJS 形式发布,可以直接 require();在 ES Module 项目中也可以用 import 引入(Node.js 会把 module.exports 作为默认导出)。

@acemir/cssom 可以商用吗?

@acemir/cssom 使用 MIT 许可。宽松许可:允许商业使用、修改和再分发,通常只需在分发时保留原版权与许可声明。以上为概括,不构成法律意见,请以许可原文为准。

@acemir/cssom 的下载量有多少?

根据 npm 官方下载统计,@acemir/cssom 在 2026/10/05 – 2026/11/03 这 30 天内被下载 50,524,645 次,日均约 1,684,155 次。下载量会把 CI 构建、镜像同步和作为其他包依赖的安装一并计入,适合横向比较热度,不能直接等同于使用人数。

@acemir/cssom 还在维护吗?

最新版本 0.10.1 发布于 2026/10/30(7 天前);至今共发布 36 个版本,其中过去 12 个月发布了 14 个;首次发布于 2025/07/12。发布频率只是参考信号:功能稳定的工具可能很少需要更新,建议结合源码仓库的 issue 和提交记录判断。