Markpad icon indicating copy to clipboard operation
Markpad copied to clipboard

Copying a sentence out of the preview puts ~19 MB on the clipboard

Open PathGao opened this issue 3 months ago • 2 comments

What was measured

Selecting about ten characters in the preview pane and copying them, then reading the macOS pasteboard:

$ osascript -e 'clipboard info'
«class weba»   19,107,758      ← WebArchive
«class RTF »          422
«class HTML»          856
«class utf8»           10      ← the text that was actually selected

Ten bytes of text, nineteen megabytes of clipboard. The document was samples/link-test.md, which is 805 bytes.

Why

The preview is a web page, and WKWebView's copy for web content writes a WebArchive alongside the plain text. A WebArchive embeds the page's subresources, so what is being copied is not the selection and not even the document — it is the app's rendering environment. The document being 805 bytes and the payload being 19 MB says the size comes almost entirely from what is around the text: stylesheets, fonts, whatever else the page has pulled in (build/_app/immutable/assets alone is 1.5 MB, and Monaco's codicon font is another 119 KB).

This is not new and not a regression — it is the platform default for copying out of a webview, and #548 does not touch the preview's copy path.

Why it may still be worth doing something about

  • Pasting into an app that reads the rich flavour has to parse all of it. On a large document this is the sort of thing that reads as "the app hung".
  • The clipboard is global. Whatever is on it stays on it, in memory, for every application.
  • The privacy shape is odd: copying one sentence hands the receiving application a snapshot of the app's own page.

Why it is not simply switched off

