2021-01-11 12:13:41 -05:00
|
|
|
// Copyright 2018-2021 the Deno authors. All rights reserved. MIT license.
|
2021-02-04 17:18:32 -05:00
|
|
|
"use strict";
|
2020-07-19 13:49:44 -04:00
|
|
|
|
|
|
|
((window) => {
|
2021-07-03 10:58:08 -04:00
|
|
|
const {
|
|
|
|
FunctionPrototypeCall,
|
|
|
|
Map,
|
|
|
|
MapPrototypeGet,
|
|
|
|
MapPrototypeSet,
|
|
|
|
ObjectDefineProperty,
|
|
|
|
TypeError,
|
|
|
|
Symbol,
|
|
|
|
} = window.__bootstrap.primordials;
|
2020-09-19 17:30:59 -04:00
|
|
|
const illegalConstructorKey = Symbol("illegalConstructorKey");
|
|
|
|
|
2020-07-19 13:49:44 -04:00
|
|
|
function requiredArguments(
|
|
|
|
name,
|
|
|
|
length,
|
|
|
|
required,
|
|
|
|
) {
|
|
|
|
if (length < required) {
|
|
|
|
const errMsg = `${name} requires at least ${required} argument${
|
|
|
|
required === 1 ? "" : "s"
|
|
|
|
}, but only ${length} present`;
|
|
|
|
throw new TypeError(errMsg);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-11-09 22:34:42 -05:00
|
|
|
const handlerSymbol = Symbol("eventHandlers");
|
2021-07-10 17:32:10 -04:00
|
|
|
function makeWrappedHandler(handler, isSpecialErrorEventHandler) {
|
2020-11-09 22:34:42 -05:00
|
|
|
function wrappedHandler(...args) {
|
|
|
|
if (typeof wrappedHandler.handler !== "function") {
|
|
|
|
return;
|
|
|
|
}
|
2021-07-10 17:32:10 -04:00
|
|
|
if (isSpecialErrorEventHandler) {
|
|
|
|
const evt = args[0];
|
|
|
|
if (evt instanceof ErrorEvent && 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-07-03 10:58:08 -04:00
|
|
|
return FunctionPrototypeCall(wrappedHandler.handler, this, ...args);
|
2020-11-09 22:34:42 -05:00
|
|
|
}
|
|
|
|
wrappedHandler.handler = handler;
|
|
|
|
return wrappedHandler;
|
|
|
|
}
|
2021-07-10 17:32:10 -04:00
|
|
|
function defineEventHandler(
|
|
|
|
emitter,
|
|
|
|
name,
|
|
|
|
defaultValue = undefined,
|
|
|
|
isSpecialErrorEventHandler = false,
|
|
|
|
) {
|
2020-11-09 22:34:42 -05:00
|
|
|
// HTML specification section 8.1.5.1
|
2021-07-03 10:58:08 -04:00
|
|
|
ObjectDefineProperty(emitter, `on${name}`, {
|
2020-11-09 22:34:42 -05:00
|
|
|
get() {
|
2021-07-03 10:58:08 -04:00
|
|
|
if (!this[handlerSymbol]) {
|
|
|
|
return defaultValue;
|
|
|
|
}
|
|
|
|
|
|
|
|
return MapPrototypeGet(this[handlerSymbol], name)?.handler ??
|
|
|
|
defaultValue;
|
2020-11-09 22:34:42 -05:00
|
|
|
},
|
|
|
|
set(value) {
|
|
|
|
if (!this[handlerSymbol]) {
|
|
|
|
this[handlerSymbol] = new Map();
|
|
|
|
}
|
2021-07-03 10:58:08 -04:00
|
|
|
let handlerWrapper = MapPrototypeGet(this[handlerSymbol], name);
|
2020-11-09 22:34:42 -05:00
|
|
|
if (handlerWrapper) {
|
|
|
|
handlerWrapper.handler = value;
|
|
|
|
} else {
|
2021-07-10 17:32:10 -04:00
|
|
|
handlerWrapper = makeWrappedHandler(
|
|
|
|
value,
|
|
|
|
isSpecialErrorEventHandler,
|
|
|
|
);
|
2020-11-09 22:34:42 -05:00
|
|
|
this.addEventListener(name, handlerWrapper);
|
|
|
|
}
|
2021-07-03 10:58:08 -04:00
|
|
|
MapPrototypeSet(this[handlerSymbol], name, handlerWrapper);
|
2020-11-09 22:34:42 -05:00
|
|
|
},
|
|
|
|
configurable: true,
|
|
|
|
enumerable: true,
|
|
|
|
});
|
|
|
|
}
|
2020-07-19 13:49:44 -04:00
|
|
|
window.__bootstrap.webUtil = {
|
2020-09-19 17:30:59 -04:00
|
|
|
illegalConstructorKey,
|
2020-07-19 13:49:44 -04:00
|
|
|
requiredArguments,
|
2020-11-09 22:34:42 -05:00
|
|
|
defineEventHandler,
|
2020-07-19 13:49:44 -04:00
|
|
|
};
|
|
|
|
})(this);
|