Skip to content

Commit

Permalink
events: improve Event compatibility
Browse files Browse the repository at this point in the history
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>
  • Loading branch information
daeyeon authored and targos committed Jul 31, 2022
1 parent c6bdb5c commit 0167988
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 5 deletions.
9 changes: 4 additions & 5 deletions lib/internal/event_target.js
Original file line number Diff line number Diff line change
Expand Up @@ -97,16 +97,15 @@ class Event {
* composed?: boolean,
* }} [options]
*/
constructor(type, options = null) {
constructor(type, options = kEmptyObject) {
if (arguments.length === 0)
throw new ERR_MISSING_ARGS('type');
validateObject(options, 'options', {
allowArray: true, allowFunction: true, nullable: true,
});
const { cancelable, bubbles, composed } = { ...options };
validateObject(options, 'options');
const { bubbles, cancelable, composed } = options;
this[kCancelable] = !!cancelable;
this[kBubbles] = !!bubbles;
this[kComposed] = !!composed;

this[kType] = `${type}`;
this[kDefaultPrevented] = false;
this[kTimestamp] = now();
Expand Down
29 changes: 29 additions & 0 deletions test/parallel/test-whatwg-events-event-constructors.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
'use strict';

require('../common');
const { test, assert_equals, assert_array_equals } =
require('../common/wpt').harness;

// Source: https://github.com/web-platform-tests/wpt/blob/6cef1d2087d6a07d7cc6cee8cf207eec92e27c5f/dom/events/Event-constructors.any.js#L91-L112
test(function() {
const called = [];
const ev = new Event('Xx', {
get cancelable() {
called.push('cancelable');
return false;
},
get bubbles() {
called.push('bubbles');
return true;
},
get sweet() {
called.push('sweet');
return 'x';
},
});
assert_array_equals(called, ['bubbles', 'cancelable']);
assert_equals(ev.type, 'Xx');
assert_equals(ev.bubbles, true);
assert_equals(ev.cancelable, false);
assert_equals(ev.sweet, undefined);
});

0 comments on commit 0167988

Please sign in to comment.