Class: Nokogiri::CSS::XPathVisitor

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

Instance Method Summary collapse

Instance Method Details

#accept(node) ⇒ Object



144
145
146
# File 'lib/nokogiri/css/xpath_visitor.rb', line 144

def accept node
  node.accept(self)
end

#visit_attribute_condition(node) ⇒ Object



59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
# File 'lib/nokogiri/css/xpath_visitor.rb', line 59

def visit_attribute_condition node
   attribute = if (node.value.first.type == :FUNCTION) or (node.value.first.value.first =~ /::/)
                 ''
               else
                 '@'
               end
  attribute += node.value.first.accept(self)

  # Support non-standard css
  attribute.gsub!(/^@@/, '@')

  return attribute unless node.value.length == 3

  value = node.value.last
  value = "'#{value}'" if value !~ /^['"]/

  case node.value[1]
  when '*='
    "contains(#{attribute}, #{value})"
  when '^='
    "starts-with(#{attribute}, #{value})"
  when '|='
    "#{attribute} = #{value} or starts-with(#{attribute}, concat(#{value}, '-'))"
  when '~='
    "contains(concat(\" \", #{attribute}, \" \"),concat(\" \", #{value}, \" \"))"
  when '$='
    "substring(#{attribute}, string-length(#{attribute}) - " +
      "string-length(#{value}) + 1, string-length(#{value})) = #{value}"
  else
    attribute + " #{node.value[1]} " + "#{value}"
  end
end

#visit_child_selector(node) ⇒ Object



134
135
136
137
138
# File 'lib/nokogiri/css/xpath_visitor.rb', line 134

def visit_child_selector node
  node.value.first.accept(self) +
  '/' +
  node.value.last.accept(self)
end

#visit_class_condition(node) ⇒ Object



114
115
116
# File 'lib/nokogiri/css/xpath_visitor.rb', line 114

def visit_class_condition node
  "contains(concat(' ', @class, ' '), ' #{node.value.first} ')"
end

#visit_combinator(node) ⇒ Object



118
119
120
121
# File 'lib/nokogiri/css/xpath_visitor.rb', line 118

def visit_combinator node
  node.value.first.accept(self) + ' and ' +
  node.value.last.accept(self)
end

#visit_conditional_selector(node) ⇒ Object



123
124
125
126
# File 'lib/nokogiri/css/xpath_visitor.rb', line 123

def visit_conditional_selector node
  node.value.first.accept(self) + '[' +
  node.value.last.accept(self) + ']'
end

#visit_descendant_selector(node) ⇒ Object



128
129
130
131
132
# File 'lib/nokogiri/css/xpath_visitor.rb', line 128

def visit_descendant_selector node
  node.value.first.accept(self) +
  '//' +
  node.value.last.accept(self)
end

#visit_direct_adjacent_selector(node) ⇒ Object



48
49
50
51
52
# File 'lib/nokogiri/css/xpath_visitor.rb', line 48

def visit_direct_adjacent_selector node
  node.value.first.accept(self) +
    "/following-sibling::*[1]/self::" +
     node.value.last.accept(self)
end

#visit_element_name(node) ⇒ Object



140
141
142
# File 'lib/nokogiri/css/xpath_visitor.rb', line 140

def visit_element_name node
  node.value.first
end

#visit_function(node) ⇒ Object



4
5
6
7
8
9
10
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
# File 'lib/nokogiri/css/xpath_visitor.rb', line 4

def visit_function node
  #  note that nth-child and nth-last-child are preprocessed in css/node.rb.
  msg = :"visit_function_#{node.value.first.gsub(/[(]/, '')}"
  return self.send(msg, node) if self.respond_to?(msg)

  case node.value.first
  when /^text\(/
    'child::text()'
  when /^self\(/
    "self::#{node.value[1]}"
  when /^(eq|nth|nth-of-type|nth-child)\(/
    if node.value[1].is_a?(Nokogiri::CSS::Node) and node.value[1].type == :AN_PLUS_B
      an_plus_b(node.value[1])
    else
      "position() = " + node.value[1]
    end
  when /^(first|first-of-type)\(/
    "position() = 1"
  when /^(last|last-of-type)\(/
    "position() = last()"
  when /^(nth-last-child|nth-last-of-type)\(/
    "position() = last() - #{node.value[1]}"
  when /^contains\(/
    "contains(., #{node.value[1]})"
  when /^gt\(/
    "position() > #{node.value[1]}"
  when /^only-child\(/
    "last() = 1"
  else
    node.value.first + ')'
  end
end

#visit_id(node) ⇒ Object



54
55
56
57
# File 'lib/nokogiri/css/xpath_visitor.rb', line 54

def visit_id node
  node.value.first =~ /^#(.*)$/
  "@id = '#{$1}'"
end

#visit_not(node) ⇒ Object



37
38
39
# File 'lib/nokogiri/css/xpath_visitor.rb', line 37

def visit_not node
  'not(' + node.value.first.accept(self) + ')'
end

#visit_preceding_selector(node) ⇒ Object



41
42
43
44
45
46
# File 'lib/nokogiri/css/xpath_visitor.rb', line 41

def visit_preceding_selector node
  node.value.last.accept(self) +
    '[preceding-sibling::' +
    node.value.first.accept(self) +
    ']'
end

#visit_pseudo_class(node) ⇒ Object



92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
# File 'lib/nokogiri/css/xpath_visitor.rb', line 92

def visit_pseudo_class node
  if node.value.first.is_a?(Nokogiri::CSS::Node) and node.value.first.type == :FUNCTION
    node.value.first.accept(self)
  else
    msg = :"visit_pseudo_class_#{node.value.first.gsub(/[(]/, '')}"
    return self.send(msg, node) if self.respond_to?(msg)

    case node.value.first
    when "first" then "position() = 1"
    when "last" then "position() = last()"
    when "first-of-type" then "position() = 1"
    when "last-of-type" then "position() = last()"
    when "only-of-type" then "last() = 1"
    when "empty" then "not(node())"
    when "parent" then "node()"
    when "root" then "not(parent::*)"
    else
      '1 = 1'
    end
  end
end