Class: Habaki::Selector

Inherits:
Node
  • Object
show all
Defined in:
lib/habaki/selector.rb

Overview

CSS selector

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods inherited from Node

#to_s

Constructor Details

#initializeSelector

Returns a new instance of Selector.



8
9
10
# File 'lib/habaki/selector.rb', line 8

def initialize
  @sub_selectors = []
end

Instance Attribute Details

#sub_selectorsArray<SubSelectors>

Array of Habaki::SubSelectors group

Returns:



6
7
8
# File 'lib/habaki/selector.rb', line 6

def sub_selectors
  @sub_selectors
end

Instance Method Details

#element_match?(element, specificity = nil) ⇒ Boolean

does selector match Visitor::Element ?

Parameters:

Returns:

  • (Boolean)


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
48
49
50
51
52
53
54
55
56
57
58
59
60
# File 'lib/habaki/selector.rb', line 16

def element_match?(element, specificity = nil)
  return false if @sub_selectors.empty?

  current_sub_selector = nil
  @sub_selectors.reverse_each do |sub_selector|
    if current_sub_selector
      case current_sub_selector.relation
      when :descendant
        parent_element = element.parent
        sub_match = false
        while parent_element do
          sub_match = sub_selector.element_match?(parent_element, specificity)
          parent_element = parent_element.parent
          break if sub_match
        end
        return false unless sub_match
      when :child
        parent_element = element.parent
        return false unless parent_element
        return false unless sub_selector.element_match?(parent_element, specificity)
      when :direct_adjacent
        previous_element = element.previous
        return false unless previous_element
        return false unless sub_selector.element_match?(previous_element, specificity)
      when :indirect_adjacent
        previous_element = element.previous
        sub_match = false
        while previous_element do
          sub_match = sub_selector.element_match?(previous_element, specificity)
          previous_element = previous_element.previous
          break if sub_match
        end
        return false unless sub_match
      else
        # STDERR.puts "Habaki: unknown relation #{current_sub_selector.relation}"
        return false
      end
    else
      return false unless sub_selector.element_match?(element, specificity)
    end

    current_sub_selector = sub_selector
  end
  true
end

#read_from_katana(sel) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Parameters:



72
73
74
# File 'lib/habaki/selector.rb', line 72

def read_from_katana(sel)
  @sub_selectors = rec_sub_sel(sel)
end

#string(format = Formatter::Base.new) ⇒ String

Parameters:

Returns:



64
65
66
67
68
# File 'lib/habaki/selector.rb', line 64

def string(format = Formatter::Base.new)
  @sub_selectors.map do |sub_sel|
    sub_sel.string(format)
  end.join("")
end