Class: Nokogiri::CSS::XPathVisitor

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

Overview

:nodoc:

Instance Method Summary collapse

Instance Method Details

#accept(node) ⇒ Object



180
181
182
# File 'lib/nokogiri/css/xpath_visitor.rb', line 180

def accept node
  node.accept(self)
end

#visit_attribute_condition(node) ⇒ Object



77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
# File 'lib/nokogiri/css/xpath_visitor.rb', line 77

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 !~ /^['"]/

  if (value[0]==value[-1]) && %q{"'}.include?(value[0])
    str_value = value[1..-2]
    if str_value.include?(value[0])
      value = 'concat("' + str_value.split('"', -1).join(%q{", '"', "}) + '", "")'
    end
  end

  case node.value[1]
  when :equal
    attribute + " = " + "#{value}"
  when :not_equal
    attribute + " != " + "#{value}"
  when :substring_match
    "contains(#{attribute}, #{value})"
  when :prefix_match
    "starts-with(#{attribute}, #{value})"
  when :dash_match
    "#{attribute} = #{value} or starts-with(#{attribute}, concat(#{value}, '-'))"
  when :includes
    "contains(concat(\" \", #{attribute}, \" \"),concat(\" \", #{value}, \" \"))"
  when :suffix_match
    "substring(#{attribute}, string-length(#{attribute}) - " +
      "string-length(#{value}) + 1, string-length(#{value})) = #{value}"
  else
    attribute + " #{node.value[1]} " + "#{value}"
  end
end

#visit_class_condition(node) ⇒ Object



146
147
148
# File 'lib/nokogiri/css/xpath_visitor.rb', line 146

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

#visit_combinator(node) ⇒ Object



150
151
152
153
154
155
156
# File 'lib/nokogiri/css/xpath_visitor.rb', line 150

def visit_combinator node
  if is_of_type_pseudo_class?(node.value.last)
    "#{node.value.first.accept(self) if node.value.first}][#{node.value.last.accept(self)}"
  else
    "#{node.value.first.accept(self) if node.value.first} and #{node.value.last.accept(self)}"
  end
end

#visit_conditional_selector(node) ⇒ Object



171
172
173
174
# File 'lib/nokogiri/css/xpath_visitor.rb', line 171

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

#visit_element_name(node) ⇒ Object



176
177
178
# File 'lib/nokogiri/css/xpath_visitor.rb', line 176

def visit_element_name node
  node.value.first
end

#visit_function(node) ⇒ Object



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

def visit_function node

  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\(/
    "position() = #{node.value[1]}"
  when /^(nth|nth-of-type)\(/
    if node.value[1].is_a?(Nokogiri::CSS::Node) and node.value[1].type == :NTH
      nth(node.value[1])
    else
      "position() = #{node.value[1]}"
    end
  when /^nth-child\(/
    if node.value[1].is_a?(Nokogiri::CSS::Node) and node.value[1].type == :NTH
      nth(node.value[1], :child => true)
    else
      "count(preceding-sibling::*) = #{node.value[1].to_i-1}"
    end
  when /^nth-last-of-type\(/
    if node.value[1].is_a?(Nokogiri::CSS::Node) and node.value[1].type == :NTH
      nth(node.value[1], :last => true)
    else
      index = node.value[1].to_i - 1
      index == 0 ? "position() = last()" : "position() = last() - #{index}"
    end
  when /^nth-last-child\(/
    if node.value[1].is_a?(Nokogiri::CSS::Node) and node.value[1].type == :NTH
      nth(node.value[1], :last => true, :child => true)
    else
      "count(following-sibling::*) = #{node.value[1].to_i-1}"
    end
  when /^(first|first-of-type)\(/
    "position() = 1"
  when /^(last|last-of-type)\(/
    "position() = last()"
  when /^contains\(/
    "contains(., #{node.value[1]})"
  when /^gt\(/
    "position() > #{node.value[1]}"
  when /^only-child\(/
    "last() = 1"
  when /^comment\(/
    "comment()"
  when /^has\(/
    is_direct = node.value[1].value[0].nil? # e.g. "has(> a)", "has(~ a)", "has(+ a)"
    ".#{"//" if !is_direct}#{node.value[1].accept(self)}"
  else
    args = ['.'] + node.value[1..-1]
    "#{node.value.first}#{args.join(', ')})"
  end
end

#visit_id(node) ⇒ Object



72
73
74
75
# File 'lib/nokogiri/css/xpath_visitor.rb', line 72

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

#visit_not(node) ⇒ Object



63
64
65
66
67
68
69
70
# File 'lib/nokogiri/css/xpath_visitor.rb', line 63

def visit_not node
  child = node.value.first
  if :ELEMENT_NAME == child.type
    "not(self::#{child.accept(self)})"
  else
    "not(#{child.accept(self)})"
  end
end

#visit_pseudo_class(node) ⇒ Object



121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
# File 'lib/nokogiri/css/xpath_visitor.rb', line 121

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 "first-child" then "count(preceding-sibling::*) = 0"
    when "last" then "position() = last()"
    when "last-child" then "count(following-sibling::*) = 0"
    when "first-of-type" then "position() = 1"
    when "last-of-type" then "position() = last()"
    when "only-child" then "count(preceding-sibling::*) = 0 and count(following-sibling::*) = 0"
    when "only-of-type" then "last() = 1"
    when "empty" then "not(node())"
    when "parent" then "node()"
    when "root" then "not(parent::*)"
    else
      node.value.first + "(.)"
    end
  end
end