Skip to content

Commit

Permalink
fix: [1706] Handle non string attribute removal gracefully (#1711)
Browse files Browse the repository at this point in the history
Co-authored-by: David Ortner <david@ortner.se>
  • Loading branch information
OlaviSau and capricorn86 authored Jan 31, 2025
1 parent df84257 commit f2d46b6
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 0 deletions.
1 change: 1 addition & 0 deletions packages/happy-dom/src/nodes/element/NamedNodeMap.ts
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,7 @@ export default class NamedNodeMap {
* @returns Item.
*/
public getNamedItem(name: string): Attr | null {
name = String(name);
if (
this[PropertySymbol.ownerElement][PropertySymbol.namespaceURI] === NamespaceURI.html &&
this[PropertySymbol.ownerElement][PropertySymbol.ownerDocument][
Expand Down
6 changes: 6 additions & 0 deletions packages/happy-dom/test/nodes/element/Element.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1646,6 +1646,12 @@ describe('Element', () => {
element.removeAttribute('key1');
expect(element.attributes.length).toBe(0);
});

it('Should stringify a non string attribute and remove it', () => {
element.setAttribute('undefined', 'value1');
element.removeAttribute(undefined);
expect(element.attributes.length).toBe(0);
});
});

describe('removeAttributeNS()', () => {
Expand Down
5 changes: 5 additions & 0 deletions packages/happy-dom/test/nodes/element/NamedNodeMap.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,11 @@ describe('NamedNodeMap', () => {
expect(element.getAttribute('key')).toBe('value1');
expect(element.getAttributeNS('namespace', 'key')).toBe('value2');
});

it('Handles non string keys as strings', () => {
element.setAttribute('undefined', 'value1');
expect(element.getAttribute(undefined)).toBe('value1');
});
});

describe('setNamedItemNS()', () => {
Expand Down

0 comments on commit f2d46b6

Please # to comment.