Skip to content

Commit

Permalink
fix: [#1628] Fixes issue where the wrong event target was used when d…
Browse files Browse the repository at this point in the history
…ispatching events on HTMLFormElement or HTMLSelectElement (#1655)
  • Loading branch information
capricorn86 authored Dec 31, 2024
1 parent 78eb240 commit b95a867
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 2 deletions.
4 changes: 2 additions & 2 deletions packages/happy-dom/src/event/EventTarget.ts
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,7 @@ export default class EventTarget {
*/
public dispatchEvent(event: Event): boolean {
if (!event[PropertySymbol.target]) {
event[PropertySymbol.target] = this;
event[PropertySymbol.target] = this[PropertySymbol.proxy] || this;

this.#goThroughDispatchEventPhases(event);

Expand Down Expand Up @@ -180,7 +180,7 @@ export default class EventTarget {

// At target phase
event[PropertySymbol.eventPhase] = EventPhaseEnum.atTarget;
event[PropertySymbol.currentTarget] = this;
event[PropertySymbol.currentTarget] = this[PropertySymbol.proxy] || this;
event[PropertySymbol.target].dispatchEvent(event);

// Bubbling phase
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import { beforeEach, describe, it, expect, vi } from 'vitest';
import THTMLFormControlElement from '../../../src/nodes/html-form-element/THTMLFormControlElement.js';
import HTMLOutputElement from '../../../src/nodes/html-output-element/HTMLOutputElement.js';
import * as PropertySymbol from '../../../src/PropertySymbol.js';
import EventTarget from '../../../src/event/EventTarget.js';

describe('HTMLFormElement', () => {
let window: Window;
Expand Down Expand Up @@ -1352,4 +1353,22 @@ describe('HTMLFormElement', () => {
});
});
}

describe('dispatchEvent()', () => {
it('Dispatches events using the proxy as the target.', () => {
const event = new Event('test');
let target: EventTarget | null = null;
let currentTarget: EventTarget | null = null;

element.addEventListener('test', (event: Event) => {
target = event.target;
currentTarget = event.currentTarget;
});

element.dispatchEvent(event);

expect(target).toBe(element);
expect(currentTarget).toBe(element);
});
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import ValidityState from '../../../src/validity-state/ValidityState.js';
import Event from '../../../src/event/Event.js';
import { beforeEach, describe, it, expect } from 'vitest';
import HTMLCollection from '../../../src/nodes/element/HTMLCollection.js';
import EventTarget from '../../../src/event/EventTarget.js';

describe('HTMLSelectElement', () => {
let window: Window;
Expand Down Expand Up @@ -772,4 +773,22 @@ describe('HTMLSelectElement', () => {
});
});
}

describe('dispatchEvent()', () => {
it('Dispatches events using the proxy as the target.', () => {
const event = new Event('test');
let target: EventTarget | null = null;
let currentTarget: EventTarget | null = null;

element.addEventListener('test', (event: Event) => {
target = event.target;
currentTarget = event.currentTarget;
});

element.dispatchEvent(event);

expect(target).toBe(element);
expect(currentTarget).toBe(element);
});
});
});

0 comments on commit b95a867

Please sign in to comment.