Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: Standardize filter params for class #10

Merged
merged 1 commit into from
Aug 5, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading