Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Specify order of flavors exported to macOS's pasteboard #137

Open
mbrodesser opened this issue Feb 16, 2021 · 14 comments
Open

Specify order of flavors exported to macOS's pasteboard #137

mbrodesser opened this issue Feb 16, 2021 · 14 comments
Labels

Comments

@mbrodesser
Copy link

When exporting images to the clipboard (via right-click and "Copy Image"), Chrome and Firefox currently order the flavors differently on macOS's pasteboard.

Chrome adds:

public.tiff
NeXT TIFF v4.0 pasteboard type
public.html
APPLE HTML pasteboard type

Firefox adds:

public.html
Apple HTML pasteboard type
public.tiff
NeXT TiFF v4.0 pasteboard type

Despite Apple's Pasteboard Programming Guide requiring the pasteboard readers to not rely on the order:

"A pasteboard reader must find the data type that best suits its capabilities (if any). Typically, this means selecting the richest type available. In the previous example, a rich text editor might provide RTFD, RTF, and NSString representations of copied data. An application that supports rich text but without images should retrieve the RTF representation; an application that only supports plain text should retrieve the NSString object, whereas an image-editing application might not be able to use the text at all."

some applications, like Pages rely on it. See here.

So for compatibility, the order should be specified.

@annevk
Copy link
Member

annevk commented Feb 16, 2021

cc @rniwa @whsieh

@rniwa
Copy link

rniwa commented Feb 16, 2021

Ah, yes. We had to deal with this problem specifically with Pages. The justification of their behavior comes from:
https://developer.apple.com/documentation/appkit/nspasteboard/1533561-declaretypes?language=objc

The types should be ordered according to the preference of the source application, with the most preferred type coming first (typically, the richest representation).

That Pasteboard Programming Guide's mentioning of preferring the most fluid / rich content type is relatively new thing. It happened as a result of continuity pasteboard since iOS's UIPasteboard didn't use to allow specifying the ordering between types.

This ordering issue is precisely what prompted us to suggest that the types in ClipboardItem should be ordered.

Now, I'm not certain that there is much we can do in terms of specification since this issue is very much platform dependent. We have a bunch of heuristics to determine in what order we want to write stuff into the pasteboard with various compatibility hacks but that's not something we want to standardize as we can't even agree on what to do in macOS vs. iOS.

@rniwa
Copy link

rniwa commented Feb 16, 2021

I'm gonna just drop this thing right here for ourselves... rdar://problem/35158037

@mbrodesser
Copy link
Author

Ah, yes. We had to deal with this problem specifically with Pages. The justification of their behavior comes from:
https://developer.apple.com/documentation/appkit/nspasteboard/1533561-declaretypes?language=objc

The types should be ordered according to the preference of the source application, with the most preferred type coming first (typically, the richest representation).

That Pasteboard Programming Guide's mentioning of preferring the most fluid / rich content type is relatively new thing. It happened as a result of continuity pasteboard since iOS's UIPasteboard didn't use to allow specifying the ordering between types.

This ordering issue is precisely what prompted us to suggest that the types in ClipboardItem should be ordered.

Now, I'm not certain that there is much we can do in terms of specification since this issue is very much platform dependent. We have a bunch of heuristics to determine in what order we want to write stuff into the pasteboard with various compatibility hacks but that's not something we want to standardize as we can't even agree on what to do in macOS vs. iOS.

Which compatibility hacks?

It would be nice specify this somehow. Perhaps even OS specific? Otherwise, it's hard for browsers to stay compatible.

@rniwa
Copy link

rniwa commented Feb 27, 2021

Ah, yes. We had to deal with this problem specifically with Pages. The justification of their behavior comes from:
https://developer.apple.com/documentation/appkit/nspasteboard/1533561-declaretypes?language=objc

The types should be ordered according to the preference of the source application, with the most preferred type coming first (typically, the richest representation).

That Pasteboard Programming Guide's mentioning of preferring the most fluid / rich content type is relatively new thing. It happened as a result of continuity pasteboard since iOS's UIPasteboard didn't use to allow specifying the ordering between types.
This ordering issue is precisely what prompted us to suggest that the types in ClipboardItem should be ordered.
Now, I'm not certain that there is much we can do in terms of specification since this issue is very much platform dependent. We have a bunch of heuristics to determine in what order we want to write stuff into the pasteboard with various compatibility hacks but that's not something we want to standardize as we can't even agree on what to do in macOS vs. iOS.

Which compatibility hacks?

It would be nice specify this somehow. Perhaps even OS specific? Otherwise, it's hard for browsers to stay compatible.

I don't think that makes much sense. Maybe it cold be an informal note somewhere but it certainly doesn't belong in an official W3C specification for the clipboard API. Having said that, you can take a look at https://trac.webkit.org/browser/webkit/trunk/Source/WebCore/platform/mac/PlatformPasteboardMac.mm and all the changes we made to that file in recent years.

