MCPcopy Create free account
hub / github.com/SLiCAP/SLiCAP_python / _rename_svg_ids

Function _rename_svg_ids

SLiCAP/schematic/export.py:498–521  ·  view source on GitHub ↗

Prefix all id attributes and internal #-references throughout an ET tree. Handles both href/xlink:href fragment references and url(#...) references that appear in presentation attributes such as marker-start, marker-end, fill, stroke, clip-path, filter, mask, and inline style strings.

(element, prefix: str)

Source from the content-addressed store, hash-verified

496
497
498def _rename_svg_ids(element, prefix: str) -> None:
499 """Prefix all id attributes and internal #-references throughout an ET tree.
500
501 Handles both href/xlink:href fragment references and url(#...) references
502 that appear in presentation attributes such as marker-start, marker-end,
503 fill, stroke, clip-path, filter, mask, and inline style strings.
504 """
505 xhref = f"{{{_XLINK_NS}}}href"
506
507 def _rewrite_url(val: str) -> str:
508 return _URL_REF_RE.sub(lambda m: f"url(#{prefix}{m.group(1)})", val)
509
510 for el in element.iter():
511 id_val = el.get("id")
512 if id_val:
513 el.set("id", prefix + id_val)
514 for attr, val in list(el.attrib.items()):
515 if not val:
516 continue
517 if attr in (xhref, "href"):
518 if val.startswith("#"):
519 el.set(attr, "#" + prefix + val[1:])
520 elif "url(#" in val:
521 el.set(attr, _rewrite_url(val))
522
523
524def _text_block(parent, item, color, fs, family, font=None):

Callers 2

_inline_image_svgFunction · 0.85
_inline_latex_svgFunction · 0.85

Calls 1

_rewrite_urlFunction · 0.85

Tested by

no test coverage detected