Learning center

Typed text works but pasted text fails: capture the clipboard payload

When typing a value works and pasting the same value fails, the page did not receive the same thing. A paste can carry several formats at once, and the page may read one the reporter never saw. Capture what the clipboard delivered, and the maintainer can replay the paste exactly.

What a paste delivers#

A paste event carries a DataTransfer with one or more types. Copying from a spreadsheet, a word processor or a web page usually puts text/plain and text/html on the clipboard, sometimes more. The page's handler, or the browser's default for a rich-text editor, decides which one to use. MDN: ClipboardEvent.clipboardData

We dispatched a paste in Chromium with a plain-text value of 1 followed by a no-break space and 234, and an HTML value of <span style="color:red">1&nbsp;234</span>. A handler that listed clipboardData.types received both, in that order. A field that reads text/plain gets a no-break space, U+00A0, that looks like a normal space. An editor that reads text/html gets the span and its style.

Capture the payload#

Ask the reporter to paste into a page with a logging handler, or run this in the Console on the failing page and then paste into the field:

document.addEventListener('paste', e => {
  const types = e.clipboardData.types;
  console.log(types.map(t => [t, JSON.stringify(e.clipboardData.getData(t))]));
}, { capture: true, once: true });

JSON.stringify shows escapes such as   and \t where the screen shows only spaces. For a plain-text value, also list its code points.

Put the output in the report as code, and say where the text was copied from: application, version and what was selected.

Replay the paste#

The maintainer can dispatch the same event in a test or the Console, with the captured types:

const dt = new DataTransfer();
dt.setData('text/plain', '1 234');
dt.setData('text/html', '<span style="color:red">1&nbsp;234</span>');
field.dispatchEvent(new ClipboardEvent('paste', { clipboardData: dt, bubbles: true, cancelable: true }));

A synthetic event is untrusted, so it runs the page's paste handlers with that data but the browser does not insert the text itself. If the bug is in the browser's own insertion rather than the page's handler, put the captured text on the real clipboard and paste with the keyboard shortcut in a visible browser.

Payloads that commonly fail#

Copied fromWhat arrivesTypical failure
Spreadsheet cellTrailing newline or tab, sometimes a number formatted with a locale's separatorsValidation rejects a trailing \n; 1.234,50 parsed as 1.234
Word processorCurly quotes, no-break spaces, text/html with stylesSearch fails to match; rich editor imports fonts and colors
Web pagetext/html with links, hidden elements and spansEditor keeps hidden text; link markup in a plain field
PDFLine breaks within sentences, and from some files ligatures such as fi (U+FB01)Words split; search for "fi" fails

Worked example: an IBAN that failed validation#

This example is fictional. A customer pastes an IBAN from their bank's PDF statement and the form says it is invalid. Support types the same IBAN and it passes.

The customer runs the paste logger and sends the output. text/plain holds the IBAN with three   between its four-character groups. The form removed ordinary spaces before checking the IBAN but not no-break spaces. The maintainer replayed the paste with a DataTransfer holding the captured plain text and saw the same error. The fix removed all whitespace characters, and the captured payload became a test case.

Questions#

Can I read the clipboard without pasting?

navigator.clipboard.read() can, after the user grants permission, and it is limited to secure contexts. A paste event is simpler because it needs no permission and shows exactly what the page received.

Should I send the HTML payload?

Send it when the field is a rich-text editor, because the editor reads it. Remove personal data from it the same way as from the text.

Does drag and drop behave the same way?

Drop events carry a DataTransfer too, often with the same types. Log e.dataTransfer in a drop handler the same way.

Return to reproducing what someone else saw for the other layers a replay needs.

One month of free code review.

New organizations get one calendar month of hosted code review for private and public GitHub repositories. No card or cloud connection required. Eligibility and fair-use terms.

Start free with GitHub

Related reading