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