@yisibl
Copy link

yisibl commented Nov 1, 2023

@tomayac We also need to look into this before Chrome ships the copied SVG.

@snianu Can you confirm that the current implementation in Chrome supports ordering ClipboardItem?
I tested this on a Mac and found that when adding image/svg+xml and text/plain at the same time, text/plain always came first, which prevented Apps such as Pages from pasting SVG correctly.

image

@snianu
Copy link
Contributor

snianu commented Nov 1, 2023

@snianu Can you confirm that the current implementation in Chrome supports ordering ClipboardItem?

In Chromium, the ordering of formats is defined here. Currently we ignore the ordering of formats in the ClipboardItems.

@yisibl
Copy link

yisibl commented Nov 2, 2023

@snianu From a simple approach, can we put SvgData in front of TextData? This is also in line with the recommended practice in the operating system, where SVG has richer information.

@yisibl
Copy link

yisibl commented Nov 5, 2023

Microsoft's development documentation has similar instructions:

Clipboard formats that contain the most information should be placed on the clipboard first, followed by less descriptive formats. A window pasting information from the clipboard typically retrieves a clipboard object in the first format it recognizes. Because clipboard formats are enumerated in the order they are placed on the clipboard, the first recognized format is also the most descriptive.

https://learn.microsoft.com/en-us/windows/win32/dataxchg/clipboard-formats#multiple-clipboard-formats

@yisibl
Copy link

yisibl commented Feb 22, 2024

It is difficult for the browser to determine which format is richer in information. For example, when copying image/svg+xml and text/html at the same time, which type is richer? This is ambiguous, so it should be left to the developer to decide based on their usage scenario.

In my testing, I found that Safari has the ability to customize the order in which information is written to the clipboard based on the type the developer passes in the ClipboardItem.

For better interoperability, the specification may be able to move in that direction..

@snianu
Copy link
Contributor

snianu commented Feb 22, 2024

Adding @evanstade to this thread as well. I agree that maintaining order in the system clipboard is important for native apps to pick the most desired format during paste. On Windows, the guidance is to place the high fidelity formats at the top:

Clipboard formats that contain the most information should be placed on the clipboard first, followed by less descriptive formats. A window pasting information from the clipboard typically retrieves a clipboard object in the first format it recognizes. Because clipboard formats are enumerated in the order they are placed on the clipboard, the first recognized format is also the most descriptive.

https://learn.microsoft.com/en-us/windows/win32/dataxchg/clipboard-formats#multiple-clipboard-formats

I'm not sure if we can standardize the ordering of the formats in the system clipboard, but perhaps we can maintain the order of the formats in the ClipboardItem as specified by the web authors during navigator.clipboard.write?

@yisibl
Copy link

yisibl commented Feb 23, 2024

I'm not sure if we can standardize the ordering of the formats in the system clipboard, but perhaps we can maintain the order of the formats in the ClipboardItem as specified by the web authors during navigator.clipboard.write?

Yes, Safari currently does that.

navigator.clipboard.write([
    new ClipboardItem({
        // Adjusting the order here in Safari will change the order in which it is written to the clipboard
        'image/png': new Promise(async resolve => {
            const response = await fetch("https://upload.wikimedia.org/wikipedia/commons/8/89/PNG-Gradient.png");
            resolve(await response.blob());
        }),
        'text/plain': new Promise(resolve => {
            const blob = new Blob([text], { type: 'text/plain' });
            resolve(blob);
        }),
    })
]);

@snianu
Copy link
Contributor

snianu commented Feb 23, 2024

Added Agenda+ tag to discuss in the EditingWG meeting.

@css-meeting-bot
Copy link
Member

The Web Editing Working Group just discussed https://github.com/w3c/clipboard-apis/issues/137.

