Letras Diferentes
Unicode research · clipboard MIME representations

Clipboard text/plain vs text/html: MIME Types, Sanitization and Fidelity

Compare the plain-text and HTML clipboard representations, ClipboardEvent and async Clipboard API behavior, sanitization boundaries and interoperable copy/paste design.

Technical referenceStandards-aware implementation guide

A clipboard entry can carry multiple representations of the same selection. Plain text preserves characters without markup, while HTML can preserve elements and formatting. Applications may choose, sanitize or transform representations when pasting, so developers must treat clipboard MIME types as separate data contracts rather than one invisible string.

Clipboard items can expose multiple representations

The Clipboard API and events specification identifies text/plain, text/html and image data as important standardized types. A copied table may be represented as tab-delimited text, an HTML table and perhaps an image, each serving a different destination. The formats are not required to preserve exactly the same presentation semantics.

A user can paste into a rich editor and retain emphasis, while pasting into a terminal typically yields only plain text. This is not necessarily data corruption. The receiving application chooses a representation appropriate for its context, permissions and security policy.

Inspect ClipboardEvent and DataTransfer

During user-driven copy, cut or paste events, the clipboardData object provides access to transferable representations where allowed. getData('text/plain') requests plain characters; getData('text/html') requests markup. Not every representation is guaranteed to be present in every clipboard item.

When implementing custom handling, verify which event is fired, whether a user gesture exists, and whether calling preventDefault() is required to override native behavior. Preserve sensible native behavior when custom formats are unavailable.

Async Clipboard API has a permission and focus model

The asynchronous navigator.clipboard API generally operates within secure contexts and browser-specific permission or transient-activation rules. The plain text convenience methods are not identical to reading or writing multiple representations with ClipboardItem.

Applications must handle rejection, unavailable formats and differing browser capabilities without assuming that an API succeeding in one tab implies unrestricted clipboard access everywhere. Clipboard reads also carry privacy implications because they may expose content from unrelated applications.

HTML clipboard content is untrusted input

Treat pasted text/html as untrusted markup. Even if a browser sanitizes some clipboard content, an application should enforce its own allowlist and output-encoding policy before insertion. Scripts, event handlers, unsafe URLs or unexpected document fragments should never become executable simply because they arrived via paste.

At the same time, aggressive sanitization can discard intended tables, semantic emphasis and useful Unicode characters. Define the supported formatting subset and verify that the plain-text fallback still communicates meaningful content.

Plain text is not guaranteed to be ASCII

The MIME label text/plain identifies a textual representation, not a constraint to seven-bit ASCII. A plain string may contain combining marks, variation selectors, RTL characters, emoji ZWJ sequences and mathematical alphanumeric symbols. Such characters remain data even when markup is absent.

Plain-text round trips can still change line endings, normalization or invisible characters depending on participating applications. Test byte-level export, scalar-level comparisons and grapheme behavior separately when the workflow requires exact fidelity.

Copying visual styles differs from copying encoded symbols

An HTML fragment can represent bold text with <strong> or a CSS style while its plain representation contains ordinary letters. A string that uses mathematical bold Unicode characters remains those scalars in text/plain, independent of style.

This distinction matters when transferring content to systems that do not accept HTML. An example of styled Unicode text can help illustrate why a visually bold result is not proof that the clipboard stores HTML; inspect actual Unicode scalars and available MIME types.

Newlines, tables and list structures require policy

HTML tables may collapse to tabs and line breaks in plain text; list markers can become literal characters, punctuation or be omitted. HTML line breaks and CSS block layout do not map one-to-one to newline code points. Decide on a deterministic plain-text serialization for content authored in your application.

Use a small matrix: paragraphs, nested lists, tables, whitespace runs, nonbreaking spaces and mixed-direction text. Compare output across browsers and popular destinations. If the application promises exact tabular import, test CSV/TSV handling separately from clipboard text semantics.

Make fallback and paste-as-plain-text explicit

A high-quality rich editor can prefer supported sanitized HTML, fall back to plain text and offer a paste-as-plain-text command. Plain-text-only destinations should not silently interpret markup as live HTML. For unsupported or rejected clipboard API operations, maintain keyboard workflows where feasible.

Describe what your editor preserves: characters, semantic formatting, visual styles, embedded media or exact source bytes. Users are less surprised by differences when the application's interchange policy is documented and tested with realistic content.

Reproducible experiment with fully specified input

Preserve the exact Unicode sequence before running the example. For browser comparisons, record the destination application, browser build, font files if relevant, system locale and any optional editor transformations. The example tests representation-level facts; it does not on its own demonstrate that every browser or native application must produce the same visual output.

// Example in an authorized user-initiated paste handler:
function onPaste(event) {
  const dt = event.clipboardData;
  const plain = dt?.getData('text/plain') ?? '';
  const html = dt?.getData('text/html') ?? '';
  console.log({plain, hasHtml:html.length > 0});
  // Never inject raw html directly into the document.
  // Use an HTML sanitizer with a documented allowlist if rich paste is needed.
}

