From a738164fed755da9a7987fb10bd1dee8e51c3d2d Mon Sep 17 00:00:00 2001 From: Santiago Gimeno Date: Mon, 3 Apr 2023 19:02:26 +0200 Subject: [PATCH] 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: https://github.com/nodejs/performance/issues/32 PR-URL: https://github.com/nodejs/node/pull/46974 Reviewed-By: Yagiz Nizipli Reviewed-By: Ben Noordhuis Reviewed-By: James M Snell --- lib/internal/event_target.js | 7 +++++-- test/parallel/test-abortcontroller.js | 6 +++--- test/parallel/test-events-customevent.js | 2 +- test/parallel/test-eventtarget.js | 2 +- test/wpt/status/dom/events.json | 7 +++++++ 5 files changed, 17 insertions(+), 7 deletions(-) diff --git a/lib/internal/event_target.js b/lib/internal/event_target.js index 60c5eb80ceabd3..fa689ef38cee01 100644 --- a/lib/internal/event_target.js +++ b/lib/internal/event_target.js @@ -119,8 +119,6 @@ class Event { isTrustedSet.add(this); } - // isTrusted is special (LegacyUnforgeable) - ObjectDefineProperty(this, 'isTrusted', isTrustedDescriptor); this[kTarget] = null; this[kIsBeingDispatched] = false; } @@ -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) { diff --git a/test/parallel/test-abortcontroller.js b/test/parallel/test-abortcontroller.js index ce6c72f4294043..4099a018df7daf 100644 --- a/test/parallel/test-abortcontroller.js +++ b/test/parallel/test-abortcontroller.js @@ -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); } diff --git a/test/parallel/test-events-customevent.js b/test/parallel/test-events-customevent.js index dc4acb312c1131..2261240c526639 100644 --- a/test/parallel/test-events-customevent.js +++ b/test/parallel/test-events-customevent.js @@ -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 diff --git a/test/parallel/test-eventtarget.js b/test/parallel/test-eventtarget.js index f6208a3b8b8844..ce372f7d6fc8f8 100644 --- a/test/parallel/test-eventtarget.js +++ b/test/parallel/test-eventtarget.js @@ -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(); diff --git a/test/wpt/status/dom/events.json b/test/wpt/status/dom/events.json index 90825d76a358d6..c0f4104c452b85 100644 --- a/test/wpt/status/dom/events.json +++ b/test/wpt/status/dom/events.json @@ -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" },