diff --git a/lib/bypass/filter.rb b/lib/bypass/filter.rb index 16e7fa7..054b644 100644 --- a/lib/bypass/filter.rb +++ b/lib/bypass/filter.rb @@ -25,5 +25,11 @@ def gsub_urls(text, &block) yield(match.to_s) end end + + def parse_uri(uri) + Bypass::URI.parse(uri) + rescue => ex + nil + end end end \ No newline at end of file diff --git a/lib/bypass/html_filter.rb b/lib/bypass/html_filter.rb index feb9830..7937b00 100644 --- a/lib/bypass/html_filter.rb +++ b/lib/bypass/html_filter.rb @@ -6,8 +6,10 @@ class HTMLFilter < Filter def replace(&block) parsed_content.traverse do |node| if node.name == "a" && href = node.get_attribute("href") - url = yield(Bypass::URI.parse(href)) - node.set_attribute("href", url.to_s) + if parsed_href = parse_uri(href) + url = yield(parsed_href) + node.set_attribute("href", url.to_s) + end end end end diff --git a/lib/bypass/text_filter.rb b/lib/bypass/text_filter.rb index 0e3877e..bc45be3 100644 --- a/lib/bypass/text_filter.rb +++ b/lib/bypass/text_filter.rb @@ -2,8 +2,10 @@ module Bypass class TextFilter < Filter def replace(&block) - @content = gsub_urls(content) do |url| - yield(Bypass::URI.parse(url)).to_s + @content = gsub_urls(content) do |url| + if parsed_url = parse_uri(url) + yield(parsed_url).to_s + end end end diff --git a/test/detour/filter_test.rb b/test/bypass/filter_test.rb similarity index 100% rename from test/detour/filter_test.rb rename to test/bypass/filter_test.rb diff --git a/test/detour/html_filter_test.rb b/test/bypass/html_filter_test.rb similarity index 90% rename from test/detour/html_filter_test.rb rename to test/bypass/html_filter_test.rb index 781f381..1b97bbc 100644 --- a/test/detour/html_filter_test.rb +++ b/test/bypass/html_filter_test.rb @@ -16,6 +16,13 @@ class Bypass::HTMLFilterTest < Test::Unit::TestCase filter.replace { "foo" } assert_equal "Yahoo", filter.content end + + should "skip malformed URLs" do + text = "Yahoo" + filter = Bypass::HTMLFilter.new(text) + filter.replace { "foo" } + assert_equal text, filter.content + end end context "#auto_link" do diff --git a/test/detour/text_filter_test.rb b/test/bypass/text_filter_test.rb similarity index 84% rename from test/detour/text_filter_test.rb rename to test/bypass/text_filter_test.rb index c363536..09457b8 100644 --- a/test/detour/text_filter_test.rb +++ b/test/bypass/text_filter_test.rb @@ -30,5 +30,12 @@ class Bypass::TextFilterTest < Test::Unit::TestCase filter.replace { "foo" } assert_equal "foo,", filter.content end + + should "skip malformed URLs" do + text = "http://#" + filter = Bypass::TextFilter.new(text) + filter.replace { "foo" } + assert_equal text, filter.content + end end end \ No newline at end of file diff --git a/test/detour/uri_test.rb b/test/bypass/uri_test.rb similarity index 100% rename from test/detour/uri_test.rb rename to test/bypass/uri_test.rb