The full IRC log of that discussion <dandclark> topic: https://github.com//issues/137
<dandclark> github: https://github.com//issues/137
<dandclark> snianu: All 3 browsers have different behaviors
<dandclark> ...: Want to standardize the order that formats get written to clipboard
<dandclark> ...: Clients tend to read topmost format, expect that to have highest fidelity
<whsieh> q+
<dandclark> ...: Chromium has fixed order, I think FF order is fixed but it seems to be wrong
<dandclark> ...: Custom formats seem to get on the top, image formats come after HTML
<dandclark> ...: Safari I haven't tested personally, someone said it preserves formats of web author
<dandclark> whsieh: We do preserve order, that's intentional
<dandclark> ...: Article you pointed out is old. Modern APIs use NSItemProvider where order does matter. Same as Windows, highest fidelity are on the top. Devs are expected to go through types in order of fideliety
<dandclark> ...: Idea is that richest supported format is the one they should consume
<dandclark> ...: In Safari we preserve order for that purpose
<dandclark> snianu: If the web authors write img after text/plain, and the user pastes in native app, does the it get reordered?
<dandclark> whsieh: We preserve order in that case
<dandclark> snianu: I wanted to discuss that; I don't know if web authors are aware that order is important. Would be good to standardize it. Should we define fixed order?
<dandclark> ...: Or leave it up to web authors?
<dandclark> smaug: Should leave it up to the webpage authors, following what Safari's doing. Don't know why Firefox is doing something else, it's some legacy behavior
<dandclark> snianu: FF had some surprising results
<dandclark> smaug: chromium's order is also strange
<dandclark> ...: We should follow Safari's model here
<dandclark> snianu: If we do that, does it affect DataTRansfer APIs?
<dandclark> ...: In Chromium we have fixed order for those, would be hard to have different order based on APIs being used by web authors
<dandclark> ...: In browser process where we interact with sys clipboard, can't know if we should preserve order or the legacy behavior
<dandclark> ...: So that could cause breakage if we change it
<dandclark> ...: Original issue was opened because of image pasting issues in Pages and Keynote.
<dandclark> ...: I only heard reports of Pages failures with Firefox. Has anyone investigated why it's failing?
<dandclark> ...: Someone commented on issue that order is the reason the paste failed. But my experience testing Office and Windows stuff, order matters when pasting images and plaintext.
<dandclark> ...: I don't see issues with paste, svg is still pasted in native app
<dandclark> smaug: I wonder if in Windows apps just do different things vs MacOS
<dandclark> smaug: On Windows it's worked the same since Win32
<dandclark> (That last comment was snianu, not smaug)
<dandclark> smaug: We should investigate if we can follow Safari's model elsewhere, see what breaks. Hard to know otherwise.
<dandclark> snianu: Proposal is to make changes in nightly build in both APIs? Worried about changing DataTransfer. I'm sure there will be breakage in Office.
<dandclark> smaug: What's the behavior in Safari for DataTransfer?
<dandclark> whsieh: Not sure, I think we respect order, if we don't we should.
<dandclark> ...: as we try to align DataTransfer to more modern async clipboard
<dandclark> johanneswilm: Are we proposing spec changes?
<dandclark> snianu: Spec says order is specified by web author.
<dandclark> ...: but spec is iterating clipboard item, doesn't define any order there.
<dandclark> ...: This order is more from system/OS perspective, if you mess that up then native app reads lower fidelity format
<dandclark> johanneswilm: So that's what we're sticking with, FF will try this out
<dandclark> smaug: It can't be just us
<dandclark> ...: Needs to be all browsers on Windows. Also don't know about Linux
<dandclark> snianu: Couldn't find Linux guidance
<dandclark> ...: MacOS & Windows, there are specific guidelines that richest format goes on top
<dandclark> smaug: Could be tricky to change if apps rely on specific behavior on Win
<dandclark> sanketj_: Is it defined about which formats are the most important?
<dandclark> smaug: I don't know which is more important. Would expect maybe HTML because it has more semantic info vs image
<dandclark> whsieh: But if you're copying single image...It's context dependent.
<dandclark> johanneswilm: What are we expecting from this meeting?
<dandclark> snianu: Was wondering if there's real issue here. If no then we're hesitant to change anything since this is very fragile.
<dandclark> ...: If we break copy/paste with old office apps, very hard to fix
<dandclark> ...: I initially proposed to keep Safari's behavior, because that's how the spec is written, but thought deeply about how to make the change in Chromium, ran into this issue that we can't figure what order the author chose
<dandclark> ...: From my own experience here it's very hard to change.
<dandclark> Christine Hollingsworth: Don't have a formal opinion, would be nice to standardize but only if reasonably possible
<dandclark> smaug: Can keep issue open but not be very active
<dandclark> sanketj_: If we mark it as closed, say we'll never do it. Leaving it open says we might do it in future. What new data would be needed such that we might do it ?
<dandclark> smaug: More testing, trying Safari's behaivor on Windows
<dandclark> sanketj_: Seems like it was a FF bug, pasting into Pages. Are you fixing that bug?
<dandclark> smaug: That was fixed years ago, looking at what was changed. Looks like the order was changed
<dandclark> ...: But it's not a generic solution, not following Safari's model. Order is still fixed.
<dandclark> sanketj_: You take image to be highest pri?
<dandclark> smaug: At least on mac
<dandclark> sanketj_: Can you confirm what's your order on different platforms? We could get inventory on different orders browsers have today on different platforms
<dandclark> ...: Based on that we could figure out what we need to change to all be spec compliant
<dandclark> smaug: Sounds good
<dandclark> ...: snianu , smaug , whsieh to document these in the issue for their respective browsers

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

7 participants