class CssParser::RuleSet
Constants
- BACKGROUND_PROPERTIES
- LIST_STYLE_PROPERTIES
- RE_ELEMENTS_AND_PSEUDO_ELEMENTS
Patterns for specificity calculations
- RE_NON_ID_ATTRIBUTES_AND_PSEUDO_CLASSES
Attributes
selectors[R]
Array of selector strings.
specificity[RW]
Integer with the specificity to use for this RuleSet.
Public Class Methods
new(selectors, block, specificity = nil)
click to toggle source
# 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
Public Instance Methods
add_declaration!(property, value)
click to toggle source
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
Also aliased as: []=
create_shorthand!()
click to toggle source
Create shorthand declarations (e.g. margin
or
font
) whenever possible.
# File lib/css_parser/rule_set.rb, line 309 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
declarations_to_s(options = {})
click to toggle source
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]+$/mx, '').strip end
each_declaration() { |property, value, is_important| ... }
click to toggle source
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
each_selector(options = {}) { |selector, declarations, specificity| ... }
click to toggle source
Iterate through selectors.
Options
-
force_important
– boolean
Example¶ ↑
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
expand_shorthand!()
click to toggle source
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
extract_background_size_from(value)
click to toggle source
# File lib/css_parser/rule_set.rb, line 158 def extract_background_size_from(value) size = value.slice!(CssParser::RE_BACKGROUND_SIZE) size.sub(/^\s*\/\s*/, '') if size end
get_value(property)
click to toggle source
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
Also aliased as: []
remove_declaration!(property)
click to toggle source
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
to_s()
click to toggle source
Return the CSS rule set as a string.
# File lib/css_parser/rule_set.rb, line 118 def to_s decs = declarations_to_s "#{@selectors.join(',')} { #{decs} }" end