Class PasteEvent
java.lang.Object
com.vaadin.flow.component.clipboard.PasteEvent
- All Implemented Interfaces:
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 asevent.clipboardData.files or
a DataTransferItem of kind "file") are not delivered by this
event. Register file pastes through
Clipboard.onFilePaste instead; the same paste gesture fires both listeners
when both are registered.
Browser caveats
- The browser only fires
pastewhen the target element is focused at the moment the user invokes paste. Non-editable elements (such as a plainDiv) need to be made focusable — typically viatabindex="0"— before they will receive paste events. - On editable targets (
<input>,<textarea>, elements withcontenteditable), the browser still performs its native paste insertion.onPastedoes not callpreventDefault(); the listener observes the paste, it does not replace it. - On Safari, some plain-text pastes do not include a
text/htmlrepresentation even when Chromium would synthesise a wrapper, sogetHtml()may benullon Safari for the same paste that yields a non-null HTML value on Chrome.
- See Also:
-
Method Summary
Modifier and TypeMethodDescription@Nullable StringgetHtml()Returns thetext/htmlcontent of the paste, ornullif the paste did not include a non-empty HTML representation.Returns the component the paste listener was registered on — the component passed toClipboard.onPaste.@Nullable ElementReturns the closest Flow-trackedElementancestor of the browser's paste target.@Nullable StringgetText()Returns thetext/plaincontent of the paste, ornullif the paste did not include a non-empty plain-text representation.booleanhasHtml()Returns whether the paste included a non-emptytext/htmlrepresentation.booleanhasText()Returns whether the paste included a non-emptytext/plainrepresentation.
-
Method Details
-
getSource
Returns the component the paste listener was registered on — the component passed toClipboard.onPaste. For listeners registered against aUIthis is the UI itself.- Returns:
- the source component, never
null
-
getTargetElement
Returns the closest Flow-trackedElementancestor 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 enclosingComponent, if any. Returnsnullonly in the rare case that no Flow-tracked element encloses the paste target in the DOM.- Returns:
- the closest enclosing Flow-tracked element, or
nullif Flow could not resolve one
-
getText
Returns thetext/plaincontent of the paste, ornullif the paste did not include a non-empty plain-text representation.- Returns:
- the pasted plain text, or
null
-
getHtml
Returns thetext/htmlcontent of the paste, ornullif 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-emptytext/plainrepresentation.- Returns:
trueifgetText()is non-null
-
hasHtml
public boolean hasHtml()Returns whether the paste included a non-emptytext/htmlrepresentation.- Returns:
trueifgetHtml()is non-null
-