From 2d372b391e3ecf6fad764dd0919ca9b29c4cf816 Mon Sep 17 00:00:00 2001 From: Daeyeon Jeong Date: Fri, 17 Jun 2022 21:02:26 +0900 Subject: [PATCH] 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 --- lib/internal/event_target.js | 8 ++--- .../test-whatwg-events-event-constructors.js | 29 +++++++++++++++++++ 2 files changed, 32 insertions(+), 5 deletions(-) create mode 100644 test/parallel/test-whatwg-events-event-constructors.js diff --git a/lib/internal/event_target.js b/lib/internal/event_target.js index 651cf55d5f3f8e..69e5ae84b1e120 100644 --- a/lib/internal/event_target.js +++ b/lib/internal/event_target.js @@ -96,13 +96,11 @@ 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.#cancelable = !!cancelable; this.#bubbles = !!bubbles; this.#composed = !!composed; diff --git a/test/parallel/test-whatwg-events-event-constructors.js b/test/parallel/test-whatwg-events-event-constructors.js new file mode 100644 index 00000000000000..7880b10043e462 --- /dev/null +++ b/test/parallel/test-whatwg-events-event-constructors.js @@ -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); +});