Kaynağa Gözat

Don't rewrite data URIs

Thibaut Courouble 9 yıl önce
ebeveyn
işleme
721adf8e21

+ 6 - 0
lib/docs/core/filter.rb

@@ -62,6 +62,12 @@ module Docs
       str[0] == '#'
     end
 
+    DATA_URL = 'data:'.freeze
+
+    def data_url_string?(str)
+      str.start_with?(DATA_URL)
+    end
+
     def relative_url_string?(str)
       !fragment_url_string?(str) && str !~ SCHEME_RGX
     end

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

@@ -11,9 +11,8 @@ module Docs
 
     def update_attribute(tag, attribute)
       css(tag.to_s).each do |node|
-        next if node[attribute].start_with?('data:image')
         next unless value = node[attribute]
-        next if fragment_url_string?(value)
+        next if fragment_url_string?(value) || data_url_string?(value)
         node[attribute] = normalize_url(value)
       end
     end

+ 5 - 0
test/lib/docs/filters/core/normalize_urls_test.rb

@@ -74,6 +74,11 @@ class NormalizeUrlsFilterTest < MiniTest::Spec
     assert_equal @body, filter_output_string
   end
 
+  it "doesn't rewrite data image urls" do
+    @body = '<img src="data:image/gif;base64,aaaa">'
+    assert_equal @body, filter_output_string
+  end
+
   context "when context[:replace_paths] is a hash" do
     before do
       context[:base_url] = 'http://example.com/dir/'