![Taylore Givens](https://web.dev/images/authors/tayloregivens.jpg) Taylore Givens [LinkedIn](https://www.linkedin.com/in/taylore-g-604648187/) ![Thomas Steiner](https://web.dev/images/authors/thomassteiner.jpg) Thomas Steiner [GitHub](https://github.com/tomayac) [LinkedIn](https://www.linkedin.com/in/thomassteinerlinkedin) [Mastodon](https://toot.cafe/@tomayac) [Bluesky](https://bsky.app/profile/tomayac.com) [Homepage](https://blog.tomayac.com/)

<br />

The
[`Clipboard` interface](https://developer.mozilla.org/docs/Web/API/Clipboard) of
the Async Clipboard API provides read and write access to the contents of the
system clipboard. This lets a web application implement cut, copy, and
paste features. You can paste data from the clipboard into an application by
calling the
[`read()`](https://developer.mozilla.org/docs/Web/API/Clipboard/read) method,
and copy data into the clipboard by calling the
[`write()`](https://developer.mozilla.org/docs/Web/API/Clipboard/write) method.
Apart from [text](https://web.dev/articles/async-clipboard#writetext),
[images](https://web.dev/articles/async-clipboard#write) in Portable Network
Graphics (PNG) format, sanitized and
[unsanitized HTML](https://developer.chrome.com/docs/web-platform/unsanitized-html-async-clipboard.md.txt), and
[Web custom formats](https://developer.chrome.com/blog/web-custom-formats-for-the-async-clipboard-api.md.txt), the
Async Clipboard API now also supports copying and pasting SVG images, which
means you can finally interact with image editing software that deals with SVGs
more naturally by copying and pasting SVG images as images instead of as text or
using [hacky work arounds](https://superuser.com/a/1509797).

## Feature detect SVG support

Detect support for SVG images (and any other MIME type) by calling the
static `ClipboardItem.supports()` method, passing it the wanted MIME type.

```javascript
const supportsSVG = () => {
  if (
    !('supports' in window.ClipboardItem) ||
    !window.ClipboardItem.supports('image/svg+xml')
  ) {
    return false;
  }
  return true;
};
```

## Copy an SVG image

Copy an SVG image by populating the
[`ClipboardItem`](https://developer.mozilla.org/docs/Web/API/ClipboardItem) with
an object. The blob with the SVG image data is the value and the blob's type
(that is, `'image/svg+xml'` in this case) as its key.

```javascript
copyButton.addEventListener('click', async () => {
  if (!supportsSVG()) {
    return;
  }
  try {
    const blob = await fetch(img.src).then((response) => response.blob());
    await navigator.clipboard.write([
      new window.ClipboardItem({
        [blob.type]: blob,
      }),
    ]);
  } catch (err) {
    console.error(err.name, err.message);
    alert(err.message);
  }
});
```

## Paste an SVG image

To paste an SVG image, read the `ClipboardItem` back from the clipboard, and
get the wanted type (that is, `'image/svg+xml'` in this case) with the
`getType()` method. This returns a blob that, once converted to a blob URL,
you can assign to the `src` attribute of an `<img>`.

```javascript
pasteButton.addEventListener('click', async () => {
  if (!supportsSVG()) {
    return;
  }
  const [clipboardItem] = await navigator.clipboard.read();
  const svgBlob = await clipboardItem.getType('image/svg+xml');
  if (!svgBlob) {
    alert('No SVG in the clipboard.');
    return;
  }
  const image = document.createElement('img');
  const blobURL = URL.createObjectURL(svgBlob);
  image.addEventListener('load', () => {
    URL.revokeObjectURL(blobURL);
  });
  image.src = blobURL;
});
```

## Sanitization

SVG is a powerful format that, for example, allows embedded scripts. This can be
dangerous when the user pastes content from unknown sources, so the
browser runs a sanitization step. When data is copied, the Async Clipboard API
produces a well-formed SVG document and then writes it to the clipboard. When
data is pasted, a strictly processed SVG fragment is produced by the fragment
parser. Therefore, before the paste operation, the `onclick` event handler
attributes are still there, but upon pasting, they get removed.
![Clipboard Viewer app on macOS inspecting the clipboard contents. It shows
that the onclick event listener attributes in the SVG are still there.](https://developer.chrome.com/static/blog/svg-support-for-async-clipboard-api/svgsupportfort--jlkftlu5qa.png) Clipboard Viewer app on macOS inspecting the clipboard contents. It shows that the onclick event listener attributes in the SVG are still there.

## Demo

Explore copying and pasting SVGs in the
[demo](https://chrome.dev/svg-copy-paste/). View the
[source code](https://github.com/GoogleChrome/samples/tree/gh-pages/glitch-migration/svg-copy-paste)
to see how it works. Be sure to try clicking any of the circles before and after
copying and pasting. After pasting, the potentially dangerous `onclick` event
handler attributes are removed.
<iframe src="https://chrome.dev/svg-copy-paste/" allow="clipboard-write;
clipboard-read" style="width: 100%; height: 1000px;"></iframe>

## Related links

- Production software using this feature:
  - [Boxy SVG](https://boxy-svg.com/app)
  - [SVGcode](https://svgco.de/)
- [ChromeStatus entry](https://chromestatus.com/feature/5125790490427392)
- [Intent to Ship](https://groups.google.com/a/chromium.org/g/blink-dev/c/KYN1ToIjCsM/m/blyE-F-bAQAJ)
- [Chromium bug](https://g-issues.chromium.org/issues/40708648)
- [WebKit Standards position](https://lists.webkit.org/pipermail/webkit-dev/2021-August/031940.html)
- [Mozilla Standards position](https://github.com/mozilla/standards-positions/issues/549)

## Acknowledgements

SVG support for the Async Clipboard API in Chromium was implemented by the
Microsoft Edge team. This post was reviewed by Rachel Andrew and Anupam Snigdha.