Skip to content

Commit

Permalink
feat: [#1551] Add support for using whitespace-separated list to getE…
Browse files Browse the repository at this point in the history
…lementsByClassName

* fix: Allow getElementsByClassName to accept a whitespace-separated list

* chore: [#1618] Improves performance for getElemenmtsByClassName space separated list

---------

Co-authored-by: David Ortner <david@ortner.se>
  • Loading branch information
kylehalleman and capricorn86 authored Dec 30, 2024
1 parent 2df1cc4 commit c738c4e
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,10 @@ export default class ParentNodeUtility {
): HTMLCollection<Element> {
return new HTMLCollection(
PropertySymbol.illegalConstructor,
() => QuerySelector.querySelectorAll(parentNode, `.${className}`)[PropertySymbol.items]
() =>
QuerySelector.querySelectorAll(parentNode, `.${className.replace(/\s+/gm, '.')}`)[
PropertySymbol.items
]
);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -153,6 +153,32 @@ describe('ParentNodeUtility', () => {
expect(elementByClassName[1]).toBe(element2);
expect(elementByClassName[2]).toBe(element3);
});

it('Supports space-separated class names.', () => {
const addedClassName = 'className otherClassName verySeparatedClassName';
const queriedClassName = 'className otherClassName verySeparatedClassName';

const parent = document.createElement('div');
const element1 = document.createElement('div');
const element2 = document.createElement('div');
const element3 = document.createElement('div');

element1.className = addedClassName;
element2.className = addedClassName;
element3.className = addedClassName;

parent.appendChild(element1);
element1.appendChild(element2);
element1.appendChild(element3);

const elementByClassName = ParentNodeUtility.getElementsByClassName(parent, queriedClassName);

expect(elementByClassName instanceof HTMLCollection).toBe(true);
expect(elementByClassName.length).toBe(3);
expect(elementByClassName[0]).toBe(element1);
expect(elementByClassName[1]).toBe(element2);
expect(elementByClassName[2]).toBe(element3);
});
});

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

0 comments on commit c738c4e

Please sign in to comment.