@@ -133,6 +133,13 @@ function makeSession(
133133 if ( focusedFrame ) focusedFrame . copy = keyCopy || focusedFrame . copy ;
134134 else pageCopy = keyCopy || pageCopy ;
135135 }
136+ if (
137+ method === "Input.dispatchMouseEvent" &&
138+ ( input as { type ?: string } ) . type === "mouseReleased"
139+ ) {
140+ if ( focusedFrame ) focusedFrame . copy = clickCopy || focusedFrame . copy ;
141+ else pageCopy = clickCopy || pageCopy ;
142+ }
136143 if ( method === "Runtime.evaluate" && ( input as { contextId ?: number } ) . contextId === 7 ) {
137144 const value = frame ? frame . copy : pageCopy ;
138145 if ( frame ) frame . copy = "" ;
@@ -302,6 +309,7 @@ const contextRequests: Array<{ profileId: string; isolated: boolean }> = [];
302309let contextGate : PromiseWithResolvers < void > | null = null ;
303310let pageCopy = "" ;
304311let keyCopy = "" ;
312+ let clickCopy = "" ;
305313let focusedFrame : ReturnType < typeof makeFrame > | null = null ;
306314let contextFailure : Error | null = null ;
307315let pageSetupFailure : Error | null = null ;
@@ -456,6 +464,7 @@ beforeEach(() => {
456464 initialNavigation = null ;
457465 pageCopy = "" ;
458466 keyCopy = "" ;
467+ clickCopy = "" ;
459468 focusedFrame = null ;
460469 desktopTabs . clear ( ) ;
461470 shownDesktopTabs . clear ( ) ;
@@ -1287,7 +1296,85 @@ it.effect.each([
12871296 ) . pipe ( Effect . provide ( layer ) ) ,
12881297) ;
12891298
1290- it . live ( "only the controlling viewer's copy and cut keys reach its clipboard" , ( ) =>
1299+ it . live ( "clipboard capture expires with the trusted click, even when propagation stops" , ( ) =>
1300+ Effect . scoped (
1301+ Effect . gen ( function * ( ) {
1302+ yield * ready ;
1303+ const session = contexts [ 0 ] ! . sessions [ 0 ] ! ;
1304+ const scripts = session . send . mock . calls . flatMap ( ( [ method , input ] ) =>
1305+ method === "Page.addScriptToEvaluateOnNewDocument"
1306+ ? [ input as { source : string ; worldName ?: string } ]
1307+ : [ ] ,
1308+ ) ;
1309+ const listeners = new Map < string , Array < ( event : object ) => void > > ( ) ;
1310+ const dispatch = ( type : string , event : object ) => {
1311+ for ( const listener of listeners . get ( type ) ?? [ ] ) listener ( event ) ;
1312+ } ;
1313+ const world = ( values : object ) =>
1314+ NodeVM . createContext ( {
1315+ Event : { NONE : 0 } ,
1316+ addEventListener : ( type : string , listener : ( event : object ) => void ) => {
1317+ listeners . set ( type , [ ...( listeners . get ( type ) ?? [ ] ) , listener ] ) ;
1318+ } ,
1319+ ...values ,
1320+ } ) ;
1321+ const isolated = world ( {
1322+ document : { activeElement : null , getSelection : ( ) => "selected" } ,
1323+ HTMLInputElement : String ,
1324+ HTMLTextAreaElement : String ,
1325+ } ) ;
1326+ const main = world ( {
1327+ Clipboard : class {
1328+ writeText ( ) {
1329+ return Promise . resolve ( ) ;
1330+ }
1331+ } ,
1332+ CustomEvent : class {
1333+ readonly type : string ;
1334+ readonly detail : string ;
1335+ constructor ( type : string , options : { detail : string } ) {
1336+ this . type = type ;
1337+ this . detail = options . detail ;
1338+ }
1339+ } ,
1340+ dispatchEvent : ( event : { type : string } ) => dispatch ( event . type , event ) ,
1341+ } ) ;
1342+ for ( const script of scripts )
1343+ NodeVM . runInContext ( script . source , script . worldName ? isolated : main ) ;
1344+ const take = ( ) => NodeVM . runInContext ( "takeCopy()" , isolated ) ;
1345+ const write = ( ) => NodeVM . runInContext ( 'new Clipboard().writeText("button")' , main ) ;
1346+ expect ( NodeVM . runInContext ( "typeof takeCopy" , main ) ) . toBe ( "undefined" ) ;
1347+ const click = { isTrusted : true , eventPhase : 1 } ;
1348+ dispatch ( "click" , click ) ;
1349+ yield * Effect . promise ( write ) ;
1350+ expect ( take ( ) ) . toBe ( "button" ) ;
1351+ dispatch ( "copy" , { isTrusted : true } ) ;
1352+ expect ( take ( ) ) . toBe ( "selected" ) ;
1353+ dispatch ( "copy" , {
1354+ isTrusted : true ,
1355+ defaultPrevented : true ,
1356+ clipboardData : { getData : ( ) => "override" } ,
1357+ } ) ;
1358+ expect ( take ( ) ) . toBe ( "override" ) ;
1359+ click . eventPhase = 0 ;
1360+ yield * Effect . promise ( write ) ;
1361+ dispatch ( "copy" , { isTrusted : true } ) ;
1362+ expect ( take ( ) ) . toBe ( "" ) ;
1363+ click . isTrusted = false ;
1364+ click . eventPhase = 1 ;
1365+ dispatch ( "click" , click ) ;
1366+ yield * Effect . promise ( write ) ;
1367+ dispatch ( "t3-preview-copy-text" , { detail : "redispatched from a timer" } ) ;
1368+ expect ( take ( ) ) . toBe ( "" ) ;
1369+ dispatch ( "click" , { isTrusted : false , eventPhase : 1 } ) ;
1370+ yield * Effect . promise ( write ) ;
1371+ dispatch ( "t3-preview-copy-text" , { detail : "forged outside a click" } ) ;
1372+ expect ( take ( ) ) . toBe ( "" ) ;
1373+ } ) ,
1374+ ) . pipe ( Effect . provide ( layer ) ) ,
1375+ ) ;
1376+
1377+ it . live ( "only the controlling viewer's copy keys and clicks offer clipboard text" , ( ) =>
12911378 Effect . scoped (
12921379 Effect . gen ( function * ( ) {
12931380 const { browser, tabId } = yield * ready ;
@@ -1319,6 +1406,19 @@ it.live("only the controlling viewer's copy and cut keys reach its clipboard", (
13191406 keyCopy = "" ;
13201407 yield * viewer . input ( { type : "key" , action : "down" , key : "c" , code : "KeyC" , modifiers : 4 } ) ;
13211408 expect ( yield * clipboard ( viewer . output ) ) . toEqual ( [ ] ) ;
1409+ clickCopy = "button copy" ;
1410+ yield * watcher . input ( { type : "mouse" , action : "up" , button : "left" , x : 10 , y : 10 } ) ;
1411+ expect ( yield * clipboard ( watcher . output ) ) . toEqual ( [ ] ) ;
1412+ yield * viewer . input ( { type : "mouse" , action : "down" , button : "left" , x : 10 , y : 10 } ) ;
1413+ expect ( yield * clipboard ( viewer . output ) ) . toEqual ( [ ] ) ;
1414+ yield * viewer . input ( { type : "mouse" , action : "up" , button : "left" , x : 10 , y : 10 } ) ;
1415+ expect ( yield * clipboard ( viewer . output ) ) . toEqual ( [
1416+ { _tag : "clipboard" , text : "button copy" , requireGesture : true } ,
1417+ ] ) ;
1418+ clickCopy = "" ;
1419+ pageCopy = "stale agent click" ;
1420+ yield * viewer . input ( { type : "mouse" , action : "up" , button : "left" , x : 10 , y : 10 } ) ;
1421+ expect ( yield * clipboard ( viewer . output ) ) . toEqual ( [ ] ) ;
13221422 expect ( yield * clipboard ( watcher . output ) ) . toEqual ( [ ] ) ;
13231423 } ) ,
13241424 ) . pipe ( Effect . provide ( layer ) ) ,
0 commit comments