Skip to content

Commit

Permalink
Working on building data URIs very inefficiently.
Browse files Browse the repository at this point in the history
  • Loading branch information
dball committed Jan 12, 2011
1 parent 0cac9b2 commit 36c57e2
Show file tree
Hide file tree
Showing 2 changed files with 92 additions and 44 deletions.
17 changes: 17 additions & 0 deletions lib/data_uri/uri.rb
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,23 @@ def initialize(*args)
@data.force_encoding(charset)
end
end

def self.build(arg)
data = nil
content_type = nil
case arg
when IO
data = arg
when Hash
data = arg[:data]
content_type = arg[:content_type]
end
raise 'Invalid build argument: ' + arg.inspect unless data
if !content_type && data.respond_to?(:content_type)
content_type = data.content_type
end
new('data', nil, nil, nil, nil, nil, "#{content_type};base64,#{Base64.encode64(data.read).chop}", nil, nil)
end
end

@@schemes['DATA'] = Data
Expand Down
119 changes: 75 additions & 44 deletions test/test_data_uri.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,64 +4,95 @@

describe URI::Data do

describe "a base64 encoded image/gif data URI" do

before do
@base64 = "R0lGODlhAQABAIABAAAAAP///yH5BAEAAAEALAAAAAABAAEAQAICTAEAOw=="
@uri = URI.parse("data:image/gif;base64,#{@base64}")
end

it "should parse as a URI::Data object" do
@uri.class.must_equal URI::Data
describe "parsing" do

describe "a base64 encoded image/gif data URI" do

before do
@base64 = "R0lGODlhAQABAIABAAAAAP///yH5BAEAAAEALAAAAAABAAEAQAICTAEAOw=="
@uri = URI.parse("data:image/gif;base64,#{@base64}")
end

it "should parse as a URI::Data object" do
@uri.class.must_equal URI::Data
end

it "should have a content_type of image/gif" do
@uri.content_type.must_equal 'image/gif'
end

it "should have data" do
require 'base64'
@uri.data.must_equal Base64.decode64(@base64)
end

end

it "should have a content_type of image/gif" do
@uri.content_type.must_equal 'image/gif'

describe "a text/plain data URI" do

before do
@uri = URI.parse("data:,A%20brief%20note")
end

it "should parse as a URI::Data object" do
@uri.class.must_equal URI::Data
end

it "should have a content_type of text/plain" do
@uri.content_type.must_equal 'text/plain'
end

it "should have data" do
@uri.data.must_equal 'A brief note'
end

end

it "should have data" do
require 'base64'
@uri.data.must_equal Base64.decode64(@base64)

describe "a text/html data URI with a charset" do

before do
@uri = URI.parse("data:text/html;charset=utf-8,%3C%21DOCTYPE%20html%3E%0D%0A%3Chtml%20lang%3D%22en%22%3E%0D%0A%3Chead%3E%3Ctitle%3EEmbedded%20Window%3C%2Ftitle%3E%3C%2Fhead%3E%0D%0A%3Cbody%3E%3Ch1%3E42%3C%2Fh1%3E%3C%2Fbody%3E%0A%3C%2Fhtml%3E%0A%0D%0A")
end

it "should parse as a URI::Data object" do
@uri.class.must_equal URI::Data
end

it "should have a content_type of text/html" do
@uri.content_type.must_equal 'text/html'
end

it "should have data" do
@uri.data.must_equal "<!DOCTYPE html>\r\n<html lang=\"en\">\r\n<head><title>Embedded Window</title></head>\r\n<body><h1>42</h1></body>\n</html>\n\r\n"
end

end

end

describe "a text/plain data URI" do
describe "building" do

before do
@uri = URI.parse("data:,A%20brief%20note")
end

it "should parse as a URI::Data object" do
@uri.class.must_equal URI::Data
end

it "should have a content_type of text/plain" do
@uri.content_type.must_equal 'text/plain'
@data = "GIF89a\001\000\001\000\200\000\000\377\377\377\000\000\000!\371\004\000\000\000\000\000,\000\000\000\000\001\000\001\000\000\002\002D\001\000;"
end

it "should have data" do
@uri.data.must_equal 'A brief note'
end

end

describe "a text/html data URI with a charset" do

before do
@uri = URI.parse("data:text/html;charset=utf-8,%3C%21DOCTYPE%20html%3E%0D%0A%3Chtml%20lang%3D%22en%22%3E%0D%0A%3Chead%3E%3Ctitle%3EEmbedded%20Window%3C%2Ftitle%3E%3C%2Fhead%3E%0D%0A%3Cbody%3E%3Ch1%3E42%3C%2Fh1%3E%3C%2Fbody%3E%0A%3C%2Fhtml%3E%0A%0D%0A")
it "given data and an explicit content_type" do
uri = URI::Data.build(:content_type => 'image/gif', :data => StringIO.new(@data))
uri.to_s.must_equal 'data:image/gif;base64,R0lGODlhAQABAIAAAP///wAAACH5BAAAAAAALAAAAAABAAEAAAICRAEAOw=='
end

it "should parse as a URI::Data object" do
@uri.class.must_equal URI::Data
it "given data with an implicit content_type" do
io = StringIO.new(@data)
(class << io; self; end).instance_eval { attr_accessor :content_type }
io.content_type = 'image/gif'
uri = URI::Data.build(:data => io)
uri.to_s.must_equal 'data:image/gif;base64,R0lGODlhAQABAIAAAP///wAAACH5BAAAAAAALAAAAAABAAEAAAICRAEAOw=='
end

it "should have a content_type of text/html" do
@uri.content_type.must_equal 'text/html'
end

it "should have data" do
@uri.data.must_equal "<!DOCTYPE html>\r\n<html lang=\"en\">\r\n<head><title>Embedded Window</title></head>\r\n<body><h1>42</h1></body>\n</html>\n\r\n"
it "given data and no content_type" do
io = StringIO.new("foobar")
uri = URI::Data.build(:data => io)
uri.to_s.must_equal 'data:;base64,Zm9vYmFy'
end

end
Expand Down

0 comments on commit 36c57e2

Please sign in to comment.