inline_svg 0.10.0 → 0.11.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: f8cc9c7834d90be148c6dab54da0583e310c6be7
4
- data.tar.gz: 7d56a72f24be4e162b998b49b8494ec5abf004b6
3
+ metadata.gz: 7fb6e64ae5311883892613c4a6b3eebf128961b6
4
+ data.tar.gz: 40f3eb0a9198ff40838f3df4fe93937a3ef1dd9c
5
5
  SHA512:
6
- metadata.gz: d2b1f8ecb2a936f8e1a8297b3766c0f11dac1318aca92f9f1ac6a43790fa268cd78aaf10b841ca2890e2493c3f27b7caaf32d8671a8df14cf4e114ca953a6c1f
7
- data.tar.gz: 5b90cc6cb9531ea88ad2c0326156453c9ef335949311c5288a8a9f75098a2bdb2e9ce784598cbaca17e64acccafe763a99ffe121439abdc9c135f68e10c6a88e
6
+ metadata.gz: 2a6d3430898827a14e0b34dfe018efe8f99991dd7362f758e63973e75d577d7cb6116ee8cb981a1534dfd7283fd170e9bbef36ad13325f7fc482423c86de0314
7
+ data.tar.gz: 126adaf50a5d41ea9225c3497cf2df7ee20c231caf33f653773ddc03d8cb5b2c32bf51248ac5bbd1cff9e19ca494422cccfadf7136530d08826b71e74d9cc17b
data/CHANGELOG.md CHANGED
@@ -5,6 +5,16 @@ This project adheres to [Semantic Versioning](http://semver.org/).
5
5
  ## [Unreleased][unreleased]
6
6
  - Nothing
7
7
 
8
+ ## [0.11.0] - 2016-07-24
9
+ ### Added
10
+ - Priority ordering for transformations
11
+
12
+ ### Fixed
13
+ - Prevent duplicate desc elements being created
14
+ [#46](https://github.com/jamesmartin/inline_svg/issues/46)
15
+ - Prevent class attributes being replaced
16
+ [#44](https://github.com/jamesmartin/inline_svg/issues/44)
17
+
8
18
  ## [0.10.0] - 2016-07-24
9
19
  ### Added
10
20
  - Rails 5 support [#43](https://github.com/jamesmartin/inline_svg/pull/43)
@@ -107,7 +117,8 @@ transformations](https://github.com/jamesmartin/inline_svg/blob/master/README.md
107
117
  ### Added
108
118
  - Basic Railtie and view helper to inline SVG documents to Rails views.
109
119
 
110
- [unreleased]: https://github.com/jamesmartin/inline_svg/compare/v0.10.0...HEAD
120
+ [unreleased]: https://github.com/jamesmartin/inline_svg/compare/v0.11.0...HEAD
121
+ [0.11.0]: https://github.com/jamesmartin/inline_svg/compare/v0.10.0...v0.11.0
111
122
  [0.10.0]: https://github.com/jamesmartin/inline_svg/compare/v0.9.1...v0.10.0
112
123
  [0.9.1]: https://github.com/jamesmartin/inline_svg/compare/v0.9.0...v0.9.1
113
124
  [0.9.0]: https://github.com/jamesmartin/inline_svg/compare/v0.8.0...v0.9.0
data/README.md CHANGED
@@ -7,7 +7,7 @@ embedding](http://css-tricks.com/using-svg/) it inline in the HTML.
7
7
  This gem is a little Rails helper method (`inline_svg`) that reads an SVG document (via Sprockets, so works with the Rails Asset Pipeline), applies a CSS class attribute to the root of the document and
8
8
  then embeds it into a view.
9
9
 
10
- Inline SVG supports [Rails version 4.0.4](http://weblog.rubyonrails.org/2014/3/14/Rails-4-0-4-has-been-released/) and newer.
10
+ Inline SVG (from [v0.10.0](https://github.com/jamesmartin/inline_svg/releases/tag/v0.10.0)) supports both [Rails 4](http://weblog.rubyonrails.org/2013/6/25/Rails-4-0-final/) and [Rails 5](http://weblog.rubyonrails.org/2016/6/30/Rails-5-0-final/).
11
11
 
12
12
  Want to embed SVGs with Javascript? You might like [RemoteSvg](https://github.com/jamesmartin/remote-svg), which features similar transforms but can also load SVGs from remote URLs (like S3 etc.).
13
13
 
@@ -188,6 +188,21 @@ And
188
188
  <svg custom="some value">...</svg>
189
189
  ```
190
190
 
191
+ Passing a `priority` option with your custom transformation allows you to
192
+ control the order that transformations are applied to the SVG document:
193
+
194
+ ```ruby
195
+ InlineSvg.configure do |config|
196
+ config.add_custom_transformation(attribute: :custom_one, transform: MyCustomTransform, priority: 1)
197
+ config.add_custom_transformation(attribute: :custom_two, transform: MyOtherCustomTransform, priority: 2)
198
+ end
199
+ ```
200
+
201
+ Transforms are applied in ascending order (lowest number first).
202
+
203
+ ***Note***: Custom transformations are always applied *after* all built-in
204
+ transformations, regardless of priority.
205
+
191
206
  ## Contributing
192
207
 
193
208
  1. Fork it ( [http://github.com/jamesmartin/inline_svg/fork](http://github.com/jamesmartin/inline_svg/fork) )
data/circle.yml ADDED
@@ -0,0 +1,3 @@
1
+ machine:
2
+ ruby:
3
+ version: 2.2.2
data/inline_svg.gemspec CHANGED
@@ -22,6 +22,7 @@ Gem::Specification.new do |spec|
22
22
  spec.add_development_dependency "rake"
23
23
  spec.add_development_dependency "rspec", "~> 3.2"
24
24
  spec.add_development_dependency "rspec_junit_formatter", "0.2.2"
25
+ spec.add_development_dependency "pry"
25
26
 
26
27
  spec.add_runtime_dependency "activesupport", ">= 4.0"
27
28
  spec.add_runtime_dependency "nokogiri", "~> 1.6", '~> 1.6'
@@ -2,8 +2,10 @@ module InlineSvg::TransformPipeline::Transformations
2
2
  class ClassAttribute < Transformation
3
3
  def transform(doc)
4
4
  doc = Nokogiri::XML::Document.parse(doc.to_html)
5
- svg = doc.at_css 'svg'
6
- svg['class'] = value
5
+ svg = doc.at_css "svg"
6
+ classes = (svg["class"] || "").split(" ")
7
+ classes << value
8
+ svg["class"] = classes.join(" ")
7
9
  doc
8
10
  end
9
11
  end
@@ -4,7 +4,8 @@ module InlineSvg::TransformPipeline::Transformations
4
4
  doc = Nokogiri::XML::Document.parse(doc.to_html)
5
5
  node = Nokogiri::XML::Node.new('desc', doc)
6
6
  node.content = value
7
- doc.at_css('svg').add_child(node)
7
+ doc.search('svg desc').each { |node| node.remove }
8
+ doc.at_css('svg').prepend_child(node)
8
9
  doc
9
10
  end
10
11
  end
@@ -1,33 +1,55 @@
1
1
  module InlineSvg::TransformPipeline::Transformations
2
+ # Transformations are run in priority order, lowest number first:
2
3
  def self.built_in_transformations
3
4
  {
4
- nocomment: { transform: NoComment },
5
+ id: { transform: IdAttribute, priority: 1 },
6
+ desc: { transform: Description, priority: 2 },
7
+ title: { transform: Title, priority: 3 },
8
+ aria: { transform: AriaAttributes },
5
9
  class: { transform: ClassAttribute },
6
- title: { transform: Title },
7
- desc: { transform: Description },
8
- size: { transform: Size },
9
- height: { transform: Height },
10
- width: { transform: Width },
11
- id: { transform: IdAttribute },
12
10
  data: { transform: DataAttributes },
11
+ height: { transform: Height },
12
+ nocomment: { transform: NoComment },
13
13
  preserve_aspect_ratio: { transform: PreserveAspectRatio },
14
- aria: { transform: AriaAttributes }
14
+ size: { transform: Size },
15
+ width: { transform: Width },
15
16
  }
16
17
  end
17
18
 
18
19
  def self.custom_transformations
19
- InlineSvg.configuration.custom_transformations
20
+ magnify_priorities(InlineSvg.configuration.custom_transformations)
21
+ end
22
+
23
+ def self.magnify_priorities(transforms)
24
+ transforms.inject({}) do |output, (name, definition)|
25
+ priority = definition.fetch(:priority, built_in_transformations.size)
26
+
27
+ output[name] = definition.merge( { priority: magnify(priority) } )
28
+ output
29
+ end
30
+ end
31
+
32
+ def self.magnify(priority=0)
33
+ (priority + 1) * built_in_transformations.size
20
34
  end
21
35
 
22
36
  def self.all_transformations
23
- built_in_transformations.merge(custom_transformations)
37
+ in_priority_order(built_in_transformations.merge(custom_transformations))
24
38
  end
25
39
 
26
40
  def self.lookup(transform_params)
27
- all_default_values.merge(without_empty_values(transform_params)).map do |key, value|
28
- options = all_transformations.fetch(key, { transform: NullTransformation })
29
- options.fetch(:transform, no_transform).create_with_value(value)
30
- end
41
+ all_transformations.map { |name, definition|
42
+ value = params_with_defaults(transform_params)[name]
43
+ definition.fetch(:transform, no_transform).create_with_value(value) if value
44
+ }.compact
45
+ end
46
+
47
+ def self.in_priority_order(transforms)
48
+ transforms.sort_by { |_, options| options.fetch(:priority, transforms.size) }
49
+ end
50
+
51
+ def self.params_with_defaults(params)
52
+ without_empty_values(all_default_values.merge(params))
31
53
  end
32
54
 
33
55
  def self.without_empty_values(params)
@@ -39,7 +61,7 @@ module InlineSvg::TransformPipeline::Transformations
39
61
  .values
40
62
  .select {|opt| opt[:default_value] != nil}
41
63
  .map {|opt| [opt[:attribute], opt[:default_value]]}
42
- .inject({}) {|hash, array| hash.merge!(array[0] => array[1])}
64
+ .inject({}) {|options, attrs| options.merge!(attrs[0] => attrs[1])}
43
65
  end
44
66
 
45
67
  def self.no_transform
@@ -1,3 +1,3 @@
1
1
  module InlineSvg
2
- VERSION = "0.10.0"
2
+ VERSION = "0.11.0"
3
3
  end
@@ -0,0 +1,21 @@
1
+ require "inline_svg/transform_pipeline"
2
+
3
+ describe InlineSvg::TransformPipeline::Transformations::ClassAttribute do
4
+ it "adds a class attribute to a SVG document" do
5
+ document = Nokogiri::XML::Document.parse('<svg>Some document</svg>')
6
+ transformation = InlineSvg::TransformPipeline::Transformations::ClassAttribute.create_with_value("some-class")
7
+
8
+ expect(transformation.transform(document).to_html).to eq(
9
+ "<svg class=\"some-class\">Some document</svg>\n"
10
+ )
11
+ end
12
+
13
+ it "preserves existing class attributes on a SVG document" do
14
+ document = Nokogiri::XML::Document.parse('<svg class="existing things">Some document</svg>')
15
+ transformation = InlineSvg::TransformPipeline::Transformations::ClassAttribute.create_with_value("some-class")
16
+
17
+ expect(transformation.transform(document).to_html).to eq(
18
+ "<svg class=\"existing things some-class\">Some document</svg>\n"
19
+ )
20
+ end
21
+ end
@@ -0,0 +1,30 @@
1
+ require 'inline_svg/transform_pipeline'
2
+
3
+ describe InlineSvg::TransformPipeline::Transformations::Description do
4
+ it "adds a desc element to the SVG document" do
5
+ document = Nokogiri::XML::Document.parse('<svg>Some document</svg>')
6
+ transformation = InlineSvg::TransformPipeline::Transformations::Description.create_with_value("Some Description")
7
+
8
+ expect(transformation.transform(document).to_html).to eq(
9
+ "<svg><desc>Some Description</desc>Some document</svg>\n"
10
+ )
11
+ end
12
+
13
+ it "overwrites the content of an existing description element" do
14
+ document = Nokogiri::XML::Document.parse('<svg><desc>My Description</desc>Some document</svg>')
15
+ transformation = InlineSvg::TransformPipeline::Transformations::Description.create_with_value("Some Description")
16
+
17
+ expect(transformation.transform(document).to_html).to eq(
18
+ "<svg><desc>Some Description</desc>Some document</svg>\n"
19
+ )
20
+ end
21
+
22
+ it "handles empty SVG documents" do
23
+ document = Nokogiri::XML::Document.parse('<svg></svg>')
24
+ transformation = InlineSvg::TransformPipeline::Transformations::Description.create_with_value("Some Description")
25
+
26
+ expect(transformation.transform(document).to_html).to eq(
27
+ "<svg><desc>Some Description</desc></svg>\n"
28
+ )
29
+ end
30
+ end
@@ -7,6 +7,8 @@ class ACustomTransform < InlineSvg::CustomTransformation
7
7
  end
8
8
  end
9
9
 
10
+ class ASecondCustomTransform < ACustomTransform; end
11
+
10
12
  describe InlineSvg::TransformPipeline::Transformations do
11
13
  context "looking up transformations" do
12
14
  it "returns built-in transformations when parameters are supplied" do
@@ -39,16 +41,40 @@ describe InlineSvg::TransformPipeline::Transformations do
39
41
  ])
40
42
  end
41
43
 
42
- it "returns a benign transformation when asked for an unknown transform" do
44
+ it "returns transformations in priority order" do
45
+ built_ins = {
46
+ desc: { transform: InlineSvg::TransformPipeline::Transformations::Description, priority: 1 },
47
+ size: { transform: InlineSvg::TransformPipeline::Transformations::Size },
48
+ title: { transform: InlineSvg::TransformPipeline::Transformations::Title, priority: 2 }
49
+ }
50
+
51
+ allow(InlineSvg::TransformPipeline::Transformations).to \
52
+ receive(:built_in_transformations).and_return(built_ins)
53
+
43
54
  transformations = InlineSvg::TransformPipeline::Transformations.lookup(
44
- not_a_real_transform: 'irrelevant'
55
+ {
56
+ desc: "irrelevant",
57
+ size: "irrelevant",
58
+ title: "irrelevant",
59
+ }
45
60
  )
46
61
 
47
- expect(transformations.map(&:class)).to match_array([
48
- InlineSvg::TransformPipeline::Transformations::NullTransformation
62
+ # Use `eq` here because we care about order
63
+ expect(transformations.map(&:class)).to eq([
64
+ InlineSvg::TransformPipeline::Transformations::Description,
65
+ InlineSvg::TransformPipeline::Transformations::Title,
66
+ InlineSvg::TransformPipeline::Transformations::Size,
49
67
  ])
50
68
  end
51
69
 
70
+ it "returns no transformations when asked for an unknown transform" do
71
+ transformations = InlineSvg::TransformPipeline::Transformations.lookup(
72
+ not_a_real_transform: 'irrelevant'
73
+ )
74
+
75
+ expect(transformations.map(&:class)).to match_array([])
76
+ end
77
+
52
78
  it "does not return a transformation when a value is not supplied" do
53
79
  transformations = InlineSvg::TransformPipeline::Transformations.lookup(
54
80
  title: nil
@@ -61,7 +87,8 @@ describe InlineSvg::TransformPipeline::Transformations do
61
87
  context "custom transformations" do
62
88
  before(:each) do
63
89
  InlineSvg.configure do |config|
64
- config.add_custom_transformation({transform: ACustomTransform, attribute: :my_transform})
90
+ config.add_custom_transformation({transform: ACustomTransform, attribute: :my_transform, priority: 2})
91
+ config.add_custom_transformation({transform: ASecondCustomTransform, attribute: :my_other_transform, priority: 1})
65
92
  end
66
93
  end
67
94
 
@@ -76,6 +103,33 @@ describe InlineSvg::TransformPipeline::Transformations do
76
103
 
77
104
  expect(transformations.map(&:class)).to match_array([ACustomTransform])
78
105
  end
106
+
107
+ it "returns configured custom transformations in priority order" do
108
+ transformations = InlineSvg::TransformPipeline::Transformations.lookup(
109
+ my_transform: :irrelevant,
110
+ my_other_transform: :irrelevant
111
+ )
112
+
113
+ # Use `eq` here because we care about order:
114
+ expect(transformations.map(&:class)).to eq([ASecondCustomTransform, ACustomTransform])
115
+ end
116
+
117
+ it "always prioritizes built-in transforms before custom transforms" do
118
+ transformations = InlineSvg::TransformPipeline::Transformations.lookup(
119
+ my_transform: :irrelevant,
120
+ my_other_transform: :irrelevant,
121
+ desc: "irrelevant"
122
+ )
123
+
124
+ # Use `eq` here because we care about order:
125
+ expect(transformations.map(&:class)).to eq(
126
+ [
127
+ InlineSvg::TransformPipeline::Transformations::Description,
128
+ ASecondCustomTransform,
129
+ ACustomTransform
130
+ ]
131
+ )
132
+ end
79
133
  end
80
134
 
81
135
  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.10.0
4
+ version: 0.11.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: 2016-07-24 00:00:00.000000000 Z
11
+ date: 2016-09-01 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler
@@ -66,6 +66,20 @@ dependencies:
66
66
  - - '='
67
67
  - !ruby/object:Gem::Version
68
68
  version: 0.2.2
69
+ - !ruby/object:Gem::Dependency
70
+ name: pry
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - ">="
74
+ - !ruby/object:Gem::Version
75
+ version: '0'
76
+ type: :development
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - ">="
81
+ - !ruby/object:Gem::Version
82
+ version: '0'
69
83
  - !ruby/object:Gem::Dependency
70
84
  name: activesupport
71
85
  requirement: !ruby/object:Gem::Requirement
@@ -121,6 +135,7 @@ files:
121
135
  - LICENSE.txt
122
136
  - README.md
123
137
  - Rakefile
138
+ - circle.yml
124
139
  - inline_svg.gemspec
125
140
  - lib/inline_svg.rb
126
141
  - lib/inline_svg/action_view/helpers.rb
@@ -153,7 +168,9 @@ files:
153
168
  - spec/inline_svg_spec.rb
154
169
  - spec/io_resource_spec.rb
155
170
  - spec/transformation_pipeline/transformations/aria_attributes_spec.rb
171
+ - spec/transformation_pipeline/transformations/class_attribute_spec.rb
156
172
  - spec/transformation_pipeline/transformations/data_attributes_spec.rb
173
+ - spec/transformation_pipeline/transformations/description_spec.rb
157
174
  - spec/transformation_pipeline/transformations/height_spec.rb
158
175
  - spec/transformation_pipeline/transformations/id_attribute_spec.rb
159
176
  - spec/transformation_pipeline/transformations/preserve_aspect_ratio_spec.rb
@@ -194,7 +211,9 @@ test_files:
194
211
  - spec/inline_svg_spec.rb
195
212
  - spec/io_resource_spec.rb
196
213
  - spec/transformation_pipeline/transformations/aria_attributes_spec.rb
214
+ - spec/transformation_pipeline/transformations/class_attribute_spec.rb
197
215
  - spec/transformation_pipeline/transformations/data_attributes_spec.rb
216
+ - spec/transformation_pipeline/transformations/description_spec.rb
198
217
  - spec/transformation_pipeline/transformations/height_spec.rb
199
218
  - spec/transformation_pipeline/transformations/id_attribute_spec.rb
200
219
  - spec/transformation_pipeline/transformations/preserve_aspect_ratio_spec.rb