Class: CSSLite

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

Instance Method Summary collapse

Constructor Details

#initialize(s) ⇒ CSSLite

Returns a new instance of CSSLite.



8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
# File 'lib/csslite.rb', line 8

def initialize(s)

  # parse the CSS
  
  @a = s.split(/}/)[0..-2].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      
  
end

Instance Method Details

#propagate(root_element) ⇒ Object



26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
# File 'lib/csslite.rb', line 26

def propagate(root_element)

  # 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|

      style.each do |k,v|

        root_element.css(selector).each do |element|

          element.style[k] = v unless override == false and element.style.has_key? k
        end
      end

    end
  end

end