Skip to content

Commit c9e4ff6

Browse files
committed
Merge pull request #34 from jamesmartin/aria-transform
Transform for common aria attributes
2 parents 09889e7 + 4933e2b commit c9e4ff6

File tree

6 files changed

+76
-6
lines changed

6 files changed

+76
-6
lines changed

CHANGELOG.md

+4-3
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,14 @@ All notable changes to this project will be documented in this file.
33
This project adheres to [Semantic Versioning](http://semver.org/).
44

55
## [Unreleased][unreleased]
6-
- Nothing.
6+
- Aria attributes transform (aria-labelledby / role etc.) Addresses issue
7+
[#28](https://github.com/jamesmartin/inline_svg/issues/28)
78

89
## [0.6.4] - 2016-04-23
910
### Fixed
1011
- Don't duplicate the `title` element. Addresses issue
11-
[#31](https://github.com/jamesmartin/inline_svg/issues/31).
12-
- Make the `title` element the first child node of the SVG document.
12+
[#31](https://github.com/jamesmartin/inline_svg/issues/31)
13+
- Make the `title` element the first child node of the SVG document
1314

1415
## [0.6.3] - 2016-04-19
1516
### Added

README.md

+2-1
Original file line numberDiff line numberDiff line change
@@ -81,12 +81,13 @@ key | description
8181
`desc` | add a \<desc\> node inside the top level of the SVG document
8282
`nocomment` | remove comment tags (and other unsafe/unknown tags) from svg (uses the [Loofah](https://github.com/flavorjones/loofah) gem)
8383
`preserve_aspect_ratio` | adds a `preserveAspectRatio` attribute to the SVG
84+
`aria` | adds common accessibility attributes to the SVG (see [PR #34](https://github.com/jamesmartin/inline_svg/pull/34#issue-152062674) for details)
8485

8586
Example:
8687

8788
```ruby
8889
inline_svg("some-document.svg", id: 'some-id', class: 'some-class', data: {some: "value"}, size: '30% * 20%', title: 'Some Title', desc:
89-
'Some description', nocomment: true, preserve_aspect_ratio: 'xMaxYMax meet')
90+
'Some description', nocomment: true, preserve_aspect_ratio: 'xMaxYMax meet', aria: true)
9091
```
9192

9293
## Custom Transformations

lib/inline_svg/transform_pipeline/transformations.rb

+3-1
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,8 @@ def self.built_in_transformations
1010
width: Width,
1111
id: IdAttribute,
1212
data: DataAttributes,
13-
preserve_aspect_ratio: PreserveAspectRatio
13+
preserve_aspect_ratio: PreserveAspectRatio,
14+
aria: AriaAttributes
1415
}
1516
end
1617

@@ -44,3 +45,4 @@ def self.without_empty_values(params)
4445
require 'inline_svg/transform_pipeline/transformations/id_attribute'
4546
require 'inline_svg/transform_pipeline/transformations/data_attributes'
4647
require 'inline_svg/transform_pipeline/transformations/preserve_aspect_ratio'
48+
require 'inline_svg/transform_pipeline/transformations/aria_attributes'
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
module InlineSvg::TransformPipeline::Transformations
2+
class AriaAttributes < Transformation
3+
def transform(doc)
4+
doc = Nokogiri::XML::Document.parse(doc.to_html)
5+
svg = doc.at_css("svg")
6+
7+
# Add role
8+
svg["role"] = "img"
9+
10+
# Build aria-labelledby string
11+
aria_elements = []
12+
doc.search("svg title").each { |_| aria_elements << "title" }
13+
doc.search("svg desc").each { |_| aria_elements << "desc" }
14+
aria_elements.uniq!
15+
16+
if aria_elements.any?
17+
svg["aria-labelledby"] = aria_elements.join(" ")
18+
end
19+
20+
doc
21+
end
22+
end
23+
end
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
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

spec/transformation_pipeline/transformations_spec.rb

+3-1
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ def transform(doc)
2121
id: 'irrelevant',
2222
data: 'irrelevant',
2323
preserve_aspect_ratio: 'irrelevant',
24+
aria: 'irrelevant',
2425
)
2526

2627
expect(transformations.map(&:class)).to match_array([
@@ -33,7 +34,8 @@ def transform(doc)
3334
InlineSvg::TransformPipeline::Transformations::Width,
3435
InlineSvg::TransformPipeline::Transformations::IdAttribute,
3536
InlineSvg::TransformPipeline::Transformations::DataAttributes,
36-
InlineSvg::TransformPipeline::Transformations::PreserveAspectRatio
37+
InlineSvg::TransformPipeline::Transformations::PreserveAspectRatio,
38+
InlineSvg::TransformPipeline::Transformations::AriaAttributes
3739
])
3840
end
3941

0 commit comments

Comments
 (0)