class BacktrackXPath

for use with the Rexle gem

Public Class Methods

new(node, use_attributes: true) click to toggle source
# File lib/backtrack-xpath.rb, line 9
def initialize(node, use_attributes: true)
  
  @use_attributes = use_attributes
  @xpath = doc_scan(node).flatten.compact.join('/')
  
end

Public Instance Methods

to_s() click to toggle source
# File lib/backtrack-xpath.rb, line 16
def to_s
  @xpath
end
Also aliased as: to_xpath
to_xpath()
Alias for: to_s

Private Instance Methods

attribute_scan(node) click to toggle source
# File lib/backtrack-xpath.rb, line 24
def attribute_scan(node)

  result = ''
  attr = %i(id class).detect {|x| node.attributes.has_key? x}
  
  if attr then
    node.attribute(attr)
    value = attr == :id ? node.attributes[attr] : \
                                node.attributes[attr][0]
    result = "[@%s='%s']" % [attr, value]
  end
  result
end
doc_scan(node) click to toggle source
# File lib/backtrack-xpath.rb, line 38
def doc_scan(node)
  
  return unless node.parent

  name = node.name
  
  attribute = (@use_attributes and node.attributes) ? attribute_scan(node) : ''
  result = doc_scan(node.parent)

  [result, name + attribute]
end