Commit 7bef736b authored by David Sveningsson's avatar David Sveningsson
Browse files

feat(dom): add iterator to `DOMTokenList`

parent 2c8efca3
......@@ -142,4 +142,29 @@ describe("DOMTokenList", () => {
expect(list.contains("spam")).toBeFalsy();
});
});
describe("iterator()", () => {
it("should loop over all items and locations", () => {
expect.assertions(1);
const list = new DOMTokenList("foo bar baz", location);
const result = Array.from(list.iterator());
expect(result).toEqual([
{
index: 0,
item: "foo",
location: expect.objectContaining({ line: 1, column: 1, size: 3 }),
},
{
index: 1,
item: "bar",
location: expect.objectContaining({ line: 1, column: 5, size: 3 }),
},
{
index: 2,
item: "baz",
location: expect.objectContaining({ line: 1, column: 9, size: 3 }),
},
]);
});
});
});
......@@ -79,4 +79,14 @@ export class DOMTokenList extends Array<string> {
public contains(token: string): boolean {
return this.includes(token);
}
public *iterator(): Generator<{ index: number; item: string; location: Location }> {
for (let index = 0; index < this.length; index++) {
/* eslint-disable @typescript-eslint/no-non-null-assertion */
const item = this.item(index)!;
const location = this.location(index)!;
/* eslint-enable @typescript-eslint/no-non-null-assertion */
yield { index, item, location };
}
}
}
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment