Skip to content

Commit 75d4cb8

Browse files
authored
Merge pull request #752 from btea/task/724-add-toggleAttribute
#724@minor: Add toggleAttribute.
2 parents 96dd76a + 441cc80 commit 75d4cb8

3 files changed

Lines changed: 47 additions & 0 deletions

File tree

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

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -591,6 +591,30 @@ export default class Element extends Node implements IElement {
591591
return null;
592592
}
593593

594+
/**
595+
* Toggle an attribute.
596+
* Returns `true` if attribute name is eventually present, and `false` otherwise.
597+
*
598+
* @param name A DOMString specifying the name of the attribute to be toggled.
599+
* @param force A boolean value to determine whether the attribute should be added or removed, no matter whether the attribute is present or not at the moment.
600+
*/
601+
public toggleAttribute(name: string, force?: boolean): boolean {
602+
name = name.toLowerCase();
603+
const attribute = this.getAttributeNode(name);
604+
if (attribute) {
605+
if (force === true) {
606+
return true;
607+
}
608+
this.removeAttributeNode(attribute);
609+
return false;
610+
}
611+
if (force === false) {
612+
return false;
613+
}
614+
this.setAttribute(name, '');
615+
return true;
616+
}
617+
594618
/**
595619
* Returns namespace attribute value.
596620
*

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

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -168,6 +168,14 @@ export default interface IElement extends IChildNode, INonDocumentTypeChildNode,
168168
*/
169169
removeAttributeNS(namespace: string, localName: string): void;
170170

171+
/**
172+
* Toggle an attribute.
173+
*
174+
* @param name A DOMString specifying the name of the attribute to be toggled.
175+
* @param force A boolean value to determine whether the attribute should be added or removed, no matter whether the attribute is present or not at the moment.
176+
*/
177+
toggleAttribute(name: string, force?: boolean): boolean;
178+
171179
/**
172180
* Attaches a shadow root.
173181
*

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

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1232,6 +1232,21 @@ describe('Element', () => {
12321232
});
12331233
});
12341234

1235+
describe('toggleAttribute()', () => {
1236+
it('Toggles an attribute.', () => {
1237+
element.toggleAttribute('key1');
1238+
expect(element.hasAttribute('key1')).toBe(true);
1239+
element.toggleAttribute('key1');
1240+
expect(element.hasAttribute('key1')).toBe(false);
1241+
element.toggleAttribute('key1', true);
1242+
expect(element.hasAttribute('key1')).toBe(true);
1243+
element.toggleAttribute('key1', true);
1244+
expect(element.hasAttribute('key1')).toBe(true);
1245+
element.toggleAttribute('key1', false);
1246+
expect(element.hasAttribute('key1')).toBe(false);
1247+
});
1248+
});
1249+
12351250
describe('attachShadow()', () => {
12361251
it('Creates a new open ShadowRoot node and sets it to the "shadowRoot" property.', () => {
12371252
element.attachShadow({ mode: 'open' });

0 commit comments

Comments
 (0)