Skip to content

Commit

Permalink
fix: Standardize filter params for class
Browse files Browse the repository at this point in the history
  • Loading branch information
mike-plummer committed Aug 2, 2024
1 parent 5b5afe4 commit 134920a
Show file tree
Hide file tree
Showing 3 changed files with 5 additions and 3 deletions.
2 changes: 1 addition & 1 deletion src/getClasses.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ export function getClasses( el, filter )

try {
return Array.prototype.slice.call( el.classList )
.filter((cls) => !filter || filter('attribute', 'class', cls));
.filter((cls) => !filter || filter('class', 'class', cls));
} catch (e) {
let className = el.getAttribute( 'class' );

Expand Down
2 changes: 1 addition & 1 deletion src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ const attrRegex = /^attribute:(.+)/m;
/**
* @typedef Filter
* @type {Function}
* @param {string} type - the trait being considered ('attribute', 'tag', 'nth-child').
* @param {string} type - the trait being considered ('attribute', 'tag', 'nth-child'). As a special case, the `class` attribute is split on whitespace and each token passed individually with a `class` type.
* @param {string} key - your trait key (for 'attribute' will be the attribute name, for others will typically be the same as 'type').
* @param {string} value - the trait value.
* @returns {boolean} whether this trait can be used when building the selector (true = allow). Defaults to 'true' if no value returned.
Expand Down
4 changes: 3 additions & 1 deletion test/unique-selector.js
Original file line number Diff line number Diff line change
Expand Up @@ -86,9 +86,11 @@ describe( 'Unique Selector Tests', () =>

it('Classes filters appropriately', () => {
const filter = (type, key, value) => {
if (type === 'attribute' && key === 'class') {
if (key === 'class') {
expect(type).to.eq('class')
return value.startsWith('a')
}
expect(type).not.to.eq('class')
return true
}
let el = $.parseHTML( '<div class="a1"></div>' )[0];
Expand Down

0 comments on commit 134920a

Please sign in to comment.