Browse Source

Fix handling of invalid iframe URLs

Fixes #590.
Thibaut Courouble 8 years ago
parent
commit
ed5a5cadd9

+ 2 - 2
lib/docs/filters/core/normalize_urls.rb

@@ -15,7 +15,7 @@ module Docs
       css(tag.to_s).each do |node|
         next unless value = node[attribute]
         next if fragment_url_string?(value) || data_url_string?(value)
-        node[attribute] = normalize_url(value)
+        node[attribute] = normalize_url(value) || (tag == :iframe ? value : '#')
       end
     end
 
@@ -31,7 +31,7 @@ module Docs
 
       url.to_s
     rescue URI::InvalidURIError
-      '#'
+      nil
     end
 
     def to_absolute_url(str)

+ 11 - 1
test/lib/docs/filters/core/normalize_urls_test.rb

@@ -39,11 +39,21 @@ class NormalizeUrlsFilterTest < MiniTest::Spec
     assert_equal link_to(context[:url]), filter_output_string
   end
 
-  it "rewrites invalid relative urls" do
+  it "rewrites invalid link urls" do
     @body = link_to '%'
     assert_equal link_to('#'), filter_output_string
   end
 
+  it "rewrites invalid image urls" do
+    @body = '<img src="%">'
+    assert_equal '<img src="#">', filter_output_string
+  end
+
+  it "doesn't rewrite invalid iframe urls" do
+    @body = '<iframe src="%"></iframe>'
+    assert_equal @body, filter_output_string
+  end
+
   it "repairs un-encoded spaces" do
     @body = link_to 'http://example.com/#foo bar '
     assert_equal link_to('http://example.com/#foo%20bar'), filter_output_string