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
# File 'lib/silk_layout/css/selector.rb', line 6

def initialize(raw)
  @raw = raw.to_s.strip
  @steps = parse_steps(@raw)
end

Instance Method Details

#match?(node) ⇒ Boolean

Returns:

  • (Boolean)


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
# File 'lib/silk_layout/css/selector.rb', line 11

def match?(node)
  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
    else
      return false
    end

    i -= 1
  end

  true
end

#specificityObject



42
43
44
45
46
47
48
49
50
51
52
53
54
55
# File 'lib/silk_layout/css/selector.rb', line 42

def specificity
  ids = 0
  classes = 0
  elements = 0

  @steps.each do |step|
    simple = step[:simple]
    ids += 1 if simple[:id]
    classes += simple[:classes].length
    elements += 1 if simple[:tag]
  end

  [ids, classes, elements]
end