Object
Patterns for specificity calculations
# File lib/css_parser/rule_set.rb, line 16 def initialize(selectors, block, specificity = nil) @selectors = [] @specificity = specificity @declarations = {} @order = 0 parse_selectors!(selectors) if selectors parse_declarations!(block) end
Add a CSS declaration to the current RuleSet.
rule_set.add_declaration!('color', 'blue')
puts rule_set['color']
=> 'blue;'
rule_set.add_declaration!('margin', '0px auto !important')
puts rule_set['margin']
=> '0px auto !important;'
If the property already exists its value will be over-written.
# File lib/css_parser/rule_set.rb, line 53 def add_declaration!(property, value) if value.nil? or value.empty? @declarations.delete(property) return end value.gsub!(/;\Z/, '') is_important = !value.gsub!(CssParser::IMPORTANT_IN_PROPERTY_RX, '').nil? property = property.downcase.strip #puts "SAVING #{property} #{value} #{is_important.inspect}" @declarations[property] = { :value => value, :is_important => is_important, :order => @order += 1 } end
Create shorthand declarations (e.g. margin or font) whenever possible.
# File lib/css_parser/rule_set.rb, line 301 def create_shorthand! create_background_shorthand! create_dimensions_shorthand! # border must be shortened after dimensions create_border_shorthand! create_font_shorthand! create_list_style_shorthand! end
Return all declarations as a string.
# File lib/css_parser/rule_set.rb, line 107 def declarations_to_s(options = {}) options = {:force_important => false}.merge(options) str = '' each_declaration do |prop, val, is_important| importance = (options[:force_important] || is_important) ? ' !important' : '' str += "#{prop}: #{val}#{importance}; " end str.gsub(/^[\s^(\{)]+|[\n\r\f\t]*|[\s]+$/x, '').strip end
Iterate through declarations.
# File lib/css_parser/rule_set.rb, line 95 def each_declaration # :yields: property, value, is_important decs = @declarations.sort { |a,b| a[1][:order].nil? || b[1][:order].nil? ? 0 : a[1][:order] <=> b[1][:order] } decs.each do |property, data| value = data[:value] yield property.downcase.strip, value.strip, data[:is_important] end end
Iterate through selectors.
Options
force_important -- boolean
ruleset.each_selector do |sel, dec, spec| ... end
# File lib/css_parser/rule_set.rb, line 85 def each_selector(options = {}) # :yields: selector, declarations, specificity declarations = declarations_to_s(options) if @specificity @selectors.each { |sel| yield sel.strip, declarations, @specificity } else @selectors.each { |sel| yield sel.strip, declarations, CssParser.calculate_specificity(sel) } end end
Split shorthand declarations (e.g. margin or font) into their constituent parts.
# File lib/css_parser/rule_set.rb, line 124 def expand_shorthand! # border must be expanded before dimensions expand_border_shorthand! expand_dimensions_shorthand! expand_font_shorthand! expand_background_shorthand! expand_list_style_shorthand! end
Get the value of a property
# File lib/css_parser/rule_set.rb, line 26 def get_value(property) return '' unless property and not property.empty? property = property.downcase.strip properties = @declarations.inject('') do |val, (key, data)| #puts "COMPARING #{key} #{key.inspect} against #{property} #{property.inspect}" importance = data[:is_important] ? ' !important' : '' val << "#{data[:value]}#{importance}; " if key.downcase.strip == property val end return properties ? properties.strip : '' end
Remove CSS declaration from the current RuleSet.
rule_set.remove_declaration!('color')
# File lib/css_parser/rule_set.rb, line 72 def remove_declaration!(property) @declarations.delete(property) end
Generated with the Darkfish Rdoc Generator 2.