2021-01-12 02:13:41 +09:00
|
|
|
// Copyright 2018-2021 the Deno authors. All rights reserved. MIT license.
|
2021-02-05 03:48:32 +05:30
|
|
|
"use strict";
|
2020-07-19 19:49:44 +02:00
|
|
|
|
|
|
|
((window) => {
|
2020-09-19 22:30:59 +01:00
|
|
|
const illegalConstructorKey = Symbol("illegalConstructorKey");
|
|
|
|
|
2020-07-19 19:49:44 +02: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-10 05:34:42 +02: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 22:22:58 +02:00
|
|
|
function defineEventHandler(emitter, name, defaultValue = undefined) {
|
2020-11-10 05:34:42 +02:00
|
|
|
// HTML specification section 8.1.5.1
|
|
|
|
Object.defineProperty(emitter, `on${name}`, {
|
|
|
|
get() {
|
2020-12-07 22:22:58 +02:00
|
|
|
return this[handlerSymbol]?.get(name)?.handler ?? defaultValue;
|
2020-11-10 05:34:42 +02: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 19:49:44 +02:00
|
|
|
window.__bootstrap.webUtil = {
|
2020-09-19 22:30:59 +01:00
|
|
|
illegalConstructorKey,
|
2020-07-19 19:49:44 +02:00
|
|
|
requiredArguments,
|
2020-11-10 05:34:42 +02:00
|
|
|
defineEventHandler,
|
2020-07-19 19:49:44 +02:00
|
|
|
};
|
|
|
|
})(this);
|