Ruby CSS Parser
Ruby
Latest commit aa4bfde Nov 29, 2016 @grosser grosser committed on GitHub Merge pull request #79 from premailer/grosser/old
support 1.9 / jruby
Permalink
Failed to load latest commit information.
lib v1.4.7 Nov 29, 2016
test Fix use of inital value for background-position in shorthand. Nov 28, 2016
.editorconfig http://editorconfig.org Jun 27, 2016
.gitignore
.jrubyrc .jrubyrc Jun 28, 2016
.travis.yml before_install: rm Gemfile.lock Jun 27, 2016
CHANGELOG.md Version bump to 1.4.5 Jun 28, 2016
Gemfile support 1.9 / jruby Nov 29, 2016
Gemfile.lock v1.4.7 Nov 29, 2016
MIT-LICENSE cleanup Mar 13, 2013
README.md Version bump to 1.4.5 Jun 28, 2016
Rakefile
css_parser.gemspec Ship the license in the package Jun 17, 2016

README.md

Ruby CSS Parser

Load, parse and cascade CSS rule sets in Ruby.

Setup

gem install css_parser

Usage

require 'css_parser'
include CssParser

parser = CssParser::Parser.new
parser.load_uri!('http://example.com/styles/style.css')

parser = CssParser::Parser.new
parser.load_uri!('file://home/user/styles/style.css')

# load a remote file, setting the base_uri and media_types
parser.load_uri!('../style.css', {:base_uri => 'http://example.com/styles/inc/', :media_types => [:screen, :handheld])

# load a local file, setting the base_dir and media_types
parser.load_file!('print.css', '~/styles/', :print)

# load a string
parser = CssParser::Parser.new
parser.load_string! 'a { color: hotpink; }'

# lookup a rule by a selector
parser.find_by_selector('#content')
#=> 'font-size: 13px; line-height: 1.2;'

# lookup a rule by a selector and media type
parser.find_by_selector('#content', [:screen, :handheld])

# iterate through selectors by media type
parser.each_selector(:screen) do |selector, declarations, specificity|
  ...
end

# add a block of CSS
css = <<-EOT
  body { margin: 0 1em; }
EOT

parser.add_block!(css)

# output all CSS rules in a single stylesheet
parser.to_s
=> #content { font-size: 13px; line-height: 1.2; }
   body { margin: 0 1em; }

Testing

bundle
bundle exec rake

Runs on Ruby/JRuby 1.9.2 or above.

Credits

By Alex Dunae (dunae.ca, e-mail 'code' at the same domain), 2007-11.

License: MIT

Thanks to all the wonderful contributors for their updates.

Made on Vancouver Island.

Build Status