inline_svg 0.2.0 → 0.3.0

Sign up to get free protection for your applications and to get access to all the features.

Potentially problematic release.


This version of inline_svg might be problematic. Click here for more details.

checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 529e4c1b0bd2b16b4684a2cd241bdb707cd14e74
4
- data.tar.gz: 86d782dd377e342b750e245001cbd50bba50dc71
3
+ metadata.gz: 76c4c056778276bacd69cc27ec4c982566f99f4b
4
+ data.tar.gz: e3678d7bf16c59dd3922d358265721e2fa05151a
5
5
  SHA512:
6
- metadata.gz: 46dba646464d60a3df6cc59f27767a58ef482faee661a15bd93841f5c32aeb405a30b77fc0dc53bf3f12e74ab12edb2dac9b0bc3c35505388432701010263dec
7
- data.tar.gz: 9ee8d699f27cd417bcdef5c42fc0036b7797d5fb5394c38fa7cfca576f1a16b3e3ee950f4cb082015d184ea0eebfedd26fd0c373777989e8e414b94326b9ad62
6
+ metadata.gz: a883fe5008e571a78008f94e8196ba84feb846bd25086bc127573c142a2e5232c90278e7002adbd0609dbf3be9dec25758743acd1f86e98280abec3a8df23f64
7
+ data.tar.gz: 3fa071d1f1e307cb3bffab9e7144dcf681aa32e5549fb950b350fe55a2eb4bc16438db2eebe508d3fd79b0b7022ade27bcb1b499a47498b9a42da389ff2be9ec
data/.gitignore CHANGED
@@ -15,3 +15,4 @@ spec/reports
15
15
  test/tmp
16
16
  test/version_tmp
17
17
  tmp
