All Projects → rgrove → Crass

rgrove / Crass

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

Programming Languages

ruby
36898 projects - #4 most used programming language

Labels

Projects that are alternatives of or similar to Crass

Plyara
Parse YARA rules and operate over them more easily.
Stars: ✭ 108 (-8.47%)
Mutual labels:  parser
Nmonvisualizer
A Java GUI tool for analyzing NMON system files
Stars: ✭ 114 (-3.39%)
Mutual labels:  parser
Ltgt
Lightweight HTML processor
Stars: ✭ 117 (-0.85%)
Mutual labels:  parser
Flap
Fortran command Line Arguments Parser for poor people
Stars: ✭ 109 (-7.63%)
Mutual labels:  parser
Netcopa
Network Configuration Parser
Stars: ✭ 112 (-5.08%)
Mutual labels:  parser
Cmd Parser
一个非常简单好用的命令解析器,占用资源极少极少,采用哈希算法超快匹配命令!
Stars: ✭ 115 (-2.54%)
Mutual labels:  parser
Forge
Functional style JSON parsing in Kotlin
Stars: ✭ 106 (-10.17%)
Mutual labels:  parser
Instagram Parser
Парсер аккаунтов подписчиков и подписок в Instagram
Stars: ✭ 118 (+0%)
Mutual labels:  parser
Save For Offline
Android app for saving webpages for offline reading.
Stars: ✭ 114 (-3.39%)
Mutual labels:  parser
Lua Gumbo
Moved to https://gitlab.com/craigbarnes/lua-gumbo
Stars: ✭ 116 (-1.69%)
Mutual labels:  parser
Solidity Parser Antlr
A Solidity parser for JS built on top of a robust ANTLR4 grammar
Stars: ✭ 111 (-5.93%)
Mutual labels:  parser
Parze
A clean, efficient parser combinator
Stars: ✭ 113 (-4.24%)
Mutual labels:  parser
Alembic
⚗️ Functional JSON Parser - Linux Ready 🐧
Stars: ✭ 115 (-2.54%)
Mutual labels:  parser
Sywac
🚫 🐭 Asynchronous, single package CLI framework for Node
Stars: ✭ 109 (-7.63%)
Mutual labels:  parser
Vte
Parser for virtual terminal emulators
Stars: ✭ 117 (-0.85%)
Mutual labels:  parser
Cppcmb
A generic C++17 parser-combinator library with a natural grammar notation.
Stars: ✭ 108 (-8.47%)
Mutual labels:  parser
Oletools
oletools - python tools to analyze MS OLE2 files (Structured Storage, Compound File Binary Format) and MS Office documents, for malware analysis, forensics and debugging.
Stars: ✭ 1,848 (+1466.1%)
Mutual labels:  parser
Luqum
A lucene query parser generating ElasticSearch queries and more !
Stars: ✭ 118 (+0%)
Mutual labels:  parser
Surelog
SystemVerilog 2017 Pre-processor, Parser, Elaborator, UHDM Compiler. Provides IEEE Design/TB C/C++ VPI and Python AST API.
Stars: ✭ 116 (-1.69%)
Mutual labels:  parser
Chirp
A modern low-level programming language
Stars: ✭ 116 (-1.69%)
Mutual labels:  parser

Crass

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

Build Status Gem Version

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:

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

Parsing it is simple:

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

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

[{:node=>:comment, :pos=>0, :raw=>"/* Comment! */", :value=>" Comment! "},
 {:node=>:whitespace, :pos=>14, :raw=>"\n"},
 {:node=>:style_rule,
  :selector=>
   {:node=>:selector,
    :value=>"a:hover",
    :tokens=>
     [{:node=>:ident, :pos=>15, :raw=>"a", :value=>"a"},
      {:node=>:colon, :pos=>16, :raw=>":"},
      {:node=>:ident, :pos=>17, :raw=>"hover", :value=>"hover"},
      {:node=>:whitespace, :pos=>22, :raw=>" "}]},
  :children=>
   [{:node=>:whitespace, :pos=>24, :raw=>"\n  "},
    {:node=>:property,
     :name=>"color",
     :value=>"#0d8bfa",
     :children=>
      [{:node=>:whitespace, :pos=>33, :raw=>" "},
       {:node=>:hash,
        :pos=>34,
        :raw=>"#0d8bfa",
        :type=>:unrestricted,
        :value=>"0d8bfa"}],
     :important=>false,
     :tokens=>
      [{:node=>:ident, :pos=>27, :raw=>"color", :value=>"color"},
       {:node=>:colon, :pos=>32, :raw=>":"},
       {:node=>:whitespace, :pos=>33, :raw=>" "},
       {:node=>:hash,
        :pos=>34,
        :raw=>"#0d8bfa",
        :type=>:unrestricted,
        :value=>"0d8bfa"}]},
    {:node=>:semicolon, :pos=>41, :raw=>";"},
    {:node=>:whitespace, :pos=>42, :raw=>"\n  "},
    {:node=>:property,
     :name=>"text-decoration",
     :value=>"underline",
     :children=>
      [{:node=>:whitespace, :pos=>61, :raw=>" "},
       {:node=>:ident, :pos=>62, :raw=>"underline", :value=>"underline"}],
     :important=>false,
     :tokens=>
      [{:node=>:ident,
        :pos=>45,
        :raw=>"text-decoration",
        :value=>"text-decoration"},
       {:node=>:colon, :pos=>60, :raw=>":"},
       {:node=>:whitespace, :pos=>61, :raw=>" "},
       {:node=>:ident, :pos=>62, :raw=>"underline", :value=>"underline"}]},
    {:node=>:semicolon, :pos=>71, :raw=>";"},
    {:node=>:whitespace, :pos=>72, :raw=>"\n"}]}]

If you want, you can stringify the parse tree:

css = Crass::Parser.stringify(tree)

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

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

Wasn't that exciting?

A Note on 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, 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 Jr. and Simon Sapin (again!) for their work on the CSS Syntax Level 3 specification, which defines the tokenizing and parsing rules that Crass implements.

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].