diff --git a/lib/webrat/core/matchers/have_selector.rb b/lib/webrat/core/matchers/have_selector.rb index 8083e63..07e0e64 100644 --- a/lib/webrat/core/matchers/have_selector.rb +++ b/lib/webrat/core/matchers/have_selector.rb @@ -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.