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 end
q = Nokogiri::CSS::Parser.parse(selector).map { |ast| ast.to_xpath }.first q = Nokogiri::CSS::Parser.parse(selector).map { |ast| ast.to_xpath }.first
if options[:content] && options[:content].include?("'") && options[:content].include?('"') if options[:content]
parts = options[:content].split("'").map do |part| q << "[contains(., #{xpath_escape(options[:content])})]"
"'#{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]}')]"
end end
q q
end 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 end
# Matches HTML content against a CSS 3 selector. # Matches HTML content against a CSS 3 selector.