Skip to content

Commit

Permalink
fix: Limits the number of class-combinations to avoid freeze (#23)
Browse files Browse the repository at this point in the history
* Limits the number of class-combinations to avoid freeze

* add test

* add jsdoc
  • Loading branch information
vwochnik authored and AvraamMavridis committed Feb 20, 2017
1 parent 5c5442c commit ab9f076
Show file tree
Hide file tree
Showing 3 changed files with 55 additions and 26 deletions.
44 changes: 44 additions & 0 deletions src/getCombinations.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
/**
* Recursively combinate items.
* @param { Array } result
* @param { Array } items
* @param { Array } data
* @param { Number } start
* @param { Number } end
* @param { Number } index
* @param { Number } k
*/
function kCombinations( result, items, data, start, end, index, k )
{
if( index === k )
{
result.push( data.slice( 0, index ).join( '' ) );
return;
}

for( let i = start; i <= end && end - i + 1 >= k - index; ++i )
{
data[index] = items[i];
kCombinations( result, items, data, i + 1, end, index + 1, k );
}
}

/**
* Returns all the possible selector combinations
* @param { Array } items
* @param { Number } k
* @return { Array }
*/
export function getCombinations( items, k )
{
const result = [],
n = items.length,
data = [];

for( var l = 1; l <= k; ++l )
{
kCombinations( result, items, data, 0, n - 1, 0, l );
}

return result;
}
28 changes: 2 additions & 26 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

import { getID } from './getID';
import { getClassSelectors } from './getClasses';
import { getCombinations } from './getCombinations';
import { getAttributes } from './getAttributes';
import { getNthChild } from './getNthChild';
import { getTag } from './getTag';
Expand Down Expand Up @@ -56,7 +57,7 @@ function testUniqueness( element, selector )
*/
function getUniqueCombination( element, items, tag )
{
const combinations = getCombinations( items );
const combinations = getCombinations( items, 3 );
const uniqCombinations = combinations.filter( testUniqueness.bind( this, element ) );
if( uniqCombinations.length ) return uniqCombinations[ 0 ];

Expand Down Expand Up @@ -132,31 +133,6 @@ function getUniqueSelector( element, selectorTypes, attributesToIgnore )
return '*';
}

/**
* Returns all the possible selector combinations
*/
function getCombinations( items )
{
items = items ? items : [];
let result = [[]];
let i, j, k, l, ref, ref1;

for ( i = k = 0, ref = items.length - 1; 0 <= ref ? k <= ref : k >= ref; i = 0 <= ref ? ++k : --k )
{
for ( j = l = 0, ref1 = result.length - 1; 0 <= ref1 ? l <= ref1 : l >= ref1; j = 0 <= ref1 ? ++l : --l )
{
result.push( result[ j ].concat( items[ i ] ) );
}
}

result.shift();
result = result.sort( ( a, b ) => a.length - b.length );
result = result.map( item => item.join( '' ) );

return result;
}


/**
* Generate unique CSS selector for given DOM element
*
Expand Down
9 changes: 9 additions & 0 deletions test/unique-selector.js
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,15 @@ describe( 'Unique Selector Tests', () =>
expect( uniqueSelector ).to.equal( 'body > :nth-child(1)' );
} );

it( 'Classes', () =>
{
$( 'body' ).get( 0 ).innerHTML = ''; //Clear previous appends
$( 'body' ).append( '<div class="test2 ca cb cc cd cx"></div><div class="test2 ca cb cc cd ce"></div><div class="test2 ca cb cc cd ce"></div><div class="test2 ca cb cd ce cf cx"></div>' );
const findNode = $( 'body' ).find( '.test2' ).get( 0 );
const uniqueSelector = unique( findNode );
expect( uniqueSelector ).to.equal( '.cc.cx' );
} );


it( 'Tag', () =>
{
Expand Down

0 comments on commit ab9f076

Please sign in to comment.