Skip to content

Commit 19f33c6

Browse files
committed
Fix data uri normalization bug
1 parent d86287e commit 19f33c6

1 file changed

Lines changed: 11 additions & 9 deletions

File tree

‎lib/data_uri.rb‎

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,17 @@ class DataUri
1313
attr_reader :uri, :match
1414

1515
def initialize(uri)
16-
match = REGEXP.match(uri)
16+
unless uri.is_a?(String)
17+
raise ArgumentError, 'invalid data URI'
18+
end
19+
20+
normalized_uri = uri
21+
22+
if normalized_uri.start_with?("data:,")
23+
normalized_uri = normalized_uri.sub("data:,", "data:text/plain,")
24+
end
25+
26+
match = REGEXP.match(normalized_uri)
1727
raise ArgumentError, 'invalid data URI' unless match
1828

1929
@uri = uri
@@ -66,18 +76,10 @@ def base64?
6676
end
6777

6878
def mimetype
69-
if String(match[:mimetype]).empty? || uri.starts_with?("data:,")
70-
return 'text/plain'
71-
end
72-
7379
match[:mimetype]
7480
end
7581

7682
def data
77-
# Special case: if it's "data:," with no mediatype, return everything after comma
78-
if uri.start_with?("data:,")
79-
return uri[6..-1] # Everything after "data:,"
80-
end
8183
match[:data]
8284
end
8385

0 commit comments

Comments
 (0)