| Class | CssParser::RuleSet |
| In: |
lib/css_parser/rule_set.rb
|
| Parent: | Object |
| RE_ELEMENTS_AND_PSEUDO_ELEMENTS | = | /((^|[\s\+\>]+)[\w]+|\:(first\-line|first\-letter|before|after))/i | Patterns for specificity calculations | |
| RE_NON_ID_ATTRIBUTES_AND_PSEUDO_CLASSES | = | /(\.[\w]+)|(\[[\w]+)|(\:(link|first\-child|lang))/i |
| selectors | [R] | Array of selector strings. |
| specificity | [RW] | Integer with the specificity to use for this RuleSet. |
# File lib/css_parser/rule_set.rb, line 13
13: def initialize(selectors, block, specificity = nil)
14: @selectors = []
15: @specificity = specificity
16: @declarations = {}
17: parse_selectors!(selectors) if selectors
18: parse_declarations!(block)
19: 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 50
50: def add_declaration!(property, value)
51: value.gsub!(/;\Z/, '')
52: is_important = !value.gsub!(CssParser::IMPORTANT_IN_PROPERTY_RX, '').nil?
53: property = property.downcase.strip
54: #puts "SAVING #{property} #{value} #{is_important.inspect}"
55: @declarations[property] = {:value => value, :is_important => is_important}
56: end
Create shorthand declarations (e.g. margin or font) whenever possible.
# File lib/css_parser/rule_set.rb, line 112
112: def create_shorthand!
113: create_background_shorthand!
114: create_dimensions_shorthand!
115: create_font_shorthand!
116: end
Return all declarations as a string.
# File lib/css_parser/rule_set.rb, line 90
90: def declarations_to_s(options = {})
91: options = {:force_important => false}.merge(options)
92: str = ''
93: importance = options[:force_important] ? ' !important' : ''
94: each_declaration { |prop, val| str += "#{prop}: #{val}#{importance}; " }
95: str.gsub(/^[\s]+|[\n\r\f\t]*|[\s]+$/mx, '').strip
96: end
Iterate through declarations.
# File lib/css_parser/rule_set.rb, line 78
78: def each_declaration # :yields: property, value, is_important
79: @declarations.each do |property, data|
80: value = data[:value]
81: #value += ' !important' if data[:is_important]
82: yield property.downcase.strip, value.strip, data[:is_important]
83: end
84: end
Iterate through selectors.
Options
ruleset.each_selector do |sel, dec, spec|
...
end
# File lib/css_parser/rule_set.rb, line 68
68: def each_selector(options = {}) # :yields: selector, declarations, specificity
69: declarations = declarations_to_s(options)
70: if @specificity
71: @selectors.each { |sel| yield sel.strip, declarations, @specificity }
72: else
73: @selectors.each { |sel| yield sel.strip, declarations, CssParser.calculate_specificity(sel) }
74: end
75: end
Split shorthand declarations (e.g. margin or font) into their constituent parts.
# File lib/css_parser/rule_set.rb, line 105
105: def expand_shorthand!
106: expand_dimensions_shorthand!
107: expand_font_shorthand!
108: expand_background_shorthand!
109: end
Get the value of a property
# File lib/css_parser/rule_set.rb, line 23
23: def get_value(property)
24: return '' unless property and not property.empty?
25:
26: property = property.downcase.strip
27: properties = @declarations.inject('') do |val, (key, data)|
28: #puts "COMPARING #{key} #{key.inspect} against #{property} #{property.inspect}"
29: importance = data[:is_important] ? ' !important' : ''
30: val << "#{data[:value]}#{importance}; " if key.downcase.strip == property
31: val
32: end
33: return properties ? properties.strip : ''
34: end