aboutsummaryrefslogtreecommitdiffstats
path: root/actionpack/lib/action_dispatch
diff options
context:
space:
mode:
authorTimm <kaspth@gmail.com>2013-07-24 16:01:36 +0200
committerTimm <kaspth@gmail.com>2014-06-16 21:03:55 +0200
commit1e4f1ab95e8059d899736d6e329f18c6d23b444e (patch)
tree9818511b071c00178e00c40ae46c59de727e3777 /actionpack/lib/action_dispatch
parent9eada2daf812ae95d47a22ebe4ecc058a980a38b (diff)
downloadrails-1e4f1ab95e8059d899736d6e329f18c6d23b444e.tar.gz
rails-1e4f1ab95e8059d899736d6e329f18c6d23b444e.tar.bz2
rails-1e4f1ab95e8059d899736d6e329f18c6d23b444e.zip
Added implementation for substitution values via Nokogiri's custom pseudo classes.
Diffstat (limited to 'actionpack/lib/action_dispatch')
-rw-r--r--actionpack/lib/action_dispatch/testing/assertions/selector.rb40
1 files changed, 35 insertions, 5 deletions
diff --git a/actionpack/lib/action_dispatch/testing/assertions/selector.rb b/actionpack/lib/action_dispatch/testing/assertions/selector.rb
index feb98f7e2b..6151491238 100644
--- a/actionpack/lib/action_dispatch/testing/assertions/selector.rb
+++ b/actionpack/lib/action_dispatch/testing/assertions/selector.rb
@@ -162,7 +162,7 @@ module ActionDispatch
selector = filter.css_selector
equals = filter.comparisons
- matches = filter.root.css(selector)
+ matches = filter.root.css(selector, filter.substitution_context)
content_mismatch = nil
matches = filter_matches(matches, equals) do |mismatch|
content_mismatch ||= mismatch
@@ -350,10 +350,7 @@ module ActionDispatch
# First or second argument is the selector
selector = @css_selector_is_second_argument ? args.shift(2).last : args.shift
- unless selector.is_a? String
- raise ArgumentError, "Expecting a selector as the first argument"
- end
- @css_selector = selector
+ @css_selector = selector_from(selector, args)
# Next argument is used for equality tests.
@comparisons = comparisons_from(args.shift)
@@ -384,6 +381,39 @@ module ActionDispatch
end
end
+ def selector_from(selector, substitution_values)
+ unless selector.is_a? String
+ raise ArgumentError, "Expecting a selector as the first argument"
+ end
+ while selector.index('?')
+ break if substitution_values.empty?
+ value = substitution_values.shift
+ if value.is_a?(Regexp)
+ value = substitution_context.add_regex value
+ end
+ selector.sub!('?', value)
+ end
+ selector
+ end
+
+ # used for regex substitution in selectors
+ def substitution_context
+ @substitution_context ||= Class.new do
+ # +regex+ will be a +String+
+ def match(matches, attribute, regex)
+ matches.find_all { |node| node[attribute] =~ @regexes[regex] }
+ end
+
+ def add_regex(regex)
+ @regexes ||= []
+ @regexes.push(regex)
+ id_for(regex)
+ end
+
+ def id_for(regex); @regexes.index(regex).to_s; end
+ end.new
+ end
+
def comparisons_from(comparator)
comparisons = {}
case comparator