Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
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
52 changes: 52 additions & 0 deletions .github/workflows/tests.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
name: Tests

on:
pull_request:
branches:
- master
paths-ignore:
- 'README.rdoc'
push:
branches:
- master
paths-ignore:
- 'README.rdoc'

jobs:
unit_tests:
name: Unit Tests
# Homemade support for [ci skip] no longer needed
# https://github.blog/changelog/2021-02-08-github-actions-skip-pull-request-and-push-workflows-with-skip-ci/
# if: "contains(github.event.commits[0].message, '[ci skip]') == false"
strategy:
fail-fast: false
matrix:
ruby:
- "3.0"
- "3.1"
- "3.2"
- "3.3"
- "jruby"
- "truffleruby"
- "truffleruby+graalvm"
allow_failures:
- false
include:
- ruby: ruby-head
allow_failures: true
- ruby: jruby-head
allow_failures: true
env:
ALLOW_FAILURES: "${{ matrix.allow_failures }}"
runs-on: ubuntu-latest
continue-on-error: ${{ endsWith(matrix.ruby, 'head') || matrix.ruby == 'debug' }}
steps:
- name: Checkout
uses: actions/checkout@v4
- name: Setup Ruby
uses: ruby/setup-ruby@v1
with:
ruby-version: ${{ matrix.ruby }}
bundler-cache: true
- name: Test
run: bundle exec rake || $ALLOW_FAILURES
4 changes: 3 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,2 +1,4 @@
*.gem
.bundle
Gemfile.lock
*.swp
data_uri-*.gem
8 changes: 0 additions & 8 deletions .travis.yml

This file was deleted.

18 changes: 0 additions & 18 deletions Gemfile.lock

This file was deleted.

5 changes: 4 additions & 1 deletion data_uri.gemspec
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,16 @@ Gem::Specification.new do |s|
s.description = "URI class for parsing data URIs"
s.summary = "A URI class for parsing data URIs as per RFC2397"

s.required_ruby_version = ">= 3.0"

s.platform = Gem::Platform::RUBY
s.has_rdoc = true
s.extra_rdoc_files = ["README.rdoc"]

s.require_path = 'lib'
s.files = %w(README.rdoc Rakefile) + Dir.glob("lib/**/*")

s.add_dependency "base64"

s.add_development_dependency 'rake'
s.add_development_dependency 'minitest'
end
4 changes: 1 addition & 3 deletions lib/data_uri.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
require 'uri'
require 'base64'
require 'stringio'
# frozen_string_literal: true

require 'data_uri/uri'
22 changes: 0 additions & 22 deletions lib/data_uri/open_uri.rb

This file was deleted.

22 changes: 16 additions & 6 deletions lib/data_uri/uri.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
# frozen_string_literal: true

require 'uri'
require 'base64'

module URI

class Data < Generic
Expand All @@ -20,25 +25,25 @@ def initialize(*args)
super(*args)
end
@data = @opaque
if md = MIME_TYPE_RE.match(@data)
if (md = MIME_TYPE_RE.match(@data))
@content_type = md[1]
@data = @data[@content_type.length .. -1]
end
@content_type ||= 'text/plain'
@mime_params = {}
while md = MIME_PARAM_RE.match(@data)
while (md = MIME_PARAM_RE.match(@data))
@mime_params[md[1]] = md[2]
@data = @data[md[0].length .. -1]
end
if base64 = /^;base64/.match(@data)
if (base64 = /^;base64/.match(@data))
@data = @data[7 .. -1]
end
unless /^,/.match(@data)
raise URI::InvalidURIError.new('Invalid data URI')
end
@data = @data[1 .. -1]
@data = base64 ? Base64.decode64(@data) : URI.decode(@data)
if @data.respond_to?(:force_encoding) && charset = @mime_params['charset']
@data = base64 ? Base64.decode64(@data) : URI.decode_www_form_component(@data)
if @data.respond_to?(:force_encoding) && (charset = @mime_params['charset'])
@data.force_encoding(charset)
end
end
Expand All @@ -61,6 +66,11 @@ def self.build(arg)
end
end

@@schemes['DATA'] = Data
unless methods(false).include?(:register_scheme)
def self.register_scheme(scheme, klass)
@@schemes[scheme] = klass
end
end
register_scheme('DATA', Data)

end
37 changes: 0 additions & 37 deletions test/test_data_open_uri.rb

This file was deleted.

68 changes: 34 additions & 34 deletions test/test_data_uri.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,71 +5,71 @@
describe URI::Data do

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
_(@uri.class).must_equal URI::Data
end

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

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

end

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
_(@uri.class).must_equal URI::Data
end

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

it "should have data" do
@uri.data.must_equal 'A brief note'
_(@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")
end

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

it "should have a content_type of text/html" do
@uri.content_type.must_equal 'text/html'
_(@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"
_(@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

describe "a big data binary data URI" do

before do
@data = Array.new(100000) { rand(256) }.pack('c*')
@raw = "data:application/octet-stream;base64,#{Base64.encode64(@data).chop}"
Expand All @@ -80,7 +80,7 @@
uri = URI.parse(@raw)
refute_equal uri.data, @data
else
proc { URI.parse(@raw) }.must_raise(URI::InvalidURIError)
_(proc { URI.parse(@raw) }).must_raise(URI::InvalidURIError)
end
end

Expand All @@ -93,8 +93,8 @@

describe "an invalid data URI" do
it "should raise an error" do
proc { URI::Data.new("This is not a data URI") }.must_raise(URI::InvalidURIError)
proc { URI::Data.new("data:Neither this") }.must_raise(URI::InvalidURIError)
_(proc { URI::Data.new("This is not a data URI") }).must_raise(URI::InvalidURIError)
_(proc { URI::Data.new("data:Neither this") }).must_raise(URI::InvalidURIError)
end
end

Expand All @@ -108,21 +108,21 @@

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=='
_(uri.to_s).must_equal 'data:image/gif;base64,R0lGODlhAQABAIAAAP///wAAACH5BAAAAAAALAAAAAABAAEAAAICRAEAOw=='
end

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=='
_(uri.to_s).must_equal 'data:image/gif;base64,R0lGODlhAQABAIAAAP///wAAACH5BAAAAAAALAAAAAABAAEAAAICRAEAOw=='
end

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'
_(uri.to_s).must_equal 'data:;base64,Zm9vYmFy'
end

end
Expand Down
Loading