Skip to content
Browse files

Now suppressing script tag content in table cells.

  • Loading branch information...
1 parent 6f54d94 commit ad913dcdc0da4e5dbb8cf5be94cf8a2b08b30055 bendycode committed Aug 4, 2009
Showing with 39 additions and 4 deletions.
  1. +15 −4 lib/matchers/table_matcher.rb
  2. +24 −0 spec/table_matcher_spec.rb
View
19 lib/matchers/table_matcher.rb
@@ -32,15 +32,26 @@ def extract_html_content html
rows = doc.search("table#{"#{@table_selector}" if @table_selector} tr")
header_elements = rows.reject{|e| e.search('th').empty? }
- header_content = header_elements.map{|n| n.search('/th').map{|n| n.inner_text.strip.gsub(/[ \t]*\n[\n \t]*/, "\n")}}
+ header_content = header_elements.map do |header_element|
+ header_element.search('/th').map do |n|
+ stripped = n.inner_html.gsub(/<script.*<\/script>/m, '')
+ temp_node = Hpricot.XML("<temp-node>#{stripped}</temp-node>")
+ temp_node.inner_text.strip.gsub(/[ \t]*\n[\n \t]*/, "\n")
+ end
+ end
body_elements = rows.reject{|e| e.search('td').empty? }
- body_content = body_elements.map{|n| n.search('/td').map{|n| n.inner_text.strip.gsub(/[ \t]*\n[\n \t]*/, "\n")}}
+ body_content = body_elements.map do |body_element|
+ body_element.search('/td').map do |n|
+ stripped = n.inner_html.gsub(/<script.*<\/script>/m, '')
+ temp_node = Hpricot.XML("<temp-node>#{stripped}</temp-node>")
+ temp_node.inner_text.strip.gsub(/[ \t]*\n[\n \t]*/, "\n")
+ end
+ end
header_content + body_content
end
-
end
end
end
-end
+end
View
24 spec/table_matcher_spec.rb
@@ -20,6 +20,30 @@
response.should have_table('#my_id', [['h1', 'h2'], ["c1a\nc1b", 'c2']])
end
end
+
+ describe 'with inline script tags in a header cell' do
+ it "should suppress script content" do
+ response = mock_model(Object, :body => '<table id="my_id"><tr><th>h1</th><th>h2a<script type="text/javascript">
+ //<![CDATA[
+ x = 1;
+ //]]>
+ </script>
+ h2b</th></tr><tr><td>c1</td><td>c2</td></tr></table>')
+ response.should have_table('#my_id', [['h1', "h2a\nh2b"], ["c1", "c2"]])
+ end
+ end
+
+ describe 'with inline script tags in a cell' do
+ it "should suppress script content" do
+ response = mock_model(Object, :body => '<table id="my_id"><tr><th>h1</th><th>h2</th></tr><tr><td>c1a<script type="text/javascript">
+ //<![CDATA[
+ x = 1;
+ //]]>
+ </script>
+ c1b</td></tr></table>')
+ response.should have_table('#my_id', [['h1', 'h2'], ["c1a\nc1b"]])
+ end
+ end
end
it 'can be called without a table_id' do

0 comments on commit ad913dc

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