Skip to content

Commit cf74f5f

Browse files
authored
fix: [#1841] Addresses an issue where an error occurred if the Element ID was set to the same name as a Window property with a null value (#1844)
1 parent bfd0fff commit cf74f5f

File tree

2 files changed

+11
-2
lines changed

2 files changed

+11
-2
lines changed

packages/happy-dom/src/nodes/element/Element.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1573,7 +1573,10 @@ export default class Element
15731573
if (!(id in window) || (<any>window)[id] === entry.elements[0]) {
15741574
(<any>window)[id] = entry.htmlCollection;
15751575
}
1576-
} else if (!(id in window) || (<any>window)[id] === entry.htmlCollection) {
1576+
} else if (
1577+
!(id in window) ||
1578+
(entry.htmlCollection !== null && (<any>window)[id] === entry.htmlCollection)
1579+
) {
15771580
(<any>window)[id] = element;
15781581
}
15791582
}

packages/happy-dom/test/nodes/element/Element.test.ts

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -153,11 +153,17 @@ describe('Element', () => {
153153
expect(window['element2']).toBe(undefined);
154154
});
155155

156-
it(`Doesn't the "id" attribute as a property to Window if it collides with Window properties.`, () => {
156+
it(`Doesn't add the "id" attribute as a property to Window if it collides with Window properties.`, () => {
157157
element.setAttribute('id', 'document');
158158
document.body.appendChild(element);
159159
expect(window['document']).toBe(document);
160160
});
161+
162+
it(`Doesn't add the "opener" attribute as a property to Window when the property value is null (#1841).`, () => {
163+
document.body.appendChild(element);
164+
element.id = 'opener';
165+
expect(window['opener']).toBe(null);
166+
});
161167
});
162168

163169
describe('get slot()', () => {

0 commit comments

Comments
 (0)