Skip to content

Commit

Permalink
fix: preserve this inside addEventListener (#496)
Browse files Browse the repository at this point in the history
Co-authored-by: Vladimir Panov <vladimir.panov@motorsport.com>
  • Loading branch information
esjs and Vladimir Panov authored Jan 7, 2024
1 parent 606dc36 commit e76d90a
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 1 deletion.
2 changes: 1 addition & 1 deletion src/lib/web-worker/worker-window.ts
Original file line number Diff line number Diff line change
Expand Up @@ -463,7 +463,7 @@ export const createWindow = (
win.Worker = undefined;
}

addEventListener(...args: any[]) {
addEventListener = (...args: any[]) => {
if (args[0] === 'load') {
if (env.$runWindowLoadEvent$) {
setTimeout(() => args[1]({ type: 'load' }));
Expand Down
9 changes: 9 additions & 0 deletions tests/platform/event/event.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -58,4 +58,13 @@ test('events', async ({ page }) => {
await page.waitForSelector('.testPreventDefault');
const testPreventDefault = page.locator('#testPreventDefault');
await expect(testPreventDefault).toHaveText('preventDefault-noop');

await page.locator('body').dblclick();

const testWinAddEventListenerNoContext = page.locator('#testWinAddEventListenerNoContext');
await expect(testWinAddEventListenerNoContext).toHaveText('Window dblclick');
const testWinAddEventListenerNoContextCount = page.locator(
'#testWinAddEventListenerNoContextCount'
);
await expect(testWinAddEventListenerNoContextCount).toHaveText('1');
});
16 changes: 16 additions & 0 deletions tests/platform/event/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,22 @@ <h1>Event</h1>
</script>
</li>

<li>
<strong>(window.attachEvent || window.addEventListener)('dblclick')</strong>
<code id="testWinAddEventListenerNoContextCount">0</code>
<code id="testWinAddEventListenerNoContext"></code>
<script type="text/partytown">
(function () {
let count = 0;
(window.attachEvent || window.addEventListener)('dblclick', function (ev) {
document.getElementById('testWinAddEventListenerNoContextCount').textContent = ++count;
document.getElementById('testWinAddEventListenerNoContext').textContent =
this.constructor.name + ' ' + ev.type;
});
})();
</script>
</li>

<li>
<strong>document.createEvent('Event')</strong>
<code id="testCreateEvent" class="currentTarget"></code>
Expand Down

1 comment on commit e76d90a

@vercel
Copy link

@vercel vercel bot commented on e76d90a Jan 7, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please sign in to comment.