Module: Dommy::DataUri

Defined in:
lib/dommy/data_uri.rb

Overview

Decode an RFC 2397 data: URI into its bytes + media type, so fetch / XHR to an inline data: URL resolves like a browser (200 + the decoded payload) instead of falling through to a 404. note.com loads its icon SVGs as data:image/svg+xml;base64,… via XHR, which hit this.

Class Method Summary collapse

Class Method Details

.parse(url) ⇒ Object

{ body:, content_type: } for a data: URL, or nil for any other URL. The payload is returned as UTF-8 text (the common case for the text/* and image/svg+xml data URLs fetched this way).



16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
# File 'lib/dommy/data_uri.rb', line 16

def parse(url)
  str = url.to_s
  return nil unless str.start_with?("data:")

  meta, data = str[5..].split(",", 2)
  return nil if data.nil?

  base64 = meta.end_with?(";base64")
  media = meta.sub(/;base64\z/, "")
  media = "text/plain;charset=US-ASCII" if media.empty?
  bytes =
    if base64
      Base64.decode64(data)
    else
      data.gsub(/%([0-9A-Fa-f]{2})/) { Regexp.last_match(1).hex.chr }
    end
  {body: bytes.dup.force_encoding(Encoding::UTF_8), content_type: media}
end