Skip to content

Commit

Permalink
Add test cases, and explicitly close non-void elements
Browse files Browse the repository at this point in the history
  • Loading branch information
Sam Ruby committed Apr 4, 2008
1 parent 775bc2a commit 594cd30
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 8 deletions.
10 changes: 2 additions & 8 deletions planet/sift.rb
Expand Up @@ -112,15 +112,9 @@ def Planet.make_absolute node, attr_name
include HTML5::HTMLSanitizeModule
@sanitizer = HTML5::HTMLSanitizer.new ''
def Planet.sanitize node, fido
# cull empty formatting elements. They can cause FF & Konq to nest badly.
# For instance, <i/> causes everything after it to italicized, including other entries.
# ensure that non-void elements don't use XML's empty element syntax
if node.elements.size == 0 && node.text == nil
if %w{abbr acronym b big cite code del dfn em i ins kbd s
samp small strike strong sub sup tt u var}.include? node.name
# If the node has no children and no text, it can only cause trouble.
node.remove
return
end
node.text = '' unless HTML5::VOID_ELEMENTS.include? node.name
end

node.elements.each {|child| sanitize child, fido}
Expand Down
14 changes: 14 additions & 0 deletions test/sift.rb
@@ -0,0 +1,14 @@
require 'test/unit'
require 'planet/sift'

class SiftTestCase < Test::Unit::TestCase
ATOMNS = 'xmlns="http://www.w3.org/2005/Atom"'

def test_empty_formatting_elements
# http://github.com/bronson/mars/commit/775bc2a397c7812ae67b9979f288c3c835aab059
title = "<title type='html' #{ATOMNS}>&lt;i/&gt;</title>"
doc = Planet::XmlParser.parse(title)
Planet.sift doc, nil
assert_equal '<i></i>', doc.elements['title/div'].to_a.join
end
end
6 changes: 6 additions & 0 deletions test/xmlparser.rb
Expand Up @@ -15,4 +15,10 @@ def test_122
doc = Planet::XmlParser.parse('<e a="&amp;"/>')
assert_nil doc.to_s.index('&amp;amp;')
end

def test_bozo
# http://github.com/bronson/mars/commit/567e2f3f459d446f0530bbd4c8acb00dde378420
doc = Planet::XmlParser.parse('<e a="&amp;">')
assert doc.bozo
end
end

0 comments on commit 594cd30

Please sign in to comment.