@@ -48,6 +48,17 @@ function testUniqueness( element, selector )
48
48
return elements . length === 1 && elements [ 0 ] === element ;
49
49
}
50
50
51
+ /**
52
+ * Tests all selectors for uniqueness and returns the first unique selector.
53
+ * @param { Object } element
54
+ * @param { Array } selectors
55
+ * @return { String }
56
+ */
57
+ function getFirstUnique ( element , selectors )
58
+ {
59
+ return selectors . find ( testUniqueness . bind ( null , element ) ) ;
60
+ }
61
+
51
62
/**
52
63
* Checks all the possible selectors of an element to find one unique and return it
53
64
* @param { Object } element
@@ -57,15 +68,23 @@ function testUniqueness( element, selector )
57
68
*/
58
69
function getUniqueCombination ( element , items , tag )
59
70
{
60
- const combinations = getCombinations ( items , 3 ) ;
61
- const uniqCombinations = combinations . filter ( testUniqueness . bind ( this , element ) ) ;
62
- if ( uniqCombinations . length ) return uniqCombinations [ 0 ] ;
71
+ let combinations = getCombinations ( items , 3 ) ,
72
+ firstUnique = getFirstUnique ( element , combinations ) ;
73
+
74
+ if ( Boolean ( firstUnique ) )
75
+ {
76
+ return firstUnique ;
77
+ }
63
78
64
79
if ( Boolean ( tag ) )
65
80
{
66
- const combinations = items . map ( item => tag + item ) ;
67
- const uniqCombinations = combinations . filter ( testUniqueness . bind ( this , element ) ) ;
68
- if ( uniqCombinations . length ) return uniqCombinations [ 0 ] ;
81
+ combinations = combinations . map ( combination => tag + combination ) ;
82
+ firstUnique = getFirstUnique ( element , combinations ) ;
83
+
84
+ if ( Boolean ( firstUnique ) )
85
+ {
86
+ return firstUnique ;
87
+ }
69
88
}
70
89
71
90
return null ;
0 commit comments