Skip to content

Fix replacement for multiple substitutions #76

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,10 @@ def initialize
end

def substitute!(selector, values, format_for_presentation = false)
selector = selector.dup

while !values.empty? && substitutable?(values.first) && selector.index(@substitute)
selector.sub! @substitute, matcher_for(values.shift, format_for_presentation)
selector.gsub @substitute do |match|
next match[0] if values.empty? || !substitutable?(values.first)
matcher_for(values.shift, format_for_presentation)
end

selector
end

def match(matches, attribute, matcher)
Expand All @@ -23,7 +20,7 @@ def matcher_for(value, format_for_presentation)
if format_for_presentation
value.inspect # Avoid to_s so Regexps aren't put in quotes.
else
value.to_s.inspect
"\"#{value}\""
end
end

Expand Down
5 changes: 5 additions & 0 deletions test/selector_assertions_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -135,6 +135,11 @@ def test_substitution_values
end
end

def test_multiple_substitution_values
render_html '<input name="foo[12]" value="34">'
assert_select ":match('name', ?):match('value', ?)", /\w+\[\d+\]/, /\d+/
end

def test_substitution_values_with_values_other_than_string_or_regexp
render_html %Q{<div id="id_string">symbol</div><div id="1">numeric</div>}
assert_select "div:match('id', ?)", :id_string do |elements|
Expand Down