Skip to content
This repository
Browse code

Fix the html-scanner to count children correctly, playing nicely with…

… :only, fixes #2181 [patrick@lenz.sh]

git-svn-id: http://svn-commit.rubyonrails.org/rails/trunk@2670 5ecf4fe2-1ee6-0310-87b1-e25e094e27de
  • Loading branch information...
commit e515ba9ecd43101d31ae9afea8de766e886192dd 1 parent 9601f96
Jamis Buck authored
6  actionpack/lib/action_controller/vendor/html-scanner/html/node.rb
@@ -435,7 +435,11 @@ def match(conditions)
435 435
       
436 436
       # count children
437 437
       if opts = conditions[:children]
438  
-        matches = children.select { |c| c.match(/./) }
  438
+        matches = children.select do |c|
  439
+          c.match(/./) or
  440
+          (c.kind_of?(HTML::Tag) and (c.closing == :self or ! c.childless?))
  441
+        end
  442
+        
439 443
         matches = matches.select { |c| c.match(opts[:only]) } if opts[:only]
440 444
         opts.each do |key, value|
441 445
           next if key == :only
21  actionpack/test/controller/test_test.rb
@@ -20,6 +20,7 @@ def test_html_output
20 20
       render :text => <<HTML
21 21
 <html>
22 22
   <body>
  23
+    <a href="/"><img src="/images/button.png" /></a>
23 24
     <div id="foo">
24 25
       <ul>
25 26
         <li class="item">hello</li>
@@ -121,8 +122,8 @@ def test_assert_tag_child
121 122
 
122 123
     # there is a tag with a child 'input' tag
123 124
     assert_tag :child => { :tag => "input" }
124  
-    # there is no tag with a child 'a' tag
125  
-    assert_no_tag :child => { :tag => "a" }
  125
+    # there is no tag with a child 'strong' tag
  126
+    assert_no_tag :child => { :tag => "strong" }
126 127
   end
127 128
 
128 129
   def test_assert_tag_ancestor
@@ -233,6 +234,22 @@ def test_assert_tag_multiple
233 234
                                    :only => { :tag => "li" } } }
234 235
   end
235 236
 
  237
+  def test_assert_tag_children_without_content
  238
+    process :test_html_output
  239
+    
  240
+    # there is a form tag with an 'input' child which is a self closing tag
  241
+    assert_tag :tag => "form",
  242
+      :children => { :count => 1,
  243
+        :only => { :tag => "input" } }
  244
+
  245
+    # the body tag has an 'a' child which in turn has an 'img' child
  246
+    assert_tag :tag => "body",
  247
+      :children => { :count => 1,
  248
+        :only => { :tag => "a",
  249
+          :children => { :count => 1,
  250
+            :only => { :tag => "img" } } } }
  251
+  end
  252
+
236 253
   def test_assert_generates
237 254
     assert_generates 'controller/action/5', :controller => 'controller', :action => 'action', :id => '5'
238 255
   end

0 notes on commit e515ba9

Please sign in to comment.
Something went wrong with that request. Please try again.