DragSourceChildContext.sys.mjs (3695B)
1 /* -*- indent-tabs-mode: nil; js-indent-level: 2 -*- */ 2 /* This Source Code Form is subject to the terms of the Mozilla Public 3 * License, v. 2.0. If a copy of the MPL was not distributed with this 4 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ 5 6 import { DragChildContextBase } from "./DragChildContextBase.sys.mjs"; 7 8 export class DragSourceChildContext extends DragChildContextBase { 9 // "editing-host" element (if any) at start of the drag 10 editingHost = null; 11 12 constructor(aDragWindow, aParams) { 13 super("dragSource", aDragWindow, aParams); 14 15 // Store current "editing-host" element so we can determine if the dragend 16 // went to the right place. 17 if (!this.dragElement.matches(":read-write")) { 18 return; 19 } 20 let lastEditableElement = this.dragElement; 21 for ( 22 let inclusiveAncestor = this.getInclusiveFlattenedTreeParentElement( 23 this.dragElement 24 ); 25 inclusiveAncestor; 26 inclusiveAncestor = this.getInclusiveFlattenedTreeParentElement( 27 inclusiveAncestor.flattenedTreeParentNode 28 ) 29 ) { 30 if (inclusiveAncestor.matches(":read-write")) { 31 lastEditableElement = inclusiveAncestor; 32 if (lastEditableElement == this.dragElement.ownerDocument.body) { 33 break; 34 } 35 } 36 } 37 this.editingHost = lastEditableElement; 38 } 39 40 async checkMouseDown() { 41 if (!this.expectNoDragEvents) { 42 this.ok(!!this.events.mousedown[0], "mousedown existed"); 43 if (this.events.mousedown[0]) { 44 this.ok( 45 this.nodeIsFlattenedTreeDescendantOf( 46 this.events.mousedown[0].composedTarget, 47 this.dragElement 48 ), 49 "event target of mousedown is srcElement or a descendant" 50 ); 51 } 52 } 53 await this.checkExpected(); 54 } 55 56 async checkDragStart() { 57 // Check that content still has drag session since it could have been 58 // canceled (by the caller's dragstart). 59 if (this.expectNoDragEvents || this.expectCancelDragStart) { 60 this.info(`Drag was pre-existing: ${this.alreadyHadSession}`); 61 this.ok( 62 !this.dragService.getCurrentSession(this.dragWindow) || 63 this.alreadyHadSession, 64 `no drag session in src process unless one was pre-existing` 65 ); 66 } else { 67 this.ok( 68 !!this.dragService.getCurrentSession(this.dragWindow), 69 `drag session was started in src process` 70 ); 71 this.ok(!!this.events.dragstart[0], "dragstart existed"); 72 if (this.events.dragstart[0]) { 73 this.ok( 74 this.nodeIsFlattenedTreeDescendantOf( 75 this.events.dragstart[0].composedTarget, 76 this.dragElement 77 ), 78 "event target of dragstart is srcElement or a descendant" 79 ); 80 } 81 await this.checkExpected(); 82 } 83 } 84 85 async checkDragEnd() { 86 if (!this.expectNoDragEvents) { 87 this.ok(!!this.events.dragend[0], "dragend existed"); 88 if (this.events.dragend[0]) { 89 this.ok( 90 this.nodeIsFlattenedTreeDescendantOf( 91 this.events.dragend[0].composedTarget, 92 this.dragElement 93 ), 94 "event target of dragend is srcElement or a descendant" 95 ); 96 if (this.editingHost) { 97 this.ok( 98 this.events.dragend[0].composedTarget == this.editingHost, 99 `event target of dragend was editingHost` 100 ); 101 } 102 } 103 } 104 await this.checkExpected(); 105 } 106 } 107 108 export function createDragSourceChildContext( 109 aDragWindow, 110 aParams, 111 aOk, 112 aIs, 113 aInfo 114 ) { 115 aDragWindow.dragSource = new DragSourceChildContext(aDragWindow, { 116 ...aParams, 117 ok: aOk, 118 is: aIs, 119 info: aInfo, 120 }); 121 }