From 86246be4d91b71a1dde10828cd84f290b8637503 Mon Sep 17 00:00:00 2001 From: Wincent Colaiuta Date: Sun, 29 Nov 2009 00:32:16 +0100 Subject: [PATCH] Allow single quotes inside "has_content" text As it is not possible to escape quotes in an XPath string, we must instead selectively use single or double quotes, and possibly split input up into groups. Signed-off-by: Wincent Colaiuta --- lib/capybara/session.rb | 13 ++++++++++++- spec/session_spec.rb | 15 +++++++++++++++ spec/test_app.rb | 6 +++++- 3 files changed, 32 insertions(+), 2 deletions(-) diff --git a/lib/capybara/session.rb b/lib/capybara/session.rb index a31de1e4a..d56c2c8f3 100644 --- a/lib/capybara/session.rb +++ b/lib/capybara/session.rb @@ -82,7 +82,7 @@ def body end def has_content?(content) - has_xpath?("//*[contains(.,'#{content}')]") + has_xpath?("//*[contains(.,#{sanitized_xpath_string(content)})]") end def has_xpath?(path, options={}) @@ -196,5 +196,16 @@ def find(locator) locator = current_scope.to_s + locator driver.find(locator) end + + def sanitized_xpath_string(string) + if string =~ /'/ + string = string.split("'", -1).map do |substr| + "'#{substr}'" + end.join(%q{,"'",}) + "concat(#{string})" + else + "'#{string}'" + end + end end end diff --git a/spec/session_spec.rb b/spec/session_spec.rb index 436075d10..3494e51f2 100644 --- a/spec/session_spec.rb +++ b/spec/session_spec.rb @@ -304,6 +304,21 @@ def extract_results(session) @session.should_not have_content('xxxxyzzz') @session.should_not have_content('monkey') end + + it 'should handle single quotes in the content' do + @session.visit('/with-quotes') + @session.should have_content("can't") + end + + it 'should handle double quotes in the content' do + @session.visit('/with-quotes') + @session.should have_content(%q{"No," he said}) + end + + it 'should handle mixed single and double quotes in the content' do + @session.visit('/with-quotes') + @session.should have_content(%q{"you can't do that."}) + end end describe '#has_xpath?' do diff --git a/spec/test_app.rb b/spec/test_app.rb index 69249366b..a354bc361 100644 --- a/spec/test_app.rb +++ b/spec/test_app.rb @@ -24,7 +24,11 @@ class TestApp < Sinatra::Base get '/landed' do "You landed" end - + + get '/with-quotes' do + %q{"No," he said, "you can't do that."} + end + get '/form/get' do '
' + params[:form].to_yaml + '
' end