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

589 lines
15 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.
// 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.
import { DOMExceptionImpl as DOMException } from "./dom_exception.ts";
import * as domTypes from "./dom_types.d.ts";
2019-05-27 09:20:34 -04:00
import {
EventImpl as Event,
EventPath,
getDispatched,
getPath,
getStopImmediatePropagation,
hasRelatedTarget,
setCurrentTarget,
setDispatched,
setEventPhase,
setInPassiveListener,
setPath,
setRelatedTarget,
setStopImmediatePropagation,
setTarget,
} from "./event.ts";
import { defineEnumerableProps, requiredArguments } from "./util.ts";
// 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: EventTarget): EventTarget | null {
return isNode(eventTarget) ? eventTarget.parentNode : null;
2019-05-27 09:20:34 -04:00
}
function getRoot(eventTarget: EventTarget): EventTarget | null {
return isNode(eventTarget)
? eventTarget.getRootNode({ composed: true })
: null;
}
2019-07-16 00:19:26 -04:00
function isNode<T extends EventTarget>(
eventTarget: T | null
): eventTarget is T & domTypes.Node {
return Boolean(eventTarget && "nodeType" in eventTarget);
}
2019-01-05 10:02:44 -05:00
// https://dom.spec.whatwg.org/#concept-shadow-including-inclusive-ancestor
function isShadowInclusiveAncestor(
ancestor: EventTarget | null,
node: EventTarget | null
): boolean {
while (isNode(node)) {
if (node === ancestor) {
return true;
}
if (isShadowRoot(node)) {
node = node && getHost(node);
} else {
node = getParent(node);
}
}
return false;
}
function isShadowRoot(nodeImpl: EventTarget | null): boolean {
return Boolean(
nodeImpl &&
isNode(nodeImpl) &&
nodeImpl.nodeType === DOCUMENT_FRAGMENT_NODE &&
getHost(nodeImpl) != null
);
}
function isSlotable<T extends EventTarget>(
nodeImpl: T | null
): nodeImpl is T & domTypes.Node & domTypes.Slotable {
return Boolean(isNode(nodeImpl) && "assignedSlot" in nodeImpl);
}
// 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: Event,
target: EventTarget,
targetOverride: EventTarget | null,
relatedTarget: EventTarget | null,
touchTargets: EventTarget[],
slotInClosedTree: boolean
): void {
const itemInShadowTree = isNode(target) && isShadowRoot(getRoot(target));
const rootOfClosedTree = isShadowRoot(target) && getMode(target) === "closed";
getPath(eventImpl).push({
item: target,
itemInShadowTree,
target: targetOverride,
relatedTarget,
touchTargetList: touchTargets,
rootOfClosedTree,
slotInClosedTree,
});
}
function dispatch(
targetImpl: EventTarget,
eventImpl: Event,
targetOverride?: EventTarget
): boolean {
let clearTargets = false;
let activationTarget: EventTarget | null = 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: EventTarget[] = [];
appendToEventPath(
eventImpl,
targetImpl,
targetOverride,
relatedTarget,
touchTargets,
false
2019-05-27 09:20:34 -04:00
);
const isActivationEvent = eventImpl.type === "click";
if (isActivationEvent && getHasActivationBehavior(targetImpl)) {
activationTarget = targetImpl;
2019-05-27 09:20:34 -04:00
}
let slotInClosedTree = false;
let slotable =
isSlotable(targetImpl) && getAssignedSlot(targetImpl) ? targetImpl : null;
let parent = getParent(targetImpl);
2019-07-16 00:19:26 -04:00
// 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 &&
getMode(parentRoot) === "closed"
) {
slotInClosedTree = true;
}
}
relatedTarget = retarget(eventRelatedTarget, parent);
2019-05-27 09:20:34 -04:00
if (
isNode(parent) &&
isShadowInclusiveAncestor(getRoot(targetImpl), parent)
2019-05-27 09:20:34 -04:00
) {
appendToEventPath(
eventImpl,
parent,
null,
relatedTarget,
touchTargets,
slotInClosedTree
);
} else if (parent === relatedTarget) {
parent = null;
} else {
targetImpl = parent;
if (
isActivationEvent &&
activationTarget === null &&
getHasActivationBehavior(targetImpl)
) {
activationTarget = targetImpl;
}
appendToEventPath(
eventImpl,
parent,
targetImpl,
relatedTarget,
touchTargets,
slotInClosedTree
);
2019-05-27 09:20:34 -04:00
}
if (parent !== null) {
parent = getParent(parent);
}
2019-01-05 10:02:44 -05:00
slotInClosedTree = false;
}
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;
}
2019-01-05 10:02:44 -05:00
}
const clearTargetsTuple = path[clearTargetsTupleIndex];
2019-05-27 09:20:34 -04:00
clearTargets =
(isNode(clearTargetsTuple.target) &&
isShadowRoot(getRoot(clearTargetsTuple.target))) ||
(isNode(clearTargetsTuple.relatedTarget) &&
isShadowRoot(getRoot(clearTargetsTuple.relatedTarget)));
2019-05-27 09:20:34 -04:00
setEventPhase(eventImpl, Event.CAPTURING_PHASE);
2019-05-27 09:20:34 -04:00
for (let i = path.length - 1; i >= 0; --i) {
const tuple = path[i];
if (tuple.target === null) {
invokeEventListeners(tuple, eventImpl);
}
2019-05-27 09:20:34 -04:00
}
for (let i = 0; i < path.length; i++) {
const tuple = path[i];
if (tuple.target !== null) {
setEventPhase(eventImpl, Event.AT_TARGET);
} else {
setEventPhase(eventImpl, Event.BUBBLING_PHASE);
}
2019-05-27 09:20:34 -04:00
if (
(eventImpl.eventPhase === Event.BUBBLING_PHASE && eventImpl.bubbles) ||
eventImpl.eventPhase === Event.AT_TARGET
2019-05-27 09:20:34 -04:00
) {
invokeEventListeners(tuple, eventImpl);
2019-05-27 09:20:34 -04:00
}
}
2019-01-05 10:02:44 -05:00
}
setEventPhase(eventImpl, Event.NONE);
setCurrentTarget(eventImpl, null);
setPath(eventImpl, []);
setDispatched(eventImpl, false);
eventImpl.cancelBubble = false;
setStopImmediatePropagation(eventImpl, false);
if (clearTargets) {
setTarget(eventImpl, null);
setRelatedTarget(eventImpl, null);
}
2019-01-05 10:02:44 -05:00
// TODO: invoke activation targets if HTML nodes will be implemented
// if (activationTarget !== null) {
// if (!eventImpl.defaultPrevented) {
// activationTarget._activationBehavior();
// }
// }
2019-05-27 09:20:34 -04:00
return !eventImpl.defaultPrevented;
}
2019-05-27 09:20:34 -04:00
/** 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: Event,
targetListeners: Record<string, Listener[]>
): boolean {
let found = false;
2019-05-27 09:20:34 -04:00
const { type } = eventImpl;
if (!targetListeners || !targetListeners[type]) {
return found;
2019-07-16 00:19:26 -04:00
}
// 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;
} else {
capture = listener.options.capture;
once = listener.options.once;
passive = listener.options.passive;
}
2019-05-27 09:20:34 -04:00
// Check if the event listener has been removed since the listeners has been cloned.
if (!targetListeners[type].includes(listener)) {
continue;
}
2019-05-27 09:20:34 -04:00
found = true;
2019-05-27 09:20:34 -04:00
if (
(eventImpl.eventPhase === Event.CAPTURING_PHASE && !capture) ||
(eventImpl.eventPhase === Event.BUBBLING_PHASE && capture)
2019-05-27 09:20:34 -04:00
) {
continue;
}
2019-05-27 09:20:34 -04:00
if (once) {
targetListeners[type].splice(targetListeners[type].indexOf(listener), 1);
}
2019-05-27 09:20:34 -04:00
if (passive) {
setInPassiveListener(eventImpl, true);
}
2019-05-27 09:20:34 -04:00
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
setInPassiveListener(eventImpl, false);
2019-05-27 09:20:34 -04:00
if (getStopImmediatePropagation(eventImpl)) {
return found;
}
}
2019-05-27 09:20:34 -04:00
return found;
}
2019-05-27 09:20:34 -04:00
/** 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: EventPath, eventImpl: Event): void {
const path = getPath(eventImpl);
const tupleIndex = path.indexOf(tuple);
for (let i = tupleIndex; i >= 0; i--) {
const t = path[i];
if (t.target) {
setTarget(eventImpl, t.target);
break;
}
}
2019-05-27 09:20:34 -04:00
setRelatedTarget(eventImpl, tuple.relatedTarget);
2019-05-27 09:20:34 -04:00
if (eventImpl.cancelBubble) {
return;
}
2019-05-27 09:20:34 -04:00
setCurrentTarget(eventImpl, tuple.item);
2019-05-27 09:20:34 -04:00
innerInvokeEventListeners(eventImpl, getListeners(tuple.item));
}
2019-05-27 09:20:34 -04:00
function normalizeAddEventHandlerOptions(
options: boolean | AddEventListenerOptions | undefined
): AddEventListenerOptions {
if (typeof options === "boolean" || typeof options === "undefined") {
return {
capture: Boolean(options),
once: false,
passive: false,
};
} else {
return options;
}
}
2019-05-27 09:20:34 -04:00
function normalizeEventHandlerOptions(
options: boolean | EventListenerOptions | undefined
): EventListenerOptions {
if (typeof options === "boolean" || typeof options === "undefined") {
return {
capture: Boolean(options),
};
} else {
return options;
}
}
2019-05-27 09:20:34 -04:00
/** Retarget the target following the spec logic.
*
* Ref: https://dom.spec.whatwg.org/#retarget */
function retarget(a: EventTarget | null, b: EventTarget): EventTarget | null {
while (true) {
if (!isNode(a)) {
return a;
2019-05-27 09:20:34 -04:00
}
const aRoot = a.getRootNode();
2019-05-27 09:20:34 -04:00
if (aRoot) {
if (
!isShadowRoot(aRoot) ||
(isNode(b) && isShadowInclusiveAncestor(aRoot, b))
) {
return a;
}
2019-05-27 09:20:34 -04:00
a = getHost(aRoot);
2019-05-27 09:20:34 -04:00
}
}
}
2019-05-27 09:20:34 -04:00
// Non-public state information for an event target that needs to held onto.
// Some of the information should be moved to other entities (like Node,
// ShowRoot, UIElement, etc.).
interface EventTargetData {
assignedSlot: boolean;
hasActivationBehavior: boolean;
host: EventTarget | null;
listeners: Record<string, Listener[]>;
mode: string;
}
2019-05-27 09:20:34 -04:00
interface Listener {
callback: EventListenerOrEventListenerObject;
options: AddEventListenerOptions;
}
2019-05-27 09:20:34 -04:00
// Accessors for non-public data
2019-05-27 09:20:34 -04:00
export const eventTargetData = new WeakMap<EventTarget, EventTargetData>();
2019-05-27 09:20:34 -04:00
function getAssignedSlot(target: EventTarget): boolean {
return Boolean(eventTargetData.get(target as EventTarget)?.assignedSlot);
}
2019-05-27 09:20:34 -04:00
function getHasActivationBehavior(target: EventTarget): boolean {
return Boolean(
eventTargetData.get(target as EventTarget)?.hasActivationBehavior
);
}
2019-05-27 09:20:34 -04:00
function getHost(target: EventTarget): EventTarget | null {
return eventTargetData.get(target as EventTarget)?.host ?? null;
}
2019-05-27 09:20:34 -04:00
function getListeners(target: EventTarget): Record<string, Listener[]> {
return eventTargetData.get(target as EventTarget)?.listeners ?? {};
}
2019-05-27 09:20:34 -04:00
function getMode(target: EventTarget): string | null {
return eventTargetData.get(target as EventTarget)?.mode ?? null;
}
2019-05-27 09:20:34 -04:00
export function getDefaultTargetData(): Readonly<EventTargetData> {
return {
assignedSlot: false,
hasActivationBehavior: false,
host: null,
listeners: Object.create(null),
mode: "",
};
}
2019-05-27 09:20:34 -04:00
export class EventTargetImpl implements EventTarget {
constructor() {
eventTargetData.set(this, getDefaultTargetData());
}
2019-05-27 09:20:34 -04:00
public addEventListener(
type: string,
callback: EventListenerOrEventListenerObject | null,
options?: AddEventListenerOptions | boolean
): void {
requiredArguments("EventTarget.addEventListener", arguments.length, 2);
if (callback === null) {
return;
}
options = normalizeAddEventHandlerOptions(options);
const { listeners } = eventTargetData.get(this ?? globalThis)!;
2019-05-27 09:20:34 -04:00
if (!(type in listeners)) {
listeners[type] = [];
}
2019-05-27 09:20:34 -04:00
for (const listener of listeners[type]) {
2019-05-27 09:20:34 -04:00
if (
((typeof listener.options === "boolean" &&
listener.options === options.capture) ||
(typeof listener.options === "object" &&
listener.options.capture === options.capture)) &&
listener.callback === callback
2019-05-27 09:20:34 -04:00
) {
return;
2019-05-27 09:20:34 -04:00
}
}
2019-05-27 09:20:34 -04:00
listeners[type].push({ callback, options });
}
2019-05-27 09:20:34 -04:00
public removeEventListener(
type: string,
callback: EventListenerOrEventListenerObject | null,
options?: EventListenerOptions | boolean
): void {
requiredArguments("EventTarget.removeEventListener", arguments.length, 2);
2019-05-27 09:20:34 -04:00
const listeners = eventTargetData.get(this ?? globalThis)!.listeners;
if (callback !== null && type in listeners) {
listeners[type] = listeners[type].filter(
(listener) => listener.callback !== callback
);
} else if (callback === null || !listeners[type]) {
return;
}
2019-05-27 09:20:34 -04:00
options = normalizeEventHandlerOptions(options);
2019-05-27 09:20:34 -04: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
) {
listeners[type].splice(i, 1);
break;
2019-01-05 10:02:44 -05:00
}
}
}
2019-05-27 09:20:34 -04:00
public dispatchEvent(event: Event): boolean {
requiredArguments("EventTarget.dispatchEvent", arguments.length, 1);
const self = this ?? globalThis;
const listeners = eventTargetData.get(self)!.listeners;
if (!(event.type in listeners)) {
return true;
2019-05-27 09:20:34 -04:00
}
if (getDispatched(event)) {
throw new DOMException("Invalid event state.", "InvalidStateError");
}
2019-05-27 09:20:34 -04:00
if (event.eventPhase !== Event.NONE) {
throw new DOMException("Invalid event state.", "InvalidStateError");
2019-05-27 09:20:34 -04:00
}
return dispatch(self, event);
}
get [Symbol.toStringTag](): string {
return "EventTarget";
}
protected getParent(_event: Event): EventTarget | null {
return null;
}
}
defineEnumerableProps(EventTargetImpl, [
"addEventListener",
"removeEventListener",
"dispatchEvent",
]);