Skip to content

Commit

Permalink
Now suppressing script tag content in table cells.
Browse files Browse the repository at this point in the history
  • Loading branch information
bendycode committed Aug 4, 2009
1 parent 6f54d94 commit ad913dc
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 4 deletions.
19 changes: 15 additions & 4 deletions lib/matchers/table_matcher.rb
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -32,15 +32,26 @@ def extract_html_content html


rows = doc.search("table#{"#{@table_selector}" if @table_selector} tr") rows = doc.search("table#{"#{@table_selector}" if @table_selector} tr")
header_elements = rows.reject{|e| e.search('th').empty? } 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_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 header_content + body_content
end end

end end
end end
end end
end end
24 changes: 24 additions & 0 deletions spec/table_matcher_spec.rb
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -20,6 +20,30 @@
response.should have_table('#my_id', [['h1', 'h2'], ["c1a\nc1b", 'c2']]) response.should have_table('#my_id', [['h1', 'h2'], ["c1a\nc1b", 'c2']])
end end
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 end


it 'can be called without a table_id' do it 'can be called without a table_id' do
Expand Down

0 comments on commit ad913dc

Please sign in to comment.