Skip to content

Commit

Permalink
fix: [#1342] Make DOMTokenList iterable
Browse files Browse the repository at this point in the history
  • Loading branch information
silverwind committed Mar 25, 2024
1 parent 0ee95b7 commit fb2f1cb
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 0 deletions.
7 changes: 7 additions & 0 deletions packages/happy-dom/src/dom-token-list/DOMTokenList.ts
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,13 @@ export default class DOMTokenList {
return this.#ownerElement.getAttribute(this.#attributeName);
}

/**
* Returns an iterator, allowing you to go through all values of the key/value pairs contained in this object.
*/
public [Symbol.iterator](): IterableIterator<string> {
return this.#getTokenList().values();
}

/**
* Get ClassName.
*
Expand Down
9 changes: 9 additions & 0 deletions packages/happy-dom/test/dom-token-list/DOMTokenList.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -155,10 +155,19 @@ describe('DOMTokenList', () => {
describe('values()', () => {
it('A stringifier property that returns the value of the list as a string.', () => {
element.className = 'class1 class2 class3';
expect(typeof classList.values()[Symbol.iterator]).toEqual('function');
expect(Array.from(classList.values())).toEqual(['class1', 'class2', 'class3']);
});
});

describe('Iterator', () => {
it('A stringifier property that returns the value of the list as a string.', () => {
element.className = 'class1 class2 class3';
expect(typeof classList[Symbol.iterator]).toEqual('function');
expect(Array.from(classList)).toEqual(['class1', 'class2', 'class3']);
});
});

describe('entries()', () => {
it('Returns an iterator, allowing you to go through all key/value pairs contained in this object.', () => {
element.className = 'class1 class2 class3';
Expand Down

0 comments on commit fb2f1cb

Please sign in to comment.