Class: Nokogiri::CSS::Node

Inherits:
Object
  • Object
show all
Defined in:
lib/nokogiri/css/node.rb

Constant Summary collapse

ALLOW_COMBINATOR_ON_SELF =
[:DIRECT_ADJACENT_SELECTOR, :FOLLOWING_SELECTOR, :CHILD_SELECTOR]

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(type, value) ⇒ Node

Create a new Node with type and value



12
13
14
15
# File 'lib/nokogiri/css/node.rb', line 12

def initialize type, value
  @type = type
  @value = value
end

Instance Attribute Details

#typeObject

Get the type of this node



7
8
9
# File 'lib/nokogiri/css/node.rb', line 7

def type
  @type
end

#valueObject

Get the value of this node



9
10
11
# File 'lib/nokogiri/css/node.rb', line 9

def value
  @value
end

Instance Method Details

#accept(visitor) ⇒ Object

Accept visitor



18
19
20
# File 'lib/nokogiri/css/node.rb', line 18

def accept visitor
  visitor.send(:"visit_#{type.to_s.downcase}", self)
end

#find_by_type(types) ⇒ Object

Find a node by type using types



80
81
82
83
84
85
86
87
# File 'lib/nokogiri/css/node.rb', line 80

def find_by_type types
  matches = []
  matches << self if to_type == types
  @value.each do |v|
    matches += v.find_by_type(types) if v.respond_to?(:find_by_type)
  end
  matches
end

#preprocess!Object

Preprocess this node tree



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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
# File 'lib/nokogiri/css/node.rb', line 31

def preprocess!
  ### Deal with nth-child
  matches = find_by_type(
    [:CONDITIONAL_SELECTOR,
      [:ELEMENT_NAME],
      [:PSEUDO_CLASS,
        [:FUNCTION]
      ]
    ]
  )
  matches.each do |match|
    if match.value[1].value[0].value[0] =~ /^nth-(last-)?child/
      tag_name = match.value[0].value.first
      match.value[0].value = ['*']
      match.value[1] = Node.new(:COMBINATOR, [
        match.value[1].value[0],
        Node.new(:FUNCTION, ['self(', tag_name])
      ])
    end
  end

  ### Deal with first-child, last-child
  matches = find_by_type(
    [:CONDITIONAL_SELECTOR,
      [:ELEMENT_NAME], [:PSEUDO_CLASS]
  ])
  matches.each do |match|
    if ['first-child', 'last-child'].include?(match.value[1].value.first)
      which = match.value[1].value.first.gsub(/-\w*$/, '')
      tag_name = match.value[0].value.first
      match.value[0].value = ['*']
      match.value[1] = Node.new(:COMBINATOR, [
        Node.new(:FUNCTION, ["#{which}("]),
        Node.new(:FUNCTION, ['self(', tag_name])
      ])
    elsif 'only-child' == match.value[1].value.first
      tag_name = match.value[0].value.first
      match.value[0].value = ['*']
      match.value[1] = Node.new(:COMBINATOR, [
        Node.new(:FUNCTION, ["#{match.value[1].value.first}("]),
        Node.new(:FUNCTION, ['self(', tag_name])
      ])
    end
  end

  self
end

#to_aObject

Convert to array



97
98
99
# File 'lib/nokogiri/css/node.rb', line 97

def to_a
  [@type] + @value.map { |n| n.respond_to?(:to_a) ? n.to_a : [n] }
end

#to_typeObject

Convert to_type



90
91
92
93
94
# File 'lib/nokogiri/css/node.rb', line 90

def to_type
  [@type] + @value.map { |n|
    n.to_type if n.respond_to?(:to_type)
  }.compact
end

#to_xpath(prefix = '//', visitor = XPathVisitor.new) ⇒ Object

Convert this CSS node to xpath with prefix using visitor



24
25
26
27
28
# File 'lib/nokogiri/css/node.rb', line 24

def to_xpath prefix = '//', visitor = XPathVisitor.new
  self.preprocess!
  prefix = '.' if ALLOW_COMBINATOR_ON_SELF.include?(type) && value.first.nil?
  prefix + visitor.accept(self)
end