File tree 8 files changed +61
-23
lines changed
8 files changed +61
-23
lines changed Original file line number Diff line number Diff line change @@ -11,11 +11,12 @@ export async function copy(this: Instance) {
11
11
return
12
12
}
13
13
14
- this . dispatchUIEvent ( target , 'copy' , {
15
- clipboardData,
16
- } )
17
-
18
- if ( this [ Config ] . writeToClipboard ) {
14
+ if (
15
+ this . dispatchUIEvent ( target , 'copy' , {
16
+ clipboardData,
17
+ } ) &&
18
+ this [ Config ] . writeToClipboard
19
+ ) {
19
20
await writeDataTransferToClipboard ( doc , clipboardData )
20
21
}
21
22
Original file line number Diff line number Diff line change 1
1
import { Config , Instance } from '../setup'
2
- import {
3
- copySelection ,
4
- input ,
5
- isEditable ,
6
- writeDataTransferToClipboard ,
7
- } from '../utils'
2
+ import { copySelection , writeDataTransferToClipboard } from '../utils'
8
3
9
4
export async function cut ( this : Instance ) {
10
5
const doc = this [ Config ] . document
@@ -16,16 +11,13 @@ export async function cut(this: Instance) {
16
11
return
17
12
}
18
13
19
- this . dispatchUIEvent ( target , 'cut' , {
20
- clipboardData,
21
- } )
22
-
23
- if ( isEditable ( target ) ) {
24
- input ( this [ Config ] , target , '' , 'deleteByCut' )
25
- }
26
-
27
- if ( this [ Config ] . writeToClipboard ) {
28
- await writeDataTransferToClipboard ( doc , clipboardData )
14
+ if (
15
+ this . dispatchUIEvent ( target , 'cut' , {
16
+ clipboardData,
17
+ } ) &&
18
+ this [ Config ] . writeToClipboard
19
+ ) {
20
+ await writeDataTransferToClipboard ( target . ownerDocument , clipboardData )
29
21
}
30
22
31
23
return clipboardData
Original file line number Diff line number Diff line change
1
+ import { input , isEditable } from '../../utils'
2
+ import { behavior } from './registry'
3
+
4
+ behavior . cut = ( event , target , config ) => {
5
+ return ( ) => {
6
+ if ( isEditable ( target ) ) {
7
+ input ( config , target , '' , 'deleteByCut' )
8
+ }
9
+ }
10
+ }
Original file line number Diff line number Diff line change 1
1
import './click'
2
+ import './cut'
2
3
import './keydown'
3
4
import './keypress'
4
5
import './keyup'
Original file line number Diff line number Diff line change @@ -93,7 +93,7 @@ export function addListeners(
93
93
eventHandlerCalls = [ ]
94
94
}
95
95
96
- function eventWasFired ( eventType : keyof GlobalEventHandlersEventMap ) {
96
+ function eventWasFired ( eventType : keyof DocumentEventMap ) {
97
97
return getEvents ( eventType ) . length > 0
98
98
}
99
99
Original file line number Diff line number Diff line change @@ -53,6 +53,22 @@ test('copy on empty selection does nothing', async () => {
53
53
expect ( getEvents ( ) ) . toHaveLength ( 0 )
54
54
} )
55
55
56
+ test ( 'prevent default behavior per event handler' , async ( ) => {
57
+ const { element, eventWasFired, getEvents, user} = setup (
58
+ `<input value="bar"/>` ,
59
+ {
60
+ selection : { anchorOffset : 0 , focusOffset : 3 } ,
61
+ } ,
62
+ )
63
+ element . addEventListener ( 'copy' , e => e . preventDefault ( ) )
64
+ await window . navigator . clipboard . writeText ( 'foo' )
65
+
66
+ await user . copy ( )
67
+ expect ( eventWasFired ( 'copy' ) ) . toBe ( true )
68
+ expect ( getEvents ( 'copy' ) [ 0 ] . clipboardData ?. getData ( 'text' ) ) . toBe ( 'bar' )
69
+ await expect ( window . navigator . clipboard . readText ( ) ) . resolves . toBe ( 'foo' )
70
+ } )
71
+
56
72
describe ( 'without Clipboard API' , ( ) => {
57
73
beforeEach ( ( ) => {
58
74
Object . defineProperty ( window . navigator , 'clipboard' , {
Original file line number Diff line number Diff line change @@ -60,6 +60,23 @@ test('cut on empty selection does nothing', async () => {
60
60
expect ( getEvents ( ) ) . toHaveLength ( 0 )
61
61
} )
62
62
63
+ test ( 'prevent default behavior per event handler' , async ( ) => {
64
+ const { element, eventWasFired, getEvents, user} = setup (
65
+ `<input value="bar"/>` ,
66
+ {
67
+ selection : { anchorOffset : 0 , focusOffset : 3 } ,
68
+ } ,
69
+ )
70
+ element . addEventListener ( 'cut' , e => e . preventDefault ( ) )
71
+ await window . navigator . clipboard . writeText ( 'foo' )
72
+
73
+ await user . cut ( )
74
+ expect ( eventWasFired ( 'cut' ) ) . toBe ( true )
75
+ expect ( getEvents ( 'cut' ) [ 0 ] . clipboardData ?. getData ( 'text' ) ) . toBe ( 'bar' )
76
+ expect ( eventWasFired ( 'input' ) ) . toBe ( false )
77
+ await expect ( window . navigator . clipboard . readText ( ) ) . resolves . toBe ( 'foo' )
78
+ } )
79
+
63
80
describe ( 'without Clipboard API' , ( ) => {
64
81
beforeEach ( ( ) => {
65
82
Object . defineProperty ( window . navigator , 'clipboard' , {
Original file line number Diff line number Diff line change 44
44
expect ( getEvents ( 'input' ) [ 0 ] ) . toHaveProperty ( 'inputType' , inputType )
45
45
if ( expectedValue !== undefined ) {
46
46
expect ( element ) . toHaveValue ( expectedValue )
47
- } else if ( expectedHtml !== undefined ) {
47
+ }
48
+ if ( expectedHtml !== undefined ) {
48
49
expect ( element ) . toHaveProperty ( 'innerHTML' , expectedHtml )
49
50
}
50
51
} ,
You can’t perform that action at this time.
0 commit comments