Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions doc/api/next_api_changes/behavior/31578-TH.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
SVG links open in new tab or window
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
`.Artist.set_url` allows to turn the Artist into a link. In SVG output,
this has been implemented without specifying a `target`_ attribute. The default
target value "_self" resulted in replacing the SVG document with the linked page
when the link was clicked.

The target is now set to "_blank" so that the link opens in a new tab or window.

.. _target: https://developer.mozilla.org/en-US/docs/Web/SVG/Reference/Attribute/target
10 changes: 5 additions & 5 deletions lib/matplotlib/backends/backend_svg.py
Original file line number Diff line number Diff line change
Expand Up @@ -697,7 +697,7 @@ def draw_path(self, gc, path, transform, rgbFace=None):
sketch=gc.get_sketch_params())

if gc.get_url() is not None:
self.writer.start('a', {'xlink:href': gc.get_url()})
self.writer.start('a', {'xlink:href': gc.get_url(), 'target': '_blank'})
self.writer.element('path', d=path_data, **self._get_clip_attrs(gc),
style=self._get_style(gc, rgbFace))
if gc.get_url() is not None:
Expand Down Expand Up @@ -730,7 +730,7 @@ def draw_markers(

writer.start('g', **self._get_clip_attrs(gc))
if gc.get_url() is not None:
self.writer.start('a', {'xlink:href': gc.get_url()})
self.writer.start('a', {'xlink:href': gc.get_url(), 'target': '_blank'})
trans_and_flip = self._make_flip_transform(trans)
attrib = {'xlink:href': f'#{oid}'}
clip = (0, 0, self.width*72, self.height*72)
Expand Down Expand Up @@ -788,7 +788,7 @@ def draw_path_collection(self, gc, master_transform, paths, all_transforms,
antialiaseds, urls, offset_position, hatchcolors=hatchcolors):
url = gc0.get_url()
if url is not None:
writer.start('a', attrib={'xlink:href': url})
writer.start('a', attrib={'xlink:href': url, 'target': '_blank'})
clip_attrs = self._get_clip_attrs(gc0)
if clip_attrs:
writer.start('g', **clip_attrs)
Expand Down Expand Up @@ -966,7 +966,7 @@ def draw_image(self, gc, x, y, im, transform=None):

url = gc.get_url()
if url is not None:
self.writer.start('a', attrib={'xlink:href': url})
self.writer.start('a', attrib={'xlink:href': url, 'target': '_blank'})

attrib = {}
oid = gc.get_gid()
Expand Down Expand Up @@ -1288,7 +1288,7 @@ def draw_text(self, gc, x, y, s, prop, angle, ismath=False, mtext=None):
self.writer.start('g', **clip_attrs)

if gc.get_url() is not None:
self.writer.start('a', {'xlink:href': gc.get_url()})
self.writer.start('a', {'xlink:href': gc.get_url(), 'target': '_blank'})

if mpl.rcParams['svg.fonttype'] == 'path':
self._draw_text_as_path(gc, x, y, s, prop, angle, ismath, mtext)
Expand Down
2 changes: 1 addition & 1 deletion lib/matplotlib/tests/test_backend_svg.py
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ def test_text_urls():
fig.savefig(fd, format='svg')
buf = fd.getvalue().decode()

expected = f'<a xlink:href="{test_url}">'
expected = f'<a xlink:href="{test_url}" target="_blank">'
assert expected in buf


Expand Down
Loading