Skip to content

FIX: URL links in SVG should have target='_blank'#31578

Merged
QuLogic merged 1 commit into
matplotlib:mainfrom
timhoffm:svg-url-target
May 11, 2026
Merged

FIX: URL links in SVG should have target='_blank'#31578
QuLogic merged 1 commit into
matplotlib:mainfrom
timhoffm:svg-url-target

Conversation

@timhoffm

@timhoffm timhoffm commented Apr 28, 2026

Copy link
Copy Markdown
Member

When embedding SVG in HTML, link hrefs can target different browsing contexts. This e.g. leads to links inside SVGs in #31497 replacing the SVG image instead of opening a new browser window.

The solution is to set target="_blank" for all links that implement Artist.get_url(). This is always intended as an external link, so universally adding target="_blank" is justified.

Background info:
https://developer.mozilla.org/en-US/docs/Web/SVG/Reference/Attribute/target
https://developer.mozilla.org/en-US/docs/Web/SVG/Reference/Attribute/target#_blank

@dstansby

Copy link
Copy Markdown
Member

This is a bit of a change in behaviour - I tried the SVG url demo example, and before this PR the link opens in the same window, and after it opens in a new window. Using _top instaed of _blank would be less of a change in behaviour. OTOH I would personally expect/choose opening in a new tab, so I'm open to this change.

It should definitely get a release note though, and optionally this might be a good oppurtunity to de-duplicate at least {'xlink:href': gc.get_url(), 'target': '_blank'} by defining it at the top of backend_svg.py somewhere?

When embedding SVG in HTML, link `href`s can target different browsing
contexts. This e.g. leads to links inside SVGs in matplotlib#31497 replacing the
SVG image instead of opening a new browser window.

The solution is to set `target="_blank"` for all links that implement
`Artist.get_url()`. This is always intended as an external link, so
universally adding `target="_blank"` is justified.

Background info:
https://developer.mozilla.org/en-US/docs/Web/SVG/Reference/Attribute/target
https://developer.mozilla.org/en-US/docs/Web/SVG/Reference/Attribute/target#_blank
@timhoffm

Copy link
Copy Markdown
Member Author

I've add a behavior change message.

I'm not convinced de-duplicating the link code is worth it. Writing a function for it doesn't make it significantly shorter, and the indirection IMHO rather reduces readability. The only advantage would be easier and more consistent changes, but since I don't expect this code to be touched a lot, that doesn't weigh strongly.

@timhoffm

Copy link
Copy Markdown
Member Author

Could / should this still go into 3.11 if we make another RC?

dstansby
dstansby approved these changes May 1, 2026
@timhoffm

Copy link
Copy Markdown
Member Author

Marking as 3.11 as it would be good to get this in.

@QuLogic feel free to re-milestone if this would otherwise block moving on with 3.11.

@timhoffm timhoffm added this to the v3.11.0 milestone May 10, 2026
@QuLogic QuLogic merged commit 2d21987 into matplotlib:main May 11, 2026
39 of 41 checks passed
QuLogic added a commit that referenced this pull request May 11, 2026
…578-on-v3.11.x

Backport PR #31578 on branch v3.11.x (FIX: URL links in SVG should have target='_blank')
@timhoffm timhoffm deleted the svg-url-target branch May 11, 2026 23:35
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants