| Class | CssParser::Parser |
| In: |
lib/css_parser/parser.rb
|
| Parent: | Object |
All CSS is converted to UTF-8.
When calling Parser#new there are some configuaration options:
| USER_AGENT | = | "Ruby CSS Parser/#{VERSION} (http://code.dunae.ca/css_parser/)" | ||
| STRIP_CSS_COMMENTS_RX | = | /\/\*.*?\*\//m | ||
| STRIP_HTML_COMMENTS_RX | = | /\<\!\-\-|\-\-\>/m | ||
| RE_AT_IMPORT_RULE | = | /\@import[\s]+(url\()?["']+(.[^'"]*)["']\)?([\w\s\,]*);?/i | Initial parsing |
| folded_declaration_cache | [R] | |
| loaded_uris | [R] | Array of CSS files that have been loaded. |
# File lib/css_parser/parser.rb, line 41
41: def initialize(options = {})
42: @options = {:absolute_paths => false,
43: :import => true,
44: :io_exceptions => true}.merge(options)
45:
46: # array of RuleSets
47: @rules = []
48:
49:
50: @loaded_uris = []
51:
52: # unprocessed blocks of CSS
53: @blocks = []
54: reset!
55: end
Add a raw block of CSS.
css = <<-EOT
body { font-size: 10pt }
p { margin: 0px; }
@media screen, print {
body { line-height: 1.2 }
}
EOT
parser = CssParser::Parser.new
parser.load_css!(css)
# File lib/css_parser/parser.rb, line 99
99: def add_block!(block, options = {})
100: options = {:base_uri => nil, :charset => nil, :media_types => :all}.merge(options)
101:
102: block = cleanup_block(block)
103:
104: if options[:base_uri] and @options[:absolute_paths]
105: block = CssParser.convert_uris(block, options[:base_uri])
106: end
107:
108: parse_block_into_rule_sets!(block, options)
109:
110: end
Add a CSS rule by setting the selectors, declarations and media_types.
media_types can be a symbol or an array of symbols.
# File lib/css_parser/parser.rb, line 115
115: def add_rule!(selectors, declarations, media_types = :all)
116: rule_set = RuleSet.new(selectors, declarations)
117: add_rule_set!(rule_set, media_types)
118: end
Add a CssParser RuleSet object.
media_types can be a symbol or an array of symbols.
# File lib/css_parser/parser.rb, line 123
123: def add_rule_set!(ruleset, media_types = :all)
124: raise ArgumentError unless ruleset.kind_of?(CssParser::RuleSet)
125:
126: media_types = [media_types] if media_types.kind_of?(Symbol)
127:
128: @rules << {:media_types => media_types, :rules => ruleset}
129: end
Iterate through RuleSet objects.
media_types can be a symbol or an array of symbols.
# File lib/css_parser/parser.rb, line 134
134: def each_rule_set(media_types = :all) # :yields: rule_set
135: media_types = [:all] if media_types.nil?
136: media_types = [media_types] if media_types.kind_of?(Symbol)
137:
138: @rules.each do |block|
139: if media_types.include?(:all) or block[:media_types].any? { |mt| media_types.include?(mt) }
140: yield block[:rules]
141: end
142: end
143: end
Iterate through CSS selectors.
media_types can be a symbol or an array of symbols. See RuleSet#each_selector for options.
# File lib/css_parser/parser.rb, line 149
149: def each_selector(media_types = :all, options = {}) # :yields: selectors, declarations, specificity
150: each_rule_set(media_types) do |rule_set|
151: #puts rule_set
152: rule_set.each_selector(options) do |selectors, declarations, specificity|
153: yield selectors, declarations, specificity
154: end
155: end
156: end
Get declarations by selector.
media_types are optional, and can be a symbol or an array of symbols. The default value is :all.
find_by_selector('#content')
=> 'font-size: 13px; line-height: 1.2;'
find_by_selector('#content', [:screen, :handheld])
=> 'font-size: 13px; line-height: 1.2;'
find_by_selector('#content', :print)
=> 'font-size: 11pt; line-height: 1.2;'
Returns an array of declarations.
# File lib/css_parser/parser.rb, line 73
73: def find_by_selector(selector, media_types = :all)
74: out = []
75: each_selector(media_types) do |sel, dec, spec|
76: out << dec if sel.strip == selector.strip
77: end
78: out
79: end
Load a remote CSS file.
# File lib/css_parser/parser.rb, line 246
246: def load_uri!(uri, base_uri = nil, media_types = :all)
247: base_uri = uri if base_uri.nil?
248: src, charset = read_remote_file(uri)
249:
250: # Load @imported CSS
251: src.scan(RE_AT_IMPORT_RULE).each do |import_rule|
252: import_path = import_rule[1].to_s.gsub(/['"]*/, '').strip
253: import_uri = URI.parse(base_uri.to_s).merge(import_path)
254: #puts import_uri.to_s
255:
256: media_types = []
257: if media_string = import_rule[import_rule.length-1]
258: media_string.split(/\s|\,/).each do |t|
259: media_types << t.to_sym unless t.empty?
260: end
261: end
262:
263: # Recurse
264: load_uri!(import_uri, nil, media_types)
265: end
266:
267: # Remove @import declarations
268: src.gsub!(RE_AT_IMPORT_RULE, '')
269:
270: # Relative paths need to be converted here
271: src = CssParser.convert_uris(src, base_uri) if base_uri and @options[:absolute_paths]
272:
273: add_block!(src, {:media_types => media_types})
274: end