Class PasteEvent

java.lang.Object
com.vaadin.flow.component.clipboard.PasteEvent
All Implemented Interfaces:
Serializable

public final class PasteEvent extends Object implements Serializable
Server-side representation of a browser paste event delivered to a listener registered with Clipboard.onPaste.

The event carries the two textual MIME types the browser exposes on event.clipboardData: text/plain via getText() and text/html via getHtml(). Either may be null when the paste did not include that representation. getTargetElement() resolves to the closest Flow-tracked Element ancestor of the paste's DOM target.

Empty vs. absent

The browser returns the empty string "" both when the requested MIME type is not present on the clipboard and when the user pasted an empty string of that type. There is no way to distinguish the two. PasteEvent collapses both into null, so callers can simply check event.hasHtml() (or event.getHtml() != null) without an extra !isEmpty() guard.

What is not on it

Files and binary clipboard items (pasted screenshots, files dragged from the OS file picker, anything that arrives as event.clipboardData.files or a DataTransferItem of kind "file") are not delivered by this event. File-paste support is tracked separately.

Browser caveats

  • The browser only fires paste when the target element is focused at the moment the user invokes paste. Non-editable elements (such as a plain Div) need to be made focusable — typically via tabindex="0" — before they will receive paste events.
  • On editable targets (<input>, <textarea>, elements with contenteditable), the browser still performs its native paste insertion. onPaste does not call preventDefault(); the listener observes the paste, it does not replace it.
  • On Safari, some plain-text pastes do not include a text/html representation even when Chromium would synthesise a wrapper, so getHtml() may be null on Safari for the same paste that yields a non-null HTML value on Chrome.
The listener runs on the UI thread.
See Also:
  • Method Summary

    Modifier and Type
    Method
    Description
    @Nullable String
    Returns the text/html content of the paste, or null if the paste did not include a non-empty HTML representation.
    Returns the component the paste listener was registered on — the component passed to Clipboard.onPaste.
    @Nullable Element
    Returns the closest Flow-tracked Element ancestor of the browser's paste target.
    @Nullable String
    Returns the text/plain content of the paste, or null if the paste did not include a non-empty plain-text representation.
    boolean
    Returns whether the paste included a non-empty text/html representation.
    boolean
    Returns whether the paste included a non-empty text/plain representation.

    Methods inherited from class java.lang.Object

    clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
  • Method Details

    • getSource

      public Component getSource()
      Returns the component the paste listener was registered on — the component passed to Clipboard.onPaste. For listeners registered against a UI this is the UI itself.
      Returns:
      the source component, never null
    • getTargetElement

      public @Nullable Element getTargetElement()
      Returns the closest Flow-tracked Element ancestor of the browser's paste target. The ancestor walk is performed by the browser against the live DOM (not the server-side state tree), so the result reflects the DOM hierarchy the user actually pasted into. For pastes inside a Vaadin web component this is typically the web component's host element, not an internal shadow-DOM child.

      Use Element.getComponent() to look up the enclosing Component, if any. Returns null only in the rare case that no Flow-tracked element encloses the paste target in the DOM.

      Returns:
      the closest enclosing Flow-tracked element, or null if Flow could not resolve one
    • getText

      public @Nullable String getText()
      Returns the text/plain content of the paste, or null if the paste did not include a non-empty plain-text representation.
      Returns:
      the pasted plain text, or null
    • getHtml

      public @Nullable String getHtml()
      Returns the text/html content of the paste, or null if the paste did not include a non-empty HTML representation.
      Returns:
      the pasted HTML, or null
    • hasText

      public boolean hasText()
      Returns whether the paste included a non-empty text/plain representation.
      Returns:
      true if getText() is non-null
    • hasHtml

      public boolean hasHtml()
      Returns whether the paste included a non-empty text/html representation.
      Returns:
      true if getHtml() is non-null