-
-
Notifications
You must be signed in to change notification settings - Fork 211
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
#1049@patch: Allows global properties to be overwritten when using Gl…
…obalRegistrator from @happy-dom/global-registrator, as some global properties may behave different from the Happy DOM implementation. The properties will be restored when unregistering.
- Loading branch information
1 parent
a81baba
commit 497db1d
Showing
3 changed files
with
46 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,57 @@ | ||
import { GlobalWindow } from 'happy-dom'; | ||
|
||
const IGNORE_LIST = ['undefined', 'NaN', 'global', 'globalThis', 'window', 'globalThis']; | ||
const SELF_REFERING = ['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] = window[key]; | ||
this.registered.push(key); | ||
} | ||
} | ||
|
||
for (const key of SELF_REFERING) { | ||
this.registered[key] = undefined; | ||
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] !== undefined) { | ||
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