Skip to content

Commit 1923af3

Browse files
lib: make event static properties non writable and configurable
The idl definition for Event makes the properties constant this means that they shouldn't be configurable and writable. However, they were, and this commit fixes that. Fixes: #50417
1 parent 6431c65 commit 1923af3

File tree

2 files changed

+44
-5
lines changed

2 files changed

+44
-5
lines changed

lib/internal/event_target.js

Lines changed: 17 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
const {
44
ArrayFrom,
5+
ArrayPrototypeReduce,
56
Boolean,
67
Error,
78
FunctionPrototypeCall,
@@ -314,11 +315,6 @@ class Event {
314315
throw new ERR_INVALID_THIS('Event');
315316
this.#propagationStopped = true;
316317
}
317-
318-
static NONE = 0;
319-
static CAPTURING_PHASE = 1;
320-
static AT_TARGET = 2;
321-
static BUBBLING_PHASE = 3;
322318
}
323319

324320
ObjectDefineProperties(
@@ -354,6 +350,22 @@ ObjectDefineProperties(
354350
isTrusted: isTrustedDescriptor,
355351
});
356352

353+
const staticProps = ['NONE', 'CAPTURING_PHASE', 'AT_TARGET', 'BUBBLING_PHASE'];
354+
355+
ObjectDefineProperties(
356+
Event,
357+
ArrayPrototypeReduce(staticProps, (result, staticProp, index = 0) => {
358+
result[staticProp] = {
359+
__proto__: null,
360+
writable: false,
361+
configurable: false,
362+
enumerable: true,
363+
value: index,
364+
};
365+
return result;
366+
}, {}),
367+
);
368+
357369
function isCustomEvent(value) {
358370
return isEvent(value) && (value?.[kDetail] !== undefined);
359371
}

test/parallel/test-event-target.js

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
'use strict';
2+
3+
require('../common');
4+
const assert = require('assert');
5+
6+
const eventPhases = {
7+
'NONE': 0,
8+
'CAPTURING_PHASE': 1,
9+
'AT_TARGET': 2,
10+
'BUBBLING_PHASE': 3
11+
};
12+
13+
// Using Object.keys to get the properties from the Event object
14+
const eventKeys = Object.keys(Event);
15+
16+
for (const [prop, value] of Object.entries(eventPhases)) {
17+
// Check if the property exists in the Event object
18+
assert.ok(eventKeys.includes(prop), `Event does not have the property: ${prop}`);
19+
20+
// Check if the value of the property matches the expected value
21+
assert.strictEqual(Event[prop], value, `Expected Event.${prop} to be ${value}, but got ${Event[prop]}`);
22+
23+
const desc = Object.getOwnPropertyDescriptor(Event, prop);
24+
assert.strictEqual(desc.writable, false, `${prop} should not be writable`);
25+
assert.strictEqual(desc.configurable, false, `${prop} should not be configurable`);
26+
assert.strictEqual(desc.enumerable, true, `${prop} should be enumerable`);
27+
}

0 commit comments

Comments
 (0)