Inkscape generates conflicting IDs for elements without IDs
Summary:
When an element does not have an id attribute, Inkscape will generate one as the document is loaded, but this ID may conflict with a different ID found later in the document. This may result in <use> referencing the wrong element.
Steps to reproduce:
- Open attached "a.svg".
- Observe that the square at the bottom is red.
What happened?
The <use> element at the bottom referenced the red square, because that group didn't have an ID, and Inkscape assigned it g1, in conflict with the existing g1 group. Inkscape screenshot below.
What should have happened?
The bottom square should be green. The <use> element should continue to reference the already existing g1 group, and the group that is lacking an ID attribute should be assigned a new unique ID. Screenshots from Firefox and Chrome are attached below.
...
Sample attachments:
The problem has to do with the structure of this input:
<svg>
<g inkscape:label="Layer 1">
<!-- Inkscape will parse this element first, but it's missing ID. -->
<!-- Inkscape will assign "g1" to this group. -->
<g>
<rect style="fill:#ff0000" />
</g>
</g>
<g inkscape:label="Layer 2">
<!-- When Inkscape encounters this group, it will rename it to "g2". -->
<g id="g1">
<rect style="fill:#00ff00" />
</g>
<!-- This element is referencing the original green "g1", -->
<!-- but Inkscape thinks it's referencing the new red "g1". -->
<use xlink:href="#g1" />
</g>
</svg>
Inkscape always saves all elements with unique IDs, so this is not an issue that users are likely to encounter. The reason I observed it is because I have tools that insert extra elements to SVGs. The fix is relatively straightforward: have those tools also assign unique id attributes to all the elements they inserted.
Still, SVG spec does not require all elements to have IDs, so I am filing this issue in case if this is something worth addressing in Inkscape. At the very least, this issue documents the behavior so that future authors knows how to fix their tools.
Version info
Inkscape 1.4-beta2 (9aaf4f3498, 2024-07-30)
Compile (Run)
GLib version: 2.80.4
GTK version: 3.24.43 (3.24.43)
glibmm version: 2.66.7
gtkmm version: 3.24.9
libxml2 version: 2.12.9
libxslt version: 1.1.42
Cairo version: 1.18.0 (1.18.0)
Pango version: 1.54.0 (1.54.0)
HarfBuzz version: 9.0.0 (9.0.0)
OS version: Windows 10 22H2

