Skip to content

Commit a738164

Browse files
santigimenoRafaelGSS
authored andcommitted
lib: define Event.isTrusted in the prototype
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 PR-URL: #46974 Reviewed-By: Yagiz Nizipli <[email protected]> Reviewed-By: Ben Noordhuis <[email protected]> Reviewed-By: James M Snell <[email protected]>
1 parent 4b4336c commit a738164

File tree

5 files changed

+17
-7
lines changed

5 files changed

+17
-7
lines changed

lib/internal/event_target.js

+5-2
Original file line numberDiff line numberDiff line change
@@ -119,8 +119,6 @@ class Event {
119119
isTrustedSet.add(this);
120120
}
121121

122-
// isTrusted is special (LegacyUnforgeable)
123-
ObjectDefineProperty(this, 'isTrusted', isTrustedDescriptor);
124122
this[kTarget] = null;
125123
this[kIsBeingDispatched] = false;
126124
}
@@ -343,6 +341,11 @@ ObjectDefineProperties(
343341
eventPhase: kEnumerableProperty,
344342
cancelBubble: kEnumerableProperty,
345343
stopPropagation: kEnumerableProperty,
344+
// Don't conform to the spec with isTrusted. The spec defines it as
345+
// LegacyUnforgeable but defining it in the constructor has a big
346+
// performance impact and the property doesn't seem to be useful outside of
347+
// browsers.
348+
isTrusted: isTrustedDescriptor,
346349
});
347350

348351
function isCustomEvent(value) {

test/parallel/test-abortcontroller.js

+3-3
Original file line numberDiff line numberDiff line change
@@ -57,9 +57,9 @@ const { setTimeout: sleep } = require('timers/promises');
5757
}));
5858
first.abort();
5959
second.abort();
60-
const firstTrusted = Reflect.getOwnPropertyDescriptor(ev1, 'isTrusted').get;
61-
const secondTrusted = Reflect.getOwnPropertyDescriptor(ev2, 'isTrusted').get;
62-
const untrusted = Reflect.getOwnPropertyDescriptor(ev3, 'isTrusted').get;
60+
const firstTrusted = Reflect.getOwnPropertyDescriptor(Object.getPrototypeOf(ev1), 'isTrusted').get;
61+
const secondTrusted = Reflect.getOwnPropertyDescriptor(Object.getPrototypeOf(ev2), 'isTrusted').get;
62+
const untrusted = Reflect.getOwnPropertyDescriptor(Object.getPrototypeOf(ev3), 'isTrusted').get;
6363
strictEqual(firstTrusted, secondTrusted);
6464
strictEqual(untrusted, firstTrusted);
6565
}

test/parallel/test-events-customevent.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -187,7 +187,7 @@ const { Event, EventTarget, CustomEvent } = require('internal/event_target');
187187
}
188188
{
189189
const ev = new CustomEvent('foo');
190-
deepStrictEqual(Object.keys(ev), ['isTrusted']);
190+
strictEqual(ev.isTrusted, false);
191191
}
192192

193193
// Works with EventTarget

test/parallel/test-eventtarget.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -126,7 +126,7 @@ let asyncTest = Promise.resolve();
126126
}
127127
{
128128
const ev = new Event('foo');
129-
deepStrictEqual(Object.keys(ev), ['isTrusted']);
129+
strictEqual(ev.isTrusted, false);
130130
}
131131
{
132132
const eventTarget = new EventTarget();

test/wpt/status/dom/events.json

+7
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,13 @@
1111
"Event-dispatch-listener-order.window.js": {
1212
"skip": "document is not defined"
1313
},
14+
"Event-isTrusted.any.js": {
15+
"fail": {
16+
"expected": [
17+
"Event-isTrusted"
18+
]
19+
}
20+
},
1421
"EventListener-addEventListener.sub.window.js": {
1522
"skip": "document is not defined"
1623
},

0 commit comments

Comments
 (0)