11# Ruby CSS Parser [ ![ Build Status] ( https://github.com/premailer/css_parser/workflows/Run%20css_parser%20CI/badge.svg )] ( https://github.com/ojab/css_parser/actions?query=workflow%3A%22Run+css_parser+CI%22 ) [ ![ Gem Version] ( https://badge.fury.io/rb/css_parser.svg )] ( https://badge.fury.io/rb/css_parser )
22
3- Load, parse and cascade CSS rule sets in Ruby.
3+ Load, parse and cascade CSS rule sets in Ruby.
44
55# Setup
66
@@ -10,35 +10,38 @@ gem install css_parser
1010
1111# Usage
1212
13+ You initiate a document ` CssParser::Document.new ` and you can start to load it with css. Main methods to add css are: load_uri! (load url and follows @imports based on the full url), load_file! (loads file and follows @imports based on path from file imported) and load_string! (load a block of css). All of these apis tries to absolute all urls
14+
15+
1316``` Ruby
1417require ' css_parser'
1518include CssParser
1619
17- parser = CssParser ::Parser .new
18- parser .load_uri!(' http://example.com/styles/style.css' )
20+ document = CssParser ::Document .new
21+ document .load_uri!(' http://example.com/styles/style.css' )
1922
20- parser = CssParser ::Parser .new
21- parser .load_uri!(' file://home/user/styles/style.css' )
23+ document = CssParser ::Document .new
24+ document .load_uri!(' file://home/user/styles/style.css' )
2225
2326# load a remote file, setting the base_uri and media_types
24- parser .load_uri!(' ../style.css' , {base_uri: ' http://example.com/styles/inc/' , media_types: [:screen , :handheld ]})
27+ document .load_uri!(' ../style.css' , {base_uri: ' http://example.com/styles/inc/' , media_types: [:screen , :handheld ]})
2528
2629# load a local file, setting the base_dir and media_types
27- parser .load_file!(' print.css' , ' ~/styles/' , :print )
30+ document .load_file!(' print.css' , ' ~/styles/' , :print )
2831
2932# load a string
30- parser = CssParser ::Parser .new
31- parser .load_string! ' a { color: hotpink; }'
33+ document = CssParser ::Document .new
34+ document .load_string! ' a { color: hotpink; }'
3235
3336# lookup a rule by a selector
34- parser .find_by_selector(' #content' )
37+ document .find_by_selector(' #content' )
3538# => 'font-size: 13px; line-height: 1.2;'
3639
3740# lookup a rule by a selector and media type
38- parser .find_by_selector(' #content' , [:screen , :handheld ])
41+ document .find_by_selector(' #content' , [:screen , :handheld ])
3942
4043# iterate through selectors by media type
41- parser .each_selector(:screen ) do |selector , declarations , specificity |
44+ document .each_selector(:screen ) do |selector , declarations , specificity |
4245 ...
4346end
4447
@@ -47,24 +50,24 @@ css = <<-EOT
4750 body { margin: 0 1em; }
4851EOT
4952
50- parser .add_block!(css)
53+ document .add_block!(css)
5154
5255# output all CSS rules in a single stylesheet
53- parser .to_s
56+ document .to_s
5457=> # content { font-size: 13px; line-height: 1.2; }
5558 body { margin: 0 1em; }
5659
5760# capturing byte offsets within a file
58- parser .load_uri!(' ../style.css' , {base_uri: ' http://example.com/styles/inc/' , capture_offsets: true )
59- content_rule = parser .find_rule_sets([' #content' ]).first
61+ document .load_uri!(' ../style.css' , {base_uri: ' http://example.com/styles/inc/' , capture_offsets: true )
62+ content_rule = document .find_rule_sets([' #content' ]).first
6063content_rule.filename
6164# => 'http://example.com/styles/styles.css'
6265content_rule.offset
6366# => 10703..10752
6467
6568# capturing byte offsets within a string
66- parser .load_string!(' a { color: hotpink; }' , {filename: ' index.html' , capture_offsets: true )
67- content_rule = parser .find_rule_sets([' a' ]).first
69+ document .load_string!(' a { color: hotpink; }' , {filename: ' index.html' , capture_offsets: true )
70+ content_rule = document .find_rule_sets([' a' ]).first
6871content_rule.filename
6972# => 'index.html'
7073content_rule.offset
0 commit comments