1
0
Fork 0
mirror of https://github.com/denoland/deno.git synced 2024-11-14 16:33:45 -05:00
denoland-deno/cli/js/web/event_target.ts

498 lines
14 KiB
TypeScript
Raw Normal View History

2020-01-02 15:13:47 -05:00
// Copyright 2018-2020 the Deno authors. All rights reserved. MIT license.
import * as domTypes from "./dom_types.ts";
import { hasOwnProperty, requiredArguments } from "./util.ts";
2019-05-27 09:20:34 -04:00
import {
getRoot,
isNode,
isShadowRoot,
isShadowInclusiveAncestor,
isSlotable,
retarget
} from "./dom_util.ts";
2019-05-27 09:20:34 -04:00
// https://dom.spec.whatwg.org/#get-the-parent
// Note: Nodes, shadow roots, and documents override this algorithm so we set it to null.
function getEventTargetParent(
_eventTarget: domTypes.EventTarget,
_event: domTypes.Event
): null {
return null;
}
2019-07-16 00:19:26 -04:00
export const eventTargetAssignedSlot: unique symbol = Symbol();
export const eventTargetHasActivationBehavior: unique symbol = Symbol();
2019-01-05 10:02:44 -05:00
export class EventTarget implements domTypes.EventTarget {
2019-07-16 00:19:26 -04:00
public [domTypes.eventTargetHost]: domTypes.EventTarget | null = null;
public [domTypes.eventTargetListeners]: {
[type in string]: domTypes.EventTargetListener[];
2019-07-16 00:19:26 -04:00
} = {};
public [domTypes.eventTargetMode] = "";
public [domTypes.eventTargetNodeType]: domTypes.NodeType =
domTypes.NodeType.DOCUMENT_FRAGMENT_NODE;
private [eventTargetAssignedSlot] = false;
private [eventTargetHasActivationBehavior] = false;
2019-01-05 10:02:44 -05:00
public addEventListener(
type: string,
callback: domTypes.EventListenerOrEventListenerObject | null,
2019-05-27 09:20:34 -04:00
options?: domTypes.AddEventListenerOptions | boolean
2019-01-05 10:02:44 -05:00
): void {
const this_ = this || globalThis;
2019-03-30 13:40:03 -04:00
requiredArguments("EventTarget.addEventListener", arguments.length, 2);
2019-07-16 00:19:26 -04:00
const normalizedOptions: domTypes.AddEventListenerOptions = eventTargetHelpers.normalizeAddEventHandlerOptions(
2019-05-27 09:20:34 -04:00
options
);
if (callback === null) {
return;
}
const listeners = this_[domTypes.eventTargetListeners];
2019-07-16 00:19:26 -04:00
if (!hasOwnProperty(listeners, type)) {
listeners[type] = [];
2019-01-05 10:02:44 -05:00
}
2019-05-27 09:20:34 -04:00
2019-07-16 00:19:26 -04:00
for (let i = 0; i < listeners[type].length; ++i) {
const listener = listeners[type][i];
2019-05-27 09:20:34 -04:00
if (
((typeof listener.options === "boolean" &&
listener.options === normalizedOptions.capture) ||
(typeof listener.options === "object" &&
listener.options.capture === normalizedOptions.capture)) &&
listener.callback === callback
) {
return;
}
2019-01-05 10:02:44 -05:00
}
2019-05-27 09:20:34 -04:00
listeners[type].push({
callback,
options: normalizedOptions
});
2019-01-05 10:02:44 -05:00
}
public removeEventListener(
type: string,
callback: domTypes.EventListenerOrEventListenerObject | null,
2019-05-27 09:20:34 -04:00
options?: domTypes.EventListenerOptions | boolean
2019-01-05 10:02:44 -05:00
): void {
const this_ = this || globalThis;
2019-03-30 13:40:03 -04:00
requiredArguments("EventTarget.removeEventListener", arguments.length, 2);
const listeners = this_[domTypes.eventTargetListeners];
2019-07-16 00:19:26 -04:00
if (hasOwnProperty(listeners, type) && callback !== null) {
listeners[type] = listeners[type].filter(
2019-05-27 09:20:34 -04:00
(listener): boolean => listener.callback !== callback
2019-01-05 10:02:44 -05:00
);
}
2019-05-27 09:20:34 -04:00
2019-07-16 00:19:26 -04:00
const normalizedOptions: domTypes.EventListenerOptions = eventTargetHelpers.normalizeEventHandlerOptions(
2019-05-27 09:20:34 -04:00
options
);
if (callback === null) {
// Optimization, not in the spec.
return;
}
2019-07-16 00:19:26 -04:00
if (!listeners[type]) {
2019-05-27 09:20:34 -04:00
return;
}
2019-07-16 00:19:26 -04:00
for (let i = 0; i < listeners[type].length; ++i) {
const listener = listeners[type][i];
2019-05-27 09:20:34 -04:00
if (
((typeof listener.options === "boolean" &&
listener.options === normalizedOptions.capture) ||
(typeof listener.options === "object" &&
listener.options.capture === normalizedOptions.capture)) &&
listener.callback === callback
) {
2019-07-16 00:19:26 -04:00
listeners[type].splice(i, 1);
2019-05-27 09:20:34 -04:00
break;
}
}
2019-01-05 10:02:44 -05:00
}
public dispatchEvent(event: domTypes.Event): boolean {
const this_ = this || globalThis;
2019-03-30 13:40:03 -04:00
requiredArguments("EventTarget.dispatchEvent", arguments.length, 1);
const listeners = this_[domTypes.eventTargetListeners];
2019-07-16 00:19:26 -04:00
if (!hasOwnProperty(listeners, event.type)) {
2019-01-05 10:02:44 -05:00
return true;
}
2019-05-27 09:20:34 -04:00
if (event.dispatched || !event.initialized) {
// TODO(bartlomieju): very likely that different error
// should be thrown here (DOMException?)
throw new TypeError("Tried to dispatch an uninitialized event");
2019-05-27 09:20:34 -04:00
}
if (event.eventPhase !== domTypes.EventPhase.NONE) {
// TODO(bartlomieju): very likely that different error
// should be thrown here (DOMException?)
throw new TypeError("Tried to dispatch a dispatching event");
2019-05-27 09:20:34 -04:00
}
return eventTargetHelpers.dispatch(this_, event);
2019-05-27 09:20:34 -04:00
}
2019-07-16 00:19:26 -04:00
get [Symbol.toStringTag](): string {
return "EventTarget";
}
}
const eventTargetHelpers = {
2019-05-27 09:20:34 -04:00
// https://dom.spec.whatwg.org/#concept-event-dispatch
2019-07-16 00:19:26 -04:00
dispatch(
targetImpl: EventTarget,
2019-05-27 09:20:34 -04:00
eventImpl: domTypes.Event,
targetOverride?: domTypes.EventTarget
): boolean {
let clearTargets = false;
let activationTarget = null;
eventImpl.dispatched = true;
targetOverride = targetOverride || targetImpl;
let relatedTarget = retarget(eventImpl.relatedTarget, targetImpl);
if (
targetImpl !== relatedTarget ||
targetImpl === eventImpl.relatedTarget
) {
const touchTargets: domTypes.EventTarget[] = [];
2019-07-16 00:19:26 -04:00
eventTargetHelpers.appendToEventPath(
2019-05-27 09:20:34 -04:00
eventImpl,
targetImpl,
targetOverride,
relatedTarget,
touchTargets,
false
);
const isActivationEvent = eventImpl.type === "click";
2019-07-16 00:19:26 -04:00
if (isActivationEvent && targetImpl[eventTargetHasActivationBehavior]) {
2019-05-27 09:20:34 -04:00
activationTarget = targetImpl;
}
let slotInClosedTree = false;
let slotable =
2019-07-16 00:19:26 -04:00
isSlotable(targetImpl) && targetImpl[eventTargetAssignedSlot]
? targetImpl
: null;
2019-05-27 09:20:34 -04:00
let parent = getEventTargetParent(targetImpl, eventImpl);
// Populate event path
// https://dom.spec.whatwg.org/#event-path
while (parent !== null) {
if (slotable !== null) {
slotable = null;
const parentRoot = getRoot(parent);
if (
isShadowRoot(parentRoot) &&
parentRoot &&
2019-07-16 00:19:26 -04:00
parentRoot[domTypes.eventTargetMode] === "closed"
2019-05-27 09:20:34 -04:00
) {
slotInClosedTree = true;
}
}
relatedTarget = retarget(eventImpl.relatedTarget, parent);
if (
isNode(parent) &&
isShadowInclusiveAncestor(getRoot(targetImpl), parent)
) {
2019-07-16 00:19:26 -04:00
eventTargetHelpers.appendToEventPath(
2019-05-27 09:20:34 -04:00
eventImpl,
parent,
null,
relatedTarget,
touchTargets,
slotInClosedTree
);
} else if (parent === relatedTarget) {
parent = null;
} else {
targetImpl = parent;
if (
isActivationEvent &&
activationTarget === null &&
2019-07-16 00:19:26 -04:00
targetImpl[eventTargetHasActivationBehavior]
2019-05-27 09:20:34 -04:00
) {
activationTarget = targetImpl;
}
2019-07-16 00:19:26 -04:00
eventTargetHelpers.appendToEventPath(
2019-05-27 09:20:34 -04:00
eventImpl,
parent,
targetImpl,
relatedTarget,
touchTargets,
slotInClosedTree
);
}
if (parent !== null) {
parent = getEventTargetParent(parent, eventImpl);
}
slotInClosedTree = false;
}
let clearTargetsTupleIndex = -1;
for (
let i = eventImpl.path.length - 1;
i >= 0 && clearTargetsTupleIndex === -1;
i--
) {
if (eventImpl.path[i].target !== null) {
clearTargetsTupleIndex = i;
}
}
const clearTargetsTuple = eventImpl.path[clearTargetsTupleIndex];
clearTargets =
(isNode(clearTargetsTuple.target) &&
isShadowRoot(getRoot(clearTargetsTuple.target))) ||
(isNode(clearTargetsTuple.relatedTarget) &&
isShadowRoot(getRoot(clearTargetsTuple.relatedTarget)));
eventImpl.eventPhase = domTypes.EventPhase.CAPTURING_PHASE;
for (let i = eventImpl.path.length - 1; i >= 0; --i) {
const tuple = eventImpl.path[i];
if (tuple.target === null) {
2019-07-16 00:19:26 -04:00
eventTargetHelpers.invokeEventListeners(targetImpl, tuple, eventImpl);
2019-05-27 09:20:34 -04:00
}
}
for (let i = 0; i < eventImpl.path.length; i++) {
const tuple = eventImpl.path[i];
if (tuple.target !== null) {
eventImpl.eventPhase = domTypes.EventPhase.AT_TARGET;
} else {
eventImpl.eventPhase = domTypes.EventPhase.BUBBLING_PHASE;
}
if (
(eventImpl.eventPhase === domTypes.EventPhase.BUBBLING_PHASE &&
eventImpl.bubbles) ||
eventImpl.eventPhase === domTypes.EventPhase.AT_TARGET
) {
2019-07-16 00:19:26 -04:00
eventTargetHelpers.invokeEventListeners(targetImpl, tuple, eventImpl);
2019-05-27 09:20:34 -04:00
}
}
}
eventImpl.eventPhase = domTypes.EventPhase.NONE;
eventImpl.currentTarget = null;
eventImpl.path = [];
eventImpl.dispatched = false;
eventImpl.cancelBubble = false;
eventImpl.cancelBubbleImmediately = false;
if (clearTargets) {
eventImpl.target = null;
eventImpl.relatedTarget = null;
}
// TODO: invoke activation targets if HTML nodes will be implemented
// if (activationTarget !== null) {
// if (!eventImpl.defaultPrevented) {
// activationTarget._activationBehavior();
// }
// }
return !eventImpl.defaultPrevented;
2019-07-16 00:19:26 -04:00
},
2019-05-27 09:20:34 -04:00
// https://dom.spec.whatwg.org/#concept-event-listener-invoke
2019-07-16 00:19:26 -04:00
invokeEventListeners(
targetImpl: EventTarget,
2019-05-27 09:20:34 -04:00
tuple: domTypes.EventPath,
eventImpl: domTypes.Event
): void {
const tupleIndex = eventImpl.path.indexOf(tuple);
for (let i = tupleIndex; i >= 0; i--) {
const t = eventImpl.path[i];
if (t.target) {
eventImpl.target = t.target;
break;
}
}
eventImpl.relatedTarget = tuple.relatedTarget;
if (eventImpl.cancelBubble) {
return;
}
eventImpl.currentTarget = tuple.item;
2019-07-16 00:19:26 -04:00
eventTargetHelpers.innerInvokeEventListeners(
targetImpl,
eventImpl,
tuple.item[domTypes.eventTargetListeners]
);
},
2019-05-27 09:20:34 -04:00
// https://dom.spec.whatwg.org/#concept-event-listener-inner-invoke
2019-07-16 00:19:26 -04:00
innerInvokeEventListeners(
targetImpl: EventTarget,
2019-05-27 09:20:34 -04:00
eventImpl: domTypes.Event,
targetListeners: { [type in string]: domTypes.EventTargetListener[] }
2019-05-27 09:20:34 -04:00
): boolean {
let found = false;
const { type } = eventImpl;
if (!targetListeners || !targetListeners[type]) {
return found;
}
// Copy event listeners before iterating since the list can be modified during the iteration.
const handlers = targetListeners[type].slice();
for (let i = 0; i < handlers.length; i++) {
const listener = handlers[i];
let capture, once, passive;
if (typeof listener.options === "boolean") {
capture = listener.options;
once = false;
passive = false;
2019-01-05 10:02:44 -05:00
} else {
2019-05-27 09:20:34 -04:00
capture = listener.options.capture;
once = listener.options.once;
passive = listener.options.passive;
}
// Check if the event listener has been removed since the listeners has been cloned.
if (!targetListeners[type].includes(listener)) {
continue;
}
found = true;
if (
(eventImpl.eventPhase === domTypes.EventPhase.CAPTURING_PHASE &&
!capture) ||
(eventImpl.eventPhase === domTypes.EventPhase.BUBBLING_PHASE && capture)
) {
continue;
}
if (once) {
targetListeners[type].splice(
targetListeners[type].indexOf(listener),
1
);
}
if (passive) {
eventImpl.inPassiveListener = true;
}
try {
if (typeof listener.callback === "object") {
if (typeof listener.callback.handleEvent === "function") {
listener.callback.handleEvent(eventImpl);
}
} else {
listener.callback.call(eventImpl.currentTarget, eventImpl);
}
2019-05-27 09:20:34 -04:00
} catch (error) {
// TODO(bartlomieju): very likely that different error
// should be thrown here (DOMException?)
throw new Error(error.message);
2019-05-27 09:20:34 -04:00
}
eventImpl.inPassiveListener = false;
if (eventImpl.cancelBubbleImmediately) {
return found;
2019-01-05 10:02:44 -05:00
}
}
2019-05-27 09:20:34 -04:00
return found;
2019-07-16 00:19:26 -04:00
},
2019-05-27 09:20:34 -04:00
2019-07-16 00:19:26 -04:00
normalizeAddEventHandlerOptions(
2019-05-27 09:20:34 -04:00
options: boolean | domTypes.AddEventListenerOptions | undefined
): domTypes.AddEventListenerOptions {
if (typeof options === "boolean" || typeof options === "undefined") {
const returnValue: domTypes.AddEventListenerOptions = {
capture: Boolean(options),
once: false,
passive: false
};
return returnValue;
} else {
return options;
}
2019-07-16 00:19:26 -04:00
},
2019-05-27 09:20:34 -04:00
2019-07-16 00:19:26 -04:00
normalizeEventHandlerOptions(
2019-05-27 09:20:34 -04:00
options: boolean | domTypes.EventListenerOptions | undefined
): domTypes.EventListenerOptions {
if (typeof options === "boolean" || typeof options === "undefined") {
const returnValue: domTypes.EventListenerOptions = {
capture: Boolean(options)
};
return returnValue;
} else {
return options;
}
2019-07-16 00:19:26 -04:00
},
2019-05-27 09:20:34 -04:00
// https://dom.spec.whatwg.org/#concept-event-path-append
2019-07-16 00:19:26 -04:00
appendToEventPath(
2019-05-27 09:20:34 -04:00
eventImpl: domTypes.Event,
target: domTypes.EventTarget,
targetOverride: domTypes.EventTarget | null,
relatedTarget: domTypes.EventTarget | null,
touchTargets: domTypes.EventTarget[],
slotInClosedTree: boolean
): void {
const itemInShadowTree = isNode(target) && isShadowRoot(getRoot(target));
2019-07-16 00:19:26 -04:00
const rootOfClosedTree =
isShadowRoot(target) && target[domTypes.eventTargetMode] === "closed";
2019-05-27 09:20:34 -04:00
eventImpl.path.push({
item: target,
itemInShadowTree,
target: targetOverride,
relatedTarget,
touchTargetList: touchTargets,
rootOfClosedTree,
slotInClosedTree
});
2019-01-05 10:02:44 -05:00
}
2019-07-16 00:19:26 -04:00
};
2019-05-27 09:20:34 -04:00
Reflect.defineProperty(EventTarget.prototype, "addEventListener", {
enumerable: true
});
Reflect.defineProperty(EventTarget.prototype, "removeEventListener", {
enumerable: true
});
Reflect.defineProperty(EventTarget.prototype, "dispatchEvent", {
enumerable: true
});