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)
| 496 | |
| 497 | |
| 498 | def _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 | |
| 524 | def _text_block(parent, item, color, fs, family, font=None): |
no test coverage detected