Class: SilkLayout::CSS::Parser

Inherits:
Object
  • Object
show all
Defined in:
lib/silk_layout/css/parser.rb

Class Method Summary collapse

Class Method Details

.parse_all(stylesheets) ⇒ Object



8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
# File 'lib/silk_layout/css/parser.rb', line 8

def self.parse_all(stylesheets)
  rules = []
  order = 0

  stylesheets.each do |css|
    Crass.parse(css).each do |node|
      next unless node[:node] == :style_rule

      selector_text = node[:selector][:value].strip
      selectors = selector_text.split(",").map(&:strip)

      declarations = {}

      node[:children].each do |child|
        next unless child[:node] == :property

        property = child[:name]
        value = child[:value]

        declarations[property] = Declaration.new(value: value, important: child[:important] ? true : false)
      end

      selectors.each do |raw_selector|
        selector = Selector.new(raw_selector)

        rules << Rule.new(
          selector: selector,
          declarations: declarations,
          specificity: selector.specificity,
          order: order,
          origin: :author
        )

        order += 1
      end
    end
  end

  rules
end