Skip to content

Commit bc5524b

Browse files
authored
Fix: bind fake event target to self #77 (#166)
fix: bind event target to self
1 parent 9d3d29a commit bc5524b

File tree

2 files changed

+20
-0
lines changed

2 files changed

+20
-0
lines changed

src/misc/event-emitter.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,10 @@ export const EventEmitter = function (nodeStyle) {
3838
const event = args.shift();
3939
if (!nodeStyle) {
4040
args[0] = mergeObjects(args[0], fakeEvent(event));
41+
Object.defineProperty(args[0], "target", {
42+
writable: false,
43+
value: this,
44+
});
4145
}
4246
const legacylistener = emitter[`on${event}`];
4347
if (legacylistener) {

tests/event-target.spec.ts

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
import { test, expect } from "@playwright/test";
2+
3+
test("event target should be xhr self", async ({ page }) => {
4+
await page.goto("http://127.0.0.1:8080/example/common.html");
5+
const res = await page.evaluate(async () => {
6+
return new Promise(resolve => {
7+
const xhr = new XMLHttpRequest();
8+
xhr.open("GET", "example1.txt");
9+
xhr.addEventListener("load", function (e) {
10+
resolve(e.target === xhr);
11+
});
12+
xhr.send();
13+
});
14+
});
15+
expect(res).toEqual(true);
16+
});

0 commit comments

Comments
 (0)