cssom

中文文档
0.5.0发布于 2021/06/21MITCommonJS零依赖近 30 天下载 1.8亿

CSS Object Model implementation and CSS parser

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

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/NV/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 cssom

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

Who uses CSSOM.js

版本与发布记录

cssom 共发布 21 个版本,首次发布于 2011/10/25;过去 12 个月发布了 0 个版本。

版本发布日期距今
0.5.0latest2021/06/215 年前
0.4.42019/11/146 年前
0.4.32019/11/146 年前
0.4.22019/11/116 年前
0.4.12019/07/047 年前
0.4.02019/07/047 年前

关于 cssom 的常见问题

cssom 是什么?

CSS Object Model implementation and CSS parser

如何安装 cssom?

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

cssom 支持 TypeScript 吗?

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

cssom 支持 ESM 还是 CommonJS?

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

cssom 可以商用吗?

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

cssom 的下载量有多少?

根据 npm 官方下载统计,cssom 在 2026/10/06 – 2026/11/04 这 30 天内被下载 184,064,427 次,日均约 6,135,481 次。下载量会把 CI 构建、镜像同步和作为其他包依赖的安装一并计入,适合横向比较热度,不能直接等同于使用人数。

cssom 还在维护吗?

最新版本 0.5.0 发布于 2021/06/21(5 年前);至今共发布 21 个版本,其中过去 12 个月发布了 0 个;首次发布于 2011/10/25。发布频率只是参考信号:功能稳定的工具可能很少需要更新,建议结合源码仓库的 issue 和提交记录判断。