diff --git a/Products/CMFPlone/browser/icons.py b/Products/CMFPlone/browser/icons.py index 72de48ddff..af927564b7 100644 --- a/Products/CMFPlone/browser/icons.py +++ b/Products/CMFPlone/browser/icons.py @@ -9,6 +9,7 @@ from zope.publisher.interfaces import IPublishTraverse import logging +import uuid logger = logging.getLogger(__name__) @@ -28,8 +29,9 @@ def _add_aria_title(svgtree, cfg): title = etree.Element("title") root.append(title) title.text = cfg["title"] + title.attrib["id"] = cfg["hash"] # add aria attr - root.attrib["aria-labelledby"] = "title" + root.attrib["aria-labelledby"] = cfg["hash"] SVG_MODIFER["add_aria_title"] = _add_aria_title @@ -49,12 +51,27 @@ def _add_css_class(svgtree, cfg): SVG_MODIFER["add_css_class"] = _add_css_class -def _strip_id(svgtree, cfg): - for el in svgtree.getroot().xpath("//*[@id]"): - del el.attrib["id"] +def _unique_id(svgtree, cfg): + # for references to work + # the ids in the svg must be unique in the document + root = svgtree.getroot() + for el in root.xpath("//*[@id]"): + _id = el.attrib["id"] + if cfg["hash"] in _id: + # might be already unique + continue + _unique_id = f"{_id}-{cfg['hash']}" + el.attrib["id"] = _unique_id + refs = root.xpath(f"//*[@* = '#{_id}' or @* = 'url(#{_id})']") + for ref in refs: + for attr, attr_val in ref.attrib.items(): + # there are multiple attributes that might contain a reference + # to an id, e.g. href, xlink:href, fill, filter, clip-path, ... + if f"#{_id}" in attr_val: + ref.attrib[attr] = attr_val.replace(f"#{_id}", f"#{_unique_id}") -SVG_MODIFER["strip_id"] = _strip_id +SVG_MODIFER["unique_id"] = _unique_id @implementer(IPublishTraverse) @@ -130,8 +147,9 @@ def tag(self, name, tag_class="", tag_alt=""): modifier_cfg = { "cssclass": tag_class, "title": tag_alt, + "hash": str(uuid.uuid4()), } - for name, modifier in SVG_MODIFER.items(): - __traceback_info__ = name + for mod_name, modifier in SVG_MODIFER.items(): + __traceback_info__ = mod_name modifier(svgtree, modifier_cfg) return etree.tostring(svgtree) diff --git a/news/4224.bugfix b/news/4224.bugfix new file mode 100644 index 0000000000..129fe7cc00 --- /dev/null +++ b/news/4224.bugfix @@ -0,0 +1,3 @@ +Make referencing IDs of inline rendered SVGs unique. +Fix ``aria-labelledby`` parameter when providing ``tag_alt``. +@petschki