Skip to content

Commit 0167988

Browse files
daeyeontargos
authored andcommitted
events: improve Event compatibility
This fixes `Event` constructor to improve `Event Web API` compatibility. The test added was written by referring to `wpt@dom/events/Event-constructors.any.js`. Signed-off-by: Daeyeon Jeong daeyeon.dev@gmail.com PR-URL: #43461 Reviewed-By: Antoine du Hamel <duhamelantoine1995@gmail.com> Reviewed-By: LiviaMedeiros <livia@cirno.name> Reviewed-By: Luigi Pinca <luigipinca@gmail.com> Reviewed-By: James M Snell <jasnell@gmail.com>
1 parent c6bdb5c commit 0167988

File tree

2 files changed

+33
-5
lines changed

2 files changed

+33
-5
lines changed

lib/internal/event_target.js

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -97,16 +97,15 @@ class Event {
9797
* composed?: boolean,
9898
* }} [options]
9999
*/
100-
constructor(type, options = null) {
100+
constructor(type, options = kEmptyObject) {
101101
if (arguments.length === 0)
102102
throw new ERR_MISSING_ARGS('type');
103-
validateObject(options, 'options', {
104-
allowArray: true, allowFunction: true, nullable: true,
105-
});
106-
const { cancelable, bubbles, composed } = { ...options };
103+
validateObject(options, 'options');
104+
const { bubbles, cancelable, composed } = options;
107105
this[kCancelable] = !!cancelable;
108106
this[kBubbles] = !!bubbles;
109107
this[kComposed] = !!composed;
108+
110109
this[kType] = `${type}`;
111110
this[kDefaultPrevented] = false;
112111
this[kTimestamp] = now();
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
'use strict';
2+
3+
require('../common');
4+
const { test, assert_equals, assert_array_equals } =
5+
require('../common/wpt').harness;
6+
7+
// Source: https://github.com/web-platform-tests/wpt/blob/6cef1d2087d6a07d7cc6cee8cf207eec92e27c5f/dom/events/Event-constructors.any.js#L91-L112
8+
test(function() {
9+
const called = [];
10+
const ev = new Event('Xx', {
11+
get cancelable() {
12+
called.push('cancelable');
13+
return false;
14+
},
15+
get bubbles() {
16+
called.push('bubbles');
17+
return true;
18+
},
19+
get sweet() {
20+
called.push('sweet');
21+
return 'x';
22+
},
23+
});
24+
assert_array_equals(called, ['bubbles', 'cancelable']);
25+
assert_equals(ev.type, 'Xx');
26+
assert_equals(ev.bubbles, true);
27+
assert_equals(ev.cancelable, false);
28+
assert_equals(ev.sweet, undefined);
29+
});

0 commit comments

Comments
 (0)