Skip to content

Commit

Permalink
fix(Window.setGlobal): window aliases should refer to globalThis (#470)
Browse files Browse the repository at this point in the history
  • Loading branch information
developit authored Oct 29, 2024
1 parent 1473a3c commit 08839d3
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 28 deletions.
5 changes: 5 additions & 0 deletions .changeset/wet-students-enjoy.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@remote-dom/polyfill': patch
---

window aliases should refer to globalThis
12 changes: 9 additions & 3 deletions packages/polyfill/source/EventTarget.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,19 @@ import type {Document} from './Document.ts';
const ONCE_LISTENERS = Symbol('onceListeners');

export class EventTarget {
[LISTENERS]?: Map<string, Set<EventListenerOrEventListenerObject>>;
[ONCE_LISTENERS]?: WeakMap<EventListenerOrEventListenerObject, EventListener>;
[LISTENERS]:
| Map<string, Set<EventListenerOrEventListenerObject>>
| undefined = undefined;

[ONCE_LISTENERS]:
| WeakMap<EventListenerOrEventListenerObject, EventListener>
| undefined = undefined;

/**
* Property set by entities that extend this class that are part of the DOM tree.
* @internal
*/
[OWNER_DOCUMENT]?: Document;
[OWNER_DOCUMENT]: Document | undefined = undefined;

addEventListener(
type: string,
Expand Down
37 changes: 12 additions & 25 deletions packages/polyfill/source/Window.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import type {Hooks} from './hooks.ts';
export class Window extends EventTarget {
[HOOKS]: Partial<Hooks> = {};
name = '';
window = this;
parent = this;
self = this;
top = this;
Expand All @@ -46,32 +47,18 @@ export class Window extends EventTarget {
MutationObserver = MutationObserver;

static setGlobal(window: Window) {
const properties = Object.getOwnPropertyDescriptors(window);

delete (properties as any).self;
for (const property in window) {
if ((window as any)[property] === window) {
(window as any)[property] = globalThis;
}
}

Object.defineProperties(globalThis, {
...properties,
window: {
value: window,
configurable: true,
writable: true,
enumerable: true,
},
});
const eventTargetPrototypeProperties = Object.getOwnPropertyDescriptors(
EventTarget.prototype,
);
const properties = Object.getOwnPropertyDescriptors(window);

if (typeof self === 'undefined') {
Object.defineProperty(globalThis, 'self', {
value: window,
configurable: true,
writable: true,
enumerable: true,
});
} else {
// There can already be a `self`, like when polyfilling the DOM
// in a Web Worker. In those cases, just mirror all the `Window`
// properties onto `self`, rather than wholly redefining it.
Object.defineProperties(self, properties);
}
Object.defineProperties(globalThis, eventTargetPrototypeProperties);
Object.defineProperties(globalThis, properties);
}
}

0 comments on commit 08839d3

Please sign in to comment.