accessible_svg_string
Mojo function 🡭
accessible_svg_string
fn def accessible_svg_string(var svg: SvgCanvas, title: String, description: String = "") -> Stringsvg.to_string() with SVG accessibility markup added: role="img" and aria-label on the root <svg> element, plus a <title> (and a <desc> when description is non-empty) as its first child elements, which is what screen readers that support SVG look for.
title is required; the same string passed to .labels(title=...)
is usually right.
SvgCanvas.set_title() does the work. This was once a
post-processing wrapper that spliced the attributes and children into
to_string()’s output as text, which meant depending on <svg ...>
being the literal start of that output and on two of canvas_mojo’s
private escaping helpers. Nothing upstream owed us either (#572).
This only helps where the SVG’s accessible tree is walked: inline
<svg> markup, a standalone .svg, or an <object>/<iframe>
embed. A plain <img src="chart.svg"> (how the docs site embeds
examples) treats the SVG as an opaque image, and a screen reader
reads the <img>’s alt text instead.
Taking the canvas by value is the one visible change: set_title()
is a mutation and SvgCanvas is movable but not copyable, so a
caller holding one passes svg^ rather than svg. Every call here
passes a freshly rendered canvas, which moves on its own.
Args:
- svg (
SvgCanvas): The rendered chart, consumed. - title (
String): The accessible name. Required. - description (
String): A longer description, omitted when empty.
Returns:
String: The SVG document, titled.
Raises:
Error: Whatever set_title() raises.