Class: CSSLite

Inherits:
Object
  • Object
show all
Defined in:
lib/csslite.rb

Instance Method Summary collapse

Constructor Details

#initialize(s, debug: false, override: false) ⇒ CSSLite

Returns a new instance of CSSLite.



11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
# File 'lib/csslite.rb', line 11

def initialize(s, debug: false, override: false)
  
  @debug, @override = debug, override

  # parse the CSS
  
  @a = s.strip.split(/}/).map do |entry|

    raw_selector,raw_styles = entry.split(/{/,2)

    h = raw_styles.strip.split(/;/).inject({}) do |r, x| 
      k, v = x.split(/:/,2).map(&:strip)
      r.merge(k.to_sym => v)
    end

    [raw_selector.split(/,\s*/).map(&:strip), h]
  end
  
  puts '@a: ' + @a.inspect if @debug    
  
end

Instance Method Details

#propagate(root_element) ⇒ Object



33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
# File 'lib/csslite.rb', line 33

def propagate(root_element)
  
  puts 'inside csslist#propagate'.info if @debug
  
  # add each CSS style attribute to the element
  # e.g. @a = [[['line'],{stroke: 'green'}]]

  @a.each do |x|

    selectors, style = x

    selectors.each do |selector|

      root_element.css(selector).each do |element|
        puts 'element: ' + element.inspect if @debug
        apply_style element, style
        
        # apply the CSS to all its children
        element.each_recursive {|x| apply_style x, style}
      end

    end
  end

end