clipboard-apis icon indicating copy to clipboard operation
clipboard-apis copied to clipboard

Make it possible to read image/svg+xml from the clipboard

Open thriolsson opened this issue 5 years ago • 19 comments

I get the impression here https://w3c.github.io/clipboard-apis/#reading-from-clipboard that the clipboard format image/svg+xml should be available when reading a data transfer object in the paste event. But I cannot get this to work. I have verified on Windows that there is indeed an image/svg+xml format on the clipboard, but I cannot see any traces of this when inspecting the clipboardData object in Chrome 75.0.3770.100. Have I misunderstood how this is supposed to work or is it simply not implemented in Chrome?

document.addEventListener('paste', async function (event) { event.preventDefault(); console.log(event.clipboardData.types); let clipText = event.clipboardData.getData("image/svg+xml"); console.log(clipText); }

thriolsson avatar Jul 11 '19 13:07 thriolsson

What’s the actual input you are trying to copy? That is, how is the svg embedded in the HTML document. Is it inline svg? And img tag? Or object?

marcoscaceres avatar Jul 11 '19 13:07 marcoscaceres

It is an SVG image copied from another program to the clipboard. This should be the same xml as is in a .svg file. It works fine to paste into other programs (Word, InkSkape etc.), but writing a clipboard handler in the browser for this format does not seem to be possible. The image format is not listed by the clipboardData element. From the specification, it is my impression that this is how it is supposed to work. The format is listed on https://w3c.github.io/clipboard-apis/#reading-from-clipboard, I cannot find any traces of it in Chromium here https://cs.chromium.org/chromium/src/third_party/blink/renderer/core/clipboard/clipboard_mime_types.cc?sq=package:chromium&g=0

thriolsson avatar Jul 11 '19 13:07 thriolsson

Interesting! And it doesn’t work in any browser? I wonder if svg is excluded because it can contain scripts?

marcoscaceres avatar Jul 11 '19 13:07 marcoscaceres

I have tested only on IE, Edge and Chrome. Don't know if it makes sense to exclude this because of scripts. It is just XML and it depends on what you do with the content. With that reasoning, text on the clipboard can also contain scripts. Adaption of SVG is quite slow, which is a pity. I guess it is because it is not supported well in many tools. It looks like Office can paste SVG in the latest version, but popular apps like Adobe Illustrator and InDesign requires a plug-in to paste SVG (unless it has changed recently). I have not seen any web pages where you can paste SVG, which I suspect is because of the reason I posted here.

thriolsson avatar Jul 11 '19 16:07 thriolsson

It seems reasonable to allow SVG. We strip away all scripts & event handlers from HTML anyway so we'd do the same here for SVG if any.

rniwa avatar Jul 12 '19 22:07 rniwa

The primary concern with SVG is with how to sanitize it. If stripping out the scripts is acceptable from both the user and security perspective, then that sgtm.

garykac avatar Jul 12 '19 23:07 garykac

@garykac : FWIW, WebKit strips away all scripts and event handlers for any cross-origin & cross-app copy & paste for HTML, and don't expose any raw RTF / RTFD content. See our blog post for the details.

In general, whenever we leave scripts or event handlers in the pasted HTML, we end up getting security bugs somewhere in our engine or XSS bugs on some websites so it's simply not unattainable to leave them in the pasted content. Furthermore, some native applications tend to put privacy & security sensitive information like the full physical address of the user and the path to app's privacy container in the system into the contents in the system pasteboard / clipboard, so some kind of sanitization step to remove any content that's not directly visible to the user is necessary from privacy perspective as well.

rniwa avatar Jul 13 '19 00:07 rniwa

It sounds like a great idea to have the same sanitation of clipboard formats like HTML that webkit has. It would be nice to have this in Chromium and also for SVG.

thriolsson avatar Jul 14 '19 06:07 thriolsson

It's also impossible to write image/svg+xml to the clipboard. It always throws a SecurityError that the MIME type is no allowed, which is really unfortunate.

felixfbecker avatar Nov 15 '20 14:11 felixfbecker

it seems to no longer throw an error for image/svg+xml but when testing it did not let me paste it into powerpoint or the finder (while it works fine with a png)

I tested via this code

setTimeout(async () => {
try {
  const imgURL = 'test.svg';  // using test.png works 
  const data = await fetch(imgURL);
  const blob = await data.blob();
  await navigator.clipboard.write([
    new ClipboardItem({
      [blob.type]: blob
    })
  ]);
  console.log('Image copied.');
} catch(e) {
  console.error(e, e.message);
}
}, 1000);

is that supposed to work?

tested in chrome 95

daKmoR avatar Nov 10 '21 22:11 daKmoR

Followed up with the @daKmoR via Twitter DM. Here's my response:

Copying SVGs works. See my demo at https://async-clipboard-demo.glitch.me and check the console. See how the SVG is there, alongside the PNG and plain text source code of the SVG? The problem is more the receiving app. It takes whatever works, and how it does that is an implementation detail. You can try flipping the order and put the SVG first, so if the receiving app can work with SVGs and uses whatever works first, then you might get lucky.

image

tomayac avatar Nov 11 '21 12:11 tomayac

I tested it now in sketch, teams desktop app, twitter web, slack web app... as soon as you add the image/svg+xml it will "freak" out and try to paste the big svg text and freeze for a few seconds...

svgomg indeed works fine as it will use the text and is ready for bigger texts.

if you only use image/svg+xml in the clipboard nothing happens when you past it (tested the same apps as before). svgomg will also throw an error "pasted value not an svg"... I assume because as it expects a string and not an actual svg

so far I could not find a single app that could handle image/svg+xml... 😅

Does anyone know of a good example?

daKmoR avatar Nov 11 '21 12:11 daKmoR

Hi @tomayac

There is an unshipped implementation in Chrome, can you help push it to ship?

yisibl avatar Oct 31 '23 09:10 yisibl

I tested my demo again that I also blogged about a while ago, and it still works, albeit only with the chrome://flags/#enable-experimental-web-platform-features set.

Screenshot 2023-10-31 at 12 42 12

Let me ask internally where we are with shipping this—apparently—ready to go implementation.

tomayac avatar Oct 31 '23 11:10 tomayac

Let me ask internally where we are with shipping this—apparently—ready to go implementation.

That's great!

There is one issue that needs further discussion before shipping: https://bugs.chromium.org/p/chromium/issues/detail?id=1226150

I don't think it's too much of a problem even if the sanitization process deletes the styles in the <style> tags, because many apps don't support <style>, e.g. <style> is lost when you paste it into Keynote 13.1 for editing.

There are two solutions:

  1. the developer converts the styles in style to SVG attributes.
  2. for 1, let the browser automate the process.

This results in better compatibility with SVG copied to the clipboard.

yisibl avatar Oct 31 '23 12:10 yisibl

  1. See my comment on the bug for a possible third option.

tomayac avatar Oct 31 '23 12:10 tomayac

  1. See my comment on the bug for a possible third option.

Yeah, I made the same request here https://github.com/w3c/clipboard-apis/pull/197#issuecomment-1786694922.

yisibl avatar Oct 31 '23 12:10 yisibl

@yisibl Would you be able to provide more details on your scenario for this API?

sanketj avatar Nov 02 '23 19:11 sanketj

It is not clear to me which format name should be used for SVG on each platform in https://w3c.github.io/clipboard-apis/#to-os-specific-well-known-format and https://w3c.github.io/clipboard-apis/#to-well-known-mime-type-from-os-specific-format. It would be beneficial if we could also define that.

EdgarChen avatar May 30 '24 11:05 EdgarChen

@EdgarChen Looks like we missed adding the SVG image mime type in those algorithms. I can make a change to add the OS formats and the SVG MIME type.

snianu avatar May 30 '24 17:05 snianu