[svg] fix duplicate svg ids

This commit is contained in:
Jan Petykiewicz 2026-04-01 20:57:35 -07:00
commit a82365ec8c
2 changed files with 47 additions and 3 deletions

View file

@ -68,3 +68,31 @@ def test_svg_ref_mirroring_changes_affine_transform(tmp_path: Path) -> None:
assert_allclose(plain_transform, (0, 2, -2, 0, 3, 4), atol=1e-10)
assert_allclose(mirrored_transform, (0, 2, 2, 0, 3, 4), atol=1e-10)
def test_svg_uses_unique_ids_for_colliding_mangled_names(tmp_path: Path) -> None:
lib = Library()
first = Pattern()
first.polygon("1", vertices=[[0, 0], [1, 0], [0, 1]])
lib["a b"] = first
second = Pattern()
second.polygon("1", vertices=[[0, 0], [2, 0], [0, 2]])
lib["a-b"] = second
top = Pattern()
top.ref("a b")
top.ref("a-b", offset=(5, 0))
lib["top"] = top
svg_path = tmp_path / "colliding_ids.svg"
svg.writefile(lib, "top", str(svg_path))
root = ET.fromstring(svg_path.read_text())
ids = [group.attrib["id"] for group in root.iter(f"{SVG_NS}g")]
hrefs = [use.attrib[XLINK_HREF] for use in root.iter(f"{SVG_NS}use")]
assert ids.count("a_b") == 1
assert len(set(ids)) == len(ids)
assert "#a_b" in hrefs
assert "#a_b_2" in hrefs