From 676381056f7cd6e6c4c134c4e73c1f2a03af967e Mon Sep 17 00:00:00 2001 From: Daijiro Wachi Date: Sat, 31 Oct 2020 07:38:26 +0900 Subject: [PATCH 1/2] lib: let abort_controller target be EventTarget The AbortController abort event should have EventTarget as a target property of the argument event. --- lib/internal/abort_controller.js | 5 ++--- lib/internal/event_target.js | 5 ++--- 2 files changed, 4 insertions(+), 6 deletions(-) diff --git a/lib/internal/abort_controller.js b/lib/internal/abort_controller.js index dc494e920aeec2..592e46e5320ce9 100644 --- a/lib/internal/abort_controller.js +++ b/lib/internal/abort_controller.js @@ -10,6 +10,7 @@ const { } = primordials; const { + defineEventHandler, EventTarget, Event, kTrustEvent @@ -53,9 +54,6 @@ function abortSignal(signal) { const event = new Event('abort', { [kTrustEvent]: true }); - if (typeof signal.onabort === 'function') { - signal.onabort(event); - } signal.dispatchEvent(event); } @@ -67,6 +65,7 @@ class AbortController { constructor() { this[kSignal] = new AbortSignal(); emitExperimentalWarning('AbortController'); + defineEventHandler(this[kSignal], 'abort'); } get signal() { return this[kSignal]; } diff --git a/lib/internal/event_target.js b/lib/internal/event_target.js index 4023e59bfe5359..e05ccd4b11f9ac 100644 --- a/lib/internal/event_target.js +++ b/lib/internal/event_target.js @@ -334,14 +334,13 @@ class EventTarget { } return event; }; + if (event !== undefined) + event[kTarget] = this; const root = this[kEvents].get(type); if (root === undefined || root.next === undefined) return true; - if (event !== undefined) - event[kTarget] = this; - let handler = root.next; let next; From 9cf54b8b8ca5d1807764ffe946667424c09b3c4a Mon Sep 17 00:00:00 2001 From: Daijiro Wachi Date: Sat, 31 Oct 2020 07:38:52 +0900 Subject: [PATCH 2/2] test: integrate abort_controller tests from wpt Refs: https://github.com/web-platform-tests/wpt/pull/9361 --- test/fixtures/wpt/README.md | 1 + test/fixtures/wpt/dom/abort/event.any.js | 67 ++++++++++++++++++++++++ test/fixtures/wpt/versions.json | 6 ++- test/parallel/test-eventtarget.js | 6 +++ test/wpt/status/dom/abort.json | 1 + test/wpt/test-abort.js | 7 +++ 6 files changed, 87 insertions(+), 1 deletion(-) create mode 100644 test/fixtures/wpt/dom/abort/event.any.js create mode 100644 test/wpt/status/dom/abort.json create mode 100644 test/wpt/test-abort.js diff --git a/test/fixtures/wpt/README.md b/test/fixtures/wpt/README.md index e0880c0efd1d22..096c876f7b2956 100644 --- a/test/fixtures/wpt/README.md +++ b/test/fixtures/wpt/README.md @@ -19,6 +19,7 @@ Last update: - html/webappapis/timers: https://github.com/web-platform-tests/wpt/tree/264f12bc7b/html/webappapis/timers - hr-time: https://github.com/web-platform-tests/wpt/tree/a5d1774ecf/hr-time - common: https://github.com/web-platform-tests/wpt/tree/4dacb6e2ff/common +- dom/abort: https://github.com/web-platform-tests/wpt/tree/7caa3de747/dom/abort [Web Platform Tests]: https://github.com/web-platform-tests/wpt [`git node wpt`]: https://github.com/nodejs/node-core-utils/blob/master/docs/git-node.md#git-node-wpt diff --git a/test/fixtures/wpt/dom/abort/event.any.js b/test/fixtures/wpt/dom/abort/event.any.js new file mode 100644 index 00000000000000..a67e6f400fcf1d --- /dev/null +++ b/test/fixtures/wpt/dom/abort/event.any.js @@ -0,0 +1,67 @@ +test(t => { + const c = new AbortController(), + s = c.signal; + let state = "begin"; + + assert_false(s.aborted); + + s.addEventListener("abort", + t.step_func(e => { + assert_equals(state, "begin"); + state = "aborted"; + }) + ); + c.abort(); + + assert_equals(state, "aborted"); + assert_true(s.aborted); + + c.abort(); +}, "AbortController abort() should fire event synchronously"); + +test(t => { + const controller = new AbortController(); + const signal = controller.signal; + assert_equals(controller.signal, signal, + "value of controller.signal should not have changed"); + controller.abort(); + assert_equals(controller.signal, signal, + "value of controller.signal should still not have changed"); +}, "controller.signal should always return the same object"); + +test(t => { + const controller = new AbortController(); + const signal = controller.signal; + let eventCount = 0; + signal.onabort = () => { + ++eventCount; + }; + controller.abort(); + assert_true(signal.aborted); + assert_equals(eventCount, 1, "event handler should have been called once"); + controller.abort(); + assert_true(signal.aborted); + assert_equals(eventCount, 1, + "event handler should not have been called again"); +}, "controller.abort() should do nothing the second time it is called"); + +test(t => { + const controller = new AbortController(); + controller.abort(); + controller.signal.onabort = + t.unreached_func("event handler should not be called"); +}, "event handler should not be called if added after controller.abort()"); + +test(t => { + const controller = new AbortController(); + const signal = controller.signal; + signal.onabort = t.step_func(e => { + assert_equals(e.type, "abort", "event type should be abort"); + assert_equals(e.target, signal, "event target should be signal"); + assert_false(e.bubbles, "event should not bubble"); + assert_true(e.isTrusted, "event should be trusted"); + }); + controller.abort(); +}, "the abort event should have the right properties"); + +done(); diff --git a/test/fixtures/wpt/versions.json b/test/fixtures/wpt/versions.json index 49a6b702ba414a..e18a5e0213fb3e 100644 --- a/test/fixtures/wpt/versions.json +++ b/test/fixtures/wpt/versions.json @@ -34,5 +34,9 @@ "common": { "commit": "4dacb6e2ff8dbabbe328fde2d8c75491598e4c10", "path": "common" + }, + "dom/abort": { + "commit": "7caa3de7471cf19b78ee9efa313c7341a462b5e3", + "path": "dom/abort" } -} \ No newline at end of file +} diff --git a/test/parallel/test-eventtarget.js b/test/parallel/test-eventtarget.js index 9487c22e26d1af..786cbc65f3371b 100644 --- a/test/parallel/test-eventtarget.js +++ b/test/parallel/test-eventtarget.js @@ -479,3 +479,9 @@ let asyncTest = Promise.resolve(); throws(() => eventTarget.addEventListener('foo', Symbol()), TypeError); }); } +{ + const eventTarget = new EventTarget(); + const event = new Event('foo'); + eventTarget.dispatchEvent(event); + strictEqual(event.target, eventTarget); +} diff --git a/test/wpt/status/dom/abort.json b/test/wpt/status/dom/abort.json new file mode 100644 index 00000000000000..0967ef424bce67 --- /dev/null +++ b/test/wpt/status/dom/abort.json @@ -0,0 +1 @@ +{} diff --git a/test/wpt/test-abort.js b/test/wpt/test-abort.js new file mode 100644 index 00000000000000..11960351b756e6 --- /dev/null +++ b/test/wpt/test-abort.js @@ -0,0 +1,7 @@ +'use strict'; +require('../common'); +const { WPTRunner } = require('../common/wpt'); + +const runner = new WPTRunner('dom/abort'); + +runner.runJsTests();