Class: CssParserMaster::Parser
- Inherits:
-
Object
- Object
- CssParserMaster::Parser
- Defined in:
- lib/css_parser_master/parser.rb
Overview
Parser class
All CSS is converted to UTF-8.
When calling Parser#new there are some configuaration options:
absolute_paths
-
Convert relative paths to absolute paths (
href
,src
andurl('')
. Boolean, default isfalse
. import
-
Follow
@import
rules. Boolean, default istrue
. io_exceptions
-
Throw an exception if a link can not be found. Boolean, default is
true
.
Constant Summary collapse
- USER_AGENT =
"Ruby CSS Parser/#{CssParserMaster::VERSION} (http://code.dunae.ca/css_parser/)"
- STRIP_CSS_COMMENTS_RX =
/\/\*.*?\*\//m
- STRIP_HTML_COMMENTS_RX =
/\<\!\-\-|\-\-\>/m
- RE_AT_IMPORT_RULE =
Initial parsing
/\@import[\s]+(url\()?["''"]?(.[^'"\s"']*)["''"]?\)?([\w\s\,^\])]*)\)?;?/
Class Attribute Summary collapse
-
.folded_declaration_cache ⇒ Object
readonly
Returns the value of attribute folded_declaration_cache.
Instance Attribute Summary collapse
-
#loaded_uris ⇒ Object
readonly
Array of CSS files that have been loaded.
Instance Method Summary collapse
-
#add_block!(block, options = {}) ⇒ Object
Add a raw block of CSS.
-
#add_rule!(selectors, declarations, media_types = :all) ⇒ Object
Add a CSS rule by setting the
selectors
,declarations
andmedia_types
. -
#add_rule_set!(ruleset, media_types = :all) ⇒ Object
Add a CssParserMaster RuleSet object.
-
#compact! ⇒ Object
Merge declarations with the same selector.
-
#each_rule_set(media_types = :all) ⇒ Object
Iterate through RuleSet objects.
-
#each_selector(media_types = :all, options = {}) ⇒ Object
Iterate through CSS selectors.
-
#each_selector_sorted(media_types = :all, options = {}) ⇒ Object
Iterate through CSS selectors.
-
#find_by_selector(selector, media_types = :all) ⇒ Object
(also: #[])
Get declarations by selector.
-
#initialize(options = {}) ⇒ Parser
constructor
A new instance of Parser.
-
#load_file!(file_name, base_dir = nil, media_types = :all) ⇒ Object
Load a local CSS file.
-
#load_uri!(uri, base_uri = nil, media_types = :all) ⇒ Object
Load a remote CSS file.
-
#parse_block_into_rule_sets!(block, options = {}) ⇒ Object
:nodoc:.
-
#selector_declarations(&block) ⇒ Object
go through all selector of the parser in order of specificity parse declarations for each selector.
-
#to_s(media_types = :all) ⇒ Object
Output all CSS rules as a single stylesheet.
Constructor Details
#initialize(options = {}) ⇒ Parser
Returns a new instance of Parser.
41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 |
# File 'lib/css_parser_master/parser.rb', line 41 def initialize( = {}) @options = {:absolute_paths => false, :import => true, :io_exceptions => true}.merge() # array of RuleSets @rules = [] @loaded_uris = [] # unprocessed blocks of CSS @blocks = [] reset! end |
Class Attribute Details
.folded_declaration_cache ⇒ Object (readonly)
Returns the value of attribute folded_declaration_cache.
39 40 41 |
# File 'lib/css_parser_master/parser.rb', line 39 def folded_declaration_cache @folded_declaration_cache end |
Instance Attribute Details
#loaded_uris ⇒ Object (readonly)
Array of CSS files that have been loaded.
31 32 33 |
# File 'lib/css_parser_master/parser.rb', line 31 def loaded_uris @loaded_uris end |
Instance Method Details
#add_block!(block, options = {}) ⇒ Object
Add a raw block of CSS.
In order to follow @import rules you must supply either a :base_dir
or :base_uri
option.
Example
css = <<-EOT
body { font-size: 10pt }
p { margin: 0px; }
@media screen, print {
body { line-height: 1.2 }
}
EOT
parser = CssParserMaster::Parser.new
parser.add_block!(css)
– TODO: add media_type ++
103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 |
# File 'lib/css_parser_master/parser.rb', line 103 def add_block!(block, = {}) = {:base_uri => nil, :base_dir => nil, :charset => nil, :media_types => :all}.merge() block = cleanup_block(block) if [:base_uri] and @options[:absolute_paths] block = CssParserMaster.convert_uris(block, [:base_uri]) end # Load @imported CSS block.scan(RE_AT_IMPORT_RULE).each do |import_rule| media_types = [] if media_string = import_rule[import_rule.length-1] media_string.split(/\s|\,/).each do |t| media_types << t.to_sym unless t.empty? end end import_path = import_rule[1].to_s.gsub(/['"]*/, '').strip if [:base_uri] import_uri = URI.parse([:base_uri].to_s).merge(import_path) load_uri!(import_uri, [:base_uri], media_types) elsif [:base_dir] load_file!(import_path, [:base_dir], media_types) end end # Remove @import declarations block.gsub!(RE_AT_IMPORT_RULE, '') parse_block_into_rule_sets!(block, ) end |
#add_rule!(selectors, declarations, media_types = :all) ⇒ Object
Add a CSS rule by setting the selectors
, declarations
and media_types
.
media_types
can be a symbol or an array of symbols.
140 141 142 143 144 |
# File 'lib/css_parser_master/parser.rb', line 140 def add_rule!(selectors, declarations, media_types = :all) rule_set = RuleSet.new(selectors, declarations) # rule_set = RuleSet.new(Selectors.new(selectors), declarations) add_rule_set!(rule_set, media_types) end |
#add_rule_set!(ruleset, media_types = :all) ⇒ Object
Add a CssParserMaster RuleSet object.
media_types
can be a symbol or an array of symbols.
149 150 151 152 153 154 155 |
# File 'lib/css_parser_master/parser.rb', line 149 def add_rule_set!(ruleset, media_types = :all) raise ArgumentError unless ruleset.kind_of?(CssParserMaster::RuleSet) media_types = [media_types] if media_types.kind_of?(Symbol) @rules << {:media_types => media_types, :rules => ruleset} end |
#compact! ⇒ Object
Merge declarations with the same selector.
221 222 223 224 225 |
# File 'lib/css_parser_master/parser.rb', line 221 def compact! # :nodoc: compacted = [] compacted end |
#each_rule_set(media_types = :all) ⇒ Object
Iterate through RuleSet objects.
media_types
can be a symbol or an array of symbols.
160 161 162 163 164 165 166 167 168 169 |
# File 'lib/css_parser_master/parser.rb', line 160 def each_rule_set(media_types = :all) # :yields: rule_set media_types = [:all] if media_types.nil? media_types = [media_types] if media_types.kind_of?(Symbol) @rules.each do |block| if media_types.include?(:all) or block[:media_types].any? { |mt| media_types.include?(mt) } yield block[:rules] end end end |
#each_selector(media_types = :all, options = {}) ⇒ Object
Iterate through CSS selectors.
media_types
can be a symbol or an array of symbols. See RuleSet#each_selector for options
.
185 186 187 188 189 190 191 192 193 |
# File 'lib/css_parser_master/parser.rb', line 185 def each_selector(media_types = :all, = {}) # :yields: selectors, declarations, specificity each_rule_set(media_types) do |rule_set| #puts rule_set rule_set.each_selector() do |selector| # puts "selector: #{selector.inspect}" yield selector end end end |
#each_selector_sorted(media_types = :all, options = {}) ⇒ Object
Iterate through CSS selectors.
media_types
can be a symbol or an array of symbols. See RuleSet#each_selector for options
.
199 200 201 202 203 204 205 206 207 208 |
# File 'lib/css_parser_master/parser.rb', line 199 def each_selector_sorted(media_types = :all, = {}) # :yields: selectors, declarations, specificity order = .delete(:order) || :desc sels = [] each_selector(media_types, ) do |sel| sels << sel end sorted_sels = sels.sort_by(&:specificity).reverse sorted_sels.reverse! if order == :asc sorted_sels.each{|s| yield s} end |
#find_by_selector(selector, media_types = :all) ⇒ Object Also known as: []
Get declarations by selector.
media_types
are optional, and can be a symbol or an array of symbols. The default value is :all
.
Examples
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.
73 74 75 76 77 78 79 80 |
# File 'lib/css_parser_master/parser.rb', line 73 def find_by_selector(selector, media_types = :all) out = [] each_selector(media_types) do |sel| # puts "selector declaration: #{sel.declarations_to_s}" out << sel.declarations_to_s if sel.selector.strip == selector.strip end out end |
#load_file!(file_name, base_dir = nil, media_types = :all) ⇒ Object
Load a local CSS file.
307 308 309 310 311 312 313 314 315 |
# File 'lib/css_parser_master/parser.rb', line 307 def load_file!(file_name, base_dir = nil, media_types = :all) file_name = File.(file_name, base_dir) return unless File.readable?(file_name) src = IO.read(file_name) base_dir = File.dirname(file_name) add_block!(src, {:media_types => media_types, :base_dir => base_dir}) end |
#load_uri!(uri, base_uri = nil, media_types = :all) ⇒ Object
Load a remote CSS file.
299 300 301 302 303 304 |
# File 'lib/css_parser_master/parser.rb', line 299 def load_uri!(uri, base_uri = nil, media_types = :all) base_uri = uri if base_uri.nil? src, charset = read_remote_file(uri) add_block!(src, {:media_types => media_types, :base_uri => base_uri}) end |
#parse_block_into_rule_sets!(block, options = {}) ⇒ Object
:nodoc:
227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 |
# File 'lib/css_parser_master/parser.rb', line 227 def parse_block_into_rule_sets!(block, = {}) # :nodoc: = {:media_types => :all}.merge() media_types = [:media_types] in_declarations = false block_depth = 0 # @charset is ignored for now in_charset = false in_string = false in_at_media_rule = false current_selectors = '' current_declarations = '' block.scan(/([\\]?[{}\s"]|(.[^\s"{}\\]*))/).each do |matches| #block.scan(/((.[^{}"\n\r\f\s]*)[\s]|(.[^{}"\n\r\f]*)\{|(.[^{}"\n\r\f]*)\}|(.[^{}"\n\r\f]*)\"|(.*)[\s]+)/).each do |matches| token = matches[0] #puts "TOKEN: #{token}" unless token =~ /^[\s]*$/ if token =~ /\A"/ # found un-escaped double quote in_string = !in_string end if in_declarations current_declarations += token if token =~ /\}/ and not in_string current_declarations.gsub!(/\}[\s]*$/, '') in_declarations = false unless current_declarations.strip.empty? #puts "SAVING #{current_selectors} -> #{current_declarations}" add_rule!(current_selectors, current_declarations, media_types) end current_selectors = '' current_declarations = '' end elsif token =~ /@media/i # found '@media', reset current media_types in_at_media_rule = true media_types = [] elsif in_at_media_rule if token =~ /\{/ block_depth = block_depth + 1 in_at_media_rule = false else token.gsub!(/[,\s]*/, '') media_types << token.strip.downcase.to_sym unless token.empty? end elsif in_charset or token =~ /@charset/i # iterate until we are out of the charset declaration in_charset = (token =~ /;/ ? false : true) else if token =~ /\}/ and not in_string block_depth = block_depth - 1 else if token =~ /\{/ and not in_string current_selectors.gsub!(/^[\s]*/, '') current_selectors.gsub!(/[\s]*$/, '') in_declarations = true else current_selectors += token end end end end end |
#selector_declarations(&block) ⇒ Object
go through all selector of the parser in order of specificity parse declarations for each selector
173 174 175 176 177 178 179 |
# File 'lib/css_parser_master/parser.rb', line 173 def selector_declarations(&block) each_selector_sorted(:all, :order => :asc) do |sel| sel.declarations.each do |dec| yield sel, dec end end end |
#to_s(media_types = :all) ⇒ Object
Output all CSS rules as a single stylesheet.
212 213 214 215 216 217 218 |
# File 'lib/css_parser_master/parser.rb', line 212 def to_s(media_types = :all) out = '' each_selector(media_types) do |selector| out << selector.to_s end out end |