Skip to content

Commit

Permalink
fix: [#1336] Changes location, history, navigator, console, screen, s…
Browse files Browse the repository at this point in the history
…essionStorage and localStorage to be getters instead of properties in Window
  • Loading branch information
capricorn86 committed Mar 20, 2024
1 parent 8dbebfd commit 50ba986
Show file tree
Hide file tree
Showing 7 changed files with 155 additions and 100 deletions.
7 changes: 7 additions & 0 deletions packages/happy-dom/src/PropertySymbol.ts
Original file line number Diff line number Diff line change
Expand Up @@ -152,3 +152,10 @@ export const setURL = Symbol('setURL');
export const localName = Symbol('localName');
export const registedClass = Symbol('registedClass');
export const nodeStream = Symbol('nodeStream');
export const location = Symbol('location');
export const history = Symbol('history');
export const navigator = Symbol('navigator');
export const console = Symbol('console');
export const screen = Symbol('screen');
export const sessionStorage = Symbol('sessionStorage');
export const localStorage = Symbol('localStorage');
86 changes: 72 additions & 14 deletions packages/happy-dom/src/window/BrowserWindow.ts
Original file line number Diff line number Diff line change
Expand Up @@ -408,18 +408,11 @@ export default class BrowserWindow extends EventTarget implements INodeJSGlobal
// Public properties.
public readonly document: DocumentImplementation;
public readonly customElements: CustomElementRegistry;
public readonly location: Location;
public readonly history: History;
public readonly navigator: Navigator;
public readonly console: Console;
public readonly self: BrowserWindow = this;
public readonly top: BrowserWindow = this;
public readonly parent: BrowserWindow = this;
public readonly window: BrowserWindow = this;
public readonly globalThis: BrowserWindow = this;
public readonly screen: Screen;
public readonly sessionStorage: Storage;
public readonly localStorage: Storage;
public readonly performance: typeof performance = performance;
public readonly screenLeft: number = 0;
public readonly screenTop: number = 0;
Expand Down Expand Up @@ -497,6 +490,13 @@ export default class BrowserWindow extends EventTarget implements INodeJSGlobal
public [PropertySymbol.captureEventListenerCount]: { [eventType: string]: number } = {};
public readonly [PropertySymbol.mutationObservers]: MutationObserver[] = [];
public readonly [PropertySymbol.readyStateManager] = new DocumentReadyStateManager(this);
public [PropertySymbol.location]: Location;
public [PropertySymbol.history]: History;
public [PropertySymbol.navigator]: Navigator;
public [PropertySymbol.console]: Console;
public [PropertySymbol.screen]: Screen;
public [PropertySymbol.sessionStorage]: Storage;
public [PropertySymbol.localStorage]: Storage;

// Private properties
#browserFrame: IBrowserFrame;
Expand All @@ -519,13 +519,13 @@ export default class BrowserWindow extends EventTarget implements INodeJSGlobal
this.#browserFrame = browserFrame;

this.customElements = new CustomElementRegistry(this);
this.navigator = new Navigator(this);
this.history = new History();
this.screen = new Screen();
this.sessionStorage = new Storage();
this.localStorage = new Storage();
this.location = new Location(this.#browserFrame, options?.url ?? 'about:blank');
this.console = browserFrame.page.console;
this[PropertySymbol.navigator] = new Navigator(this);
this[PropertySymbol.history] = new History();
this[PropertySymbol.screen] = new Screen();
this[PropertySymbol.sessionStorage] = new Storage();
this[PropertySymbol.localStorage] = new Storage();
this[PropertySymbol.location] = new Location(this.#browserFrame, options?.url ?? 'about:blank');
this[PropertySymbol.console] = browserFrame.page.console;

WindowBrowserSettingsReader.setSettings(this, this.#browserFrame.page.context.browser.settings);

Expand Down Expand Up @@ -673,6 +673,64 @@ export default class BrowserWindow extends EventTarget implements INodeJSGlobal
});
}

/**
* Returns location.
*/
public get location(): Location {
return this[PropertySymbol.location];
}

/**
* Returns location.
*
* @param href Href.
*/
public set location(href: string) {
this[PropertySymbol.location].href = href;
}

/**
* Returns history.
*/
public get history(): History {
return this[PropertySymbol.history];
}

/**
* Returns navigator.
*/
public get navigator(): Navigator {
return this[PropertySymbol.navigator];
}

/**
* Returns console.
*/
public get console(): Console {
return this[PropertySymbol.console];
}

/**
* Returns screen.
*/
public get screen(): Screen {
return this[PropertySymbol.screen];
}

/**
* Returns session storage.
*/
public get sessionStorage(): Storage {
return this[PropertySymbol.sessionStorage];
}

/**
* Returns local storage.
*/
public get localStorage(): Storage {
return this[PropertySymbol.localStorage];
}

/**
* Returns opener.
*
Expand Down
37 changes: 17 additions & 20 deletions packages/happy-dom/test/fetch/Fetch.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,13 @@ import * as PropertySymbol from '../../src/PropertySymbol.js';

const LAST_CHUNK = Buffer.from('0\r\n\r\n');

const PLATFORM =
'X11; ' +
process.platform.charAt(0).toUpperCase() +
process.platform.slice(1) +
' ' +
process.arch;

describe('Fetch', () => {
afterEach(() => {
resetMockedModules();
Expand Down Expand Up @@ -3701,8 +3708,7 @@ describe('Fetch', () => {
'Accept-Encoding': 'gzip, deflate, br',
Connection: 'close',
Referer: 'https://localhost:8080/',
'User-Agent':
'Mozilla/5.0 (X11; Linux x64) AppleWebKit/537.36 (KHTML, like Gecko) HappyDOM/0.0.0',
'User-Agent': `Mozilla/5.0 (${PLATFORM}) AppleWebKit/537.36 (KHTML, like Gecko) HappyDOM/0.0.0`,
key1: 'value1'
},
method: 'GET',
Expand All @@ -3721,8 +3727,7 @@ describe('Fetch', () => {
Connection: 'close',
Referer: 'https://localhost:8080/',
'If-Modified-Since': 'Mon, 11 Dec 2023 01:00:00 GMT',
'User-Agent':
'Mozilla/5.0 (X11; Linux x64) AppleWebKit/537.36 (KHTML, like Gecko) HappyDOM/0.0.0',
'User-Agent': `Mozilla/5.0 (${PLATFORM}) AppleWebKit/537.36 (KHTML, like Gecko) HappyDOM/0.0.0`,
key1: 'value1'
},
method: 'GET',
Expand Down Expand Up @@ -3878,8 +3883,7 @@ describe('Fetch', () => {
'Accept-Encoding': 'gzip, deflate, br',
Connection: 'close',
Referer: 'https://localhost:8080/',
'User-Agent':
'Mozilla/5.0 (X11; Linux x64) AppleWebKit/537.36 (KHTML, like Gecko) HappyDOM/0.0.0',
'User-Agent': `Mozilla/5.0 (${PLATFORM}) AppleWebKit/537.36 (KHTML, like Gecko) HappyDOM/0.0.0`,
key1: 'value1'
},
method: 'GET',
Expand All @@ -3898,8 +3902,7 @@ describe('Fetch', () => {
Connection: 'close',
Referer: 'https://localhost:8080/',
'If-Modified-Since': 'Mon, 11 Dec 2023 01:00:00 GMT',
'User-Agent':
'Mozilla/5.0 (X11; Linux x64) AppleWebKit/537.36 (KHTML, like Gecko) HappyDOM/0.0.0',
'User-Agent': `Mozilla/5.0 (${PLATFORM}) AppleWebKit/537.36 (KHTML, like Gecko) HappyDOM/0.0.0`,
key1: 'value1'
},
method: 'GET',
Expand Down Expand Up @@ -4039,8 +4042,7 @@ describe('Fetch', () => {
'Accept-Encoding': 'gzip, deflate, br',
Connection: 'close',
Referer: 'https://localhost:8080/',
'User-Agent':
'Mozilla/5.0 (X11; Linux x64) AppleWebKit/537.36 (KHTML, like Gecko) HappyDOM/0.0.0',
'User-Agent': `Mozilla/5.0 (${PLATFORM}) AppleWebKit/537.36 (KHTML, like Gecko) HappyDOM/0.0.0`,
key1: 'value1'
},
method: 'HEAD',
Expand All @@ -4059,8 +4061,7 @@ describe('Fetch', () => {
Connection: 'close',
Referer: 'https://localhost:8080/',
'If-None-Match': etag1,
'User-Agent':
'Mozilla/5.0 (X11; Linux x64) AppleWebKit/537.36 (KHTML, like Gecko) HappyDOM/0.0.0',
'User-Agent': `Mozilla/5.0 (${PLATFORM}) AppleWebKit/537.36 (KHTML, like Gecko) HappyDOM/0.0.0`,
key1: 'value1'
},
method: 'HEAD',
Expand Down Expand Up @@ -4208,8 +4209,7 @@ describe('Fetch', () => {
'Accept-Encoding': 'gzip, deflate, br',
Connection: 'close',
Referer: 'https://localhost:8080/',
'User-Agent':
'Mozilla/5.0 (X11; Linux x64) AppleWebKit/537.36 (KHTML, like Gecko) HappyDOM/0.0.0',
'User-Agent': `Mozilla/5.0 (${PLATFORM}) AppleWebKit/537.36 (KHTML, like Gecko) HappyDOM/0.0.0`,
key1: 'value1'
},
method: 'GET',
Expand All @@ -4228,8 +4228,7 @@ describe('Fetch', () => {
Connection: 'close',
Referer: 'https://localhost:8080/',
'If-None-Match': etag1,
'User-Agent':
'Mozilla/5.0 (X11; Linux x64) AppleWebKit/537.36 (KHTML, like Gecko) HappyDOM/0.0.0',
'User-Agent': `Mozilla/5.0 (${PLATFORM}) AppleWebKit/537.36 (KHTML, like Gecko) HappyDOM/0.0.0`,
key1: 'value1'
},
method: 'GET',
Expand Down Expand Up @@ -4417,8 +4416,7 @@ describe('Fetch', () => {
'Accept-Encoding': 'gzip, deflate, br',
Connection: 'close',
Referer: 'https://localhost:8080/',
'User-Agent':
'Mozilla/5.0 (X11; Linux x64) AppleWebKit/537.36 (KHTML, like Gecko) HappyDOM/0.0.0',
'User-Agent': `Mozilla/5.0 (${PLATFORM}) AppleWebKit/537.36 (KHTML, like Gecko) HappyDOM/0.0.0`,
'vary-header': 'vary1'
},
method: 'GET',
Expand All @@ -4436,8 +4434,7 @@ describe('Fetch', () => {
'Accept-Encoding': 'gzip, deflate, br',
Connection: 'close',
Referer: 'https://localhost:8080/',
'User-Agent':
'Mozilla/5.0 (X11; Linux x64) AppleWebKit/537.36 (KHTML, like Gecko) HappyDOM/0.0.0',
'User-Agent': `Mozilla/5.0 (${PLATFORM}) AppleWebKit/537.36 (KHTML, like Gecko) HappyDOM/0.0.0`,
'vary-header': 'vary2'
},
method: 'GET',
Expand Down
37 changes: 17 additions & 20 deletions packages/happy-dom/test/fetch/SyncFetch.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,13 @@ import IBrowserFrame from '../../src/browser/types/IBrowserFrame.js';
import Browser from '../../src/browser/Browser.js';
import '../types.d.js';

const PLATFORM =
'X11; ' +
process.platform.charAt(0).toUpperCase() +
process.platform.slice(1) +
' ' +
process.arch;

describe('SyncFetch', () => {
let browserFrame: IBrowserFrame;
let window: BrowserWindow;
Expand Down Expand Up @@ -2306,8 +2313,7 @@ describe('SyncFetch', () => {
'Accept-Encoding': 'gzip, deflate, br',
Connection: 'close',
Referer: 'https://localhost:8080/',
'User-Agent':
'Mozilla/5.0 (X11; Linux x64) AppleWebKit/537.36 (KHTML, like Gecko) HappyDOM/0.0.0',
'User-Agent': `Mozilla/5.0 (${PLATFORM}) AppleWebKit/537.36 (KHTML, like Gecko) HappyDOM/0.0.0`,
key1: 'value1'
},
body: null
Expand All @@ -2321,8 +2327,7 @@ describe('SyncFetch', () => {
Connection: 'close',
Referer: 'https://localhost:8080/',
'If-Modified-Since': 'Mon, 11 Dec 2023 01:00:00 GMT',
'User-Agent':
'Mozilla/5.0 (X11; Linux x64) AppleWebKit/537.36 (KHTML, like Gecko) HappyDOM/0.0.0',
'User-Agent': `Mozilla/5.0 (${PLATFORM}) AppleWebKit/537.36 (KHTML, like Gecko) HappyDOM/0.0.0`,
key1: 'value1'
},
body: null
Expand Down Expand Up @@ -2461,8 +2466,7 @@ describe('SyncFetch', () => {
'Accept-Encoding': 'gzip, deflate, br',
Connection: 'close',
Referer: 'https://localhost:8080/',
'User-Agent':
'Mozilla/5.0 (X11; Linux x64) AppleWebKit/537.36 (KHTML, like Gecko) HappyDOM/0.0.0',
'User-Agent': `Mozilla/5.0 (${PLATFORM}) AppleWebKit/537.36 (KHTML, like Gecko) HappyDOM/0.0.0`,
key1: 'value1'
},
body: null
Expand All @@ -2476,8 +2480,7 @@ describe('SyncFetch', () => {
Connection: 'close',
Referer: 'https://localhost:8080/',
'If-Modified-Since': 'Mon, 11 Dec 2023 01:00:00 GMT',
'User-Agent':
'Mozilla/5.0 (X11; Linux x64) AppleWebKit/537.36 (KHTML, like Gecko) HappyDOM/0.0.0',
'User-Agent': `Mozilla/5.0 (${PLATFORM}) AppleWebKit/537.36 (KHTML, like Gecko) HappyDOM/0.0.0`,
key1: 'value1'
},
body: null
Expand Down Expand Up @@ -2604,8 +2607,7 @@ describe('SyncFetch', () => {
'Accept-Encoding': 'gzip, deflate, br',
Connection: 'close',
Referer: 'https://localhost:8080/',
'User-Agent':
'Mozilla/5.0 (X11; Linux x64) AppleWebKit/537.36 (KHTML, like Gecko) HappyDOM/0.0.0',
'User-Agent': `Mozilla/5.0 (${PLATFORM}) AppleWebKit/537.36 (KHTML, like Gecko) HappyDOM/0.0.0`,
key1: 'value1'
},
body: null
Expand All @@ -2619,8 +2621,7 @@ describe('SyncFetch', () => {
Connection: 'close',
Referer: 'https://localhost:8080/',
'If-None-Match': etag1,
'User-Agent':
'Mozilla/5.0 (X11; Linux x64) AppleWebKit/537.36 (KHTML, like Gecko) HappyDOM/0.0.0',
'User-Agent': `Mozilla/5.0 (${PLATFORM}) AppleWebKit/537.36 (KHTML, like Gecko) HappyDOM/0.0.0`,
key1: 'value1'
},
body: null
Expand Down Expand Up @@ -2751,8 +2752,7 @@ describe('SyncFetch', () => {
'Accept-Encoding': 'gzip, deflate, br',
Connection: 'close',
Referer: 'https://localhost:8080/',
'User-Agent':
'Mozilla/5.0 (X11; Linux x64) AppleWebKit/537.36 (KHTML, like Gecko) HappyDOM/0.0.0',
'User-Agent': `Mozilla/5.0 (${PLATFORM}) AppleWebKit/537.36 (KHTML, like Gecko) HappyDOM/0.0.0`,
key1: 'value1'
},
body: null
Expand All @@ -2766,8 +2766,7 @@ describe('SyncFetch', () => {
Connection: 'close',
Referer: 'https://localhost:8080/',
'If-None-Match': etag1,
'User-Agent':
'Mozilla/5.0 (X11; Linux x64) AppleWebKit/537.36 (KHTML, like Gecko) HappyDOM/0.0.0',
'User-Agent': `Mozilla/5.0 (${PLATFORM}) AppleWebKit/537.36 (KHTML, like Gecko) HappyDOM/0.0.0`,
key1: 'value1'
},
body: null
Expand Down Expand Up @@ -2953,8 +2952,7 @@ describe('SyncFetch', () => {
'Accept-Encoding': 'gzip, deflate, br',
Connection: 'close',
Referer: 'https://localhost:8080/',
'User-Agent':
'Mozilla/5.0 (X11; Linux x64) AppleWebKit/537.36 (KHTML, like Gecko) HappyDOM/0.0.0',
'User-Agent': `Mozilla/5.0 (${PLATFORM}) AppleWebKit/537.36 (KHTML, like Gecko) HappyDOM/0.0.0`,
'vary-header': 'vary1'
},
body: null
Expand All @@ -2967,8 +2965,7 @@ describe('SyncFetch', () => {
'Accept-Encoding': 'gzip, deflate, br',
Connection: 'close',
Referer: 'https://localhost:8080/',
'User-Agent':
'Mozilla/5.0 (X11; Linux x64) AppleWebKit/537.36 (KHTML, like Gecko) HappyDOM/0.0.0',
'User-Agent': `Mozilla/5.0 (${PLATFORM}) AppleWebKit/537.36 (KHTML, like Gecko) HappyDOM/0.0.0`,
'vary-header': 'vary2'
},
body: null
Expand Down
Loading

0 comments on commit 50ba986

Please sign in to comment.