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) => {
|
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");
|
|
|
|
function makeWrappedHandler(handler) {
|
|
|
|
function wrappedHandler(...args) {
|
|
|
|
if (typeof wrappedHandler.handler !== "function") {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
return wrappedHandler.handler.call(this, ...args);
|
|
|
|
}
|
|
|
|
wrappedHandler.handler = handler;
|
|
|
|
return wrappedHandler;
|
|
|
|
}
|
2020-12-07 15:22:58 -05:00
|
|
|
function defineEventHandler(emitter, name, defaultValue = undefined) {
|
2020-11-09 22:34:42 -05:00
|
|
|
// HTML specification section 8.1.5.1
|
|
|
|
Object.defineProperty(emitter, `on${name}`, {
|
|
|
|
get() {
|
2020-12-07 15:22:58 -05:00
|
|
|
return this[handlerSymbol]?.get(name)?.handler ?? defaultValue;
|
2020-11-09 22:34:42 -05:00
|
|
|
},
|
|
|
|
set(value) {
|
|
|
|
if (!this[handlerSymbol]) {
|
|
|
|
this[handlerSymbol] = new Map();
|
|
|
|
}
|
|
|
|
let handlerWrapper = this[handlerSymbol]?.get(name);
|
|
|
|
if (handlerWrapper) {
|
|
|
|
handlerWrapper.handler = value;
|
|
|
|
} else {
|
|
|
|
handlerWrapper = makeWrappedHandler(value);
|
|
|
|
this.addEventListener(name, handlerWrapper);
|
|
|
|
}
|
|
|
|
this[handlerSymbol].set(name, handlerWrapper);
|
|
|
|
},
|
|
|
|
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);
|