Class: SilkLayout::CSS::Cascade

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

Class Method Summary collapse

Class Method Details

.apply(node, rules, parent_style = nil) ⇒ Object



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

def self.apply(node, rules, parent_style = nil)
  return unless node
  return unless node.respond_to?(:element?)

  matching = rules.select { |rule| rule.selector.match?(node) }

  inline = inline_rule(node)
  matching << inline if inline

  matching.sort_by! { |rule| [rule.specificity, rule.order] }

  node.computed_style = ComputedStyle.new(matching, parent_style)

  node.children.each do |child|
    apply(child, rules, node.computed_style)
  end
end

.inline_rule(node) ⇒ Object



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

def self.inline_rule(node)
  return nil unless node.attributes

  style = node.attributes["style"].to_s.strip
  return nil if style.empty?

  declarations = {}
  Crass.parse_properties(style).each do |child|
    next unless child[:node] == :property

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

  Rule.new(
    selector: nil,
    declarations: declarations,
    specificity: [1000, 0, 0],
    order: 1_000_000_000,
    origin: :author
  )
end