2023-01-02 16:00:42 -05:00
|
|
|
// Copyright 2018-2023 the Deno authors. All rights reserved. MIT license.
|
2020-07-19 13:49:44 -04:00
|
|
|
|
|
|
|
// This module follows most of the WHATWG Living Standard for the DOM logic.
|
|
|
|
// Many parts of the DOM are not implemented in Deno, but the logic for those
|
|
|
|
// parts still exists. This means you will observe a lot of strange structures
|
|
|
|
// and impossible logic branches based on what Deno currently supports.
|
2021-01-05 16:43:25 -05:00
|
|
|
|
2023-02-07 14:22:46 -05:00
|
|
|
const core = globalThis.Deno.core;
|
|
|
|
const ops = core.ops;
|
2023-03-08 06:44:54 -05:00
|
|
|
import * as webidl from "ext:deno_webidl/00_webidl.js";
|
|
|
|
import DOMException from "ext:deno_web/01_dom_exception.js";
|
|
|
|
import { createFilteredInspectProxy } from "ext:deno_console/02_console.js";
|
2023-02-07 14:22:46 -05:00
|
|
|
const primordials = globalThis.__bootstrap.primordials;
|
|
|
|
const {
|
|
|
|
ArrayPrototypeFilter,
|
|
|
|
ArrayPrototypeIncludes,
|
|
|
|
ArrayPrototypeIndexOf,
|
|
|
|
ArrayPrototypeMap,
|
|
|
|
ArrayPrototypePush,
|
|
|
|
ArrayPrototypeSlice,
|
|
|
|
ArrayPrototypeSplice,
|
|
|
|
ArrayPrototypeUnshift,
|
|
|
|
Boolean,
|
|
|
|
DateNow,
|
|
|
|
Error,
|
|
|
|
FunctionPrototypeCall,
|
|
|
|
Map,
|
|
|
|
MapPrototypeGet,
|
|
|
|
MapPrototypeSet,
|
|
|
|
ObjectCreate,
|
|
|
|
ObjectDefineProperty,
|
|
|
|
ObjectGetOwnPropertyDescriptor,
|
|
|
|
ObjectPrototypeIsPrototypeOf,
|
|
|
|
ReflectDefineProperty,
|
|
|
|
ReflectHas,
|
|
|
|
SafeArrayIterator,
|
|
|
|
StringPrototypeStartsWith,
|
|
|
|
Symbol,
|
|
|
|
SymbolFor,
|
|
|
|
SymbolToStringTag,
|
|
|
|
TypeError,
|
|
|
|
} = primordials;
|
|
|
|
|
|
|
|
// This should be set via setGlobalThis this is required so that if even
|
|
|
|
// user deletes globalThis it is still usable
|
|
|
|
let globalThis_;
|
|
|
|
|
|
|
|
function saveGlobalThisReference(val) {
|
|
|
|
globalThis_ = val;
|
|
|
|
}
|
|
|
|
|
|
|
|
// accessors for non runtime visible data
|
|
|
|
|
|
|
|
function getDispatched(event) {
|
|
|
|
return Boolean(event[_dispatched]);
|
|
|
|
}
|
|
|
|
|
|
|
|
function getPath(event) {
|
|
|
|
return event[_path] ?? [];
|
|
|
|
}
|
|
|
|
|
|
|
|
function getStopImmediatePropagation(event) {
|
|
|
|
return Boolean(event[_stopImmediatePropagationFlag]);
|
|
|
|
}
|
|
|
|
|
|
|
|
function setCurrentTarget(
|
|
|
|
event,
|
|
|
|
value,
|
|
|
|
) {
|
|
|
|
event[_attributes].currentTarget = value;
|
|
|
|
}
|
|
|
|
|
|
|
|
function setIsTrusted(event, value) {
|
|
|
|
event[_isTrusted] = value;
|
|
|
|
}
|
|
|
|
|
|
|
|
function setDispatched(event, value) {
|
|
|
|
event[_dispatched] = value;
|
|
|
|
}
|
|
|
|
|
|
|
|
function setEventPhase(event, value) {
|
|
|
|
event[_attributes].eventPhase = value;
|
|
|
|
}
|
|
|
|
|
|
|
|
function setInPassiveListener(event, value) {
|
|
|
|
event[_inPassiveListener] = value;
|
|
|
|
}
|
|
|
|
|
|
|
|
function setPath(event, value) {
|
|
|
|
event[_path] = value;
|
|
|
|
}
|
|
|
|
|
|
|
|
function setRelatedTarget(
|
|
|
|
event,
|
|
|
|
value,
|
|
|
|
) {
|
|
|
|
event[_attributes].relatedTarget = value;
|
|
|
|
}
|
|
|
|
|
|
|
|
function setTarget(event, value) {
|
|
|
|
event[_attributes].target = value;
|
|
|
|
}
|
|
|
|
|
|
|
|
function setStopImmediatePropagation(
|
|
|
|
event,
|
|
|
|
value,
|
|
|
|
) {
|
|
|
|
event[_stopImmediatePropagationFlag] = value;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Type guards that widen the event type
|
|
|
|
|
|
|
|
function hasRelatedTarget(
|
|
|
|
event,
|
|
|
|
) {
|
|
|
|
return ReflectHas(event, "relatedTarget");
|
|
|
|
}
|
|
|
|
|
|
|
|
const isTrusted = ObjectGetOwnPropertyDescriptor({
|
|
|
|
get isTrusted() {
|
|
|
|
return this[_isTrusted];
|
|
|
|
},
|
|
|
|
}, "isTrusted").get;
|
|
|
|
|
|
|
|
const eventInitConverter = webidl.createDictionaryConverter("EventInit", [{
|
|
|
|
key: "bubbles",
|
|
|
|
defaultValue: false,
|
|
|
|
converter: webidl.converters.boolean,
|
|
|
|
}, {
|
|
|
|
key: "cancelable",
|
|
|
|
defaultValue: false,
|
|
|
|
converter: webidl.converters.boolean,
|
|
|
|
}, {
|
|
|
|
key: "composed",
|
|
|
|
defaultValue: false,
|
|
|
|
converter: webidl.converters.boolean,
|
|
|
|
}]);
|
|
|
|
|
|
|
|
const _attributes = Symbol("[[attributes]]");
|
|
|
|
const _canceledFlag = Symbol("[[canceledFlag]]");
|
|
|
|
const _stopPropagationFlag = Symbol("[[stopPropagationFlag]]");
|
|
|
|
const _stopImmediatePropagationFlag = Symbol(
|
|
|
|
"[[stopImmediatePropagationFlag]]",
|
|
|
|
);
|
|
|
|
const _inPassiveListener = Symbol("[[inPassiveListener]]");
|
|
|
|
const _dispatched = Symbol("[[dispatched]]");
|
|
|
|
const _isTrusted = Symbol("[[isTrusted]]");
|
|
|
|
const _path = Symbol("[[path]]");
|
|
|
|
// internal.
|
|
|
|
const _skipInternalInit = Symbol("[[skipSlowInit]]");
|
|
|
|
|
|
|
|
class Event {
|
|
|
|
constructor(type, eventInitDict = {}) {
|
|
|
|
// TODO(lucacasonato): remove when this interface is spec aligned
|
|
|
|
this[SymbolToStringTag] = "Event";
|
|
|
|
this[_canceledFlag] = false;
|
|
|
|
this[_stopPropagationFlag] = false;
|
|
|
|
this[_stopImmediatePropagationFlag] = false;
|
|
|
|
this[_inPassiveListener] = false;
|
|
|
|
this[_dispatched] = false;
|
|
|
|
this[_isTrusted] = false;
|
|
|
|
this[_path] = [];
|
|
|
|
|
|
|
|
if (!eventInitDict[_skipInternalInit]) {
|
|
|
|
webidl.requiredArguments(arguments.length, 1, {
|
|
|
|
prefix: "Failed to construct 'Event'",
|
|
|
|
});
|
|
|
|
type = webidl.converters.DOMString(type, {
|
|
|
|
prefix: "Failed to construct 'Event'",
|
|
|
|
context: "Argument 1",
|
|
|
|
});
|
|
|
|
const eventInit = eventInitConverter(eventInitDict, {
|
|
|
|
prefix: "Failed to construct 'Event'",
|
|
|
|
context: "Argument 2",
|
|
|
|
});
|
|
|
|
this[_attributes] = {
|
|
|
|
type,
|
|
|
|
...eventInit,
|
|
|
|
currentTarget: null,
|
|
|
|
eventPhase: Event.NONE,
|
|
|
|
target: null,
|
|
|
|
timeStamp: DateNow(),
|
|
|
|
};
|
|
|
|
// [LegacyUnforgeable]
|
|
|
|
ReflectDefineProperty(this, "isTrusted", {
|
|
|
|
enumerable: true,
|
|
|
|
get: isTrusted,
|
|
|
|
});
|
|
|
|
} else {
|
|
|
|
this[_attributes] = {
|
|
|
|
type,
|
|
|
|
data: eventInitDict.data ?? null,
|
|
|
|
bubbles: eventInitDict.bubbles ?? false,
|
|
|
|
cancelable: eventInitDict.cancelable ?? false,
|
|
|
|
composed: eventInitDict.composed ?? false,
|
|
|
|
currentTarget: null,
|
|
|
|
eventPhase: Event.NONE,
|
|
|
|
target: null,
|
|
|
|
timeStamp: DateNow(),
|
|
|
|
};
|
|
|
|
// TODO(@littledivy): Not spec compliant but performance is hurt badly
|
|
|
|
// for users of `_skipInternalInit`.
|
|
|
|
this.isTrusted = false;
|
|
|
|
}
|
2020-07-19 13:49:44 -04:00
|
|
|
}
|
|
|
|
|
2023-02-07 14:22:46 -05:00
|
|
|
[SymbolFor("Deno.privateCustomInspect")](inspect) {
|
|
|
|
return inspect(createFilteredInspectProxy({
|
|
|
|
object: this,
|
|
|
|
evaluate: ObjectPrototypeIsPrototypeOf(Event.prototype, this),
|
|
|
|
keys: EVENT_PROPS,
|
|
|
|
}));
|
2020-07-19 13:49:44 -04:00
|
|
|
}
|
|
|
|
|
2023-02-07 14:22:46 -05:00
|
|
|
get type() {
|
|
|
|
return this[_attributes].type;
|
2020-07-19 13:49:44 -04:00
|
|
|
}
|
|
|
|
|
2023-02-07 14:22:46 -05:00
|
|
|
get target() {
|
|
|
|
return this[_attributes].target;
|
2020-07-19 13:49:44 -04:00
|
|
|
}
|
|
|
|
|
2023-02-07 14:22:46 -05:00
|
|
|
get srcElement() {
|
|
|
|
return null;
|
2020-07-19 13:49:44 -04:00
|
|
|
}
|
|
|
|
|
2023-02-07 14:22:46 -05:00
|
|
|
set srcElement(_) {
|
|
|
|
// this member is deprecated
|
2020-07-19 13:49:44 -04:00
|
|
|
}
|
|
|
|
|
2023-02-07 14:22:46 -05:00
|
|
|
get currentTarget() {
|
|
|
|
return this[_attributes].currentTarget;
|
2020-07-19 13:49:44 -04:00
|
|
|
}
|
|
|
|
|
2023-02-07 14:22:46 -05:00
|
|
|
composedPath() {
|
|
|
|
const path = this[_path];
|
|
|
|
if (path.length === 0) {
|
|
|
|
return [];
|
2020-07-19 13:49:44 -04:00
|
|
|
}
|
|
|
|
|
2023-02-07 14:22:46 -05:00
|
|
|
if (!this.currentTarget) {
|
|
|
|
throw new Error("assertion error");
|
2020-10-26 18:22:03 -04:00
|
|
|
}
|
2023-02-07 14:22:46 -05:00
|
|
|
const composedPath = [
|
|
|
|
{
|
|
|
|
item: this.currentTarget,
|
|
|
|
itemInShadowTree: false,
|
|
|
|
relatedTarget: null,
|
|
|
|
rootOfClosedTree: false,
|
|
|
|
slotInClosedTree: false,
|
|
|
|
target: null,
|
|
|
|
touchTargetList: [],
|
|
|
|
},
|
|
|
|
];
|
2020-10-26 18:22:03 -04:00
|
|
|
|
2023-02-07 14:22:46 -05:00
|
|
|
let currentTargetIndex = 0;
|
|
|
|
let currentTargetHiddenSubtreeLevel = 0;
|
2021-06-24 14:21:13 -04:00
|
|
|
|
2023-02-07 14:22:46 -05:00
|
|
|
for (let index = path.length - 1; index >= 0; index--) {
|
|
|
|
const { item, rootOfClosedTree, slotInClosedTree } = path[index];
|
2021-06-24 14:21:13 -04:00
|
|
|
|
2023-02-07 14:22:46 -05:00
|
|
|
if (rootOfClosedTree) {
|
|
|
|
currentTargetHiddenSubtreeLevel++;
|
|
|
|
}
|
2021-06-24 14:21:13 -04:00
|
|
|
|
2023-02-07 14:22:46 -05:00
|
|
|
if (item === this.currentTarget) {
|
|
|
|
currentTargetIndex = index;
|
|
|
|
break;
|
|
|
|
}
|
2021-06-24 14:21:13 -04:00
|
|
|
|
2023-02-07 14:22:46 -05:00
|
|
|
if (slotInClosedTree) {
|
|
|
|
currentTargetHiddenSubtreeLevel--;
|
|
|
|
}
|
2020-07-19 13:49:44 -04:00
|
|
|
}
|
2021-06-24 14:21:13 -04:00
|
|
|
|
2023-02-07 14:22:46 -05:00
|
|
|
let currentHiddenLevel = currentTargetHiddenSubtreeLevel;
|
|
|
|
let maxHiddenLevel = currentTargetHiddenSubtreeLevel;
|
|
|
|
|
|
|
|
for (let i = currentTargetIndex - 1; i >= 0; i--) {
|
|
|
|
const { item, rootOfClosedTree, slotInClosedTree } = path[i];
|
2020-07-19 13:49:44 -04:00
|
|
|
|
2023-02-07 14:22:46 -05:00
|
|
|
if (rootOfClosedTree) {
|
|
|
|
currentHiddenLevel++;
|
2020-07-19 13:49:44 -04:00
|
|
|
}
|
2023-02-07 14:22:46 -05:00
|
|
|
|
|
|
|
if (currentHiddenLevel <= maxHiddenLevel) {
|
|
|
|
ArrayPrototypeUnshift(composedPath, {
|
|
|
|
item,
|
2020-07-19 13:49:44 -04:00
|
|
|
itemInShadowTree: false,
|
|
|
|
relatedTarget: null,
|
|
|
|
rootOfClosedTree: false,
|
|
|
|
slotInClosedTree: false,
|
|
|
|
target: null,
|
|
|
|
touchTargetList: [],
|
2023-02-07 14:22:46 -05:00
|
|
|
});
|
2020-07-19 13:49:44 -04:00
|
|
|
}
|
|
|
|
|
2023-02-07 14:22:46 -05:00
|
|
|
if (slotInClosedTree) {
|
|
|
|
currentHiddenLevel--;
|
2020-07-19 13:49:44 -04:00
|
|
|
|
2023-02-07 14:22:46 -05:00
|
|
|
if (currentHiddenLevel < maxHiddenLevel) {
|
|
|
|
maxHiddenLevel = currentHiddenLevel;
|
2020-07-19 13:49:44 -04:00
|
|
|
}
|
|
|
|
}
|
2023-02-07 14:22:46 -05:00
|
|
|
}
|
2020-07-19 13:49:44 -04:00
|
|
|
|
2023-02-07 14:22:46 -05:00
|
|
|
currentHiddenLevel = currentTargetHiddenSubtreeLevel;
|
|
|
|
maxHiddenLevel = currentTargetHiddenSubtreeLevel;
|
2020-07-19 13:49:44 -04:00
|
|
|
|
2023-02-07 14:22:46 -05:00
|
|
|
for (let index = currentTargetIndex + 1; index < path.length; index++) {
|
|
|
|
const { item, rootOfClosedTree, slotInClosedTree } = path[index];
|
2020-07-19 13:49:44 -04:00
|
|
|
|
2023-02-07 14:22:46 -05:00
|
|
|
if (slotInClosedTree) {
|
|
|
|
currentHiddenLevel++;
|
|
|
|
}
|
2020-07-19 13:49:44 -04:00
|
|
|
|
2023-02-07 14:22:46 -05:00
|
|
|
if (currentHiddenLevel <= maxHiddenLevel) {
|
|
|
|
ArrayPrototypePush(composedPath, {
|
|
|
|
item,
|
|
|
|
itemInShadowTree: false,
|
|
|
|
relatedTarget: null,
|
|
|
|
rootOfClosedTree: false,
|
|
|
|
slotInClosedTree: false,
|
|
|
|
target: null,
|
|
|
|
touchTargetList: [],
|
|
|
|
});
|
|
|
|
}
|
2020-07-19 13:49:44 -04:00
|
|
|
|
2023-02-07 14:22:46 -05:00
|
|
|
if (rootOfClosedTree) {
|
|
|
|
currentHiddenLevel--;
|
2020-07-19 13:49:44 -04:00
|
|
|
|
2023-02-07 14:22:46 -05:00
|
|
|
if (currentHiddenLevel < maxHiddenLevel) {
|
|
|
|
maxHiddenLevel = currentHiddenLevel;
|
2020-07-19 13:49:44 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2023-02-07 14:22:46 -05:00
|
|
|
return ArrayPrototypeMap(composedPath, (p) => p.item);
|
|
|
|
}
|
2021-06-24 14:21:13 -04:00
|
|
|
|
2023-02-07 14:22:46 -05:00
|
|
|
get NONE() {
|
|
|
|
return Event.NONE;
|
|
|
|
}
|
2021-06-24 14:21:13 -04:00
|
|
|
|
2023-02-07 14:22:46 -05:00
|
|
|
get CAPTURING_PHASE() {
|
|
|
|
return Event.CAPTURING_PHASE;
|
|
|
|
}
|
2021-06-24 14:21:13 -04:00
|
|
|
|
2023-02-07 14:22:46 -05:00
|
|
|
get AT_TARGET() {
|
|
|
|
return Event.AT_TARGET;
|
|
|
|
}
|
2021-06-24 14:21:13 -04:00
|
|
|
|
2023-02-07 14:22:46 -05:00
|
|
|
get BUBBLING_PHASE() {
|
|
|
|
return Event.BUBBLING_PHASE;
|
|
|
|
}
|
2021-02-13 09:58:12 -05:00
|
|
|
|
2023-02-07 14:22:46 -05:00
|
|
|
static get NONE() {
|
|
|
|
return 0;
|
|
|
|
}
|
2021-06-24 14:21:13 -04:00
|
|
|
|
2023-02-07 14:22:46 -05:00
|
|
|
static get CAPTURING_PHASE() {
|
|
|
|
return 1;
|
|
|
|
}
|
2021-06-24 14:21:13 -04:00
|
|
|
|
2023-02-07 14:22:46 -05:00
|
|
|
static get AT_TARGET() {
|
|
|
|
return 2;
|
|
|
|
}
|
2021-06-24 14:21:13 -04:00
|
|
|
|
2023-02-07 14:22:46 -05:00
|
|
|
static get BUBBLING_PHASE() {
|
|
|
|
return 3;
|
|
|
|
}
|
2021-02-13 09:58:12 -05:00
|
|
|
|
2023-02-07 14:22:46 -05:00
|
|
|
get eventPhase() {
|
|
|
|
return this[_attributes].eventPhase;
|
|
|
|
}
|
2021-06-24 14:21:13 -04:00
|
|
|
|
2023-02-07 14:22:46 -05:00
|
|
|
stopPropagation() {
|
|
|
|
this[_stopPropagationFlag] = true;
|
|
|
|
}
|
2021-06-24 14:21:13 -04:00
|
|
|
|
2023-02-07 14:22:46 -05:00
|
|
|
get cancelBubble() {
|
|
|
|
return this[_stopPropagationFlag];
|
|
|
|
}
|
2021-06-24 14:21:13 -04:00
|
|
|
|
2023-02-07 14:22:46 -05:00
|
|
|
set cancelBubble(value) {
|
|
|
|
this[_stopPropagationFlag] = webidl.converters.boolean(value);
|
|
|
|
}
|
2021-06-24 14:21:13 -04:00
|
|
|
|
2023-02-07 14:22:46 -05:00
|
|
|
stopImmediatePropagation() {
|
|
|
|
this[_stopPropagationFlag] = true;
|
|
|
|
this[_stopImmediatePropagationFlag] = true;
|
|
|
|
}
|
2021-06-24 14:21:13 -04:00
|
|
|
|
2023-02-07 14:22:46 -05:00
|
|
|
get bubbles() {
|
|
|
|
return this[_attributes].bubbles;
|
|
|
|
}
|
2021-06-24 14:21:13 -04:00
|
|
|
|
2023-02-07 14:22:46 -05:00
|
|
|
get cancelable() {
|
|
|
|
return this[_attributes].cancelable;
|
|
|
|
}
|
2021-02-13 09:58:12 -05:00
|
|
|
|
2023-02-07 14:22:46 -05:00
|
|
|
get returnValue() {
|
|
|
|
return !this[_canceledFlag];
|
|
|
|
}
|
2021-02-13 09:58:12 -05:00
|
|
|
|
2023-02-07 14:22:46 -05:00
|
|
|
set returnValue(value) {
|
|
|
|
if (!webidl.converters.boolean(value)) {
|
|
|
|
this[_canceledFlag] = true;
|
2021-02-13 09:58:12 -05:00
|
|
|
}
|
2020-07-19 13:49:44 -04:00
|
|
|
}
|
|
|
|
|
2023-02-07 14:22:46 -05:00
|
|
|
preventDefault() {
|
|
|
|
if (this[_attributes].cancelable && !this[_inPassiveListener]) {
|
|
|
|
this[_canceledFlag] = true;
|
2020-07-19 13:49:44 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-02-07 14:22:46 -05:00
|
|
|
get defaultPrevented() {
|
|
|
|
return this[_canceledFlag];
|
2020-07-19 13:49:44 -04:00
|
|
|
}
|
|
|
|
|
2023-02-07 14:22:46 -05:00
|
|
|
get composed() {
|
|
|
|
return this[_attributes].composed;
|
2020-07-19 13:49:44 -04:00
|
|
|
}
|
|
|
|
|
2023-02-07 14:22:46 -05:00
|
|
|
get initialized() {
|
|
|
|
return true;
|
2020-07-19 13:49:44 -04:00
|
|
|
}
|
|
|
|
|
2023-02-07 14:22:46 -05:00
|
|
|
get timeStamp() {
|
|
|
|
return this[_attributes].timeStamp;
|
2020-07-19 13:49:44 -04:00
|
|
|
}
|
2023-02-07 14:22:46 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
function defineEnumerableProps(
|
|
|
|
Ctor,
|
|
|
|
props,
|
|
|
|
) {
|
|
|
|
for (let i = 0; i < props.length; ++i) {
|
|
|
|
const prop = props[i];
|
|
|
|
ReflectDefineProperty(Ctor.prototype, prop, { enumerable: true });
|
2020-07-19 13:49:44 -04:00
|
|
|
}
|
2023-02-07 14:22:46 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
const EVENT_PROPS = [
|
|
|
|
"bubbles",
|
|
|
|
"cancelable",
|
|
|
|
"composed",
|
|
|
|
"currentTarget",
|
|
|
|
"defaultPrevented",
|
|
|
|
"eventPhase",
|
|
|
|
"srcElement",
|
|
|
|
"target",
|
|
|
|
"returnValue",
|
|
|
|
"timeStamp",
|
|
|
|
"type",
|
|
|
|
];
|
|
|
|
|
|
|
|
defineEnumerableProps(Event, EVENT_PROPS);
|
|
|
|
|
|
|
|
// This is currently the only node type we are using, so instead of implementing
|
|
|
|
// the whole of the Node interface at the moment, this just gives us the one
|
|
|
|
// value to power the standards based logic
|
|
|
|
const DOCUMENT_FRAGMENT_NODE = 11;
|
|
|
|
|
|
|
|
// DOM Logic Helper functions and type guards
|
|
|
|
|
|
|
|
/** Get the parent node, for event targets that have a parent.
|
|
|
|
*
|
|
|
|
* Ref: https://dom.spec.whatwg.org/#get-the-parent */
|
|
|
|
function getParent(eventTarget) {
|
|
|
|
return isNode(eventTarget) ? eventTarget.parentNode : null;
|
|
|
|
}
|
|
|
|
|
|
|
|
function getRoot(eventTarget) {
|
|
|
|
return isNode(eventTarget)
|
|
|
|
? eventTarget.getRootNode({ composed: true })
|
|
|
|
: null;
|
|
|
|
}
|
|
|
|
|
|
|
|
function isNode(
|
|
|
|
eventTarget,
|
|
|
|
) {
|
|
|
|
return Boolean(eventTarget && ReflectHas(eventTarget, "nodeType"));
|
|
|
|
}
|
|
|
|
|
|
|
|
// https://dom.spec.whatwg.org/#concept-shadow-including-inclusive-ancestor
|
|
|
|
function isShadowInclusiveAncestor(
|
|
|
|
ancestor,
|
|
|
|
node,
|
|
|
|
) {
|
|
|
|
while (isNode(node)) {
|
|
|
|
if (node === ancestor) {
|
|
|
|
return true;
|
|
|
|
}
|
2020-07-19 13:49:44 -04:00
|
|
|
|
2023-02-07 14:22:46 -05:00
|
|
|
if (isShadowRoot(node)) {
|
|
|
|
node = node && getHost(node);
|
|
|
|
} else {
|
|
|
|
node = getParent(node);
|
|
|
|
}
|
2020-07-19 13:49:44 -04:00
|
|
|
}
|
|
|
|
|
2023-02-07 14:22:46 -05:00
|
|
|
return false;
|
|
|
|
}
|
2020-07-19 13:49:44 -04:00
|
|
|
|
2023-02-07 14:22:46 -05:00
|
|
|
function isShadowRoot(nodeImpl) {
|
|
|
|
return Boolean(
|
|
|
|
nodeImpl &&
|
|
|
|
isNode(nodeImpl) &&
|
|
|
|
nodeImpl.nodeType === DOCUMENT_FRAGMENT_NODE &&
|
|
|
|
getHost(nodeImpl) != null,
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
function isSlotable(
|
|
|
|
nodeImpl,
|
|
|
|
) {
|
|
|
|
return Boolean(isNode(nodeImpl) && ReflectHas(nodeImpl, "assignedSlot"));
|
|
|
|
}
|
|
|
|
|
|
|
|
// DOM Logic functions
|
|
|
|
|
|
|
|
/** Append a path item to an event's path.
|
|
|
|
*
|
|
|
|
* Ref: https://dom.spec.whatwg.org/#concept-event-path-append
|
|
|
|
*/
|
|
|
|
function appendToEventPath(
|
|
|
|
eventImpl,
|
|
|
|
target,
|
|
|
|
targetOverride,
|
|
|
|
relatedTarget,
|
|
|
|
touchTargets,
|
|
|
|
slotInClosedTree,
|
|
|
|
) {
|
|
|
|
const itemInShadowTree = isNode(target) && isShadowRoot(getRoot(target));
|
|
|
|
const rootOfClosedTree = isShadowRoot(target) &&
|
|
|
|
getMode(target) === "closed";
|
|
|
|
|
|
|
|
ArrayPrototypePush(getPath(eventImpl), {
|
|
|
|
item: target,
|
|
|
|
itemInShadowTree,
|
|
|
|
target: targetOverride,
|
2020-07-19 13:49:44 -04:00
|
|
|
relatedTarget,
|
2023-02-07 14:22:46 -05:00
|
|
|
touchTargetList: touchTargets,
|
|
|
|
rootOfClosedTree,
|
2020-07-19 13:49:44 -04:00
|
|
|
slotInClosedTree,
|
2023-02-07 14:22:46 -05:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
function dispatch(
|
|
|
|
targetImpl,
|
|
|
|
eventImpl,
|
|
|
|
targetOverride,
|
|
|
|
) {
|
|
|
|
let clearTargets = false;
|
|
|
|
let activationTarget = null;
|
|
|
|
|
|
|
|
setDispatched(eventImpl, true);
|
|
|
|
|
|
|
|
targetOverride = targetOverride ?? targetImpl;
|
|
|
|
const eventRelatedTarget = hasRelatedTarget(eventImpl)
|
|
|
|
? eventImpl.relatedTarget
|
|
|
|
: null;
|
|
|
|
let relatedTarget = retarget(eventRelatedTarget, targetImpl);
|
|
|
|
|
|
|
|
if (targetImpl !== relatedTarget || targetImpl === eventRelatedTarget) {
|
|
|
|
const touchTargets = [];
|
|
|
|
|
|
|
|
appendToEventPath(
|
|
|
|
eventImpl,
|
|
|
|
targetImpl,
|
|
|
|
targetOverride,
|
2020-07-19 13:49:44 -04:00
|
|
|
relatedTarget,
|
2023-02-07 14:22:46 -05:00
|
|
|
touchTargets,
|
|
|
|
false,
|
|
|
|
);
|
2020-07-19 13:49:44 -04:00
|
|
|
|
2023-02-07 14:22:46 -05:00
|
|
|
const isActivationEvent = eventImpl.type === "click";
|
2020-07-19 13:49:44 -04:00
|
|
|
|
2023-02-07 14:22:46 -05:00
|
|
|
if (isActivationEvent && getHasActivationBehavior(targetImpl)) {
|
|
|
|
activationTarget = targetImpl;
|
|
|
|
}
|
2020-07-19 13:49:44 -04:00
|
|
|
|
2023-02-07 14:22:46 -05:00
|
|
|
let slotInClosedTree = false;
|
|
|
|
let slotable = isSlotable(targetImpl) && getAssignedSlot(targetImpl)
|
|
|
|
? targetImpl
|
2020-07-19 13:49:44 -04:00
|
|
|
: null;
|
2023-02-07 14:22:46 -05:00
|
|
|
let parent = getParent(targetImpl);
|
2020-07-19 13:49:44 -04:00
|
|
|
|
2023-02-07 14:22:46 -05:00
|
|
|
// Populate event path
|
|
|
|
// https://dom.spec.whatwg.org/#event-path
|
|
|
|
while (parent !== null) {
|
|
|
|
if (slotable !== null) {
|
|
|
|
slotable = null;
|
2020-07-19 13:49:44 -04:00
|
|
|
|
2023-02-07 14:22:46 -05:00
|
|
|
const parentRoot = getRoot(parent);
|
2020-07-19 13:49:44 -04:00
|
|
|
if (
|
2023-02-07 14:22:46 -05:00
|
|
|
isShadowRoot(parentRoot) &&
|
|
|
|
parentRoot &&
|
|
|
|
getMode(parentRoot) === "closed"
|
2020-07-19 13:49:44 -04:00
|
|
|
) {
|
2023-02-07 14:22:46 -05:00
|
|
|
slotInClosedTree = true;
|
2020-07-19 13:49:44 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-02-07 14:22:46 -05:00
|
|
|
relatedTarget = retarget(eventRelatedTarget, parent);
|
2020-07-19 13:49:44 -04:00
|
|
|
|
2023-02-07 14:22:46 -05:00
|
|
|
if (
|
|
|
|
isNode(parent) &&
|
|
|
|
isShadowInclusiveAncestor(getRoot(targetImpl), parent)
|
|
|
|
) {
|
|
|
|
appendToEventPath(
|
|
|
|
eventImpl,
|
|
|
|
parent,
|
|
|
|
null,
|
|
|
|
relatedTarget,
|
|
|
|
touchTargets,
|
|
|
|
slotInClosedTree,
|
|
|
|
);
|
|
|
|
} else if (parent === relatedTarget) {
|
|
|
|
parent = null;
|
|
|
|
} else {
|
|
|
|
targetImpl = parent;
|
2020-07-19 13:49:44 -04:00
|
|
|
|
|
|
|
if (
|
2023-02-07 14:22:46 -05:00
|
|
|
isActivationEvent &&
|
|
|
|
activationTarget === null &&
|
|
|
|
getHasActivationBehavior(targetImpl)
|
2020-07-19 13:49:44 -04:00
|
|
|
) {
|
2023-02-07 14:22:46 -05:00
|
|
|
activationTarget = targetImpl;
|
2020-07-19 13:49:44 -04:00
|
|
|
}
|
2023-02-07 14:22:46 -05:00
|
|
|
|
|
|
|
appendToEventPath(
|
|
|
|
eventImpl,
|
|
|
|
parent,
|
|
|
|
targetImpl,
|
|
|
|
relatedTarget,
|
|
|
|
touchTargets,
|
|
|
|
slotInClosedTree,
|
|
|
|
);
|
2020-07-19 13:49:44 -04:00
|
|
|
}
|
|
|
|
|
2023-02-07 14:22:46 -05:00
|
|
|
if (parent !== null) {
|
|
|
|
parent = getParent(parent);
|
|
|
|
}
|
2020-07-19 13:49:44 -04:00
|
|
|
|
2023-02-07 14:22:46 -05:00
|
|
|
slotInClosedTree = false;
|
2020-07-19 13:49:44 -04:00
|
|
|
}
|
|
|
|
|
2023-02-07 14:22:46 -05:00
|
|
|
let clearTargetsTupleIndex = -1;
|
|
|
|
const path = getPath(eventImpl);
|
|
|
|
for (
|
|
|
|
let i = path.length - 1;
|
|
|
|
i >= 0 && clearTargetsTupleIndex === -1;
|
|
|
|
i--
|
|
|
|
) {
|
|
|
|
if (path[i].target !== null) {
|
|
|
|
clearTargetsTupleIndex = i;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
const clearTargetsTuple = path[clearTargetsTupleIndex];
|
2020-07-19 13:49:44 -04:00
|
|
|
|
2023-02-07 14:22:46 -05:00
|
|
|
clearTargets = (isNode(clearTargetsTuple.target) &&
|
|
|
|
isShadowRoot(getRoot(clearTargetsTuple.target))) ||
|
|
|
|
(isNode(clearTargetsTuple.relatedTarget) &&
|
|
|
|
isShadowRoot(getRoot(clearTargetsTuple.relatedTarget)));
|
2020-07-19 13:49:44 -04:00
|
|
|
|
2023-02-07 14:22:46 -05:00
|
|
|
setEventPhase(eventImpl, Event.CAPTURING_PHASE);
|
2020-07-19 13:49:44 -04:00
|
|
|
|
2023-02-07 14:22:46 -05:00
|
|
|
for (let i = path.length - 1; i >= 0; --i) {
|
|
|
|
const tuple = path[i];
|
2020-07-19 13:49:44 -04:00
|
|
|
|
2023-02-07 14:22:46 -05:00
|
|
|
if (tuple.target === null) {
|
|
|
|
invokeEventListeners(tuple, eventImpl);
|
|
|
|
}
|
2020-07-19 13:49:44 -04:00
|
|
|
}
|
|
|
|
|
2023-02-07 14:22:46 -05:00
|
|
|
for (let i = 0; i < path.length; i++) {
|
|
|
|
const tuple = path[i];
|
2020-07-19 13:49:44 -04:00
|
|
|
|
2023-02-07 14:22:46 -05:00
|
|
|
if (tuple.target !== null) {
|
|
|
|
setEventPhase(eventImpl, Event.AT_TARGET);
|
2020-07-19 13:49:44 -04:00
|
|
|
} else {
|
2023-02-07 14:22:46 -05:00
|
|
|
setEventPhase(eventImpl, Event.BUBBLING_PHASE);
|
2020-07-19 13:49:44 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
if (
|
2023-02-07 14:22:46 -05:00
|
|
|
(eventImpl.eventPhase === Event.BUBBLING_PHASE &&
|
|
|
|
eventImpl.bubbles) ||
|
|
|
|
eventImpl.eventPhase === Event.AT_TARGET
|
2020-07-19 13:49:44 -04:00
|
|
|
) {
|
2023-02-07 14:22:46 -05:00
|
|
|
invokeEventListeners(tuple, eventImpl);
|
2020-07-19 13:49:44 -04:00
|
|
|
}
|
2023-02-07 14:22:46 -05:00
|
|
|
}
|
|
|
|
}
|
2020-07-19 13:49:44 -04:00
|
|
|
|
2023-02-07 14:22:46 -05:00
|
|
|
setEventPhase(eventImpl, Event.NONE);
|
|
|
|
setCurrentTarget(eventImpl, null);
|
|
|
|
setPath(eventImpl, []);
|
|
|
|
setDispatched(eventImpl, false);
|
|
|
|
eventImpl.cancelBubble = false;
|
|
|
|
setStopImmediatePropagation(eventImpl, false);
|
2020-07-19 13:49:44 -04:00
|
|
|
|
2023-02-07 14:22:46 -05:00
|
|
|
if (clearTargets) {
|
|
|
|
setTarget(eventImpl, null);
|
|
|
|
setRelatedTarget(eventImpl, null);
|
|
|
|
}
|
2020-07-19 13:49:44 -04:00
|
|
|
|
2023-02-07 14:22:46 -05:00
|
|
|
// TODO(bartlomieju): invoke activation targets if HTML nodes will be implemented
|
|
|
|
// if (activationTarget !== null) {
|
|
|
|
// if (!eventImpl.defaultPrevented) {
|
|
|
|
// activationTarget._activationBehavior();
|
|
|
|
// }
|
|
|
|
// }
|
|
|
|
|
|
|
|
return !eventImpl.defaultPrevented;
|
|
|
|
}
|
|
|
|
|
|
|
|
/** Inner invoking of the event listeners where the resolved listeners are
|
|
|
|
* called.
|
|
|
|
*
|
|
|
|
* Ref: https://dom.spec.whatwg.org/#concept-event-listener-inner-invoke */
|
|
|
|
function innerInvokeEventListeners(
|
|
|
|
eventImpl,
|
|
|
|
targetListeners,
|
|
|
|
) {
|
|
|
|
let found = false;
|
|
|
|
|
|
|
|
const { type } = eventImpl;
|
|
|
|
|
|
|
|
if (!targetListeners || !targetListeners[type]) {
|
2020-07-19 13:49:44 -04:00
|
|
|
return found;
|
|
|
|
}
|
|
|
|
|
2023-02-07 14:22:46 -05:00
|
|
|
// Copy event listeners before iterating since the list can be modified during the iteration.
|
|
|
|
const handlers = ArrayPrototypeSlice(targetListeners[type]);
|
2020-07-19 13:49:44 -04:00
|
|
|
|
2023-02-07 14:22:46 -05:00
|
|
|
for (let i = 0; i < handlers.length; i++) {
|
|
|
|
const listener = handlers[i];
|
2020-07-19 13:49:44 -04:00
|
|
|
|
2023-02-07 14:22:46 -05:00
|
|
|
let capture, once, passive;
|
|
|
|
if (typeof listener.options === "boolean") {
|
|
|
|
capture = listener.options;
|
|
|
|
once = false;
|
|
|
|
passive = false;
|
|
|
|
} else {
|
|
|
|
capture = listener.options.capture;
|
|
|
|
once = listener.options.once;
|
|
|
|
passive = listener.options.passive;
|
2020-07-19 13:49:44 -04:00
|
|
|
}
|
|
|
|
|
2023-02-07 14:22:46 -05:00
|
|
|
// Check if the event listener has been removed since the listeners has been cloned.
|
|
|
|
if (!ArrayPrototypeIncludes(targetListeners[type], listener)) {
|
|
|
|
continue;
|
2022-04-13 05:50:57 -04:00
|
|
|
}
|
2020-07-19 13:49:44 -04:00
|
|
|
|
2023-02-07 14:22:46 -05:00
|
|
|
found = true;
|
2020-07-19 13:49:44 -04:00
|
|
|
|
2023-02-07 14:22:46 -05:00
|
|
|
if (
|
|
|
|
(eventImpl.eventPhase === Event.CAPTURING_PHASE && !capture) ||
|
|
|
|
(eventImpl.eventPhase === Event.BUBBLING_PHASE && capture)
|
|
|
|
) {
|
|
|
|
continue;
|
|
|
|
}
|
2020-07-19 13:49:44 -04:00
|
|
|
|
2023-02-07 14:22:46 -05:00
|
|
|
if (once) {
|
|
|
|
ArrayPrototypeSplice(
|
|
|
|
targetListeners[type],
|
|
|
|
ArrayPrototypeIndexOf(targetListeners[type], listener),
|
|
|
|
1,
|
|
|
|
);
|
|
|
|
}
|
2020-07-19 13:49:44 -04:00
|
|
|
|
2023-02-07 14:22:46 -05:00
|
|
|
if (passive) {
|
|
|
|
setInPassiveListener(eventImpl, true);
|
|
|
|
}
|
2020-07-19 13:49:44 -04:00
|
|
|
|
2023-02-07 14:22:46 -05:00
|
|
|
if (typeof listener.callback === "object") {
|
|
|
|
if (typeof listener.callback.handleEvent === "function") {
|
|
|
|
listener.callback.handleEvent(eventImpl);
|
2020-07-19 13:49:44 -04:00
|
|
|
}
|
2023-02-07 14:22:46 -05:00
|
|
|
} else {
|
|
|
|
FunctionPrototypeCall(
|
|
|
|
listener.callback,
|
|
|
|
eventImpl.currentTarget,
|
|
|
|
eventImpl,
|
|
|
|
);
|
2020-07-19 13:49:44 -04:00
|
|
|
}
|
|
|
|
|
2023-02-07 14:22:46 -05:00
|
|
|
setInPassiveListener(eventImpl, false);
|
2020-07-19 13:49:44 -04:00
|
|
|
|
2023-02-07 14:22:46 -05:00
|
|
|
if (getStopImmediatePropagation(eventImpl)) {
|
|
|
|
return found;
|
|
|
|
}
|
2020-07-19 13:49:44 -04:00
|
|
|
}
|
|
|
|
|
2023-02-07 14:22:46 -05:00
|
|
|
return found;
|
|
|
|
}
|
|
|
|
|
|
|
|
/** Invokes the listeners on a given event path with the supplied event.
|
|
|
|
*
|
|
|
|
* Ref: https://dom.spec.whatwg.org/#concept-event-listener-invoke */
|
|
|
|
function invokeEventListeners(tuple, eventImpl) {
|
|
|
|
const path = getPath(eventImpl);
|
|
|
|
const tupleIndex = ArrayPrototypeIndexOf(path, tuple);
|
|
|
|
for (let i = tupleIndex; i >= 0; i--) {
|
|
|
|
const t = path[i];
|
|
|
|
if (t.target) {
|
|
|
|
setTarget(eventImpl, t.target);
|
|
|
|
break;
|
|
|
|
}
|
2020-07-19 13:49:44 -04:00
|
|
|
}
|
|
|
|
|
2023-02-07 14:22:46 -05:00
|
|
|
setRelatedTarget(eventImpl, tuple.relatedTarget);
|
2020-07-19 13:49:44 -04:00
|
|
|
|
2023-02-07 14:22:46 -05:00
|
|
|
if (eventImpl.cancelBubble) {
|
|
|
|
return;
|
2020-07-19 13:49:44 -04:00
|
|
|
}
|
|
|
|
|
2023-02-07 14:22:46 -05:00
|
|
|
setCurrentTarget(eventImpl, tuple.item);
|
2020-07-19 13:49:44 -04:00
|
|
|
|
2023-02-07 14:22:46 -05:00
|
|
|
try {
|
|
|
|
innerInvokeEventListeners(eventImpl, getListeners(tuple.item));
|
|
|
|
} catch (error) {
|
|
|
|
reportException(error);
|
2022-03-14 15:19:22 -04:00
|
|
|
}
|
2023-02-07 14:22:46 -05:00
|
|
|
}
|
2022-03-14 15:19:22 -04:00
|
|
|
|
2023-02-07 14:22:46 -05:00
|
|
|
function normalizeEventHandlerOptions(
|
|
|
|
options,
|
|
|
|
) {
|
|
|
|
if (typeof options === "boolean" || typeof options === "undefined") {
|
2020-07-19 13:49:44 -04:00
|
|
|
return {
|
2023-02-07 14:22:46 -05:00
|
|
|
capture: Boolean(options),
|
2020-07-19 13:49:44 -04:00
|
|
|
};
|
2023-02-07 14:22:46 -05:00
|
|
|
} else {
|
|
|
|
return options;
|
2020-07-19 13:49:44 -04:00
|
|
|
}
|
2023-02-07 14:22:46 -05:00
|
|
|
}
|
2020-07-19 13:49:44 -04:00
|
|
|
|
2023-02-07 14:22:46 -05:00
|
|
|
/** Retarget the target following the spec logic.
|
|
|
|
*
|
|
|
|
* Ref: https://dom.spec.whatwg.org/#retarget */
|
|
|
|
function retarget(a, b) {
|
|
|
|
while (true) {
|
|
|
|
if (!isNode(a)) {
|
|
|
|
return a;
|
2020-07-19 13:49:44 -04:00
|
|
|
}
|
|
|
|
|
2023-02-07 14:22:46 -05:00
|
|
|
const aRoot = a.getRootNode();
|
2022-05-16 10:46:39 -04:00
|
|
|
|
2023-02-07 14:22:46 -05:00
|
|
|
if (aRoot) {
|
|
|
|
if (
|
|
|
|
!isShadowRoot(aRoot) ||
|
|
|
|
(isNode(b) && isShadowInclusiveAncestor(aRoot, b))
|
|
|
|
) {
|
|
|
|
return a;
|
|
|
|
}
|
2022-05-16 10:46:39 -04:00
|
|
|
|
2023-02-07 14:22:46 -05:00
|
|
|
a = getHost(aRoot);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2022-05-16 10:46:39 -04:00
|
|
|
|
2023-02-07 14:22:46 -05:00
|
|
|
// Accessors for non-public data
|
2020-07-19 13:49:44 -04:00
|
|
|
|
2023-02-07 14:22:46 -05:00
|
|
|
const eventTargetData = Symbol();
|
2020-07-19 13:49:44 -04:00
|
|
|
|
2023-02-07 14:22:46 -05:00
|
|
|
function setEventTargetData(target) {
|
|
|
|
target[eventTargetData] = getDefaultTargetData();
|
|
|
|
}
|
2020-07-19 13:49:44 -04:00
|
|
|
|
2023-02-07 14:22:46 -05:00
|
|
|
function getAssignedSlot(target) {
|
|
|
|
return Boolean(target?.[eventTargetData]?.assignedSlot);
|
|
|
|
}
|
2021-07-10 10:13:58 -04:00
|
|
|
|
2023-02-07 14:22:46 -05:00
|
|
|
function getHasActivationBehavior(target) {
|
|
|
|
return Boolean(target?.[eventTargetData]?.hasActivationBehavior);
|
|
|
|
}
|
2020-07-19 13:49:44 -04:00
|
|
|
|
2023-02-07 14:22:46 -05:00
|
|
|
function getHost(target) {
|
|
|
|
return target?.[eventTargetData]?.host ?? null;
|
|
|
|
}
|
2020-07-19 13:49:44 -04:00
|
|
|
|
2023-02-07 14:22:46 -05:00
|
|
|
function getListeners(target) {
|
|
|
|
return target?.[eventTargetData]?.listeners ?? {};
|
|
|
|
}
|
2020-07-19 13:49:44 -04:00
|
|
|
|
2023-02-07 14:22:46 -05:00
|
|
|
function getMode(target) {
|
|
|
|
return target?.[eventTargetData]?.mode ?? null;
|
|
|
|
}
|
2020-07-19 13:49:44 -04:00
|
|
|
|
2023-02-07 14:22:46 -05:00
|
|
|
function listenerCount(target, type) {
|
|
|
|
return getListeners(target)?.[type]?.length ?? 0;
|
|
|
|
}
|
2020-07-19 13:49:44 -04:00
|
|
|
|
2023-02-07 14:22:46 -05:00
|
|
|
function getDefaultTargetData() {
|
|
|
|
return {
|
|
|
|
assignedSlot: false,
|
|
|
|
hasActivationBehavior: false,
|
|
|
|
host: null,
|
|
|
|
listeners: ObjectCreate(null),
|
|
|
|
mode: "",
|
|
|
|
};
|
|
|
|
}
|
2020-07-19 13:49:44 -04:00
|
|
|
|
2023-02-07 14:22:46 -05:00
|
|
|
// This is lazy loaded because there is a circular dependency with AbortSignal.
|
|
|
|
let addEventListenerOptionsConverter;
|
2020-07-19 13:49:44 -04:00
|
|
|
|
2023-02-07 14:22:46 -05:00
|
|
|
function lazyAddEventListenerOptionsConverter() {
|
|
|
|
addEventListenerOptionsConverter ??= webidl.createDictionaryConverter(
|
|
|
|
"AddEventListenerOptions",
|
|
|
|
[
|
|
|
|
{
|
|
|
|
key: "capture",
|
|
|
|
defaultValue: false,
|
|
|
|
converter: webidl.converters.boolean,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
key: "passive",
|
|
|
|
defaultValue: false,
|
|
|
|
converter: webidl.converters.boolean,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
key: "once",
|
|
|
|
defaultValue: false,
|
|
|
|
converter: webidl.converters.boolean,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
key: "signal",
|
|
|
|
converter: webidl.converters.AbortSignal,
|
|
|
|
},
|
|
|
|
],
|
|
|
|
);
|
|
|
|
}
|
2020-07-19 13:49:44 -04:00
|
|
|
|
2023-02-07 14:22:46 -05:00
|
|
|
webidl.converters.AddEventListenerOptions = (V, opts) => {
|
|
|
|
if (webidl.type(V) !== "Object" || V === null) {
|
|
|
|
V = { capture: Boolean(V) };
|
|
|
|
}
|
2020-07-19 13:49:44 -04:00
|
|
|
|
2023-02-07 14:22:46 -05:00
|
|
|
lazyAddEventListenerOptionsConverter();
|
|
|
|
return addEventListenerOptionsConverter(V, opts);
|
|
|
|
};
|
2020-07-19 13:49:44 -04:00
|
|
|
|
2023-02-07 14:22:46 -05:00
|
|
|
class EventTarget {
|
|
|
|
constructor() {
|
|
|
|
this[eventTargetData] = getDefaultTargetData();
|
|
|
|
this[webidl.brand] = webidl.brand;
|
2020-07-19 13:49:44 -04:00
|
|
|
}
|
|
|
|
|
2023-02-07 14:22:46 -05:00
|
|
|
addEventListener(
|
|
|
|
type,
|
|
|
|
callback,
|
|
|
|
options,
|
|
|
|
) {
|
|
|
|
const self = this ?? globalThis_;
|
|
|
|
webidl.assertBranded(self, EventTargetPrototype);
|
|
|
|
const prefix = "Failed to execute 'addEventListener' on 'EventTarget'";
|
2021-09-24 13:07:22 -04:00
|
|
|
|
2023-02-07 14:22:46 -05:00
|
|
|
webidl.requiredArguments(arguments.length, 2, {
|
|
|
|
prefix,
|
|
|
|
});
|
2020-07-19 13:49:44 -04:00
|
|
|
|
2023-02-07 14:22:46 -05:00
|
|
|
options = webidl.converters.AddEventListenerOptions(options, {
|
|
|
|
prefix,
|
|
|
|
context: "Argument 3",
|
|
|
|
});
|
2020-07-19 13:49:44 -04:00
|
|
|
|
2023-02-07 14:22:46 -05:00
|
|
|
if (callback === null) {
|
|
|
|
return;
|
2020-07-19 13:49:44 -04:00
|
|
|
}
|
2023-02-07 14:22:46 -05:00
|
|
|
|
|
|
|
const { listeners } = self[eventTargetData];
|
|
|
|
|
|
|
|
if (!(ReflectHas(listeners, type))) {
|
|
|
|
listeners[type] = [];
|
2020-07-19 13:49:44 -04:00
|
|
|
}
|
2023-02-07 14:22:46 -05:00
|
|
|
|
|
|
|
const listenerList = listeners[type];
|
|
|
|
for (let i = 0; i < listenerList.length; ++i) {
|
|
|
|
const listener = listenerList[i];
|
|
|
|
if (
|
|
|
|
((typeof listener.options === "boolean" &&
|
|
|
|
listener.options === options.capture) ||
|
|
|
|
(typeof listener.options === "object" &&
|
|
|
|
listener.options.capture === options.capture)) &&
|
|
|
|
listener.callback === callback
|
|
|
|
) {
|
|
|
|
return;
|
|
|
|
}
|
2020-07-19 13:49:44 -04:00
|
|
|
}
|
2023-02-07 14:22:46 -05:00
|
|
|
if (options?.signal) {
|
|
|
|
const signal = options?.signal;
|
|
|
|
if (signal.aborted) {
|
|
|
|
// If signal is not null and its aborted flag is set, then return.
|
|
|
|
return;
|
|
|
|
} else {
|
2023-03-22 21:34:14 -04:00
|
|
|
// If listener's signal is not null, then add the following abort
|
2023-02-07 14:22:46 -05:00
|
|
|
// abort steps to it: Remove an event listener.
|
|
|
|
signal.addEventListener("abort", () => {
|
|
|
|
self.removeEventListener(type, callback, options);
|
|
|
|
});
|
|
|
|
}
|
2020-07-19 13:49:44 -04:00
|
|
|
}
|
|
|
|
|
2023-02-07 14:22:46 -05:00
|
|
|
ArrayPrototypePush(listeners[type], { callback, options });
|
|
|
|
}
|
2020-07-19 13:49:44 -04:00
|
|
|
|
2023-02-07 14:22:46 -05:00
|
|
|
removeEventListener(
|
|
|
|
type,
|
|
|
|
callback,
|
|
|
|
options,
|
|
|
|
) {
|
|
|
|
const self = this ?? globalThis_;
|
|
|
|
webidl.assertBranded(self, EventTargetPrototype);
|
|
|
|
webidl.requiredArguments(arguments.length, 2, {
|
|
|
|
prefix: "Failed to execute 'removeEventListener' on 'EventTarget'",
|
|
|
|
});
|
2020-07-19 13:49:44 -04:00
|
|
|
|
2023-02-07 14:22:46 -05:00
|
|
|
const { listeners } = self[eventTargetData];
|
|
|
|
if (callback !== null && ReflectHas(listeners, type)) {
|
|
|
|
listeners[type] = ArrayPrototypeFilter(
|
|
|
|
listeners[type],
|
|
|
|
(listener) => listener.callback !== callback,
|
|
|
|
);
|
|
|
|
} else if (callback === null || !listeners[type]) {
|
|
|
|
return;
|
2020-10-26 18:22:03 -04:00
|
|
|
}
|
2021-09-24 13:07:22 -04:00
|
|
|
|
2023-02-07 14:22:46 -05:00
|
|
|
options = normalizeEventHandlerOptions(options);
|
2020-07-19 13:49:44 -04:00
|
|
|
|
2023-02-07 14:22:46 -05:00
|
|
|
for (let i = 0; i < listeners[type].length; ++i) {
|
|
|
|
const listener = listeners[type][i];
|
|
|
|
if (
|
|
|
|
((typeof listener.options === "boolean" &&
|
|
|
|
listener.options === options.capture) ||
|
|
|
|
(typeof listener.options === "object" &&
|
|
|
|
listener.options.capture === options.capture)) &&
|
|
|
|
listener.callback === callback
|
|
|
|
) {
|
|
|
|
ArrayPrototypeSplice(listeners[type], i, 1);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2020-07-19 13:49:44 -04:00
|
|
|
|
2023-02-07 14:22:46 -05:00
|
|
|
dispatchEvent(event) {
|
|
|
|
// If `this` is not present, then fallback to global scope. We don't use
|
|
|
|
// `globalThis` directly here, because it could be deleted by user.
|
|
|
|
// Instead use saved reference to global scope when the script was
|
|
|
|
// executed.
|
|
|
|
const self = this ?? globalThis_;
|
|
|
|
webidl.assertBranded(self, EventTargetPrototype);
|
|
|
|
webidl.requiredArguments(arguments.length, 1, {
|
|
|
|
prefix: "Failed to execute 'dispatchEvent' on 'EventTarget'",
|
|
|
|
});
|
2020-09-05 10:39:25 -04:00
|
|
|
|
2023-03-08 20:08:54 -05:00
|
|
|
// This is an optimization to avoid creating an event listener
|
|
|
|
// on each startup.
|
|
|
|
// Stores the flag for checking whether unload is dispatched or not.
|
|
|
|
// This prevents the recursive dispatches of unload events.
|
|
|
|
// See https://github.com/denoland/deno/issues/9201.
|
|
|
|
if (event.type === "unload" && self === globalThis_) {
|
2023-03-08 23:09:40 -05:00
|
|
|
globalThis_[SymbolFor("Deno.isUnloadDispatched")] = true;
|
2023-03-08 20:08:54 -05:00
|
|
|
}
|
|
|
|
|
2023-02-07 14:22:46 -05:00
|
|
|
const { listeners } = self[eventTargetData];
|
|
|
|
if (!ReflectHas(listeners, event.type)) {
|
|
|
|
setTarget(event, this);
|
|
|
|
return true;
|
2020-09-05 10:39:25 -04:00
|
|
|
}
|
2023-02-07 14:22:46 -05:00
|
|
|
|
|
|
|
if (getDispatched(event)) {
|
|
|
|
throw new DOMException("Invalid event state.", "InvalidStateError");
|
2020-09-05 10:39:25 -04:00
|
|
|
}
|
2023-02-07 14:22:46 -05:00
|
|
|
|
|
|
|
if (event.eventPhase !== Event.NONE) {
|
|
|
|
throw new DOMException("Invalid event state.", "InvalidStateError");
|
2020-09-05 10:39:25 -04:00
|
|
|
}
|
|
|
|
|
2023-02-07 14:22:46 -05:00
|
|
|
return dispatch(self, event);
|
|
|
|
}
|
|
|
|
|
|
|
|
getParent(_event) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
webidl.configurePrototype(EventTarget);
|
|
|
|
const EventTargetPrototype = EventTarget.prototype;
|
|
|
|
|
|
|
|
defineEnumerableProps(EventTarget, [
|
|
|
|
"addEventListener",
|
|
|
|
"removeEventListener",
|
|
|
|
"dispatchEvent",
|
|
|
|
]);
|
|
|
|
|
|
|
|
class ErrorEvent extends Event {
|
|
|
|
#message = "";
|
|
|
|
#filename = "";
|
|
|
|
#lineno = "";
|
|
|
|
#colno = "";
|
|
|
|
#error = "";
|
|
|
|
|
|
|
|
get message() {
|
|
|
|
return this.#message;
|
|
|
|
}
|
|
|
|
get filename() {
|
|
|
|
return this.#filename;
|
|
|
|
}
|
|
|
|
get lineno() {
|
|
|
|
return this.#lineno;
|
|
|
|
}
|
|
|
|
get colno() {
|
|
|
|
return this.#colno;
|
|
|
|
}
|
|
|
|
get error() {
|
|
|
|
return this.#error;
|
|
|
|
}
|
|
|
|
|
|
|
|
constructor(
|
|
|
|
type,
|
|
|
|
{
|
2020-09-05 10:39:25 -04:00
|
|
|
bubbles,
|
|
|
|
cancelable,
|
|
|
|
composed,
|
2023-02-07 14:22:46 -05:00
|
|
|
message = "",
|
|
|
|
filename = "",
|
|
|
|
lineno = 0,
|
|
|
|
colno = 0,
|
|
|
|
error,
|
|
|
|
} = {},
|
|
|
|
) {
|
|
|
|
super(type, {
|
|
|
|
bubbles: bubbles,
|
|
|
|
cancelable: cancelable,
|
|
|
|
composed: composed,
|
|
|
|
});
|
2020-09-05 10:39:25 -04:00
|
|
|
|
2023-02-07 14:22:46 -05:00
|
|
|
this.#message = message;
|
|
|
|
this.#filename = filename;
|
|
|
|
this.#lineno = lineno;
|
|
|
|
this.#colno = colno;
|
|
|
|
this.#error = error;
|
|
|
|
}
|
2020-10-26 18:22:03 -04:00
|
|
|
|
2023-02-07 14:22:46 -05:00
|
|
|
[SymbolFor("Deno.privateCustomInspect")](inspect) {
|
|
|
|
return inspect(createFilteredInspectProxy({
|
|
|
|
object: this,
|
|
|
|
evaluate: ObjectPrototypeIsPrototypeOf(ErrorEvent.prototype, this),
|
|
|
|
keys: [
|
|
|
|
...new SafeArrayIterator(EVENT_PROPS),
|
|
|
|
"message",
|
|
|
|
"filename",
|
|
|
|
"lineno",
|
|
|
|
"colno",
|
|
|
|
"error",
|
|
|
|
],
|
|
|
|
}));
|
2020-09-05 10:39:25 -04:00
|
|
|
}
|
|
|
|
|
2023-02-07 14:22:46 -05:00
|
|
|
// TODO(lucacasonato): remove when this interface is spec aligned
|
|
|
|
[SymbolToStringTag] = "ErrorEvent";
|
|
|
|
}
|
|
|
|
|
|
|
|
defineEnumerableProps(ErrorEvent, [
|
|
|
|
"message",
|
|
|
|
"filename",
|
|
|
|
"lineno",
|
|
|
|
"colno",
|
|
|
|
"error",
|
|
|
|
]);
|
|
|
|
|
|
|
|
class CloseEvent extends Event {
|
|
|
|
#wasClean = "";
|
|
|
|
#code = "";
|
|
|
|
#reason = "";
|
|
|
|
|
|
|
|
get wasClean() {
|
|
|
|
return this.#wasClean;
|
|
|
|
}
|
|
|
|
get code() {
|
|
|
|
return this.#code;
|
|
|
|
}
|
|
|
|
get reason() {
|
|
|
|
return this.#reason;
|
|
|
|
}
|
2021-05-22 12:08:24 -04:00
|
|
|
|
2023-02-07 14:22:46 -05:00
|
|
|
constructor(type, {
|
|
|
|
bubbles,
|
|
|
|
cancelable,
|
|
|
|
composed,
|
|
|
|
wasClean = false,
|
|
|
|
code = 0,
|
|
|
|
reason = "",
|
|
|
|
} = {}) {
|
|
|
|
super(type, {
|
|
|
|
bubbles: bubbles,
|
|
|
|
cancelable: cancelable,
|
|
|
|
composed: composed,
|
|
|
|
});
|
2020-09-05 10:39:25 -04:00
|
|
|
|
2023-02-07 14:22:46 -05:00
|
|
|
this.#wasClean = wasClean;
|
|
|
|
this.#code = code;
|
|
|
|
this.#reason = reason;
|
|
|
|
}
|
2020-10-26 18:22:03 -04:00
|
|
|
|
2023-02-07 14:22:46 -05:00
|
|
|
[SymbolFor("Deno.privateCustomInspect")](inspect) {
|
|
|
|
return inspect(createFilteredInspectProxy({
|
|
|
|
object: this,
|
|
|
|
evaluate: ObjectPrototypeIsPrototypeOf(CloseEvent.prototype, this),
|
|
|
|
keys: [
|
|
|
|
...new SafeArrayIterator(EVENT_PROPS),
|
|
|
|
"wasClean",
|
|
|
|
"code",
|
|
|
|
"reason",
|
|
|
|
],
|
|
|
|
}));
|
|
|
|
}
|
|
|
|
}
|
2021-09-24 13:07:22 -04:00
|
|
|
|
2023-02-07 14:22:46 -05:00
|
|
|
class MessageEvent extends Event {
|
|
|
|
get source() {
|
|
|
|
return null;
|
2020-09-05 10:39:25 -04:00
|
|
|
}
|
|
|
|
|
2023-02-07 14:22:46 -05:00
|
|
|
constructor(type, eventInitDict) {
|
|
|
|
super(type, {
|
|
|
|
bubbles: eventInitDict?.bubbles ?? false,
|
|
|
|
cancelable: eventInitDict?.cancelable ?? false,
|
|
|
|
composed: eventInitDict?.composed ?? false,
|
|
|
|
[_skipInternalInit]: eventInitDict?.[_skipInternalInit],
|
|
|
|
});
|
2020-07-19 13:49:44 -04:00
|
|
|
|
2023-02-07 14:22:46 -05:00
|
|
|
this.data = eventInitDict?.data ?? null;
|
|
|
|
this.ports = eventInitDict?.ports ?? [];
|
|
|
|
this.origin = eventInitDict?.origin ?? "";
|
|
|
|
this.lastEventId = eventInitDict?.lastEventId ?? "";
|
|
|
|
}
|
2020-07-19 13:49:44 -04:00
|
|
|
|
2023-02-07 14:22:46 -05:00
|
|
|
[SymbolFor("Deno.privateCustomInspect")](inspect) {
|
|
|
|
return inspect(createFilteredInspectProxy({
|
|
|
|
object: this,
|
|
|
|
evaluate: ObjectPrototypeIsPrototypeOf(MessageEvent.prototype, this),
|
|
|
|
keys: [
|
|
|
|
...new SafeArrayIterator(EVENT_PROPS),
|
|
|
|
"data",
|
|
|
|
"origin",
|
|
|
|
"lastEventId",
|
|
|
|
],
|
|
|
|
}));
|
|
|
|
}
|
2020-07-19 13:49:44 -04:00
|
|
|
|
2023-02-07 14:22:46 -05:00
|
|
|
// TODO(lucacasonato): remove when this interface is spec aligned
|
|
|
|
[SymbolToStringTag] = "CloseEvent";
|
|
|
|
}
|
2021-09-24 13:07:22 -04:00
|
|
|
|
2023-02-07 14:22:46 -05:00
|
|
|
class CustomEvent extends Event {
|
|
|
|
#detail = null;
|
|
|
|
|
|
|
|
constructor(type, eventInitDict = {}) {
|
|
|
|
super(type, eventInitDict);
|
|
|
|
webidl.requiredArguments(arguments.length, 1, {
|
|
|
|
prefix: "Failed to construct 'CustomEvent'",
|
|
|
|
});
|
|
|
|
const { detail } = eventInitDict;
|
|
|
|
this.#detail = detail;
|
2020-07-19 13:49:44 -04:00
|
|
|
}
|
|
|
|
|
2023-02-07 14:22:46 -05:00
|
|
|
get detail() {
|
|
|
|
return this.#detail;
|
|
|
|
}
|
2020-07-19 13:49:44 -04:00
|
|
|
|
2023-02-07 14:22:46 -05:00
|
|
|
[SymbolFor("Deno.privateCustomInspect")](inspect) {
|
|
|
|
return inspect(createFilteredInspectProxy({
|
|
|
|
object: this,
|
|
|
|
evaluate: ObjectPrototypeIsPrototypeOf(CustomEvent.prototype, this),
|
|
|
|
keys: [
|
|
|
|
...new SafeArrayIterator(EVENT_PROPS),
|
|
|
|
"detail",
|
|
|
|
],
|
|
|
|
}));
|
|
|
|
}
|
2020-09-18 10:01:50 -04:00
|
|
|
|
2023-02-07 14:22:46 -05:00
|
|
|
// TODO(lucacasonato): remove when this interface is spec aligned
|
|
|
|
[SymbolToStringTag] = "CustomEvent";
|
|
|
|
}
|
2020-10-26 18:22:03 -04:00
|
|
|
|
2023-02-07 14:22:46 -05:00
|
|
|
ReflectDefineProperty(CustomEvent.prototype, "detail", {
|
|
|
|
enumerable: true,
|
|
|
|
});
|
2021-09-24 13:07:22 -04:00
|
|
|
|
2023-02-07 14:22:46 -05:00
|
|
|
// ProgressEvent could also be used in other DOM progress event emits.
|
|
|
|
// Current use is for FileReader.
|
|
|
|
class ProgressEvent extends Event {
|
|
|
|
constructor(type, eventInitDict = {}) {
|
|
|
|
super(type, eventInitDict);
|
|
|
|
|
|
|
|
this.lengthComputable = eventInitDict?.lengthComputable ?? false;
|
|
|
|
this.loaded = eventInitDict?.loaded ?? 0;
|
|
|
|
this.total = eventInitDict?.total ?? 0;
|
2020-09-18 10:01:50 -04:00
|
|
|
}
|
|
|
|
|
2023-02-07 14:22:46 -05:00
|
|
|
[SymbolFor("Deno.privateCustomInspect")](inspect) {
|
|
|
|
return inspect(createFilteredInspectProxy({
|
|
|
|
object: this,
|
|
|
|
evaluate: ObjectPrototypeIsPrototypeOf(ProgressEvent.prototype, this),
|
|
|
|
keys: [
|
|
|
|
...new SafeArrayIterator(EVENT_PROPS),
|
|
|
|
"lengthComputable",
|
|
|
|
"loaded",
|
|
|
|
"total",
|
|
|
|
],
|
|
|
|
}));
|
|
|
|
}
|
2022-07-20 14:28:19 -04:00
|
|
|
|
2023-02-07 14:22:46 -05:00
|
|
|
// TODO(lucacasonato): remove when this interface is spec aligned
|
|
|
|
[SymbolToStringTag] = "ProgressEvent";
|
|
|
|
}
|
2022-07-20 14:28:19 -04:00
|
|
|
|
2023-02-07 14:22:46 -05:00
|
|
|
class PromiseRejectionEvent extends Event {
|
|
|
|
#promise = null;
|
|
|
|
#reason = null;
|
2022-07-20 14:28:19 -04:00
|
|
|
|
2023-02-07 14:22:46 -05:00
|
|
|
get promise() {
|
|
|
|
return this.#promise;
|
|
|
|
}
|
|
|
|
get reason() {
|
|
|
|
return this.#reason;
|
|
|
|
}
|
2022-07-20 14:28:19 -04:00
|
|
|
|
2023-02-07 14:22:46 -05:00
|
|
|
constructor(
|
|
|
|
type,
|
|
|
|
{
|
|
|
|
bubbles,
|
|
|
|
cancelable,
|
|
|
|
composed,
|
|
|
|
promise,
|
|
|
|
reason,
|
|
|
|
} = {},
|
|
|
|
) {
|
|
|
|
super(type, {
|
|
|
|
bubbles: bubbles,
|
|
|
|
cancelable: cancelable,
|
|
|
|
composed: composed,
|
|
|
|
});
|
2022-07-20 14:28:19 -04:00
|
|
|
|
2023-02-07 14:22:46 -05:00
|
|
|
this.#promise = promise;
|
|
|
|
this.#reason = reason;
|
2022-07-20 14:28:19 -04:00
|
|
|
}
|
|
|
|
|
2023-02-07 14:22:46 -05:00
|
|
|
[SymbolFor("Deno.privateCustomInspect")](inspect) {
|
|
|
|
return inspect(createFilteredInspectProxy({
|
|
|
|
object: this,
|
|
|
|
evaluate: ObjectPrototypeIsPrototypeOf(
|
|
|
|
PromiseRejectionEvent.prototype,
|
|
|
|
this,
|
|
|
|
),
|
|
|
|
keys: [
|
|
|
|
...new SafeArrayIterator(EVENT_PROPS),
|
|
|
|
"promise",
|
|
|
|
"reason",
|
|
|
|
],
|
|
|
|
}));
|
|
|
|
}
|
2022-07-20 14:28:19 -04:00
|
|
|
|
2023-02-07 14:22:46 -05:00
|
|
|
// TODO(lucacasonato): remove when this interface is spec aligned
|
|
|
|
[SymbolToStringTag] = "PromiseRejectionEvent";
|
|
|
|
}
|
2021-06-21 13:53:52 -04:00
|
|
|
|
2023-02-07 14:22:46 -05:00
|
|
|
defineEnumerableProps(PromiseRejectionEvent, [
|
|
|
|
"promise",
|
|
|
|
"reason",
|
|
|
|
]);
|
2021-10-08 03:53:31 -04:00
|
|
|
|
2023-02-07 14:22:46 -05:00
|
|
|
const _eventHandlers = Symbol("eventHandlers");
|
2021-10-08 03:53:31 -04:00
|
|
|
|
2023-02-07 14:22:46 -05:00
|
|
|
function makeWrappedHandler(handler, isSpecialErrorEventHandler) {
|
|
|
|
function wrappedHandler(evt) {
|
|
|
|
if (typeof wrappedHandler.handler !== "function") {
|
|
|
|
return;
|
2021-06-21 13:53:52 -04:00
|
|
|
}
|
2021-07-04 10:43:21 -04:00
|
|
|
|
2023-02-07 14:22:46 -05:00
|
|
|
if (
|
|
|
|
isSpecialErrorEventHandler &&
|
|
|
|
ObjectPrototypeIsPrototypeOf(ErrorEvent.prototype, evt) &&
|
|
|
|
evt.type === "error"
|
|
|
|
) {
|
|
|
|
const ret = FunctionPrototypeCall(
|
|
|
|
wrappedHandler.handler,
|
|
|
|
this,
|
|
|
|
evt.message,
|
|
|
|
evt.filename,
|
|
|
|
evt.lineno,
|
|
|
|
evt.colno,
|
|
|
|
evt.error,
|
|
|
|
);
|
|
|
|
if (ret === true) {
|
|
|
|
evt.preventDefault();
|
|
|
|
}
|
|
|
|
return;
|
|
|
|
}
|
2021-10-08 03:53:31 -04:00
|
|
|
|
2023-02-07 14:22:46 -05:00
|
|
|
return FunctionPrototypeCall(wrappedHandler.handler, this, evt);
|
2021-06-21 13:53:52 -04:00
|
|
|
}
|
2023-02-07 14:22:46 -05:00
|
|
|
wrappedHandler.handler = handler;
|
|
|
|
return wrappedHandler;
|
|
|
|
}
|
|
|
|
|
|
|
|
// `init` is an optional function that will be called the first time that the
|
|
|
|
// event handler property is set. It will be called with the object on which
|
|
|
|
// the property is set as its argument.
|
|
|
|
// `isSpecialErrorEventHandler` can be set to true to opt into the special
|
|
|
|
// behavior of event handlers for the "error" event in a global scope.
|
|
|
|
function defineEventHandler(
|
|
|
|
emitter,
|
|
|
|
name,
|
|
|
|
init = undefined,
|
|
|
|
isSpecialErrorEventHandler = false,
|
|
|
|
) {
|
|
|
|
// HTML specification section 8.1.7.1
|
|
|
|
ObjectDefineProperty(emitter, `on${name}`, {
|
|
|
|
get() {
|
|
|
|
if (!this[_eventHandlers]) {
|
|
|
|
return null;
|
|
|
|
}
|
2021-06-21 13:53:52 -04:00
|
|
|
|
2023-02-07 14:22:46 -05:00
|
|
|
return MapPrototypeGet(this[_eventHandlers], name)?.handler ?? null;
|
|
|
|
},
|
|
|
|
set(value) {
|
|
|
|
// All three Web IDL event handler types are nullable callback functions
|
|
|
|
// with the [LegacyTreatNonObjectAsNull] extended attribute, meaning
|
|
|
|
// anything other than an object is treated as null.
|
|
|
|
if (typeof value !== "object" && typeof value !== "function") {
|
|
|
|
value = null;
|
2022-04-13 05:50:57 -04:00
|
|
|
}
|
|
|
|
|
2023-02-07 14:22:46 -05:00
|
|
|
if (!this[_eventHandlers]) {
|
|
|
|
this[_eventHandlers] = new Map();
|
|
|
|
}
|
|
|
|
let handlerWrapper = MapPrototypeGet(this[_eventHandlers], name);
|
|
|
|
if (handlerWrapper) {
|
|
|
|
handlerWrapper.handler = value;
|
|
|
|
} else if (value !== null) {
|
|
|
|
handlerWrapper = makeWrappedHandler(
|
|
|
|
value,
|
|
|
|
isSpecialErrorEventHandler,
|
|
|
|
);
|
|
|
|
this.addEventListener(name, handlerWrapper);
|
|
|
|
init?.(this);
|
|
|
|
}
|
|
|
|
MapPrototypeSet(this[_eventHandlers], name, handlerWrapper);
|
|
|
|
},
|
|
|
|
configurable: true,
|
|
|
|
enumerable: true,
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
let reportExceptionStackedCalls = 0;
|
|
|
|
|
|
|
|
// https://html.spec.whatwg.org/#report-the-exception
|
|
|
|
function reportException(error) {
|
|
|
|
reportExceptionStackedCalls++;
|
|
|
|
const jsError = core.destructureError(error);
|
|
|
|
const message = jsError.exceptionMessage;
|
|
|
|
let filename = "";
|
|
|
|
let lineno = 0;
|
|
|
|
let colno = 0;
|
|
|
|
if (jsError.frames.length > 0) {
|
|
|
|
filename = jsError.frames[0].fileName;
|
|
|
|
lineno = jsError.frames[0].lineNumber;
|
|
|
|
colno = jsError.frames[0].columnNumber;
|
|
|
|
} else {
|
|
|
|
const jsError = core.destructureError(new Error());
|
|
|
|
const frames = jsError.frames;
|
|
|
|
for (let i = 0; i < frames.length; ++i) {
|
|
|
|
const frame = frames[i];
|
|
|
|
if (
|
|
|
|
typeof frame.fileName == "string" &&
|
2023-03-08 06:44:54 -05:00
|
|
|
!StringPrototypeStartsWith(frame.fileName, "ext:")
|
2023-02-07 14:22:46 -05:00
|
|
|
) {
|
|
|
|
filename = frame.fileName;
|
|
|
|
lineno = frame.lineNumber;
|
|
|
|
colno = frame.columnNumber;
|
|
|
|
break;
|
|
|
|
}
|
2022-04-19 04:59:51 -04:00
|
|
|
}
|
|
|
|
}
|
2023-02-07 14:22:46 -05:00
|
|
|
const event = new ErrorEvent("error", {
|
|
|
|
cancelable: true,
|
|
|
|
message,
|
|
|
|
filename,
|
|
|
|
lineno,
|
|
|
|
colno,
|
|
|
|
error,
|
|
|
|
});
|
|
|
|
// Avoid recursing `reportException()` via error handlers more than once.
|
|
|
|
if (reportExceptionStackedCalls > 1 || globalThis_.dispatchEvent(event)) {
|
|
|
|
ops.op_dispatch_exception(error);
|
2022-04-19 04:59:51 -04:00
|
|
|
}
|
2023-02-07 14:22:46 -05:00
|
|
|
reportExceptionStackedCalls--;
|
|
|
|
}
|
2022-04-19 04:59:51 -04:00
|
|
|
|
2023-02-07 14:22:46 -05:00
|
|
|
function checkThis(thisArg) {
|
|
|
|
if (thisArg !== null && thisArg !== undefined && thisArg !== globalThis_) {
|
|
|
|
throw new TypeError("Illegal invocation");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// https://html.spec.whatwg.org/#dom-reporterror
|
|
|
|
function reportError(error) {
|
|
|
|
checkThis(this);
|
|
|
|
const prefix = "Failed to call 'reportError'";
|
|
|
|
webidl.requiredArguments(arguments.length, 1, { prefix });
|
|
|
|
reportException(error);
|
|
|
|
}
|
|
|
|
|
|
|
|
export {
|
|
|
|
_skipInternalInit,
|
|
|
|
CloseEvent,
|
|
|
|
CustomEvent,
|
|
|
|
defineEventHandler,
|
|
|
|
ErrorEvent,
|
|
|
|
Event,
|
|
|
|
EventTarget,
|
|
|
|
listenerCount,
|
|
|
|
MessageEvent,
|
|
|
|
ProgressEvent,
|
|
|
|
PromiseRejectionEvent,
|
|
|
|
reportError,
|
|
|
|
reportException,
|
|
|
|
saveGlobalThisReference,
|
|
|
|
setEventTargetData,
|
|
|
|
setIsTrusted,
|
|
|
|
setTarget,
|
|
|
|
};
|