Skip to content

Commit f4801c3

Browse files
committed
closes #2738
1 parent b09e38c commit f4801c3

File tree

1 file changed

+31
-12
lines changed
  • 2-ui/4-forms-controls/3-events-change-input

1 file changed

+31
-12
lines changed

2-ui/4-forms-controls/3-events-change-input/article.md

Lines changed: 31 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -58,31 +58,50 @@ So we can't use `event.preventDefault()` there -- it's just too late, there woul
5858
5959
These events occur on cutting/copying/pasting a value.
6060
61-
They belong to [ClipboardEvent](https://www.w3.org/TR/clipboard-apis/#clipboard-event-interfaces) class and provide access to the data that is copied/pasted.
61+
They belong to [ClipboardEvent](https://www.w3.org/TR/clipboard-apis/#clipboard-event-interfaces) class and provide access to the data that is cut/copied/pasted.
6262
6363
We also can use `event.preventDefault()` to abort the action, then nothing gets copied/pasted.
6464
65-
For instance, the code below prevents all such events and shows what we are trying to cut/copy/paste:
65+
For instance, the code below prevents all `cut/copy/paste` events and shows the text we're trying to cut/copy/paste:
6666
6767
```html autorun height=40 run
6868
<input type="text" id="input">
6969
<script>
70-
input.oncut = input.oncopy = input.onpaste = function(event) {
71-
alert(event.type + ' - ' + event.clipboardData.getData('text/plain'));
72-
return false;
70+
input.onpaste = function(event) {
71+
alert("paste: " + event.clipboardData.getData('text/plain'));
72+
event.preventDefault();
73+
};
74+
75+
input.oncut = input.oncopy = function(event) {
76+
alert(event.type + '-' + document.getSelection());
77+
event.preventDefault();
7378
};
7479
</script>
7580
```
7681

77-
Please note, that it's possible to copy/paste not just text, but everything. For instance, we can copy a file in the OS file manager, and paste it.
82+
Please note: inside `cut` and `copy` event handlers a call to `event.clipboardData.getData(...)` returns an empty string. That's because technically the data isn't in the clipboard yet. If we use `event.preventDefault()` it won't be copied at all.
7883

79-
That's because `clipboardData` implements `DataTransfer` interface, commonly used for drag'n'drop and copy/pasting. It's bit beyond our scope now, but you can find its methods [in the specification](https://html.spec.whatwg.org/multipage/dnd.html#the-datatransfer-interface).
84+
So the example above uses `document.getSelection()` to get the selected text. You can find more details about document selection in the article <info:selection-range>.
8085

81-
```warn header="ClipboardAPI: user safety restrictions"
82-
The clipboard is a "global" OS-level thing. So most browsers allow read/write access to the clipboard only in the scope of certain user actions for the safety, e.g. in `onclick` event handlers.
86+
It's possible to copy/paste not just text, but everything. For instance, we can copy a file in the OS file manager, and paste it.
8387

84-
Also it's forbidden to generate "custom" clipboard events with `dispatchEvent` in all browsers except Firefox.
85-
```
88+
That's because `clipboardData` implements `DataTransfer` interface, commonly used for drag'n'drop and copy/pasting. It's bit beyond our scope now, but you can find its methods in the [DataTransfer specification](https://html.spec.whatwg.org/multipage/dnd.html#the-datatransfer-interface).
89+
90+
Also, there's an additional asynchronous API of accessing the clipboard: `navigator.clipboard`. More about it in the specification [Clipboard API and events](https://www.w3.org/TR/clipboard-apis/), [not supported by Firefox](https://caniuse.com/async-clipboard).
91+
92+
### Safety restrictions
93+
94+
The clipboard is a "global" OS-level thing. A user may switch between various applications, copy/paste different things, and a browser page shouldn't see all that.
95+
96+
So most browsers allow seamless read/write access to the clipboard only in the scope of certain user actions, such as copying/pasting etc.
97+
98+
It's forbidden to generate "custom" clipboard events with `dispatchEvent` in all browsers except Firefox. And even if we manage to dispatch such event, the specification clearly states that such "syntetic" events must not provide access to the clipboard.
99+
100+
Even if someone decides to save `event.clipboardData` in an event handler, and then access it later -- it won't work.
101+
102+
To reiterate, [event.clipboardData](https://www.w3.org/TR/clipboard-apis/#clipboardevent-clipboarddata) works solely in the context of user-initiated event handlers.
103+
104+
On the other hand, [navigator.clipboard](https://www.w3.org/TR/clipboard-apis/#h-navigator-clipboard) is the more recent API, meant for use in any context. It asks for user permission, if needed. Not supported in Firefox.
86105

87106
## Summary
88107

@@ -92,4 +111,4 @@ Data change events:
92111
|---------|----------|-------------|
93112
| `change`| A value was changed. | For text inputs triggers on focus loss. |
94113
| `input` | For text inputs on every change. | Triggers immediately unlike `change`. |
95-
| `cut/copy/paste` | Cut/copy/paste actions. | The action can be prevented. The `event.clipboardData` property gives read/write access to the clipboard. |
114+
| `cut/copy/paste` | Cut/copy/paste actions. | The action can be prevented. The `event.clipboardData` property gives access to the clipboard. All browsers except Firefox also support `navigator.clipboard`. |

0 commit comments

Comments
 (0)