A reliable technical report separates the original input, the representation delivered to an API and the outcome visible to the user. When a copy/paste or rendering test fails, retain the raw specimen and compare successive representations so that display-only differences do not get mislabeled as encoding changes.

Evidence matrix for clipboard MIME representations

FeatureControlled specimenInterpretation
text/plainUnicode charactersdoes not inherently contain typography markup
text/htmlHTML fragmentrequires sanitization and feature policy
ClipboardEventclipboardDataevent-based access depends on user action
ClipboardItemmultiple representationsnot all browser/OS combinations support all types
paste policyrich / plain fallbackdestination may deliberately discard formatting

The entries above serve distinct roles: some characterize Unicode text data, while others specify font selection, layout, security policy or clipboard representation. Do not convert an environment-specific observation into a claim that Unicode mandates identical visuals or editing behavior across every platform.

Validation protocol for real-world applications

  1. text/plain: Establish a controlled specimen for Unicode characters, record the input at the scalar level, then compare its output in the intended editor or rendering environment. does not inherently contain typography markup. Document the location and circumstances of any transformation instead of treating a screenshot as sufficient evidence.
  2. text/html: Establish a controlled specimen for HTML fragment, record the input at the scalar level, then compare its output in the intended editor or rendering environment. requires sanitization and feature policy. Document the location and circumstances of any transformation instead of treating a screenshot as sufficient evidence.
  3. ClipboardEvent: Establish a controlled specimen for clipboardData, record the input at the scalar level, then compare its output in the intended editor or rendering environment. event-based access depends on user action. Document the location and circumstances of any transformation instead of treating a screenshot as sufficient evidence.
  4. ClipboardItem: Establish a controlled specimen for multiple representations, record the input at the scalar level, then compare its output in the intended editor or rendering environment. not all browser/OS combinations support all types. Document the location and circumstances of any transformation instead of treating a screenshot as sufficient evidence.
  5. paste policy: Establish a controlled specimen for rich / plain fallback, record the input at the scalar level, then compare its output in the intended editor or rendering environment. destination may deliberately discard formatting. Document the location and circumstances of any transformation instead of treating a screenshot as sufficient evidence.

Include adversarial and multilingual input, not only a short English word. Save code-point lists, serialized bytes when available, input method context, observed UI behavior and environment metadata. Repeat after browser, editor, operating-system or font updates; changes in renderer behavior do not imply that source strings were modified.

Frequent failure patterns and how to interpret them

  • Confusing evidence about text/plain: A visual or API observation involving Unicode characters is not a comprehensive test of Unicode integrity. does not inherently contain typography markup. Preserve the input, label the expected behavior and compare two independent stages before deciding whether a regression occurred.
  • Confusing evidence about text/html: A visual or API observation involving HTML fragment is not a comprehensive test of Unicode integrity. requires sanitization and feature policy. Preserve the input, label the expected behavior and compare two independent stages before deciding whether a regression occurred.
  • Confusing evidence about clipboardevent: A visual or API observation involving clipboardData is not a comprehensive test of Unicode integrity. event-based access depends on user action. Preserve the input, label the expected behavior and compare two independent stages before deciding whether a regression occurred.
  • Confusing evidence about clipboarditem: A visual or API observation involving multiple representations is not a comprehensive test of Unicode integrity. not all browser/OS combinations support all types. Preserve the input, label the expected behavior and compare two independent stages before deciding whether a regression occurred.
  • Confusing evidence about paste policy: A visual or API observation involving rich / plain fallback is not a comprehensive test of Unicode integrity. destination may deliberately discard formatting. Preserve the input, label the expected behavior and compare two independent stages before deciding whether a regression occurred.

In corrective workflows, preserve the user's original text until the responsible transformation has been identified. Avoid blanket removal of combining marks, variation selectors, joiners or directional controls: each can carry useful meaning in its intended context. If a product offers cleanup, make the behavior explicit and reversible.

Primary standards and implementation documentation

The references distinguish Unicode character properties and algorithms from browser APIs, CSS layout and particular implementation strategies. Normative conformance is narrower than identical visual output; document assumptions whenever a standard permits implementation-dependent behavior.

  • W3C Clipboard API and Events — normative definitions or authoritative technical documentation relevant to clipboard MIME representations. Check the precise implementation conditions and distinguish platform behavior from the standard's guarantee.
  • MDN ClipboardEvent.clipboardData — normative definitions or authoritative technical documentation relevant to clipboard MIME representations. Check the precise implementation conditions and distinguish platform behavior from the standard's guarantee.
  • MDN Clipboard API — normative definitions or authoritative technical documentation relevant to clipboard MIME representations. Check the precise implementation conditions and distinguish platform behavior from the standard's guarantee.
  • WHATWG HTML editing model — normative definitions or authoritative technical documentation relevant to clipboard MIME representations. Check the precise implementation conditions and distinguish platform behavior from the standard's guarantee.

The Clipboard, Editors & Markup virtual silo groups practical investigations without adding category pages. The following already-published references cover adjacent layers needed to interpret this problem.

In a reproducible bug report, always preserve the exact text fixture and record the implementation version. The difference between an actual data transformation and a change of presentation determines both the correct fix and the risk of introducing further loss.