Class: InlineSvg::TransformPipeline::Transformations::AriaAttributes
- Inherits:
-
Transformation
- Object
- Transformation
- InlineSvg::TransformPipeline::Transformations::AriaAttributes
- Defined in:
- lib/inline_svg/transform_pipeline/transformations/aria_attributes.rb
Instance Attribute Summary
Attributes inherited from Transformation
Instance Method Summary collapse
Methods inherited from Transformation
create_with_value, #initialize, #with_svg
Constructor Details
This class inherits a constructor from InlineSvg::TransformPipeline::Transformations::Transformation
Instance Method Details
#element_id_for(base, element) ⇒ Object
24 25 26 27 28 29 30 |
# File 'lib/inline_svg/transform_pipeline/transformations/aria_attributes.rb', line 24 def element_id_for(base, element) if element["id"].nil? InlineSvg::IdGenerator.generate(base, element.text) else InlineSvg::IdGenerator.generate(element["id"], element.text) end end |
#transform(doc) ⇒ Object
3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 |
# File 'lib/inline_svg/transform_pipeline/transformations/aria_attributes.rb', line 3 def transform(doc) with_svg(doc) do |svg| # Add role svg["role"] = "img" # Build aria-labelledby string aria_elements = [] svg.search("title").each do |element| aria_elements << element["id"] = element_id_for("title", element) end svg.search("desc").each do |element| aria_elements << element["id"] = element_id_for("desc", element) end if aria_elements.any? svg["aria-labelledby"] = aria_elements.join(" ") end end end |