Skip to content
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 1 addition & 14 deletions cli/tests/unit/event_test.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
// Copyright 2018-2023 the Deno authors. All rights reserved. MIT license.
import { assert, assertEquals, assertStringIncludes } from "./test_util.ts";
import { assertEquals, assertStringIncludes } from "./test_util.ts";

Deno.test(function eventInitializedWithType() {
const type = "click";
Expand Down Expand Up @@ -80,19 +80,6 @@ Deno.test(function eventInitializedWithNonStringType() {
assertEquals(event.cancelable, false);
});

// ref https://github.com/web-platform-tests/wpt/blob/master/dom/events/Event-isTrusted.any.js
Deno.test(function eventIsTrusted() {
const desc1 = Object.getOwnPropertyDescriptor(new Event("x"), "isTrusted");
assert(desc1);
assertEquals(typeof desc1.get, "function");

const desc2 = Object.getOwnPropertyDescriptor(new Event("x"), "isTrusted");
assert(desc2);
assertEquals(typeof desc2!.get, "function");

assertEquals(desc1!.get, desc2!.get);
});

Deno.test(function eventInspectOutput() {
// deno-lint-ignore no-explicit-any
const cases: Array<[any, (event: any) => string]> = [
Expand Down
84 changes: 51 additions & 33 deletions ext/web/02_event.js
Original file line number Diff line number Diff line change
Expand Up @@ -150,7 +150,7 @@ const _path = Symbol("[[path]]");
const _skipInternalInit = Symbol("[[skipSlowInit]]");

class Event {
constructor(type, eventInitDict = {}) {
constructor(type, eventInitDict) {
// TODO(lucacasonato): remove when this interface is spec aligned
this[SymbolToStringTag] = "Event";
this[_canceledFlag] = false;
Expand All @@ -161,51 +161,62 @@ class Event {
this[_isTrusted] = false;
this[_path] = [];

if (!eventInitDict[_skipInternalInit]) {
webidl.requiredArguments(
arguments.length,
1,
"Failed to construct 'Event'",
);
type = webidl.converters.DOMString(
type,
"Failed to construct 'Event'",
"Argument 1",
);
const eventInit = webidl.converters.EventInit(
eventInitDict,
"Failed to construct 'Event'",
"Argument 2",
);
if (eventInitDict?.[_skipInternalInit]) {
eventInitDict = eventInitDict ?? {};
this[_attributes] = {
type,
...eventInit,
data: eventInitDict.data ?? null,
bubbles: eventInitDict.bubbles ?? false,
cancelable: eventInitDict.cancelable ?? false,
composed: eventInitDict.composed ?? false,
currentTarget: null,
eventPhase: Event.NONE,
target: null,
timeStamp: DateNow(),
timeStamp: 0,
};
// [LegacyUnforgeable]
ReflectDefineProperty(this, "isTrusted", {
enumerable: true,
get: isTrusted,
});
} else {
return;
}

webidl.requiredArguments(
arguments.length,
1,
"Failed to construct 'Event'",
);
type = webidl.converters.DOMString(
type,
"Failed to construct 'Event'",
"Argument 1",
);

if (!eventInitDict) {
// fast path
this[_attributes] = {
type,
data: eventInitDict.data ?? null,
bubbles: eventInitDict.bubbles ?? false,
cancelable: eventInitDict.cancelable ?? false,
composed: eventInitDict.composed ?? false,
currentTarget: null,
eventPhase: Event.NONE,
target: null,
timeStamp: 0,
timeStamp: DateNow(),
// inline default eventInit
bubbles: false,
cancelable: false,
composed: false,
};
// TODO(@littledivy): Not spec compliant but performance is hurt badly
// for users of `_skipInternalInit`.
this.isTrusted = false;
return;
}

const eventInit = webidl.converters.EventInit(
eventInitDict,
"Failed to construct 'Event'",
"Argument 2",
);
this[_attributes] = {
type,
...eventInit,
currentTarget: null,
eventPhase: Event.NONE,
target: null,
timeStamp: DateNow(),
};
}

[SymbolFor("Deno.privateCustomInspect")](inspect) {
Expand Down Expand Up @@ -435,6 +446,13 @@ class Event {
}
}

// Not spec compliant. The spec defines it as [LegacyUnforgeable]
// but doing so has a big performance hit
ReflectDefineProperty(Event.prototype, "isTrusted", {
Comment on lines +415 to +417
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM! Nice

enumerable: true,
get: isTrusted,
});

function defineEnumerableProps(
Ctor,
props,
Expand Down
5 changes: 3 additions & 2 deletions tools/wpt/expectation.json
Original file line number Diff line number Diff line change
Expand Up @@ -2229,8 +2229,8 @@
],
"AddEventListenerOptions-signal.any.html": true,
"AddEventListenerOptions-signal.any.worker.html": true,
"Event-isTrusted.any.html": true,
"Event-isTrusted.any.worker.html": true,
"Event-isTrusted.any.html": false,
"Event-isTrusted.any.worker.html": false,
"EventTarget-add-remove-listener.any.html": true,
"EventTarget-add-remove-listener.any.worker.html": true,
"EventTarget-addEventListener.any.html": true,
Expand All @@ -2257,6 +2257,7 @@
},
"idlharness-shadowrealm.window.html": false,
"idlharness.any.worker.html": [
"Event interface object length",
"Event interface: attribute srcElement",
"Event interface: operation composedPath()",
"Event interface: constant NONE on interface object",
Expand Down