Baike.dev
All toolsOnline toolsAI codingTrendingOpen sourceNewsSubmit
Log in
< Back to tools
C

crass

> 前端框架
Open source

A Ruby CSS parser that's fully compliant with the CSS Syntax Level 3 specification.

160 stars0 likes2 views
WebsiteGitHub

About

A Ruby CSS parser that's fully compliant with the CSS Syntax Level 3 specification.

Crass

Crass is a Ruby CSS parser that's fully compliant with the CSS Syntax Level 3 specification.

Links

  • Home
  • API Docs

Features

  • Pure Ruby, with no runtime dependencies other than Ruby 1.9.x or higher.

  • Tokenizes and parses CSS according to the rules defined in the 14 November 2014 editor's draft of the CSS Syntax Level 3 specification.

  • Extremely tolerant of broken or invalid CSS. If a browser can handle it, Crass should be able to handle it too.

  • Optionally includes comments in the token stream.

  • Optionally preserves certain CSS hacks, such as the IE "*" hack, which would otherwise be discarded according to CSS3 tokenizing rules.

  • Capable of serializing the parse tree back to CSS while maintaining all original whitespace, comments, and indentation.

Problems

  • Crass isn't terribly fast. I mean, it's Ruby, and it's not really slow by Ruby standards. But compared to the CSS parser in your average browser? Yeah, it's slow.

  • Crass only parses the CSS syntax; it doesn't understand what any of it means, doesn't coalesce selectors, etc. You can do this yourself by consuming the parse tree, though.

  • While any node in the parse tree (or the parse tree as a whole) can be serialized back to CSS with perfect fidelity, changes made to those nodes (except for wholesale removal of nodes) are not reflected in the serialized output.

  • Crass only supports UTF-8 input and doesn't respect @charset rules. Input in any other encoding will be converted to UTF-8.

Installing

gem install crass

Examples

Say you have a string containing some CSS:

css
/* Comment! */
a:hover {
  color: #0d8bfa;
  text-decoration: underline;
}

Parsing it is simple:

ruby
tree = Crass.parse(css, :preserve_comments => true)

This returns a big beautiful parse tree, which looks like this:

…

If you want, you can stringify the parse tree:

ruby
css = Crass::Parser.stringify(tree)

...which gives you back exactly what you put in!

css
/* Comment! */
a:hover {
  color: #0d8bfa;
  text-decoration: underline;
}

Wasn't that exciting?

Versioning

As of version 1.0.0, Crass adheres strictly to SemVer 2.0.

Contributing

The best way to contribute is to use Crass and create issues when you run into problems.

Pull requests that fix bugs are more than welcome as long as they include tests. Please adhere to the style and format of the surrounding code, or I might ask you to change things.

If you want to add a feature or refactor something, please get in touch first to make sure I'm on board with your idea and approach; I'm pretty picky, and I'd hate to have to turn down a pull request you spent a lot of time on.

Acknowledgments

I'm deeply grateful to Simon Sapin for his wonderfully comprehensive CSS parsing tests, which I adapted to create many of Crass's tests. They've been invaluable in helping me fix bugs and handle weird edge cases, and Crass would be much crappier without them.

I'm also grateful to Tab Atkins-Bittner and Simon Sapin (again!) for their work on the CSS Syntax Level 3 specification, which defines the tokenizing and parsing rules that Crass implements.

Issues· 2 open

View all issuesOpen on GitHub

No open issues yet, or sync has not completed.

> Tags

Rubycssparserruby

No comments yet. Be the first to share.

> Details

PublishedAug 1, 2026
UpdatedSep 18, 2026
Category前端框架
PricingOpen source

> Related tools

R
React
用于构建用户界面的 JavaScript 库
V
Vue.js
渐进式 JavaScript 框架
N
Next.js
基于 React 的全栈 Web 框架