Skip to content

Commit

Permalink
lib: event static properties non writable and configurable
Browse files Browse the repository at this point in the history
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: nodejs#50417
  • Loading branch information
BenzeneAlcohol committed Nov 1, 2023
1 parent 53502c7 commit 88a46fd
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 31 deletions.
36 changes: 10 additions & 26 deletions lib/internal/event_target.js
Original file line number Diff line number Diff line change
Expand Up @@ -349,37 +349,21 @@ ObjectDefineProperties(
isTrusted: isTrustedDescriptor,
});

const staticProps = ['NONE', 'CAPTURING_PHASE', 'AT_TARGET', 'BUBBLING_PHASE'];

ObjectDefineProperties(
Event, {
NONE: {
__proto__: null,
writable: false,
configurable: false,
enumerable: true,
value: 0,
},
CAPTURING_PHASE: {
__proto__: null,
writable: false,
configurable: false,
enumerable: true,
value: 1,
},
AT_TARGET: {
__proto__: null,
writable: false,
configurable: false,
enumerable: true,
value: 2,
},
BUBBLING_PHASE: {
Event,
staticProps.reduce((result, staticProp, index = 0) => {
result[staticProp] = {
__proto__: null,
writable: false,
configurable: false,
enumerable: true,
value: 3,
},
});
value: index,
};
return result;
}, {}),
);

function isCustomEvent(value) {
return isEvent(value) && (value?.[kDetail] !== undefined);
Expand Down
27 changes: 22 additions & 5 deletions test/parallel/test-event-target.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,28 @@
require('../common');
const assert = require('assert');

const props = ['NONE', 'CAPTURING_PHASE', 'AT_TARGET', 'BUBBLING_PHASE'];
const eventPhases = {
'NONE': 0,
'CAPTURING_PHASE': 1,
'AT_TARGET': 2,
'BUBBLING_PHASE': 3
};

// Using Object.keys to get the properties from the Event object
const eventKeys = Object.keys(Event);
console.log(eventKeys);

for (const [prop, value] of Object.entries(eventPhases)) {
console.log(prop);
console.log(value);
// Check if the property exists in the Event object
assert.ok(eventKeys.includes(prop), `Event does not have the property: ${prop}`);

// Check if the value of the property matches the expected value
assert.strictEqual(Event[prop], value, `Expected Event.${prop} to be ${value}, but got ${Event[prop]}`);

for (const prop of props) {
const desc = Object.getOwnPropertyDescriptor(Event, prop);
assert.strictEqual(desc.writable, false);
assert.strictEqual(desc.configurable, false);
assert.strictEqual(desc.enumerable, true);
assert.strictEqual(desc.writable, false, `${prop} should not be writable`);
assert.strictEqual(desc.configurable, false, `${prop} should not be configurable`);
assert.strictEqual(desc.enumerable, true, `${prop} should be enumerable`);
}

0 comments on commit 88a46fd

Please sign in to comment.