Created
January 26, 2011 11:14
-
-
Save rngtng/796571 to your computer and use it in GitHub Desktop.
fix irregular HTML by replacing not closed '<' with '<'
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
module SanitizeHelper | |
def fix_irregular_html(html) | |
regexp = /<([^<>]*)(<|$)/ | |
#we need to do this multiple time as regex are overlapping | |
while (fixed_html = html.gsub(regexp, "<\\1\\2")) && fixed_html != html | |
html = fixed_html | |
end | |
fixed_html | |
end | |
end | |
# Test | |
describe SanitizeHelper do | |
it "should fix irregular html" do | |
replaces = { | |
"Foo" => "Foo", | |
"Foo <3" => "Foo <3", | |
"Foo 3>" => "Foo 3>", | |
"Foo <3>" => "Foo <3>", | |
"Foo <3<4" => "Foo <3<4", | |
"Foo 3><4" => "Foo 3><4", | |
"Foo <3><4" => "Foo <3><4", | |
"Foo <3<4>" => "Foo <3<4>", | |
"Foo <3<<4>" => "Foo <3<<4>", | |
"Foo 3><4>" => "Foo 3><4>", | |
"Foo <3><4>" => "Foo <3><4>", | |
"Foo <3<4<5" => "Foo <3<4<5", | |
"Foo 3><4<5" => "Foo 3><4<5", | |
"Foo <3><4<5" => "Foo <3><4<5", | |
"Foo <3<4><5" => "Foo <3<4><5", | |
"Foo 3><4><5" => "Foo 3><4><5", | |
"Foo <3><4><5" => "Foo <3><4><5", | |
"Foo <3<4<5<6" => "Foo <3<4<5<6", | |
"Foo 3><4<5<6" => "Foo 3><4<5<6", | |
"Foo <3><4<5<6" => "Foo <3><4<5<6", | |
"Foo <3<4><5<6" => "Foo <3<4><5<6", | |
"Foo 3><4><5<6" => "Foo 3><4><5<6", | |
"Foo <3><4><5<6" => "Foo <3><4><5<6", | |
"Foo <3<4<5<6<7" => "Foo <3<4<5<6<7", | |
"Foo <3<4<5<6<7<8" => "Foo <3<4<5<6<7<8", | |
"Foo <3<4<5<6<7<8<9" => "Foo <3<4<5<6<7<8<9", | |
}.each do |is, should| | |
fix_irregular_html(is).should == should | |
end | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
See discussion here:
http://stackoverflow.com/questions/3268387/nokogiri-parsing-irregular