class XPath::Renderer
Public Class Methods
Source
# File lib/xpath/renderer.rb, line 5 def self.render(node, type) new(type).render(node) end
Public Instance Methods
Source
# File lib/xpath/renderer.rb, line 55 def anywhere(element_names) with_element_conditions('//', element_names) end
Source
# File lib/xpath/renderer.rb, line 63 def attribute(current, name) if valid_xml_name?(name) "#{current}/@#{name}" else "#{current}/attribute::*[local-name(.) = #{string_literal(name)}]" end end
Source
# File lib/xpath/renderer.rb, line 51 def axis(current, name, element_names) with_element_conditions("#{current}/#{name}::", element_names) end
Source
# File lib/xpath/renderer.rb, line 71 def binary_operator(name, left, right) "(#{left} #{name} #{right})" end
Source
# File lib/xpath/renderer.rb, line 47 def child(current, element_names) with_element_conditions("#{current}/", element_names) end
Source
# File lib/xpath/renderer.rb, line 18 def convert_argument(argument) case argument when Expression, Union then render(argument) when Array then argument.map { |element| convert_argument(element) } when String then string_literal(argument) when Literal then argument.value else argument.to_s end end
Source
# File lib/xpath/renderer.rb, line 95 def css(current, selector) paths = Nokogiri::CSS.xpath_for(selector).map do |xpath_selector| "#{current}#{xpath_selector}" end union(paths) end
Source
# File lib/xpath/renderer.rb, line 43 def descendant(current, element_names) with_element_conditions("#{current}//", element_names) end
Source
# File lib/xpath/renderer.rb, line 106 def function(name, *arguments) "#{name}(#{arguments.join(', ')})" end
Source
# File lib/xpath/renderer.rb, line 75 def is(one, two) if @type == :exact binary_operator('=', one, two) else function(:contains, one, two) end end
Source
# File lib/xpath/renderer.rb, line 13 def render(node) arguments = node.arguments.map { |argument| convert_argument(argument) } send(node.expression, *arguments) end
Source
# File lib/xpath/renderer.rb, line 28 def string_literal(string) if string.include?("'") string = string.split("'", -1).map do |substr| "'#{substr}'" end.join(%q(,"'",)) "concat(#{string})" else "'#{string}'" end end
Source
# File lib/xpath/renderer.rb, line 87 def text(current) "#{current}/text()" end
Source
# File lib/xpath/renderer.rb, line 102 def union(*expressions) expressions.join(' | ') end
Source
# File lib/xpath/renderer.rb, line 59 def where(on, condition) "#{on}[#{condition}]" end
Private Instance Methods
Source
# File lib/xpath/renderer.rb, line 122 def valid_xml_name?(name) name =~ /^[a-zA-Z_:][a-zA-Z0-9_:\.\-]*$/ end
Source
# File lib/xpath/renderer.rb, line 112 def with_element_conditions(expression, element_names) if element_names.length == 1 "#{expression}#{element_names.first}" elsif element_names.length > 1 "#{expression}*[#{element_names.map { |e| "self::#{e}" }.join(' | ')}]" else "#{expression}*" end end