Skip to content

Commit

Permalink
Get quoting working for content
Browse files Browse the repository at this point in the history
  • Loading branch information
brynary committed Feb 16, 2009
1 parent 7ba620f commit 932fdab
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 5 deletions.
23 changes: 18 additions & 5 deletions lib/webrat/core/matchers/have_tag.rb
Expand Up @@ -36,14 +36,27 @@ def tag_inspect
def query
options = @expected.last.dup
selector = @expected.first.to_s

selector << ":contains('#{options.delete(:content)}')" if options[:content]


options.each do |key, value|
next if key == :content
selector << "[#{key}='#{value}']"
end

Nokogiri::CSS::Parser.parse(selector).map { |ast| ast.to_xpath }

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]}')]"
end
q
end
end

Expand Down
15 changes: 15 additions & 0 deletions spec/public/matchers_spec.rb
Expand Up @@ -8,6 +8,9 @@
@body = <<-HTML
<div id='main'>
<div class='inner'>hello, world!</div>
<h2>Welcome "Bryan"</h2>
<h3>Welcome 'Bryan'</h3>
<h4>Welcome 'Bryan"</h4>
<ul>
<li>First</li>
<li>Second</li>
Expand Down Expand Up @@ -178,6 +181,18 @@
@body.should have_tag("div", :content => "hello, world!")
end

it "should be able to specify the content of the tag with double quotes in it" do
@body.should have_tag("h2", :content => 'Welcome "Bryan"')
end

it "should be able to specify the content of the tag with single quotes in it" do
@body.should have_tag("h3", :content => "Welcome 'Bryan'")
end

it "should be able to specify the content of the tag with both kinds of quotes" do
@body.should have_tag("h4", :content => "Welcome 'Bryan\"")
end

it "should be able to specify the attributes of the tag" do
@body.should have_tag("div", :class => "inner")
end
Expand Down

0 comments on commit 932fdab

Please sign in to comment.