Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(Window.setGlobal): window aliases should refer to globalThis #470

Merged
merged 4 commits into from
Oct 29, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Needed to set these to undefined so undefined is copied over in setGlobal

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

These could just be set to maps, but I assumed that was being avoided for performance reasons?


[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);
}
}
Loading