Skip to content

Commit 21af47b

Browse files
committed
chore(all): adopt pal changes
1 parent d2f747d commit 21af47b

File tree

4 files changed

+46
-16
lines changed

4 files changed

+46
-16
lines changed

src/dom.ts

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -29,14 +29,14 @@ export interface IDom {
2929
* @param callback The function that receives a notification when an event of the specified type occurs.
3030
* @param capture If true, useCapture indicates that the user wishes to initiate capture.
3131
*/
32-
addEventListener(eventName: string, callback: EventListener, capture: boolean): void;
32+
addEventListener(eventName: string, callback: EventListenerOrEventListenerObject, capture: boolean): void;
3333
/**
3434
* Remove an event listener from the document.
3535
* @param eventName A string representing the event type to listen for.
3636
* @param callback The function to remove from the event.
3737
* @param capture Specifies whether the listener to be removed was registered as a capturing listener or not.
3838
*/
39-
removeEventListener(eventName: string, callback: EventListener, capture: boolean): void;
39+
removeEventListener(eventName: string, callback: EventListenerOrEventListenerObject, capture: boolean): void;
4040
/**
4141
* Adopts a node from an external document.
4242
* @param node The node to be adopted.
@@ -48,7 +48,8 @@ export interface IDom {
4848
* @param tagName A string that specifies the type of element to be created.
4949
* @return The created element.
5050
*/
51-
createElement(tagName: string): Element;
51+
createElement<T extends keyof HTMLElementTagNameMap>(tagName: T): HTMLElementTagNameMap[T];
52+
createElement(tagName: string): HTMLElement;
5253
/**
5354
* Creates the specified HTML attribute
5455
* @param name A string that specifies the name of attribute to be created.
@@ -102,12 +103,20 @@ export interface IDom {
102103
* @return The found element.
103104
*/
104105
getElementById(id: string): Element;
106+
107+
/**
108+
* Performs a query selector on the document and returns first matched element, depth first.
109+
* @param query The query to use in searching the document.
110+
* @return A list of all matched elements in the document.
111+
*/
112+
querySelector<E extends Element = Element>(selectors: string): E | null;
113+
105114
/**
106115
* Performs a query selector on the document and returns all located matches.
107116
* @param query The query to use in searching the document.
108117
* @return A list of all matched elements in the document.
109118
*/
110-
querySelectorAll(query: string): NodeList;
119+
querySelectorAll<E extends Element = Element>(selectors: string): NodeListOf<E>;
111120
/**
112121
* Gets the element that is the next sibling of the provided element.
113122
* @param element The element whose next sibling is being located.
@@ -119,7 +128,7 @@ export interface IDom {
119128
* @param markup A string containing the markup to turn into a template. Note: This string must contain the template element as well.
120129
* @return The instance of HTMLTemplateElement that was created from the provided markup.
121130
*/
122-
createTemplateFromMarkup(markup: string): Element;
131+
createTemplateFromMarkup(markup: string): HTMLTemplateElement;
123132
/**
124133
* Appends a node to the parent, if provided, or the document.body otherwise.
125134
* @param newNode The node to append.

src/nodejs-dom.ts

Lines changed: 16 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,13 @@
11
import { IDom } from './dom';
22
import { IGlobal } from './global';
33

4+
declare module './global' {
5+
interface IGlobal {
6+
window: any;
7+
document: any;
8+
}
9+
}
10+
411
/**
512
* Represents the core APIs of the DOM.
613
*/
@@ -17,12 +24,13 @@ export class NodeJsDom implements IDom {
1724
title: string = "";
1825
activeElement: Element = null;
1926

20-
addEventListener(eventName: string, callback: EventListener, capture: boolean): void {
27+
addEventListener(eventName: string, callback: EventListenerOrEventListenerObject, capture: boolean): void {
2128
return this.global.document.addEventListener(eventName, callback, capture);
2229
}
23-
removeEventListener(eventName: string, callback: EventListener, capture: boolean): void {
30+
removeEventListener(eventName: string, callback: EventListenerOrEventListenerObject, capture: boolean): void {
2431
return this.global.document.removeEventListener(eventName, callback, capture);
2532
}
33+
createElement<T extends keyof HTMLElementTagNameMap>(tagName: T): HTMLElementTagNameMap[T];
2634
createElement(tagName: string): Element {
2735
return this.global.document.createElement(tagName);
2836
}
@@ -41,7 +49,7 @@ export class NodeJsDom implements IDom {
4149
createMutationObserver(callback: (changes: MutationRecord[], instance: MutationObserver) => void): MutationObserver {
4250
return new ((<any>this.global.window).MutationObserver)(callback);
4351
}
44-
createCustomEvent(eventType: string, options: Object): CustomEvent {
52+
createCustomEvent(eventType: string, options?: Object): CustomEvent {
4553
return new this.global.CustomEvent(eventType, options);
4654
}
4755
dispatchEvent(evt: Event): void {
@@ -53,13 +61,16 @@ export class NodeJsDom implements IDom {
5361
getElementById(id: string): Element {
5462
return this.global.document.getElementById(id);
5563
}
56-
querySelectorAll(query: string): NodeList {
64+
querySelector<E extends Element = Element>(query: string): E | null {
65+
return this.global.document.querySelector(query);
66+
}
67+
querySelectorAll<E extends Element = Element>(query: string): NodeListOf<E> {
5768
return this.global.document.querySelectorAll(query);
5869
}
5970
nextElementSibling(element: Element): Element {
6071
return element.nextElementSibling;
6172
}
62-
createTemplateFromMarkup(markup: string): Element {
73+
createTemplateFromMarkup(markup: string): HTMLTemplateElement {
6374
let parser = this.global.document.createElement('div');
6475
parser.innerHTML = markup;
6576

src/nodejs-platform.ts

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,16 @@ import { IPerformance } from './performance';
33
import { IGlobal } from './global';
44
import { JSDOM } from 'jsdom';
55

6+
declare module './global' {
7+
interface IGlobal {
8+
performance: any;
9+
location: any;
10+
history: any;
11+
addEventListener(eventName: string, callback: EventListenerOrEventListenerObject, capture?: boolean): void;
12+
removeEventListener(eventName: string, callback: EventListenerOrEventListenerObject, capture?: boolean): void;
13+
}
14+
}
15+
616
export class NodeJsPlatform implements IPlatform {
717

818
constructor(public global: IGlobal, public jsdom: JSDOM) {
@@ -50,17 +60,17 @@ export class NodeJsPlatform implements IPlatform {
5060
* @param callback The function that receives a notification when an event of the specified type occurs.
5161
* @param capture If true, useCapture indicates that the user wishes to initiate capture.
5262
*/
53-
addEventListener(eventName: string, callback: Function, capture?: boolean): void {
54-
this.global.addEventListener(eventName, <any>callback, capture);
63+
addEventListener(eventName: string, callback: EventListenerOrEventListenerObject, capture?: boolean): void {
64+
this.global.addEventListener(eventName, callback, capture);
5565
}
5666
/**
5767
* Remove a global event listener.
5868
* @param eventName A string representing the event type to listen for.
5969
* @param callback The function to remove from the event.
6070
* @param capture Specifies whether the listener to be removed was registered as a capturing listener or not.
6171
*/
62-
removeEventListener(eventName: string, callback: Function, capture?: boolean): void {
63-
this.global.removeEventListener(eventName, <any>callback, capture);
72+
removeEventListener(eventName: string, callback: EventListenerOrEventListenerObject, capture?: boolean): void {
73+
this.global.removeEventListener(eventName, callback, capture);
6474
}
6575

6676
/**

src/platform.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -41,14 +41,14 @@ export interface IPlatform {
4141
* @param callback The function that receives a notification when an event of the specified type occurs.
4242
* @param capture If true, useCapture indicates that the user wishes to initiate capture.
4343
*/
44-
addEventListener(eventName: string, callback: Function, capture?: boolean): void;
44+
addEventListener(eventName: string, callback: EventListenerOrEventListenerObject, capture?: boolean): void;
4545
/**
4646
* Remove a global event listener.
4747
* @param eventName A string representing the event type to listen for.
4848
* @param callback The function to remove from the event.
4949
* @param capture Specifies whether the listener to be removed was registered as a capturing listener or not.
5050
*/
51-
removeEventListener(eventName: string, callback: Function, capture?: boolean): void;
51+
removeEventListener(eventName: string, callback: EventListenerOrEventListenerObject, capture?: boolean): void;
5252
/**
5353
* The runtime's XMLHttpRequest API.
5454
*/

0 commit comments

Comments
 (0)