Skip to content

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

Open
timhoffm wants to merge 1 commit intomatplotlib:mainfrom
timhoffm:svg-url-target
Open

FIX: URL links in SVG should have target='_blank'#31578
timhoffm wants to merge 1 commit intomatplotlib:mainfrom
timhoffm:svg-url-target

Conversation

@timhoffm
Copy link
Copy Markdown
Member

@timhoffm timhoffm commented Apr 28, 2026

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

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
@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?

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.

2 participants