From 134920ae854860aa44f727d3a72a7be1caf43c7a Mon Sep 17 00:00:00 2001 From: Mike Plummer Date: Fri, 2 Aug 2024 14:24:04 -0500 Subject: [PATCH] fix: Standardize filter params for `class` --- src/getClasses.js | 2 +- src/index.js | 2 +- test/unique-selector.js | 4 +++- 3 files changed, 5 insertions(+), 3 deletions(-) diff --git a/src/getClasses.js b/src/getClasses.js index b2bdb4d..f4ab76c 100644 --- a/src/getClasses.js +++ b/src/getClasses.js @@ -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' ); diff --git a/src/index.js b/src/index.js index 55d4e72..d2d9bd2 100644 --- a/src/index.js +++ b/src/index.js @@ -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. diff --git a/test/unique-selector.js b/test/unique-selector.js index d9e7332..0b1c5f0 100644 --- a/test/unique-selector.js +++ b/test/unique-selector.js @@ -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( '
' )[0];