jamesmartin / inline_svg

Embed SVG documents in your Rails views and style them with CSS
MIT License
716 stars 73 forks source link

Improve likelihood of Aria IDs being unique #77

Closed jamesmartin closed 7 years ago

jamesmartin commented 7 years ago

Fixes #76.

In #39 we introduced an ID generator that we use to add the aria-labelled-by attribute to the SVG root element containing compounded digest values that match the title and desc elements' id attributes. E.g.

<svg role="img" aria-labelledby="some-id some-other-id">
  <title id="some-id">Some title</title>
  <desc id="some-other-id">Some description</desc>
  Some document
</svg>

The ID values are generated based on either the existing ID of the element and its text, or a generic string (title or desc) and the element's text value. The values are deterministic, which leads to elements with duplicate IDs if the same SVG is added to a page multiple times.

This branch adds randomness (SecureRandom) to the IdGenerator class, to effectively reduce the likelihood of ID collision to zero.

/cc @tysongach for you input on the original feature /cc @sonniesedge for reporting the issue