Skip to content

Commit

Permalink
lib: define Event.isTrusted in the prototype
Browse files Browse the repository at this point in the history
Don't conform to the spec with isTrusted. The spec defines it as
`LegacyUnforgeable` but defining it in the constructor has a big
performance impact and the property doesn't seem to be useful outside of
browsers.

Refs: nodejs/performance#32
  • Loading branch information
santigimeno committed Mar 20, 2023
1 parent fbd526b commit f7c80d9
Show file tree
Hide file tree
Showing 5 changed files with 17 additions and 7 deletions.
7 changes: 5 additions & 2 deletions lib/internal/event_target.js
Original file line number Diff line number Diff line change
Expand Up @@ -119,8 +119,6 @@ class Event {
isTrustedSet.add(this);
}

// isTrusted is special (LegacyUnforgeable)
ObjectDefineProperty(this, 'isTrusted', isTrustedDescriptor);
this[kTarget] = null;
this[kIsBeingDispatched] = false;
}
Expand Down Expand Up @@ -343,6 +341,11 @@ ObjectDefineProperties(
eventPhase: kEnumerableProperty,
cancelBubble: kEnumerableProperty,
stopPropagation: kEnumerableProperty,
// Don't conform to the spec with isTrusted. The spec defines it as
// LegacyUnforgeable but defining it in the constructor has a big
// performance impact and the property doesn't seem to be useful outside of
// browsers.
isTrusted: isTrustedDescriptor,
});

function isCustomEvent(value) {
Expand Down
6 changes: 3 additions & 3 deletions test/parallel/test-abortcontroller.js
Original file line number Diff line number Diff line change
Expand Up @@ -57,9 +57,9 @@ const { setTimeout: sleep } = require('timers/promises');
}));
first.abort();
second.abort();
const firstTrusted = Reflect.getOwnPropertyDescriptor(ev1, 'isTrusted').get;
const secondTrusted = Reflect.getOwnPropertyDescriptor(ev2, 'isTrusted').get;
const untrusted = Reflect.getOwnPropertyDescriptor(ev3, 'isTrusted').get;
const firstTrusted = Reflect.getOwnPropertyDescriptor(Object.getPrototypeOf(ev1), 'isTrusted').get;
const secondTrusted = Reflect.getOwnPropertyDescriptor(Object.getPrototypeOf(ev2), 'isTrusted').get;
const untrusted = Reflect.getOwnPropertyDescriptor(Object.getPrototypeOf(ev3), 'isTrusted').get;
strictEqual(firstTrusted, secondTrusted);
strictEqual(untrusted, firstTrusted);
}
Expand Down
2 changes: 1 addition & 1 deletion test/parallel/test-events-customevent.js
Original file line number Diff line number Diff line change
Expand Up @@ -187,7 +187,7 @@ const { Event, EventTarget, CustomEvent } = require('internal/event_target');
}
{
const ev = new CustomEvent('foo');
deepStrictEqual(Object.keys(ev), ['isTrusted']);
strictEqual(ev.isTrusted, false);
}

// Works with EventTarget
Expand Down
2 changes: 1 addition & 1 deletion test/parallel/test-eventtarget.js
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,7 @@ let asyncTest = Promise.resolve();
}
{
const ev = new Event('foo');
deepStrictEqual(Object.keys(ev), ['isTrusted']);
strictEqual(ev.isTrusted, false);
}
{
const eventTarget = new EventTarget();
Expand Down
7 changes: 7 additions & 0 deletions test/wpt/status/dom/events.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,13 @@
"Event-dispatch-listener-order.window.js": {
"skip": "document is not defined"
},
"Event-isTrusted.any.js": {
"fail": {
"expected": [
"Event-isTrusted"
]
}
},
"EventListener-addEventListener.sub.window.js": {
"skip": "document is not defined"
},
Expand Down

0 comments on commit f7c80d9

Please sign in to comment.