From 6a5177dc11936687e6da95c3c45e3e41a7856d79 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E8=BF=B7=E6=B8=A1?= Date: Thu, 20 Jun 2019 20:21:43 +0800 Subject: [PATCH] event `isTrusted` is enumerable (#2543) --- js/event.ts | 31 +++++++++++-------------------- js/event_target.ts | 2 -- js/event_test.ts | 15 ++++++++++++++- 3 files changed, 25 insertions(+), 23 deletions(-) diff --git a/js/event.ts b/js/event.ts index 92d2b5fef1..222b562a25 100644 --- a/js/event.ts +++ b/js/event.ts @@ -6,6 +6,10 @@ import { getPrivateValue, requiredArguments } from "./util"; // https://developer.mozilla.org/en-US/docs/Archive/Add-ons/Add-on_SDK/Guides/Contributor_s_Guide/Private_Properties#Using_WeakMaps export const eventAttributes = new WeakMap(); +function isTrusted(this: Event): boolean { + return getPrivateValue(this, eventAttributes, "isTrusted"); +} + export class EventInit implements domTypes.EventInit { bubbles = false; cancelable = false; @@ -19,6 +23,9 @@ export class EventInit implements domTypes.EventInit { } export class Event implements domTypes.Event { + // The default value is `false`. + // Use `defineProperty` to define on each instance, NOT on the prototype. + isTrusted!: boolean; // Each event has the following associated flags private _canceledFlag = false; private _dispatchedFlag = false; @@ -46,6 +53,10 @@ export class Event implements domTypes.Event { target: null, timeStamp: Date.now() }); + Reflect.defineProperty(this, "isTrusted", { + enumerable: true, + get: isTrusted + }); } get bubbles(): boolean { @@ -134,25 +145,6 @@ export class Event implements domTypes.Event { this._inPassiveListenerFlag = value; } - get isTrusted(): boolean { - return getPrivateValue(this, eventAttributes, "isTrusted"); - } - - set isTrusted(value: boolean) { - eventAttributes.set(this, { - type: this.type, - bubbles: this.bubbles, - cancelable: this.cancelable, - composed: this.composed, - currentTarget: this.currentTarget, - eventPhase: this.eventPhase, - isTrusted: value, - relatedTarget: this.relatedTarget, - target: this.target, - timeStamp: this.timeStamp - }); - } - get path(): domTypes.EventPath[] { return this._path; } @@ -363,7 +355,6 @@ Reflect.defineProperty(Event.prototype, "defaultPrevented", { }); Reflect.defineProperty(Event.prototype, "dispatched", { enumerable: true }); Reflect.defineProperty(Event.prototype, "eventPhase", { enumerable: true }); -Reflect.defineProperty(Event.prototype, "isTrusted", { enumerable: true }); Reflect.defineProperty(Event.prototype, "target", { enumerable: true }); Reflect.defineProperty(Event.prototype, "timeStamp", { enumerable: true }); Reflect.defineProperty(Event.prototype, "type", { enumerable: true }); diff --git a/js/event_target.ts b/js/event_target.ts index bb1166237c..0e06dc34ff 100644 --- a/js/event_target.ts +++ b/js/event_target.ts @@ -201,8 +201,6 @@ export class EventTarget implements domTypes.EventTarget { ); } - event.isTrusted = false; - return this._dispatch(event); } diff --git a/js/event_test.ts b/js/event_test.ts index 75ff8bed1e..b31ffdecfb 100644 --- a/js/event_test.ts +++ b/js/event_test.ts @@ -1,5 +1,5 @@ // Copyright 2018-2019 the Deno authors. All rights reserved. MIT license. -import { test, assertEquals } from "./test_util.ts"; +import { test, assertEquals, assertNotEquals } from "./test_util.ts"; test(function eventInitializedWithType(): void { const type = "click"; @@ -80,3 +80,16 @@ test(function eventInitializedWithNonStringType(): void { assertEquals(event.bubbles, false); assertEquals(event.cancelable, false); }); + +// ref https://github.com/web-platform-tests/wpt/blob/master/dom/events/Event-isTrusted.any.js +test(function eventIsTrusted(): void { + const desc1 = Object.getOwnPropertyDescriptor(new Event("x"), "isTrusted"); + assertNotEquals(desc1, undefined); + assertEquals(typeof desc1.get, "function"); + + const desc2 = Object.getOwnPropertyDescriptor(new Event("x"), "isTrusted"); + assertNotEquals(desc2, undefined); + assertEquals(typeof desc2.get, "function"); + + assertEquals(desc1.get, desc2.get); +});