Module: CssParserMaster::DeclarationAPI

Included in:
RuleSet, Selector
Defined in:
lib/css_parser_master/declaration_api.rb

Instance Method Summary collapse

Instance Method Details

#add_declaration!(property, value) ⇒ Object Also known as: []=

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.



55
56
57
58
59
60
61
62
63
64
65
66
67
68
# File 'lib/css_parser_master/declaration_api.rb', line 55

def add_declaration!(property, value)        
  if value.nil? or value.empty?
    @declarations.delete(property)
    return
  end

  value.gsub!(/;\Z/, '')
  is_important = !value.gsub!(CssParserMaster::IMPORTANT_IN_PROPERTY_RX, '').nil?
  property = property.downcase.strip
                            
  decl = CssParserMaster::Declaration.new property.downcase.strip, value.strip, is_important, @order += 1
  # puts "new decl: #{decl.inspect}, #{decl.class}"
  @declarations[property] = decl 
end

#declarations_to_s(options = {}) ⇒ Object

Return all declarations as a string. – TODO: Clean-up regexp doesn’t seem to work ++



31
32
33
34
35
36
37
38
39
# File 'lib/css_parser_master/declaration_api.rb', line 31

def declarations_to_s(options = {})
 options = {:force_important => false}.merge(options)
 str = ''
 importance = options[:force_important] # ? ' !important' : ''
 self.each_declaration do |decl| 
   str += "#{decl.to_text(importance)}"
 end                     
 str.gsub(/^[\s]+|[\n\r\f\t]*|[\s]+$/mx, '').strip
end

#each_declarationObject

:yields: property, value, is_important



17
18
19
20
21
22
23
24
# File 'lib/css_parser_master/declaration_api.rb', line 17

def each_declaration # :yields: property, value, is_important      
  ensure_valid_declarations!       
  decs = @declarations.sort { |a,b| a[1].order <=> b[1].order }
  # puts "decs: #{decs.inspect}"
  decs.each do |decl|
    yield decl[1]
  end
end

#ensure_valid_declarations!Object



4
5
6
7
8
9
10
11
12
13
14
# File 'lib/css_parser_master/declaration_api.rb', line 4

def ensure_valid_declarations! 
  @declarations.each do |d|
    name = d[0]
    prop = d[1]
    if prop.kind_of? Hash
      value = prop[:value]
      important = prop[:is_important]
      @declarations[d[0]] = Declaration.new(name, value, important, @order += 1)
    end
  end
end

#parse_declarations!(block) ⇒ Object

:nodoc:



71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
# File 'lib/css_parser_master/declaration_api.rb', line 71

def parse_declarations!(block) # :nodoc: 
  @declarations ||= {}  

  return unless block

  block.gsub!(/(^[\s]*)|([\s]*$)/, '')

  decs = block.split(/[\;$]+/m)

  decs.each do |decs|
    if matches = decs.match(/(.[^:]*)\:(.[^;]*)(;|\Z)/i)              
      property, value, end_of_declaration = matches.captures

      # puts "parse - property: #{property} , value: #{value}"
      add_declaration!(property, value)          
    end
  end
  
end