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.



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

def initialize(s)

  # 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
  
  @elements = {}
  
end

Instance Method Details

#propagate(root_element) ⇒ Object



29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
# File 'lib/csslite.rb', line 29

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|

          id = element.object_id
          @elements[id] = element.style.to_h unless @elements.has_key? id
          element.style[k] = v unless @elements[id].has_key? k
        end
      end

    end
  end

end