Skip to content

SVG: draw a <use> that references another <use>, or a later element - #2193

Merged
andersonhc merged 5 commits into
py-pdf:masterfrom
RavSinghChandan:svg-use-of-use
Nov 14, 2026
Merged

andersonhc merged 5 commits into
py-pdf:masterfrom
RavSinghChandan:svg-use-of-use

Conversation

@RavSinghChandan

Copy link
Copy Markdown

A <use> that references another <use> fails with ValueError: use ... references nonexistent ref id #use3396. Inkscape writes these for a clone of a clone, e.g. in the flags of the Vatican and the British Indian Ocean Territory in mledoze/countries, the cubemap figure of the WebGPU spec and the demo image of picnic.

Two things were missing:

  • build_xref() never registered a <use> under its own id, and handle_defs() skipped a <use> in <defs>.
  • Outside <defs>, a <use> could only reference an element built before it. In the British Indian Ocean Territory flag, use3398 references use3396, which comes after it. Such an element is now built when it is first referenced, apart from its ancestors, as <defs> content already is.

test_svg_use_of_use covers a clone of a clone in the document, in <defs>, and one that references a later clone; all fail without the change. The five files above now convert.

A <use> was never registered under its own id, and a <use> in <defs> was
skipped, so a clone of a clone, as Inkscape writes them, failed with
ValueError: use ... references nonexistent ref id.
Outside <defs>, a <use> could only reference an element already built.
Build a later element when it is first referenced, as for <defs>, so a
clone that comes before the clone it references is drawn too.

@andersonhc andersonhc left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I found one small edge case: when a references a hidden element, build_xref() returns before registering the clone’s own ID. Another referencing that clone then fails with “nonexistent ref id.”

<defs>
  <rect id="hidden" display="none" width="10" height="10"/>
  <use id="clone" href="#hidden"/>
</defs>
<use href="#clone"/>

Could we register the empty clone before returning and add a regression test? This should render nothing without raising an error.

@andersonhc
andersonhc merged commit b5e7c83 into py-pdf:master Nov 14, 2026
23 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants