Extracting xpath_escape method
This commit is contained in:
parent
9761b64b60
commit
a688c28b19
|
@ -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.
|
||||
|
|
Loading…
Reference in New Issue