Skip to content

Commit

Permalink
feat: Enable name attribute as an independent selector config (#6)
Browse files Browse the repository at this point in the history
BREAKING CHANGE: Default selectorTypes is now ['id', 'name', 'class', 'tag', 'nth-child']
  • Loading branch information
mike-plummer authored Jan 17, 2024
1 parent fade65d commit cdf1116
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 1 deletion.
15 changes: 15 additions & 0 deletions src/getName.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
/**
* Returns the `name` attribute of the element (if one exists)
* @param { Object } element
* @return { String }
*/
export function getName( el )
{
const name = el.getAttribute( 'name' );

if( name !== null && name !== '')
{
return `[name="${name}"]`;
}
return null;
}
5 changes: 4 additions & 1 deletion src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import { getID } from './getID';
import { getClassSelectors } from './getClasses';
import { getCombinations } from './getCombinations';
import { getAttributes } from './getAttributes';
import { getName } from './getName'
import { getNthChild } from './getNthChild';
import { getTag } from './getTag';
import { isUnique } from './isUnique';
Expand All @@ -28,6 +29,7 @@ function getAllSelectors( el, selectors, attributesToIgnore )
'attributes' : elem => getAttributes( elem, attributesToIgnore ),
'class' : getClassSelectors,
'id' : getID,
'name' : getName,
};

return selectors
Expand Down Expand Up @@ -135,6 +137,7 @@ function getUniqueSelector( element, selectorTypes, attributesToIgnore )
{
case 'data' :
case 'id' :
case 'name':
case 'tag':
if ( testUniqueness( element, selector ) )
{
Expand Down Expand Up @@ -173,7 +176,7 @@ function getUniqueSelector( element, selectorTypes, attributesToIgnore )

export default function unique( el, options={} ) {
const {
selectorTypes=['id', 'class', 'tag', 'nth-child'],
selectorTypes=['id', 'name', 'class', 'tag', 'nth-child'],
attributesToIgnore= ['id', 'class', 'length'],
selectorCache,
isUniqueCache
Expand Down
21 changes: 21 additions & 0 deletions test/unique-selector.js
Original file line number Diff line number Diff line change
Expand Up @@ -143,4 +143,25 @@ describe( 'Unique Selector Tests', () =>
expect( uniqueSelector ).to.equal( '[data-foo]' );
} );

describe('name', () => {
beforeEach(() => {
$( 'body' ).get( 0 ).innerHTML = ''; // Clear previous appends
})

it( 'with value', () =>
{
$( 'body' ).append( '<div name="so" class="test3"></div>' );
const findNode = $( 'body' ).find( '.test3' ).get( 0 );
const uniqueSelector = unique( findNode );
expect( uniqueSelector ).to.equal( '[name="so"]' );
} );

it( 'without value', () =>
{
$( 'body' ).append( '<div name class="test3"></div>' );
const findNode = $( 'body' ).find( '.test3' ).get( 0 );
const uniqueSelector = unique( findNode );
expect( uniqueSelector ).to.equal( '.test3' );
} );
})
} );

0 comments on commit cdf1116

Please sign in to comment.