inline_svg 0.8.0 → 0.9.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 +4 -4
- data/CHANGELOG.md +7 -1
- data/README.md +21 -0
- data/lib/inline_svg/random_id_generator.rb +8 -0
- data/lib/inline_svg/transform_pipeline/transformations/aria_attributes.rb +15 -3
- data/lib/inline_svg/transform_pipeline.rb +1 -0
- data/lib/inline_svg/version.rb +1 -1
- data/spec/random_id_generator_spec.rb +8 -0
- data/spec/transformation_pipeline/transformations/aria_attributes_spec.rb +66 -0
- metadata +7 -4
- data/spec/transformation_pipeline/aria_attributes_spec.rb +0 -41
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 2f130c4a553b022e11c90d79588f9b7047db3cf1
|
4
|
+
data.tar.gz: bedb58bb1f1533cf9df734190fff189ea66d60d5
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 9f33f0e86a12858fcac918aeb5ebd10fbc1639062db98beece15fa778942383ec07febefe67276d726311fd13c8c82e8905958f0b07fb96cd38da148c2bf596f
|
7
|
+
data.tar.gz: b3706ac43d6532d07a62b9acdd857dbe08c53dfc352ebcf7a93932fab30faef3a8e378c06e4ca8fc74ae366856a4ab009d6de43ad8b40674be51e3c48dd257fa
|
data/CHANGELOG.md
CHANGED
@@ -5,6 +5,11 @@ This project adheres to [Semantic Versioning](http://semver.org/).
|
|
5
5
|
## [Unreleased][unreleased]
|
6
6
|
- Nothing
|
7
7
|
|
8
|
+
## [0.9.0] - 2016-06-30
|
9
|
+
### Fixed
|
10
|
+
- Hashed IDs for desc and title elements in aria-labeled-by attribute
|
11
|
+
[#38](https://github.com/jamesmartin/inline_svg/issues/38)
|
12
|
+
|
8
13
|
## [0.8.0] - 2016-05-23
|
9
14
|
### Added
|
10
15
|
- Default values for custom transformations
|
@@ -91,7 +96,8 @@ transformations](https://github.com/jamesmartin/inline_svg/blob/master/README.md
|
|
91
96
|
### Added
|
92
97
|
- Basic Railtie and view helper to inline SVG documents to Rails views.
|
93
98
|
|
94
|
-
[unreleased]: https://github.com/jamesmartin/inline_svg/compare/v0.
|
99
|
+
[unreleased]: https://github.com/jamesmartin/inline_svg/compare/v0.9.0...HEAD
|
100
|
+
[0.9.0]: https://github.com/jamesmartin/inline_svg/compare/v0.8.0...v0.9.0
|
95
101
|
[0.8.0]: https://github.com/jamesmartin/inline_svg/compare/v0.7.0...v0.8.0
|
96
102
|
[0.7.0]: https://github.com/jamesmartin/inline_svg/compare/v0.6.4...v0.7.0
|
97
103
|
[0.6.4]: https://github.com/jamesmartin/inline_svg/compare/v0.6.3...v0.6.4
|
data/README.md
CHANGED
@@ -90,6 +90,27 @@ inline_svg("some-document.svg", id: 'some-id', class: 'some-class', data: {some:
|
|
90
90
|
'Some description', nocomment: true, preserve_aspect_ratio: 'xMaxYMax meet', aria: true)
|
91
91
|
```
|
92
92
|
|
93
|
+
## Accessibility
|
94
|
+
|
95
|
+
Use the `aria: true` option to make `inline_svg` add the following
|
96
|
+
accessibility (a11y) attributes to your embedded SVG:
|
97
|
+
|
98
|
+
* Adds a `role="img"` attribute to the root SVG element
|
99
|
+
* Adds a `aria-labelled-by="title-id desc-id"` attribute to the root SVG
|
100
|
+
element, if the document contains `<title>` or `<desc>` elements
|
101
|
+
|
102
|
+
Here's an example:
|
103
|
+
|
104
|
+
```haml
|
105
|
+
= inline_svg('iconmonstr-glasses-12-icon.svg', class: 'medium-blue', title: 'An SVG', desc: 'This is my SVG. There are many like it. You get the picture', aria: true)
|
106
|
+
```
|
107
|
+
|
108
|
+
```xml
|
109
|
+
<svg xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" version="1.1" x="0px" y="0px" width="512px" height="512px" viewBox="0 0 512 512" style="enable-background:new 0 0 512 512;" xml:space="preserve" data-default-transform="some-default" class="medium-blue" role="img" aria-labelledby="bx6wix4t9pxpwxnohrhrmms3wexsw2o m439lk7mopdzmouktv2o689pl59wmd2"><title id="bx6wix4t9pxpwxnohrhrmms3wexsw2o">An SVG</title>
|
110
|
+
<path id="glasses-12" d="M358.045,135.833c-19.531,0-40.788,2.213-58.256,8.433c-39.195,14.216-47.93,14.393-87.594,0 c-17.452-6.22-38.742-8.433-58.256-8.433c-33.831,0-71.584,6.639-103.939,15.071v30.704c14.535,4.384,18.34,10.763,20.654,25.624 c6.538,41.953,21.542,83.102,87.007,83.102c52.137,0,69.538-38.785,81.055-74.594c5.851-18.189,28.55-18.658,34.484-0.251 c11.551,35.858,28.868,74.845,81.106,74.845c65.48,0,80.502-41.148,87.023-83.102c2.312-14.861,6.119-21.24,20.67-25.624v-30.704 C429.611,142.472,391.875,135.833,358.045,135.833z M226.16,201.307c-8.885,37.317-23.336,71.859-68.499,71.859 c-56.664,0-68.214-32.665-73.395-85.363c-1.24-12.816,1.576-17.846,3.789-20.512c16.261-19.472,102.178-18.691,130.661-1.584 C226.864,170.603,232.161,176.018,226.16,201.307z M427.7,187.803c-5.163,52.698-16.731,85.363-73.394,85.363 c-45.131,0-59.598-34.542-68.482-71.859c-6.02-25.289-0.721-30.704,7.443-35.6c28.582-17.167,114.449-17.812,130.66,1.584 C426.158,169.957,428.975,174.986,427.7,187.803z M344.65,168.557c26.42-4.023,56.965-0.52,65.078,9.213 c1.777,2.129,4.023,6.152,3.018,16.403c-0.536,5.432-1.156,10.578-1.928,15.457C403.795,184.114,385.822,170.435,344.65,168.557z M101.165,209.63c-0.771-4.879-1.408-10.025-1.944-15.457c-1.006-10.251,1.257-14.274,3.034-16.403 c8.131-9.732,38.675-13.236,65.096-9.213C126.177,170.435,108.206,184.114,101.165,209.63z M376.166,342.923 C356.418,392.177,272.631,380.106,256,349.26c-16.647,30.847-100.418,42.917-120.167-6.337c9.924,8.685,26.873,11.483,39.798,9.589 c38.105-5.582,35.054-41.592,61.693-41.592c7.292,0,13.897,3.034,18.675,7.963c4.777-4.929,11.366-7.963,18.658-7.963 c26.639,0,23.604,36.01,61.693,41.592C349.277,354.406,366.226,351.607,376.166,342.923z"></path>
|
111
|
+
<desc id="m439lk7mopdzmouktv2o689pl59wmd2">This is my SVG. There are many like it. You get the picture</desc></svg>
|
112
|
+
```
|
113
|
+
|
93
114
|
## Custom Transformations
|
94
115
|
|
95
116
|
The transformation behavior of `inline_svg` can be customized by creating custom transformation classes.
|
@@ -9,9 +9,13 @@ module InlineSvg::TransformPipeline::Transformations
|
|
9
9
|
|
10
10
|
# Build aria-labelledby string
|
11
11
|
aria_elements = []
|
12
|
-
doc.search("svg title").each
|
13
|
-
|
14
|
-
|
12
|
+
doc.search("svg title").each do |element|
|
13
|
+
aria_elements << element['id'] = element_id_for("title", element)
|
14
|
+
end
|
15
|
+
|
16
|
+
doc.search("svg desc").each do |element|
|
17
|
+
aria_elements << element['id'] = element_id_for("desc", element)
|
18
|
+
end
|
15
19
|
|
16
20
|
if aria_elements.any?
|
17
21
|
svg["aria-labelledby"] = aria_elements.join(" ")
|
@@ -19,5 +23,13 @@ module InlineSvg::TransformPipeline::Transformations
|
|
19
23
|
|
20
24
|
doc
|
21
25
|
end
|
26
|
+
|
27
|
+
def element_id_for(base, element)
|
28
|
+
if element['id'].nil?
|
29
|
+
InlineSvg::RandomIdGenerator.generate(base, value)
|
30
|
+
else
|
31
|
+
InlineSvg::RandomIdGenerator.generate(element['id'], value)
|
32
|
+
end
|
33
|
+
end
|
22
34
|
end
|
23
35
|
end
|
data/lib/inline_svg/version.rb
CHANGED
@@ -0,0 +1,8 @@
|
|
1
|
+
require_relative '../lib/inline_svg/random_id_generator'
|
2
|
+
|
3
|
+
describe InlineSvg::RandomIdGenerator do
|
4
|
+
it "generates a hexencoded ID based on a salt" do
|
5
|
+
expect(InlineSvg::RandomIdGenerator.generate("some-base", "some-salt")).
|
6
|
+
to eq("ksiuuy1jduycacqpoj5smn2kyt9iv02")
|
7
|
+
end
|
8
|
+
end
|
@@ -0,0 +1,66 @@
|
|
1
|
+
require 'inline_svg/transform_pipeline'
|
2
|
+
|
3
|
+
describe InlineSvg::TransformPipeline::Transformations::AriaAttributes do
|
4
|
+
it "adds a role attribute to the SVG document" do
|
5
|
+
document = Nokogiri::XML::Document.parse('<svg>Some document</svg>')
|
6
|
+
transformation = InlineSvg::TransformPipeline::Transformations::AriaAttributes.create_with_value({})
|
7
|
+
|
8
|
+
expect(transformation.transform(document).to_html).to eq(
|
9
|
+
"<svg role=\"img\">Some document</svg>\n"
|
10
|
+
)
|
11
|
+
end
|
12
|
+
|
13
|
+
context "aria-labelledby attribute" do
|
14
|
+
it "adds 'title' when a title element is present" do
|
15
|
+
document = Nokogiri::XML::Document.parse('<svg><title>Some title</title>Some document</svg>')
|
16
|
+
transformation = InlineSvg::TransformPipeline::Transformations::AriaAttributes.create_with_value("some-salt")
|
17
|
+
|
18
|
+
expect(InlineSvg::RandomIdGenerator).to receive(:generate).with("title", "some-salt").
|
19
|
+
and_return("some-id")
|
20
|
+
|
21
|
+
expect(transformation.transform(document).to_html).to eq(
|
22
|
+
"<svg role=\"img\" aria-labelledby=\"some-id\"><title id=\"some-id\">Some title</title>Some document</svg>\n"
|
23
|
+
)
|
24
|
+
end
|
25
|
+
|
26
|
+
it "adds 'desc' when a description element is present" do
|
27
|
+
document = Nokogiri::XML::Document.parse('<svg><desc>Some description</desc>Some document</svg>')
|
28
|
+
transformation = InlineSvg::TransformPipeline::Transformations::AriaAttributes.create_with_value("some-salt")
|
29
|
+
|
30
|
+
expect(InlineSvg::RandomIdGenerator).to receive(:generate).with("desc", "some-salt").
|
31
|
+
and_return("some-id")
|
32
|
+
|
33
|
+
expect(transformation.transform(document).to_html).to eq(
|
34
|
+
"<svg role=\"img\" aria-labelledby=\"some-id\"><desc id=\"some-id\">Some description</desc>Some document</svg>\n"
|
35
|
+
)
|
36
|
+
end
|
37
|
+
|
38
|
+
it "adds both 'desc' and 'title' when title and description elements are present" do
|
39
|
+
document = Nokogiri::XML::Document.parse('<svg><title>Some title</title><desc>Some description</desc>Some document</svg>')
|
40
|
+
transformation = InlineSvg::TransformPipeline::Transformations::AriaAttributes.create_with_value("some-salt")
|
41
|
+
|
42
|
+
expect(InlineSvg::RandomIdGenerator).to receive(:generate).with("title", "some-salt").
|
43
|
+
and_return("some-id")
|
44
|
+
expect(InlineSvg::RandomIdGenerator).to receive(:generate).with("desc", "some-salt").
|
45
|
+
and_return("some-other-id")
|
46
|
+
|
47
|
+
expect(transformation.transform(document).to_html).to eq(
|
48
|
+
"<svg role=\"img\" aria-labelledby=\"some-id some-other-id\"><title id=\"some-id\">Some title</title>\n<desc id=\"some-other-id\">Some description</desc>Some document</svg>\n"
|
49
|
+
)
|
50
|
+
end
|
51
|
+
|
52
|
+
it "uses existing IDs when they exist" do
|
53
|
+
document = Nokogiri::XML::Document.parse('<svg><title id="my-title">Some title</title><desc id="my-desc">Some description</desc>Some document</svg>')
|
54
|
+
transformation = InlineSvg::TransformPipeline::Transformations::AriaAttributes.create_with_value("some-salt")
|
55
|
+
|
56
|
+
expect(InlineSvg::RandomIdGenerator).to receive(:generate).with("my-title", "some-salt").
|
57
|
+
and_return("some-id")
|
58
|
+
expect(InlineSvg::RandomIdGenerator).to receive(:generate).with("my-desc", "some-salt").
|
59
|
+
and_return("some-other-id")
|
60
|
+
|
61
|
+
expect(transformation.transform(document).to_html).to eq(
|
62
|
+
"<svg role=\"img\" aria-labelledby=\"some-id some-other-id\"><title id=\"some-id\">Some title</title>\n<desc id=\"some-other-id\">Some description</desc>Some document</svg>\n"
|
63
|
+
)
|
64
|
+
end
|
65
|
+
end
|
66
|
+
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.
|
4
|
+
version: 0.9.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-
|
11
|
+
date: 2016-06-30 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: bundler
|
@@ -128,6 +128,7 @@ files:
|
|
128
128
|
- lib/inline_svg/finds_asset_paths.rb
|
129
129
|
- lib/inline_svg/io_resource.rb
|
130
130
|
- lib/inline_svg/railtie.rb
|
131
|
+
- lib/inline_svg/random_id_generator.rb
|
131
132
|
- lib/inline_svg/static_asset_finder.rb
|
132
133
|
- lib/inline_svg/transform_pipeline.rb
|
133
134
|
- lib/inline_svg/transform_pipeline/transformations.rb
|
@@ -150,7 +151,8 @@ files:
|
|
150
151
|
- spec/helpers/inline_svg_spec.rb
|
151
152
|
- spec/inline_svg_spec.rb
|
152
153
|
- spec/io_resource_spec.rb
|
153
|
-
- spec/
|
154
|
+
- spec/random_id_generator_spec.rb
|
155
|
+
- spec/transformation_pipeline/transformations/aria_attributes_spec.rb
|
154
156
|
- spec/transformation_pipeline/transformations/data_attributes_spec.rb
|
155
157
|
- spec/transformation_pipeline/transformations/height_spec.rb
|
156
158
|
- spec/transformation_pipeline/transformations/id_attribute_spec.rb
|
@@ -190,7 +192,8 @@ test_files:
|
|
190
192
|
- spec/helpers/inline_svg_spec.rb
|
191
193
|
- spec/inline_svg_spec.rb
|
192
194
|
- spec/io_resource_spec.rb
|
193
|
-
- spec/
|
195
|
+
- spec/random_id_generator_spec.rb
|
196
|
+
- spec/transformation_pipeline/transformations/aria_attributes_spec.rb
|
194
197
|
- spec/transformation_pipeline/transformations/data_attributes_spec.rb
|
195
198
|
- spec/transformation_pipeline/transformations/height_spec.rb
|
196
199
|
- spec/transformation_pipeline/transformations/id_attribute_spec.rb
|
@@ -1,41 +0,0 @@
|
|
1
|
-
require 'inline_svg/transform_pipeline'
|
2
|
-
|
3
|
-
describe InlineSvg::TransformPipeline::Transformations::AriaAttributes do
|
4
|
-
it "adds a role attribute to the SVG document" do
|
5
|
-
document = Nokogiri::XML::Document.parse('<svg>Some document</svg>')
|
6
|
-
transformation = InlineSvg::TransformPipeline::Transformations::AriaAttributes.create_with_value({})
|
7
|
-
|
8
|
-
expect(transformation.transform(document).to_html).to eq(
|
9
|
-
"<svg role=\"img\">Some document</svg>\n"
|
10
|
-
)
|
11
|
-
end
|
12
|
-
|
13
|
-
context "aria-labelledby attribute" do
|
14
|
-
it "adds 'title' when a title element is present" do
|
15
|
-
document = Nokogiri::XML::Document.parse('<svg><title>Some title</title>Some document</svg>')
|
16
|
-
transformation = InlineSvg::TransformPipeline::Transformations::AriaAttributes.create_with_value({})
|
17
|
-
|
18
|
-
expect(transformation.transform(document).to_html).to eq(
|
19
|
-
"<svg role=\"img\" aria-labelledby=\"title\"><title>Some title</title>Some document</svg>\n"
|
20
|
-
)
|
21
|
-
end
|
22
|
-
|
23
|
-
it "adds 'desc' when a description element is present" do
|
24
|
-
document = Nokogiri::XML::Document.parse('<svg><desc>Some description</desc>Some document</svg>')
|
25
|
-
transformation = InlineSvg::TransformPipeline::Transformations::AriaAttributes.create_with_value({})
|
26
|
-
|
27
|
-
expect(transformation.transform(document).to_html).to eq(
|
28
|
-
"<svg role=\"img\" aria-labelledby=\"desc\"><desc>Some description</desc>Some document</svg>\n"
|
29
|
-
)
|
30
|
-
end
|
31
|
-
|
32
|
-
it "adds both 'desc' and 'title' when title and description elements are present" do
|
33
|
-
document = Nokogiri::XML::Document.parse('<svg><title>Some title</title><desc>Some description</desc>Some document</svg>')
|
34
|
-
transformation = InlineSvg::TransformPipeline::Transformations::AriaAttributes.create_with_value({})
|
35
|
-
|
36
|
-
expect(transformation.transform(document).to_html).to eq(
|
37
|
-
"<svg role=\"img\" aria-labelledby=\"title desc\"><title>Some title</title>\n<desc>Some description</desc>Some document</svg>\n"
|
38
|
-
)
|
39
|
-
end
|
40
|
-
end
|
41
|
-
end
|