Skip to content
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 11 additions & 9 deletions lib/data_uri.rb
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,17 @@ class DataUri
attr_reader :uri, :match

def initialize(uri)
match = REGEXP.match(uri)
unless uri.is_a?(String)
raise ArgumentError, 'invalid data URI'
end

normalized_uri = uri

if normalized_uri.start_with?("data:,")
normalized_uri = normalized_uri.sub("data:,", "data:text/plain,")
end

Copilot AI Oct 13, 2025

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The normalization only handles the exact case 'data:,' but should also handle variations with whitespace or parameters like 'data: ,' or 'data:;base64,'. Consider using a more robust pattern match.

Suggested change
if normalized_uri.start_with?("data:,")
normalized_uri = normalized_uri.sub("data:,", "data:text/plain,")
end
# Normalize any data URI with empty mediatype (with optional whitespace/parameters) to data:text/plain,
normalized_uri = normalized_uri.sub(/\Adata:(\s*(;[a-zA-Z0-9\-]+=[^;,]*)*)?,/, "data:text/plain,")

Copilot uses AI. Check for mistakes.

match = REGEXP.match(normalized_uri)
Comment thread
cursor[bot] marked this conversation as resolved.
Outdated
raise ArgumentError, 'invalid data URI' unless match

@uri = uri
Expand Down Expand Up @@ -66,18 +76,10 @@ def base64?
end

def mimetype
if String(match[:mimetype]).empty? || uri.starts_with?("data:,")
return 'text/plain'
end

match[:mimetype]

Copilot AI Oct 13, 2025

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The mimetype method should handle cases where match[:mimetype] is nil or empty. Without the previous fallback logic, this could return nil instead of the expected 'text/plain' default.

Suggested change
match[:mimetype]
mt = match[:mimetype]
mt.nil? || mt.empty? ? 'text/plain' : mt

Copilot uses AI. Check for mistakes.
end

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

Expand Down