diff --git a/lib/capybara/session.rb b/lib/capybara/session.rb index 19235564a..482b0d2ca 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={}) @@ -197,5 +197,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 4b188c5ca..52d834e57 100644 --- a/spec/session_spec.rb +++ b/spec/session_spec.rb @@ -325,6 +325,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