Copying from the preview with formatting is correct. A reader in reading mode who copies a paragraph and pastes it into a mail or a document should get the bold and the headings — that is the point of the preview. The editor is the opposite case, and is already plain-text-only through Rust (#207, #548).

So the question is not "plain or rich" but whether the rich flavour has to be a WebArchive. text/html at 856 bytes carries the formatting; the WebArchive carries the environment and is what costs 19 MB. If WKWebView can be asked for one without the other, that is the whole fix.

Not yet established

  • Whether the 19 MB scales with the document or is roughly constant (dominated by app assets). One measurement on an 805-byte document is not enough to say, and samples/stress-test.md at 121 KB is the obvious second data point.
  • Whether wry or Tauri exposes any control over the pasteboard types a webview writes, or whether this needs an NSPasteboard intercept.
  • Windows and Linux: measured on macOS only. WebArchive is a WebKit format; WebView2 and WebKitGTK will do something else, and it may not have this shape at all.

Reproducing

samples/link-test.md is in the repo. Open it, stay in preview, select a few words, copy, and run osascript -e 'clipboard info'.

Device

macOS, dev build of master at 3a3bd55.

PathGao avatar Aug 08 '26 10:08 PathGao

Measured both builds. The 19MB is a tauri dev artefact and does not exist in a shipped build — but the plain-text clipboard a shipped build does produce is an accident of the URL scheme rather than anything this app decided, so #551 makes it a decision.

Why the archive is 19MB

Not the stylesheets or the fonts. addSubresourcesForCSSStyleSheetsIfNecessary returns early unless options.mainResourceFileName is set, and it is only set when a .webarchive is being written to disk — never for a copy. Fonts only arrive via getSubresourceURLs() on the selected nodes. build/_app/immutable/assets never enters the archive.

It is the scripts. LegacyWebArchive::createFromSelection → createInternal (LegacyWebArchive.cpp:752):

if (options.shouldSaveScriptsFromMemoryCache == Yes && responseURL.protocolIsInHTTPFamily()) {
    RegistrableDomain domain { responseURL };
    MemoryCache::singleton().forEachSessionResource(sessionID, [&](auto& resource) {
        if (domain.matches(resource.url()) && resource.hasClients()
            && (resource.type() == Script || resource.type() == JSON))
            subresourceURLs.add(resource.url());
    });
}

Every script in the memory cache for the page's registrable domain, for any selection, regardless of what is selected. A WebArchive is meant to be a replayable page, so scripts belong in it; the same code serves "Save As WebArchive" and "copy selection", and only the stylesheet half distinguishes them.

Why dev and not a shipped build

protocolIsInHTTPFamily().

build origin http family sweep
npm run tauri dev http://localhost:1420 (tauri.conf.json devUrl) yes runs
tauri build tauri://localhost no skipped

On macOS get_app_url falls through to tauri_protocol_url when frontendDist is a path (tauri-2.10.2/src/manager/mod.rs:331). And under dev the sweep has far more to collect: Vite serves the module graph unbundled and unminified — node_modules/.vite/deps is 54MB, monaco's ESM tree is 1,227 modules.

Same 805-byte samples/link-test.md, same gesture, both builds:

tauri build --debug   «class utf8» 70                                          (no weba)
tauri dev             «class weba» 19,081,919  RTF 616  HTML 603  utf8 79

Note tauri build --debug is a valid control: it does not set cfg(dev), so it gets tauri://localhost like a release bundle.

Answering the three open questions

Does it scale with the document? No, and not with the selection either. Two selections in the same 805-byte file:

utf8 79  ->  weba 19,081,919
utf8 73  ->  weba 19,081,913

The archive shrank by exactly the six bytes the text did — the only selection-proportional part is the main resource, ~the size of the text/html flavour. samples/stress-test.md at 121,794 bytes gives the same number. (The original reading here, 19,107,758 for a 10-byte selection, differs by ~26KB, which is memory-cache state drifting between sessions, not content.)

Does wry or Tauri expose control over the pasteboard types? No. In wry 0.54.2 the only pasteboard reference under wkwebview/ is read-only — draggingPasteboard() for file drops. WryWebView overrides performKeyEquivalent:, acceptsFirstMouse:, the dragging-destination methods and otherMouseDown/Up:; nothing clipboard-adjacent. WebViewAttributes::clipboard is read only under webkitgtk/ and webview2/. WKWebViewConfiguration has no pasteboard-flavour knob. An ObjC interposition via with_webview is possible — swizzling copy: or validRequestorForSendType: on objc_getClass("WryWebView"), plus pinned objc2* deps — and it is strictly worse than cancelling the DOM copy event, which reaches the branch WebKit checks first (Editor::copy → tryDHTMLCopy, before performCutOrCopy builds anything).

Windows and Linux? No equivalent. Pasteboard.h:88 keeps dataInWebArchiveFormat under PLATFORM(COCOA); the GTK/WPE struct carries text and markup only. WebView2 is Chromium: CF_UNICODETEXT + a small CF_HTML fragment, no page-snapshot flavour.

On the framing

Copying from the preview with formatting is correct.

Worth recording that this is not what the app does, and was not before #548 either. The preview's right-click ▸ Copy has always gone through clipboard_write_text (plain text), and in a shipped build ⌘C and Edit ▸ Copy produce no rich flavour at all — so a reader pasting into Mail today gets plain text by every route. #551 keeps that and makes it explicit rather than a property of the scheme.

Whether the preview should paste with formatting is a real question, but it is a change in behaviour rather than this bug, and it should be decided on its own.

PathGao avatar Aug 08 '26 11:08 PathGao

Fixed in #551 (a71d4d7).

To restate the resolution, since it is not the one this issue proposed: the 19MB does not exist in a shipped build — it needs an http origin, which only tauri dev has. What #551 changes for a shipped build is nothing observable; it makes the plain-text clipboard a decision of this app's rather than a side effect of the custom scheme, so an upstream change cannot quietly reintroduce the archive. And it removes the divergence, so a clipboard measured under tauri dev now matches what users get.

Left open deliberately: whether the preview should paste with formatting. Today it does not, by every route, and that predates this issue — it is a change in behaviour rather than a fix, and worth its own issue if wanted.

PathGao avatar Aug 08 '26 11:08 PathGao