Class: SilkLayout::CSS::Selector

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

Instance Method Summary collapse

Constructor Details

#initialize(raw) ⇒ Selector

Returns a new instance of Selector.



6
7
8
9
10
11
# File 'lib/silk_layout/css/selector.rb', line 6

def initialize(raw)
  @raw = raw.to_s.strip
  @valid = !@raw.empty?
  @steps = @valid ? parse_steps(@raw) : []
  @valid &&= @steps.any?
end

Instance Method Details

#match?(node) ⇒ Boolean

Returns:

  • (Boolean)


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/selector.rb', line 13

def match?(node)
  return false unless @valid
  return false unless node.respond_to?(:element?)
  return false unless node.element?
  return false if node.tag.nil?

  return false unless step_match?(@steps.last[:simple], node)

  current = node
  i = @steps.length - 2
  while i >= 0
    comb = @steps[i + 1][:combinator]
    simple = @steps[i][:simple]

    case comb
    when :child
      current = current.parent
      return false unless current
      return false unless step_match?(simple, current)
    when :descendant
      current = find_ancestor(current.parent, simple)
      return false unless current
    when :adjacent
      current = previous_element_sibling(current)
      return false unless current
      return false unless step_match?(simple, current)
    else
      return false
    end

    i -= 1
  end

  true
end

#specificityObject



49
50
51
52
53
54
55
56
57
58
59
60
61
62
# File 'lib/silk_layout/css/selector.rb', line 49

def specificity
  ids = 0
  classes = 0
  elements = 0

  @steps.each do |step|
    simple_ids, simple_classes, simple_elements = simple_specificity(step[:simple])
    ids += simple_ids
    classes += simple_classes
    elements += simple_elements
  end

  [ids, classes, elements]
end