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

feat: Enable name attribute as an independent selector config #6

Merged
merged 2 commits into from
Jan 17, 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
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' );
} );
})
} );