18
+ .ruby-*
data/CHANGELOG.md ADDED
@@ -0,0 +1,27 @@
1
+ # Change Log
2
+ All notable changes to this project will be documented in this file.
3
+ This project adheres to [Semantic Versioning](http://semver.org/).
4
+
5
+ ## [Unreleased][unreleased]
6
+ Nothing.
7
+
8
+ ## [0.3.0] - 2015-03-20
9
+ ### Added
10
+ - Use Sprockets to find canonical asset paths (fingerprinted, post asset-pipeline).
11
+
12
+ ## [0.2.0] - 2014-12-31
13
+ ### Added
14
+ - Optionally remove comments from SVG files. Thanks, @jmarceli.
15
+
16
+ ## [0.1.0] - 2014-12-15
17
+ ### Added
18
+ - Optionally add a title and description to a document. Thanks, ludwig.schubert@qlearning.de.
19
+ - Add integration tests for main view helper. Thanks, ludwig.schubert@qlearning.de.
20
+
21
+ ## 0.0.1 - 2014-11-24
22
+ ### Added
23
+ - Basic Railtie and view helper to inline SVG documents to Rails views.
24
+
25
+ [unreleased]: https://github.com/jamesmartin/inline_svg/compare/v0.2.0...HEAD
26
+ [0.2.0]: https://github.com/jamesmartin/inline_svg/compare/v0.1.0...v0.2.0
27
+ [0.1.0]: https://github.com/jamesmartin/inline_svg/compare/v0.0.1...v0.1.0
data/README.md CHANGED
@@ -1,4 +1,4 @@
1
- # Inline Svg
1
+ # Inline SVG
2
2
 
3
3
  Styling a SVG document with CSS for use on the web is most reliably achieved by
4
4
  [adding classes to the document and
@@ -8,6 +8,11 @@ This gem is a little helper method (`inline_svg`) that reads an SVG document fro
8
8
  image directory, applies a CSS class attribute to the root of the document and
9
9
  then embeds it into a view.
10
10
 
11
+ ## Changelog
12
+
13
+ All notable changes to this project are documented in the
14
+ [CHANGELOG](https://github.com/jamesmartin/inline_svg/blob/master/CHANGELOG.md).
15
+
11
16
  ## Installation
12
17
 
13
18
  Add this line to your application's Gemfile:
@@ -72,7 +77,7 @@ inline_svg("some-document.svg", class: 'some-class', title: 'Some Title', desc:
72
77
 
73
78
  ## Contributing
74
79
 
75
- 1. Fork it ( [http://github.com/<my-github-username>/inline_svg/fork](http://github.com/<my-github-username>/inline_svg/fork) )
80
+ 1. Fork it ( [http://github.com/jamesmartin/inline_svg/fork](http://github.com/jamesmartin/inline_svg/fork) )
76
81
  2. Create your feature branch (`git checkout -b my-new-feature`)
77
82
  3. Commit your changes (`git commit -am 'Add some feature'`)
78
83
  4. Push to the branch (`git push origin my-new-feature`)
data/inline_svg.gemspec CHANGED
@@ -20,9 +20,9 @@ Gem::Specification.new do |spec|
20
20
 
21
21
  spec.add_development_dependency "bundler", "~> 1.5"
22
22
  spec.add_development_dependency "rake"
23
- spec.add_development_dependency "rspec", "~> 2.6"
24
- spec.add_development_dependency "activesupport"
23
+ spec.add_development_dependency "rspec", "~> 3.2"
25
24
 
25
+ spec.add_runtime_dependency "activesupport", "~> 4.1.9"
26
26
  spec.add_runtime_dependency "nokogiri", "~> 1.6", '~> 1.6'
27
27
  spec.add_runtime_dependency "loofah", ">= 2.0"
28
28
  end
@@ -7,28 +7,29 @@ module InlineSvg
7
7
  module ActionView
8
8
  module Helpers
9
9
  def inline_svg(filename, options={})
10
- file = File.read(Rails.root.join('app', 'assets', 'images', filename))
11
- doc = Nokogiri::HTML::DocumentFragment.parse file
10
+ begin
11
+ doc = Loofah::HTML::DocumentFragment.parse(AssetFile.named(filename))
12
+ rescue InlineSvg::AssetFile::FileNotFound
13
+ return "<svg><!-- SVG file not found: '#{filename}' --></svg>".html_safe
14
+ end
12
15
 
13
- # remove comments from svg file
14
16
  if options[:nocomment].present?
15
- doc = Loofah.fragment(doc.to_s).scrub!(:strip)
17
+ doc.scrub!(:strip)
16
18
  end
17
19
 
18
20
  svg = doc.at_css 'svg'
19
21
  if options[:class]
20
22
  svg['class'] = options[:class]
21
23
  end
22
- if options[:title].present?
23
- title = Nokogiri::XML::Node.new("title", doc)
24
- title.content = options[:title]
25
- svg.add_child title
26
- end
27
- if options[:desc].present?
28
- desc = Nokogiri::XML::Node.new("desc", doc)
29
- desc.content = options[:desc]
30
- svg.add_child desc
24
+
25
+ %i(title desc).each do |child|
26
+ if options[child].present?
27
+ node = Nokogiri::XML::Node.new(child.to_s, doc)
28
+ node.content = options[child]
29
+ svg.add_child node
30
+ end
31
31
  end
32
+
32
33
  doc.to_html.html_safe
33
34
  end
34
35
  end
@@ -0,0 +1,13 @@
1
+ module InlineSvg
2
+ class AssetFile
3
+ class FileNotFound < IOError; end
4
+ UNREADABLE_PATH = ''
5
+
6
+ def self.named(filename)
7
+ asset_path = FindsAssetPaths.by_filename(filename)
8
+ File.read(asset_path || UNREADABLE_PATH)
9
+ rescue Errno::ENOENT
10
+ raise FileNotFound.new("Asset not found: #{asset_path}")
11
+ end
12
+ end
13
+ end
@@ -0,0 +1,11 @@
1
+ module InlineSvg
2
+ class FindsAssetPaths
3
+ def self.by_filename(filename)
4
+ configured_asset_finder.find_asset(filename)
5
+ end
6
+
7
+ def self.configured_asset_finder
8
+ InlineSvg.configuration.asset_finder
9
+ end
10
+ end
11
+ end
@@ -5,6 +5,9 @@ module InlineSvg
5
5
  ActiveSupport.on_load :action_view do
6
6
  require "inline_svg/action_view/helpers"
7
7
  include InlineSvg::ActionView::Helpers
8
+ InlineSvg.configure do |config|
9
+ config.asset_finder = app.instance_variable_get(:@assets) # In most cases this will be the Sprockets::Environment instance of the Rails app.
10
+ end
8
11
  end
9
12
  end
10
13
  end
@@ -1,3 +1,3 @@
1
1
  module InlineSvg
2
- VERSION = "0.2.0"
2
+ VERSION = "0.3.0"
3
3
  end
data/lib/inline_svg.rb CHANGED
@@ -1,7 +1,38 @@
1
1
  require "inline_svg/version"
2
2
  require "inline_svg/action_view/helpers"
3
+ require "inline_svg/finds_asset_paths"
4
+ require "inline_svg/asset_file"
3
5
  require "inline_svg/railtie" if defined?(Rails)
6
+ require 'active_support/core_ext'
7
+ require 'nokogiri'
4
8
 
5
9
  module InlineSvg
6
- # Your code goes here...
10
+ class Configuration
11
+ class Invalid < ArgumentError; end
12
+
13
+ attr_reader :asset_finder
14
+
15
+ def asset_finder=(finder)
16
+ if finder.respond_to?(:find_asset)
17
+ @asset_finder = finder
18
+ else
19
+ raise InlineSvg::Configuration::Invalid.new("Asset Finder should implement the #find_asset method")
20
+ end
21
+ asset_finder
22
+ end
23
+ end
24
+
25
+ @configuration = InlineSvg::Configuration.new
26
+
27
+ class << self
28
+ attr_reader :configuration
29
+
30
+ def configure
31
+ if block_given?
32
+ yield configuration
33
+ else
34
+ raise InlineSvg::Configuration::Invalid.new('Please set configuration options with a block')
35
+ end
36
+ end
37
+ end
7
38
  end
@@ -0,0 +1,27 @@
1
+ require_relative '../lib/inline_svg/finds_asset_paths'
2
+ require_relative '../lib/inline_svg/asset_file'
3
+
4
+ describe InlineSvg::AssetFile do
5
+ it "reads data from a file, after qualifying a full path" do
6
+ example_svg_path = File.expand_path(__FILE__, 'files/example.svg')
7
+ expect(InlineSvg::FindsAssetPaths).to receive(:by_filename).with('some filename').and_return example_svg_path
8
+
9
+ expect(InlineSvg::AssetFile.named('some filename')).to include('This is a test')
10
+ end
11
+
12
+ it "complains when the file cannot be read" do
13
+ allow(InlineSvg::FindsAssetPaths).to receive(:by_filename).and_return('/this/path/does/not/exist')
14
+
15
+ expect do
16
+ InlineSvg::AssetFile.named('some missing file')
17
+ end.to raise_error InlineSvg::AssetFile::FileNotFound
18
+ end
19
+
20
+ it "complains when the file path was not found" do
21
+ allow(InlineSvg::FindsAssetPaths).to receive(:by_filename).and_return(nil)
22
+
23
+ expect do
24
+ InlineSvg::AssetFile.named('some missing file')
25
+ end.to raise_error InlineSvg::AssetFile::FileNotFound
26
+ end
27
+ end
@@ -1,3 +1,3 @@
1
1
  <svg xmlns="http://www.w3.org/2000/svg"
2
2
  xml:lang="en"
3
- role="presentation"><!-- This is a comment --></svg>
3
+ role="presentation"><!-- This is a test comment --></svg>
@@ -0,0 +1,16 @@
1
+ require_relative '../lib/inline_svg'
2
+
3
+ describe InlineSvg::FindsAssetPaths do
4
+ it "returns fully qualified file paths from Sprockets" do
5
+ sprockets = double('SprocketsDouble')
6
+
7
+ expect(sprockets).to receive(:find_asset).with('some-file').
8
+ and_return('/full/path/to/some-file')
9
+
10
+ InlineSvg.configure do |config|
11
+ config.asset_finder = sprockets
12
+ end
13
+
14
+ expect(InlineSvg::FindsAssetPaths.by_filename('some-file')).to eq '/full/path/to/some-file'
15
+ end
16
+ end
@@ -1,62 +1,83 @@
1
1
  require 'inline_svg'
2
- require 'nokogiri'
3
- require 'active_support/core_ext'
4
2
 
5
3
  describe InlineSvg::ActionView::Helpers do
6
4
 
7
- before(:each) do
8
- # Mock Rails
9
- unless defined?(::Rails)
10
- @mocked_rails_class = true
11
- class ::Rails
5
+ let(:helper) { ( Class.new { include InlineSvg::ActionView::Helpers } ).new }
6
+
7
+ describe "#inline_svg" do
8
+
9
+ context "when passed the name of an SVG that does not exist" do
10
+ it "returns an empty, html safe, SVG document as a placeholder" do
11
+ allow(InlineSvg::AssetFile).to receive(:named).with('some-missing-file').and_raise(InlineSvg::AssetFile::FileNotFound.new)
12
+ output = helper.inline_svg('some-missing-file')
13
+ expect(output).to eq "<svg><!-- SVG file not found: 'some-missing-file' --></svg>"
14
+ expect(output).to be_html_safe
12
15
  end
13
16
  end
14
- # Allow mock Rails to give a path to our SVG
15
- path_mocker = double("path_mocker")
16
- allow(path_mocker).to receive(:join) { 'spec/files/example.svg' }
17
- allow(Rails).to receive(:root) { path_mocker }
18
- end
19
17
 
20
- let(:mock_helper) { ( Class.new { include InlineSvg::ActionView::Helpers } ).new }
18
+ context "when passed an existing SVG file" do
21
19
 
22
- describe "#inline_svg" do
23
-
24
- context 'when passed a SVG file' do
25
-
26
- subject { mock_helper.inline_svg( 'mocked_path' ).strip }
27
- let(:example_svg) {
28
- <<-SVG.sub(/\n$/, '')
29
- <svg xmlns="http://www.w3.org/2000/svg" role="presentation" xml:lang="en"><!-- This is a comment --></svg>
20
+ context "and no options" do
21
+ it "returns a html safe version of the file's contents" do
22
+ example_file = <<-SVG.sub(/\n$/, '')
23
+ <svg xmlns="http://www.w3.org/2000/svg" role="presentation" xml:lang="en"><!-- This is a comment --></svg>
24
+ SVG
25
+ allow(InlineSvg::AssetFile).to receive(:named).with('some-file').and_return(example_file)
26
+ expect(helper.inline_svg('some-file')).to eq example_file
27
+ end
28
+ end
29
+
30
+ context "and the 'title' option" do
31
+ it "adds the title node to the SVG output" do
32
+ input_svg = <<-SVG.sub(/\n$/, '')
33
+ <svg xmlns="http://www.w3.org/2000/svg" role="presentation" xml:lang="en"></svg>
34
+ SVG
35
+ expected_output = <<-SVG.sub(/\n$/, '')
36
+ <svg xmlns="http://www.w3.org/2000/svg" role="presentation" xml:lang="en"><title>A title</title></svg>
30
37
  SVG
31
- }
32
-
33
- it { is_expected.to eql example_svg }
34
-
35
- context 'and title and description options' do
36
-
37
- subject { mock_helper.inline_svg( 'mocked_path', title: 'A title', desc: 'A desc' ).strip }
38
- let(:example_svg) {
39
- <<-SVG.sub(/\n$/, '')
40
- <svg xmlns="http://www.w3.org/2000/svg" role="presentation" xml:lang="en"><!-- This is a comment --><title>A title</title>
41
- <desc>A desc</desc></svg>
42
- SVG
43
- }
44
-
45
- it { is_expected.to eql example_svg }
46
-
38
+ allow(InlineSvg::AssetFile).to receive(:named).with('some-file').and_return(input_svg)
39
+ expect(helper.inline_svg('some-file', title: 'A title')).to eq expected_output
40
+ end
47
41
  end
48
42
 
49
- context 'and the "nocomment" option' do
43
+ context "and the 'desc' option" do
44
+ it "adds the description node to the SVG output" do
45
+ input_svg = <<-SVG.sub(/\n$/, '')
46
+ <svg xmlns="http://www.w3.org/2000/svg" role="presentation" xml:lang="en"></svg>
47
+ SVG
48
+ expected_output = <<-SVG.sub(/\n$/, '')
49
+ <svg xmlns="http://www.w3.org/2000/svg" role="presentation" xml:lang="en"><desc>A description</desc></svg>
50
+ SVG
51
+ allow(InlineSvg::AssetFile).to receive(:named).with('some-file').and_return(input_svg)
52
+ expect(helper.inline_svg('some-file', desc: 'A description')).to eq expected_output
53
+ end
54
+ end
50
55
 
51
- subject { mock_helper.inline_svg( 'mocked_path', nocomment: true).strip }
52
- let(:example_svg) {
53
- <<-SVG.sub(/\n$/, '')
56
+ context "and the 'nocomment' option" do
57
+ it "strips comments and other unknown/unsafe nodes from the output" do
58
+ input_svg = <<-SVG.sub(/\n$/, '')
59
+ <svg xmlns="http://www.w3.org/2000/svg" role="presentation" xml:lang="en"><!-- This is a comment --></svg>
60
+ SVG
61
+ expected_output = <<-SVG.sub(/\n$/, '')
54
62
  <svg xmlns="http://www.w3.org/2000/svg" xml:lang="en"></svg>
55
63
  SVG
56
- }
57
-
58
- it { is_expected.to eql example_svg }
64
+ allow(InlineSvg::AssetFile).to receive(:named).with('some-file').and_return(input_svg)
65
+ expect(helper.inline_svg('some-file', nocomment: true)).to eq expected_output
66
+ end
67
+ end
59
68
 
69
+ context "and all options" do
70
+ it "applies all expected transformations to the output" do
71
+ input_svg = <<-SVG.sub(/\n$/, '')
72
+ <svg xmlns="http://www.w3.org/2000/svg" role="presentation" xml:lang="en"><!-- This is a comment --></svg>
73
+ SVG
74
+ expected_output = <<-SVG.sub(/\n$/, '')
75
+ <svg xmlns="http://www.w3.org/2000/svg" xml:lang="en"><title>A title</title>
76
+ <desc>A description</desc></svg>
77
+ SVG
78
+ allow(InlineSvg::AssetFile).to receive(:named).with('some-file').and_return(input_svg)
79
+ expect(helper.inline_svg('some-file', title: 'A title', desc: 'A description', nocomment: true)).to eq expected_output
80
+ end
60
81
  end
61
82
  end
62
83
  end
@@ -0,0 +1,32 @@
1
+ require_relative '../lib/inline_svg'
2
+
3
+ describe InlineSvg do
4
+ describe "configuration" do
5
+ context "when a block is not given" do
6
+ it "complains" do
7
+ expect do
8
+ InlineSvg.configure
9
+ end.to raise_error(InlineSvg::Configuration::Invalid)
10
+ end
11
+ end
12
+
13
+ context "asset finder" do
14
+ it "allows an asset finder to be assigned" do
15
+ sprockets = double('SomethingLikeSprockets', find_asset: 'some asset')
16
+ InlineSvg.configure do |config|
17
+ config.asset_finder = sprockets
18
+ end
19
+
20
+ expect(InlineSvg.configuration.asset_finder).to eq sprockets
21
+ end
22
+
23
+ it "complains when the provided asset finder does not implement #find_asset" do
24
+ expect do
25
+ InlineSvg.configure do |config|
26
+ config.asset_finder = 'Not a real asset finder'
27
+ end
28
+ end.to raise_error(InlineSvg::Configuration::Invalid, /asset finder.*find_asset/i)
29
+ end
30
+ end
31
+ end
32
+ end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: inline_svg
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.0
4
+ version: 0.3.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - James Martin
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2014-12-31 00:00:00.000000000 Z
11
+ date: 2015-03-20 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler
@@ -44,28 +44,28 @@ dependencies:
44
44
  requirements:
45
45
  - - "~>"
46
46
  - !ruby/object:Gem::Version
47
- version: '2.6'
47
+ version: '3.2'
48
48
  type: :development
49
49
  prerelease: false
50
50
  version_requirements: !ruby/object:Gem::Requirement
51
51
  requirements:
52
52
  - - "~>"
53
53
  - !ruby/object:Gem::Version
54
- version: '2.6'
54
+ version: '3.2'
55
55
  - !ruby/object:Gem::Dependency
56
56
  name: activesupport
57
57
  requirement: !ruby/object:Gem::Requirement
58
58
  requirements:
59
- - - ">="
59
+ - - "~>"
60
60
  - !ruby/object:Gem::Version
61
- version: '0'
62
- type: :development
61
+ version: 4.1.9
62
+ type: :runtime
63
63
  prerelease: false
64
64
  version_requirements: !ruby/object:Gem::Requirement
65
65
  requirements:
66
- - - ">="
66
+ - - "~>"
67
67
  - !ruby/object:Gem::Version
68
- version: '0'
68
+ version: 4.1.9
69
69
  - !ruby/object:Gem::Dependency
70
70
  name: nokogiri
71
71
  requirement: !ruby/object:Gem::Requirement
@@ -102,6 +102,7 @@ extensions: []
102
102
  extra_rdoc_files: []
103
103
  files:
104
104
  - ".gitignore"
105
+ - CHANGELOG.md
105
106
  - Gemfile
106
107
  - LICENSE.txt
107
108
  - README.md
@@ -109,10 +110,15 @@ files:
109
110
  - inline_svg.gemspec
110
111
  - lib/inline_svg.rb
111
112
  - lib/inline_svg/action_view/helpers.rb
113
+ - lib/inline_svg/asset_file.rb
114
+ - lib/inline_svg/finds_asset_paths.rb
112
115
  - lib/inline_svg/railtie.rb
113
116
  - lib/inline_svg/version.rb
117
+ - spec/asset_file_spec.rb
114
118
  - spec/files/example.svg
119
+ - spec/finds_asset_paths_spec.rb
115
120
  - spec/helpers/inline_svg_spec.rb
121
+ - spec/inline_svg_spec.rb
116
122
  homepage: ''
117
123
  licenses:
118
124
  - MIT
@@ -133,10 +139,13 @@ required_rubygems_version: !ruby/object:Gem::Requirement
133
139
  version: '0'
134
140
  requirements: []
135
141
  rubyforge_project:
136
- rubygems_version: 2.2.2
142
+ rubygems_version: 2.4.3
137
143
  signing_key:
138
144
  specification_version: 4
139
145
  summary: Embeds an SVG document, inline.
140
146
  test_files:
147
+ - spec/asset_file_spec.rb
141
148
  - spec/files/example.svg
149
+ - spec/finds_asset_paths_spec.rb
142
150
  - spec/helpers/inline_svg_spec.rb
151
+ - spec/inline_svg_spec.rb