Module: Kelp::XPaths

Included in:
Navigation, Visibility
Defined in:
lib/kelp/xpath.rb

Overview

This module defines helper methods for building XPath expressions.

Instance Method Summary collapse

Instance Method Details

#xpath_row_containing(texts) ⇒ Object

Return an XPath for any table row containing all strings in texts, within the current context.



7
8
9
10
11
12
13
# File 'lib/kelp/xpath.rb', line 7

def xpath_row_containing(texts)
  texts = [texts] if texts.class == String
  conditions = texts.collect do |text|
    "contains(., #{xpath_sanitize(text)})"
  end.join(' and ')
  return ".//tr[#{conditions}]"
end

#xpath_sanitize(text) ⇒ Object

Return the given text string in an XPath-safe form, with any single-quotes escaped by using the XPath concat function to combine them with the rest of the text.

Examples:

xpath_sanitize("Bob's")
# => concat('Bob', "'", 's')


23
24
25
26
27
28
29
30
31
# File 'lib/kelp/xpath.rb', line 23

def xpath_sanitize(text)
  # If there's nothing to escape, just wrap text in single-quotes
  if !text.include?("'")
    return "'#{text}'"
  else
    result = text.gsub(/'/, %{', "'", '})
    return "concat('#{result}')"
  end
end