-
-
Notifications
You must be signed in to change notification settings - Fork 184
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #1080 from capricorn86/task/1049-windowdispatcheve…
…ntnew-eventtest-throws-error Task/1049 windowdispatcheventnew eventtest throws error
- Loading branch information
Showing
3 changed files
with
47 additions
and
9 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,39 +1,58 @@ | ||
import { GlobalWindow } from 'happy-dom'; | ||
|
||
const IGNORE_LIST = ['undefined', 'NaN', 'global', 'globalThis', 'window', 'globalThis']; | ||
const SELF_REFERRING = ['self', 'top', 'parent', 'window']; | ||
|
||
/** | ||
* | ||
*/ | ||
export default class GlobalRegistrator { | ||
private static registered = []; | ||
private static registered: { [key: string]: string } | null = null; | ||
|
||
/** | ||
* Registers Happy DOM globally. | ||
*/ | ||
public static register(): void { | ||
if (this.registered.length) { | ||
if (this.registered !== null) { | ||
throw new Error('Failed to register. Happy DOM has already been globally registered.'); | ||
} | ||
|
||
const window = new GlobalWindow(); | ||
|
||
this.registered = {}; | ||
|
||
for (const key of Object.keys(window)) { | ||
if (global[key] === undefined && key !== 'undefined') { | ||
if (global[key] !== window[key] && !IGNORE_LIST.includes(key)) { | ||
this.registered[key] = | ||
global[key] !== window[key] && global[key] !== undefined ? global[key] : null; | ||
global[key] = window[key]; | ||
this.registered.push(key); | ||
} | ||
} | ||
|
||
for (const key of SELF_REFERRING) { | ||
this.registered[key] = null; | ||
global[key] = global; | ||
} | ||
} | ||
|
||
/** | ||
* Registers Happy DOM globally. | ||
*/ | ||
public static unregister(): void { | ||
if (!this.registered.length) { | ||
if (this.registered === null) { | ||
throw new Error( | ||
'Failed to unregister. Happy DOM has not previously been globally registered.' | ||
); | ||
} | ||
while (this.registered.length) { | ||
const key = this.registered.pop(); | ||
delete global[key]; | ||
|
||
for (const key of Object.keys(this.registered)) { | ||
if (this.registered[key] !== null) { | ||
global[key] = this.registered[key]; | ||
} else { | ||
delete global[key]; | ||
} | ||
} | ||
|
||
this.registered = null; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters