-
Notifications
You must be signed in to change notification settings - Fork 56
/
index.js
200 lines (176 loc) · 4.75 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
/**
* Expose `unique`
*/
import { getID } from './getID';
import { getClassSelectors } from './getClasses';
import { getCombinations } from './getCombinations';
import { getAttributes } from './getAttributes';
import { getNthChild } from './getNthChild';
import { getTag } from './getTag';
import { isUnique } from './isUnique';
import { getParents } from './getParents';
/**
* Returns all the selectors of the elmenet
* @param { Object } element
* @return { Object }
*/
function getAllSelectors( el, selectors, attributesToIgnore )
{
const funcs =
{
'Tag' : getTag,
'NthChild' : getNthChild,
'Attributes' : elem => getAttributes( elem, attributesToIgnore ),
'Class' : getClassSelectors,
'ID' : getID,
};
return selectors.reduce( ( res, next ) =>
{
res[ next ] = funcs[ next ]( el );
return res;
}, {} );
}
/**
* Tests uniqueNess of the element inside its parent
* @param { Object } element
* @param { String } Selectors
* @return { Boolean }
*/
function testUniqueness( element, selector )
{
const { parentNode } = element;
const elements = parentNode.querySelectorAll( selector );
return elements.length === 1 && elements[ 0 ] === element;
}
/**
* Tests all selectors for uniqueness and returns the first unique selector.
* @param { Object } element
* @param { Array } selectors
* @return { String }
*/
function getFirstUnique( element, selectors )
{
return selectors.find( testUniqueness.bind( null, element ) );
}
/**
* Checks all the possible selectors of an element to find one unique and return it
* @param { Object } element
* @param { Array } items
* @param { String } tag
* @return { String }
*/
function getUniqueCombination( element, items, tag )
{
let combinations = getCombinations( items, 3 ),
firstUnique = getFirstUnique( element, combinations );
if( Boolean( firstUnique ) )
{
return firstUnique;
}
if( Boolean( tag ) )
{
combinations = combinations.map( combination => tag + combination );
firstUnique = getFirstUnique( element, combinations );
if( Boolean( firstUnique ) )
{
return firstUnique;
}
}
return null;
}
/**
* Returns a uniqueSelector based on the passed options
* @param { DOM } element
* @param { Array } options
* @return { String }
*/
function getUniqueSelector( element, selectorTypes, attributesToIgnore, excludeRegex )
{
let foundSelector;
const elementSelectors = getAllSelectors( element, selectorTypes, attributesToIgnore );
if( excludeRegex && excludeRegex instanceof RegExp )
{
elementSelectors.ID = excludeRegex.test( elementSelectors.ID ) ? null : elementSelectors.ID;
elementSelectors.Class = elementSelectors.Class.filter( className => !excludeRegex.test( className ) );
}
for( let selectorType of selectorTypes )
{
const { ID, Tag, Class : Classes, Attributes, NthChild } = elementSelectors;
switch ( selectorType )
{
case 'ID' :
if ( Boolean( ID ) && testUniqueness( element, ID ) )
{
return ID;
}
break;
case 'Tag':
if ( Boolean( Tag ) && testUniqueness( element, Tag ) )
{
return Tag;
}
break;
case 'Class':
if ( Boolean( Classes ) && Classes.length )
{
foundSelector = getUniqueCombination( element, Classes, Tag );
if (foundSelector) {
return foundSelector;
}
}
break;
case 'Attributes':
if ( Boolean( Attributes ) && Attributes.length )
{
foundSelector = getUniqueCombination( element, Attributes, Tag );
if ( foundSelector )
{
return foundSelector;
}
}
break;
case 'NthChild':
if ( Boolean( NthChild ) )
{
return NthChild
}
}
}
return '*';
}
/**
* Generate unique CSS selector for given DOM element
*
* @param {Element} el
* @return {String}
* @api private
*/
export default function unique( el, options={} )
{
const {
selectorTypes = ['ID', 'Class', 'Tag', 'NthChild'],
attributesToIgnore = ['id', 'class', 'length'],
excludeRegex = null,
} = options;
const allSelectors = [];
const parents = getParents( el );
for( let elem of parents )
{
const selector = getUniqueSelector( elem, selectorTypes, attributesToIgnore, excludeRegex );
if( Boolean( selector ) )
{
allSelectors.push( selector );
}
}
const selectors = [];
for( let it of allSelectors )
{
selectors.unshift( it );
const selector = selectors.join( ' > ' );
if( isUnique( el, selector ) )
{
return selector;
}
}
return null;
}