| 1 |
/** |
| 2 |
* @license |
| 3 |
* Copyright 2023 Google LLC |
| 4 |
* SPDX-License-Identifier: BSD-3-Clause |
| 5 |
*/ |
| 6 |
|
| 7 |
/* Adapted from Node.js https://github.com/nodejs/node/blob/main/lib/internal/event_target.js */ |
| 8 |
|
| 9 |
type EventInterface = Event; |
| 10 |
|
| 11 |
// Event phases |
| 12 |
const NONE = 0; |
| 13 |
const CAPTURING_PHASE = 1; |
| 14 |
const AT_TARGET = 2; |
| 15 |
const BUBBLING_PHASE = 3; |
| 16 |
|
| 17 |
const enumerableProperty: Record<string, unknown> = {__proto__: null}; |
| 18 |
enumerableProperty.enumerable = true; |
| 19 |
Object.freeze(enumerableProperty); |
| 20 |
|
| 21 |
// TODO: Remove this when we remove support for vm modules (--experimental-vm-modules). |
| 22 |
const EventShim = class Event implements EventInterface { |
| 23 |
#cancelable = false; |
| 24 |
#bubbles = false; |
| 25 |
#composed = false; |
| 26 |
#defaultPrevented = false; |
| 27 |
#timestamp = Date.now(); |
| 28 |
#propagationStopped = false; |
| 29 |
#type: string; |
| 30 |
#target: globalThis.EventTarget | null; |
| 31 |
#isBeingDispatched: boolean; |
| 32 |
readonly NONE = NONE; |
| 33 |
readonly CAPTURING_PHASE = CAPTURING_PHASE; |
| 34 |
readonly AT_TARGET = AT_TARGET; |
| 35 |
readonly BUBBLING_PHASE = BUBBLING_PHASE; |
| 36 |
static readonly NONE = NONE; |
| 37 |
static readonly CAPTURING_PHASE = CAPTURING_PHASE; |
| 38 |
static readonly AT_TARGET = AT_TARGET; |
| 39 |
static readonly BUBBLING_PHASE = BUBBLING_PHASE; |
| 40 |
|
| 41 |
constructor(type: string, options: EventInit = {}) { |
| 42 |
if (arguments.length === 0) |
| 43 |
throw new Error(`The type argument must be specified`); |
| 44 |
if (typeof options !== 'object' || !options) { |
| 45 |
throw new Error(`The "options" argument must be an object`); |
| 46 |
} |
| 47 |
const {bubbles, cancelable, composed} = options; |
| 48 |
this.#cancelable = !!cancelable; |
| 49 |
this.#bubbles = !!bubbles; |
| 50 |
this.#composed = !!composed; |
| 51 |
|
| 52 |
this.#type = `${type}`; |
| 53 |
this.#target = null; |
| 54 |
this.#isBeingDispatched = false; |
| 55 |
} |
| 56 |
|
| 57 |
initEvent(_type: string, _bubbles?: boolean, _cancelable?: boolean): void { |
| 58 |
throw new Error('Method not implemented.'); |
| 59 |
} |
| 60 |
|
| 61 |
stopImmediatePropagation() { |
| 62 |
this.stopPropagation(); |
| 63 |
} |
| 64 |
|
| 65 |
preventDefault() { |
| 66 |
this.#defaultPrevented = true; |
| 67 |
} |
| 68 |
|
| 69 |
get target(): globalThis.EventTarget | null { |
| 70 |
return this.#target; |
| 71 |
} |
| 72 |
|
| 73 |
get currentTarget(): globalThis.EventTarget | null { |
| 74 |
return this.#target; |
| 75 |
} |
| 76 |
|
| 77 |
get srcElement(): globalThis.EventTarget | null { |
| 78 |
return this.#target; |
| 79 |
} |
| 80 |
|
| 81 |
get type(): string { |
| 82 |
return this.#type; |
| 83 |
} |
| 84 |
|
| 85 |
get cancelable(): boolean { |
| 86 |
return this.#cancelable; |
| 87 |
} |
| 88 |
|
| 89 |
get defaultPrevented(): boolean { |
| 90 |
return this.#cancelable && this.#defaultPrevented; |
| 91 |
} |
| 92 |
|
| 93 |
get timeStamp(): number { |
| 94 |
return this.#timestamp; |
| 95 |
} |
| 96 |
|
| 97 |
composedPath(): globalThis.EventTarget[] { |
| 98 |
return this.#isBeingDispatched ? [this.#target!] : []; |
| 99 |
} |
| 100 |
|
| 101 |
get returnValue(): boolean { |
| 102 |
return !this.#cancelable || !this.#defaultPrevented; |
| 103 |
} |
| 104 |
|
| 105 |
get bubbles(): boolean { |
| 106 |
return this.#bubbles; |
| 107 |
} |
| 108 |
|
| 109 |
get composed(): boolean { |
| 110 |
return this.#composed; |
| 111 |
} |
| 112 |
|
| 113 |
get eventPhase(): number { |
| 114 |
return this.#isBeingDispatched ? Event.AT_TARGET : Event.NONE; |
| 115 |
} |
| 116 |
|
| 117 |
get cancelBubble(): boolean { |
| 118 |
return this.#propagationStopped; |
| 119 |
} |
| 120 |
|
| 121 |
set cancelBubble(value) { |
| 122 |
if (value) { |
| 123 |
this.#propagationStopped = true; |
| 124 |
} |
| 125 |
} |
| 126 |
|
| 127 |
stopPropagation(): void { |
| 128 |
this.#propagationStopped = true; |
| 129 |
} |
| 130 |
|
| 131 |
get isTrusted(): boolean { |
| 132 |
return false; |
| 133 |
} |
| 134 |
}; |
| 135 |
|
| 136 |
Object.defineProperties(EventShim.prototype, { |
| 137 |
initEvent: enumerableProperty, |
| 138 |
stopImmediatePropagation: enumerableProperty, |
| 139 |
preventDefault: enumerableProperty, |
| 140 |
target: enumerableProperty, |
| 141 |
currentTarget: enumerableProperty, |
| 142 |
srcElement: enumerableProperty, |
| 143 |
type: enumerableProperty, |
| 144 |
cancelable: enumerableProperty, |
| 145 |
defaultPrevented: enumerableProperty, |
| 146 |
timeStamp: enumerableProperty, |
| 147 |
composedPath: enumerableProperty, |
| 148 |
returnValue: enumerableProperty, |
| 149 |
bubbles: enumerableProperty, |
| 150 |
composed: enumerableProperty, |
| 151 |
eventPhase: enumerableProperty, |
| 152 |
cancelBubble: enumerableProperty, |
| 153 |
stopPropagation: enumerableProperty, |
| 154 |
isTrusted: enumerableProperty, |
| 155 |
}); |
| 156 |
|
| 157 |
type CustomEventInterface = CustomEvent; |
| 158 |
|
| 159 |
// TODO: Remove this when we remove support for vm modules (--experimental-vm-modules). |
| 160 |
const CustomEventShim = class CustomEvent<T> |
| 161 |
extends EventShim |
| 162 |
implements CustomEventInterface |
| 163 |
{ |
| 164 |
#detail: T | null; |
| 165 |
|
| 166 |
constructor(type: string, options: CustomEventInit<T> = {}) { |
| 167 |
super(type, options); |
| 168 |
this.#detail = options?.detail ?? null; |
| 169 |
} |
| 170 |
|
| 171 |
initCustomEvent( |
| 172 |
_type: string, |
| 173 |
_bubbles?: boolean, |
| 174 |
_cancelable?: boolean, |
| 175 |
_detail?: T |
| 176 |
): void { |
| 177 |
throw new Error('Method not implemented.'); |
| 178 |
} |
| 179 |
|
| 180 |
get detail(): T { |
| 181 |
return this.#detail!; |
| 182 |
} |
| 183 |
}; |
| 184 |
|
| 185 |
Object.defineProperties(CustomEventShim.prototype, { |
| 186 |
detail: enumerableProperty, |
| 187 |
}); |
| 188 |
|
| 189 |
const EventShimWithRealType = EventShim as object as typeof Event; |
| 190 |
const CustomEventShimWithRealType = |
| 191 |
CustomEventShim as object as typeof CustomEvent; |
| 192 |
export { |
| 193 |
EventShimWithRealType as Event, |
| 194 |
EventShimWithRealType as EventShim, |
| 195 |
CustomEventShimWithRealType as CustomEvent, |
| 196 |
CustomEventShimWithRealType as CustomEventShim, |
| 197 |
}; |