Skip to content

Commit

Permalink
Merge pull request #1093 from tkrotoff/fix-PointerEvent
Browse files Browse the repository at this point in the history
#1092@patch: Makes PointerEvent implementation more specification compliant.
  • Loading branch information
capricorn86 authored Sep 30, 2023
2 parents c68e51a + 04b2fcc commit aa8bdf1
Show file tree
Hide file tree
Showing 19 changed files with 187 additions and 189 deletions.
18 changes: 8 additions & 10 deletions packages/happy-dom/src/event/Event.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,17 +12,17 @@ import IDocument from '../nodes/document/IDocument.js';
* Event.
*/
export default class Event {
public composed = false;
public bubbles = false;
public cancelable = false;
public composed: boolean;
public bubbles: boolean;
public cancelable: boolean;
public defaultPrevented = false;
public eventPhase: EventPhaseEnum = EventPhaseEnum.none;
public _immediatePropagationStopped = false;
public _propagationStopped = false;
public _target: IEventTarget = null;
public _currentTarget: IEventTarget = null;
public timeStamp: number = performance.now();
public type: string = null;
public type: string;
public _isInPassiveEventListener = false;
public NONE = EventPhaseEnum.none;
public CAPTURING_PHASE = EventPhaseEnum.capturing;
Expand All @@ -35,14 +35,12 @@ export default class Event {
* @param type Event type.
* @param [eventInit] Event init.
*/
constructor(type: string, eventInit: IEventInit = null) {
constructor(type: string, eventInit: IEventInit | null = null) {
this.type = type;

if (eventInit) {
this.bubbles = eventInit.bubbles || false;
this.cancelable = eventInit.cancelable || false;
this.composed = eventInit.composed || false;
}
this.bubbles = eventInit?.bubbles ?? false;
this.cancelable = eventInit?.cancelable ?? false;
this.composed = eventInit?.composed ?? false;
}

/**
Expand Down
12 changes: 5 additions & 7 deletions packages/happy-dom/src/event/UIEvent.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,25 +10,23 @@ export default class UIEvent extends Event {
public static CAPTURING_PHASE = 1;
public static AT_TARGET = 2;
public static BUBBLING_PHASE = 3;
public readonly detail: number = 0;
public readonly detail: number;
public readonly layerX: number = 0;
public readonly layerY: number = 0;
public readonly pageX: number = 0;
public readonly pageY: number = 0;
public readonly view: IWindow = null;
public readonly view: IWindow | null;

/**
* Constructor.
*
* @param type Event type.
* @param [eventInit] Event init.
*/
constructor(type: string, eventInit: IUIEventInit = null) {
constructor(type: string, eventInit: IUIEventInit | null = null) {
super(type, eventInit);

if (eventInit) {
this.detail = eventInit.detail !== undefined ? eventInit.detail : 0;
this.view = eventInit.view || null;
}
this.detail = eventInit?.detail ?? 0;
this.view = eventInit?.view ?? null;
}
}
15 changes: 8 additions & 7 deletions packages/happy-dom/src/event/events/AnimationEvent.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,20 +5,21 @@ import IAnimationEventInit from './IAnimationEventInit.js';
*
*/
export default class AnimationEvent extends Event {
public animationName = '';
public elapsedTime = 0;
public pseudoElement = '';
public readonly animationName: string;
public readonly elapsedTime: number;
public readonly pseudoElement: string;

/**
* Constructor.
*
* @param type Event type.
* @param [eventInit] Event init.
*/
constructor(type: string, eventInit?: IAnimationEventInit) {
constructor(type: string, eventInit: IAnimationEventInit | null = null) {
super(type, eventInit);
this.animationName = eventInit?.animationName || '';
this.elapsedTime = eventInit?.elapsedTime || 0;
this.pseudoElement = eventInit?.pseudoElement || '';

this.animationName = eventInit?.animationName ?? '';
this.elapsedTime = eventInit?.elapsedTime ?? 0;
this.pseudoElement = eventInit?.pseudoElement ?? '';
}
}
9 changes: 4 additions & 5 deletions packages/happy-dom/src/event/events/CustomEvent.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,20 +5,19 @@ import ICustomEventInit from './ICustomEventInit.js';
*
*/
export default class CustomEvent extends Event {
public detail: object = null;
// eslint-disable-next-line @typescript-eslint/no-explicit-any
public detail: any;

/**
* Constructor.
*
* @param type Event type.
* @param [eventInit] Event init.
*/
constructor(type: string, eventInit: ICustomEventInit = null) {
constructor(type: string, eventInit: ICustomEventInit | null = null) {
super(type, eventInit);

if (eventInit) {
this.detail = eventInit.detail !== undefined ? eventInit.detail : null;
}
this.detail = eventInit?.detail ?? null;
}

/**
Expand Down
24 changes: 11 additions & 13 deletions packages/happy-dom/src/event/events/ErrorEvent.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,27 +5,25 @@ import IErrorEventInit from './IErrorEventInit.js';
*
*/
export default class ErrorEvent extends UIEvent {
public readonly message: string = '';
public readonly filename: string = '';
public readonly lineno: number = 0;
public readonly colno: number = 0;
public readonly error: Error | null = null;
public readonly message: string;
public readonly filename: string;
public readonly lineno: number;
public readonly colno: number;
public readonly error: Error | null;

/**
* Constructor.
*
* @param type Event type.
* @param [eventInit] Event init.
*/
constructor(type: string, eventInit: IErrorEventInit = null) {
constructor(type: string, eventInit: IErrorEventInit | null = null) {
super(type, eventInit);

if (eventInit) {
this.message = eventInit.message || '';
this.filename = eventInit.filename || '';
this.lineno = eventInit.lineno !== undefined ? eventInit.lineno : 0;
this.colno = eventInit.colno !== undefined ? eventInit.colno : 0;
this.error = eventInit.error || null;
}
this.message = eventInit?.message ?? '';
this.filename = eventInit?.filename ?? '';
this.lineno = eventInit?.lineno ?? 0;
this.colno = eventInit?.colno ?? 0;
this.error = eventInit?.error ?? null;
}
}
8 changes: 3 additions & 5 deletions packages/happy-dom/src/event/events/FocusEvent.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,19 +6,17 @@ import IFocusEventInit from './IFocusEventInit.js';
*
*/
export default class FocusEvent extends UIEvent {
public readonly relatedTarget: EventTarget = null;
public readonly relatedTarget: EventTarget | null;

/**
* Constructor.
*
* @param type Event type.
* @param [eventInit] Event init.
*/
constructor(type: string, eventInit: IFocusEventInit = null) {
constructor(type: string, eventInit: IFocusEventInit | null = null) {
super(type, eventInit);

if (eventInit) {
this.relatedTarget = eventInit.relatedTarget || null;
}
this.relatedTarget = eventInit?.relatedTarget ?? null;
}
}
5 changes: 5 additions & 0 deletions packages/happy-dom/src/event/events/IPointerEventInit.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import IMouseEventInit from './IMouseEventInit.js';
import PointerEvent from './PointerEvent.js';

export default interface IPointerEventInit extends IMouseEventInit {
pointerId?: number;
Expand All @@ -9,6 +10,10 @@ export default interface IPointerEventInit extends IMouseEventInit {
tiltX?: number;
tiltY?: number;
twist?: number;
altitudeAngle?: number;
azimuthAngle?: number;
pointerType?: string;
isPrimary?: boolean;
coalescedEvents?: PointerEvent[];
predictedEvents?: PointerEvent[];
}
3 changes: 2 additions & 1 deletion packages/happy-dom/src/event/events/IStorageEventInit.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,8 @@ import Storage from '../../storage/Storage.js';

export default interface IStorageEventInit extends IEventInit {
key?: string;
newValue?: string;
oldValue?: string;
newValue?: string;
url?: string;
storageArea?: Storage;
}
20 changes: 9 additions & 11 deletions packages/happy-dom/src/event/events/InputEvent.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,25 +6,23 @@ import IInputEventInit from './IInputEventInit.js';
*
*/
export default class InputEvent extends UIEvent {
public readonly data: string = '';
public readonly dataTransfer: DataTransfer = null;
public readonly inputType: string = '';
public readonly isComposing: boolean = false;
public readonly data: string;
public readonly dataTransfer: DataTransfer;
public readonly inputType: string;
public readonly isComposing: boolean;

/**
* Constructor.
*
* @param type Event type.
* @param [eventInit] Event init.
*/
constructor(type: string, eventInit: IInputEventInit = null) {
constructor(type: string, eventInit: IInputEventInit | null = null) {
super(type, eventInit);

if (eventInit) {
this.data = eventInit.data || '';
this.dataTransfer = eventInit.dataTransfer || null;
this.inputType = eventInit.inputType || '';
this.isComposing = eventInit.isComposing || false;
}
this.data = eventInit?.data ?? '';
this.dataTransfer = eventInit?.dataTransfer ?? null;
this.inputType = eventInit?.inputType ?? '';
this.isComposing = eventInit?.isComposing ?? false;
}
}
40 changes: 19 additions & 21 deletions packages/happy-dom/src/event/events/KeyboardEvent.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,35 +9,33 @@ export default class KeyboardEvent extends UIEvent {
public static DOM_KEY_LOCATION_LEFT = 1;
public static DOM_KEY_LOCATION_RIGHT = 2;
public static DOM_KEY_LOCATION_NUMPAD = 3;
public readonly altKey: boolean = false;
public readonly code: string = '';
public readonly ctrlKey: boolean = false;
public readonly isComposing: boolean = false;
public readonly key: string = '';
public readonly location: number = 0;
public readonly metaKey: boolean = false;
public readonly repeat: boolean = false;
public readonly shiftKey: boolean = false;
public readonly altKey: boolean;
public readonly code: string;
public readonly ctrlKey: boolean;
public readonly isComposing: boolean;
public readonly key: string;
public readonly location: number;
public readonly metaKey: boolean;
public readonly repeat: boolean;
public readonly shiftKey: boolean;

/**
* Constructor.
*
* @param type Event type.
* @param [eventInit] Event init.
*/
constructor(type: string, eventInit: IKeyboardEventInit = null) {
constructor(type: string, eventInit: IKeyboardEventInit | null = null) {
super(type, eventInit);

if (eventInit) {
this.altKey = eventInit.altKey || false;
this.code = eventInit.code || '';
this.ctrlKey = eventInit.ctrlKey || false;
this.isComposing = eventInit.isComposing || false;
this.key = eventInit.key || '';
this.location = eventInit.location !== undefined ? eventInit.location : 0;
this.metaKey = eventInit.metaKey || false;
this.repeat = eventInit.repeat || false;
this.shiftKey = eventInit.shiftKey || false;
}
this.altKey = eventInit?.altKey ?? false;
this.code = eventInit?.code ?? '';
this.ctrlKey = eventInit?.ctrlKey ?? false;
this.isComposing = eventInit?.isComposing ?? false;
this.key = eventInit?.key ?? '';
this.location = eventInit?.location ?? 0;
this.metaKey = eventInit?.metaKey ?? false;
this.repeat = eventInit?.repeat ?? false;
this.shiftKey = eventInit?.shiftKey ?? false;
}
}
12 changes: 5 additions & 7 deletions packages/happy-dom/src/event/events/MediaQueryListEvent.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,21 +5,19 @@ import IMediaQueryListInit from './IMediaQueryListInit.js';
*
*/
export default class MediaQueryListEvent extends Event {
public readonly matches: boolean = false;
public readonly media: string = '';
public readonly matches: boolean;
public readonly media: string;

/**
* Constructor.
*
* @param type Event type.
* @param [eventInit] Event init.
*/
constructor(type: string, eventInit: IMediaQueryListInit = null) {
constructor(type: string, eventInit: IMediaQueryListInit | null = null) {
super(type, eventInit);

if (eventInit) {
this.matches = eventInit.matches || false;
this.media = eventInit.media || '';
}
this.matches = eventInit?.matches ?? false;
this.media = eventInit?.media ?? '';
}
}
23 changes: 12 additions & 11 deletions packages/happy-dom/src/event/events/MessageEvent.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,24 +9,25 @@ import IMessageEventInit from './IMessageEventInit.js';
* @see https://developer.mozilla.org/en-US/docs/Web/API/MessageEvent
*/
export default class MessageEvent extends Event {
public data?: unknown | null = null;
public origin?: string = '';
public lastEventId?: string = '';
public source?: IWindow | null = null;
public ports?: IMessagePort[] = [];
public readonly data: unknown | null;
public readonly origin: string;
public readonly lastEventId: string;
public readonly source: IWindow | null;
public readonly ports: IMessagePort[];

/**
* Constructor.
*
* @param type Event type.
* @param [eventInit] Event init.
*/
constructor(type: string, eventInit?: IMessageEventInit) {
constructor(type: string, eventInit: IMessageEventInit | null = null) {
super(type, eventInit);
this.data = eventInit?.data !== undefined ? eventInit.data : null;
this.origin = eventInit?.origin || '';
this.lastEventId = eventInit?.lastEventId || '';
this.source = eventInit?.source || null;
this.ports = eventInit?.ports || [];

this.data = eventInit?.data ?? null;
this.origin = eventInit?.origin ?? '';
this.lastEventId = eventInit?.lastEventId ?? '';
this.source = eventInit?.source ?? null;
this.ports = eventInit?.ports ?? [];
}
}
Loading

0 comments on commit aa8bdf1

Please sign in to comment.