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 <win@wincent.com>
This commit is contained in:
Wincent Colaiuta 2009-11-29 00:32:16 +01:00
parent df188f952b
commit 86246be4d9
3 changed files with 32 additions and 2 deletions

View File

@ -82,7 +82,7 @@ module Capybara
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 @@ module Capybara
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

View File

@ -304,6 +304,21 @@ shared_examples_for "session" do
@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

View File

@ -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
'<pre id="results">' + params[:form].to_yaml + '</pre>'
end