diff options
author | David Heinemeier Hansson <david@loudthinking.com> | 2005-05-06 16:42:01 +0000 |
---|---|---|
committer | David Heinemeier Hansson <david@loudthinking.com> | 2005-05-06 16:42:01 +0000 |
commit | 409bc0970aa6145dfaeb6d79363a74e0244002a0 (patch) | |
tree | 1f24cbb1693132b7820ba2f8896a727ef394df80 /actionpack/lib | |
parent | 483931ea615d49a31d6de225f273e0945ad6cbb3 (diff) | |
download | rails-409bc0970aa6145dfaeb6d79363a74e0244002a0.tar.gz rails-409bc0970aa6145dfaeb6d79363a74e0244002a0.tar.bz2 rails-409bc0970aa6145dfaeb6d79363a74e0244002a0.zip |
Added functionality to assert_tag, so you can now do tests on the siblings of a node, to assert that some element comes before or after the element in question, or just to assert that some element exists as a sibling #1226 [Jamis Buck]
git-svn-id: http://svn-commit.rubyonrails.org/rails/trunk@1291 5ecf4fe2-1ee6-0310-87b1-e25e094e27de
Diffstat (limited to 'actionpack/lib')
3 files changed, 57 insertions, 5 deletions
diff --git a/actionpack/lib/action_controller/assertions.rb b/actionpack/lib/action_controller/assertions.rb index 00388f8f59..5d1826b72a 100644 --- a/actionpack/lib/action_controller/assertions.rb +++ b/actionpack/lib/action_controller/assertions.rb @@ -136,6 +136,12 @@ module Test #:nodoc: # meet the criteria described by the hash. # * <tt>:descendant</tt>: a hash. At least one of the node's descendants # must meet the criteria described by the hash. + # * <tt>:sibling</tt>: a hash. At least one of the node's siblings must + # meet the criteria described by the hash. + # * <tt>:after</tt>: a hash. The node must be after any sibling meeting + # the criteria described by the hash, and at least one sibling must match. + # * <tt>:before</tt>: a hash. The node must be before any sibling meeting + # the criteria described by the hash, and at least one sibling must match. # * <tt>:children</tt>: a hash, for counting children of a node. Accepts # the keys: # * <tt>:count</tt>: either a number or a range which must equal (or diff --git a/actionpack/lib/action_controller/vendor/html-scanner/html/node.rb b/actionpack/lib/action_controller/vendor/html-scanner/html/node.rb index b3979fa3cb..2d32962ba8 100644 --- a/actionpack/lib/action_controller/vendor/html-scanner/html/node.rb +++ b/actionpack/lib/action_controller/vendor/html-scanner/html/node.rb @@ -13,7 +13,8 @@ module HTML#:nodoc: # keys are valid, and require no further processing when :attributes then hash[k] = keys_to_strings(v) - when :parent, :child, :ancestor, :descendant + when :parent, :child, :ancestor, :descendant, :sibling, :before, + :after hash[k] = Conditions.new(v) when :children hash[k] = v = keys_to_symbols(v) @@ -119,12 +120,20 @@ module HTML#:nodoc: end class <<self - def parse(parent, line, pos, content) + def parse(parent, line, pos, content, strict=true) if content !~ /^<\S/ Text.new(parent, line, pos, content) else scanner = StringScanner.new(content) - scanner.skip(/</) or raise "expected <" + + unless scanner.skip(/</) + if strict + raise "expected <" + else + return Text.new(parent, line, pos, content) + end + end + closing = ( scanner.scan(/\//) ? :close : nil ) return Text.new(parent, line, pos, content) unless name = scanner.scan(/[\w:]+/) name.downcase! @@ -158,7 +167,14 @@ module HTML#:nodoc: closing = ( scanner.scan(/\//) ? :self : nil ) end - scanner.scan(/\s*>/) or raise "expected > (got #{scanner.rest.inspect} for #{content}, #{attributes.inspect})" + unless scanner.scan(/\s*>/) + if strict + raise "expected > (got #{scanner.rest.inspect} for #{content}, #{attributes.inspect})" + else + # throw away all text until we find what we're looking for + scanner.skip_until(/>/) or scanner.terminate + end + end Tag.new(parent, line, pos, name, attributes, closing) end @@ -296,6 +312,12 @@ module HTML#:nodoc: # meet the criteria described by the hash. # * <tt>:descendant</tt>: a hash. At least one of the node's descendants # must meet the criteria described by the hash. + # * <tt>:sibling</tt>: a hash. At least one of the node's siblings must + # meet the criteria described by the hash. + # * <tt>:after</tt>: a hash. The node must be after any sibling meeting + # the criteria described by the hash, and at least one sibling must match. + # * <tt>:before</tt>: a hash. The node must be before any sibling meeting + # the criteria described by the hash, and at least one sibling must match. # * <tt>:children</tt>: a hash, for counting children of a node. Accepts the # keys: # ** <tt>:count</tt>: either a number or a range which must equal (or @@ -404,6 +426,30 @@ module HTML#:nodoc: end end end + + # test siblings + if conditions[:sibling] || conditions[:before] || conditions[:after] + siblings = parent ? parent.children : [] + self_index = siblings.index(self) + + if conditions[:sibling] + return false unless siblings.detect do |s| + s != self && s.match(conditions[:sibling]) + end + end + + if conditions[:before] + return false unless siblings[self_index+1..-1].detect do |s| + s != self && s.match(conditions[:before]) + end + end + + if conditions[:after] + return false unless siblings[0,self_index].detect do |s| + s != self && s.match(conditions[:after]) + end + end + end true end diff --git a/actionpack/lib/action_controller/vendor/html-scanner/html/tokenizer.rb b/actionpack/lib/action_controller/vendor/html-scanner/html/tokenizer.rb index 2c3ee922d9..45066319f5 100644 --- a/actionpack/lib/action_controller/vendor/html-scanner/html/tokenizer.rb +++ b/actionpack/lib/action_controller/vendor/html-scanner/html/tokenizer.rb @@ -92,4 +92,4 @@ module HTML#:nodoc: end end -end
\ No newline at end of file +end |