Extracting xpath_escape method

This commit is contained in:
Bryan Helmkamp 2009-02-23 20:21:23 -05:00
parent 9761b64b60
commit a688c28b19
1 changed files with 17 additions and 12 deletions

View File

@ -44,23 +44,28 @@ module Webrat
end
q = Nokogiri::CSS::Parser.parse(selector).map { |ast| ast.to_xpath }.first
if options[:content] && options[:content].include?("'") && options[:content].include?('"')
parts = options[:content].split("'").map do |part|
"'#{part}'"
end
string = "concat(" + parts.join(", \"'\", ") + ")"
q << "[contains(., #{string})]"
elsif options[:content] && options[:content].include?("'")
q << "[contains(., \"#{options[:content]}\")]"
elsif options[:content]
q << "[contains(., '#{options[:content]}')]"
if options[:content]
q << "[contains(., #{xpath_escape(options[:content])})]"
end
q
end
def xpath_escape(string)
if string.include?("'") && string.include?('"')
parts = string.split("'").map do |part|
"'#{part}'"
end
"concat(" + parts.join(", \"'\", ") + ")"
elsif string.include?("'")
"\"#{string}\""
else
"'#{string}'"
end
end
end
# Matches HTML content against a CSS 3 selector.