|
11 | 11 | if (el.id) { |
12 | 12 | elid = el.id; |
13 | 13 | if (elid.includes(',') || elid.includes('.') || |
14 | | - elid.includes('(') || elid.includes(')') || hasNumber(elid[0])) |
| 14 | + elid.includes('(') || elid.includes(')') || hasDigit(elid[0])) |
15 | 15 | return cssPathByAttribute(el, 'id'); |
16 | 16 | selector += '#' + elid; |
17 | 17 | path.unshift(selector); |
|
87 | 87 | return path.join(' > '); |
88 | 88 | }; |
89 | 89 | var ssOccurrences = function(string, subString, allowOverlapping) { |
90 | | - string += ''; |
91 | | - subString += ''; |
92 | 90 | if (subString.length <= 0) |
93 | 91 | return (string.length + 1); |
94 | 92 | var n = 0; |
|
101 | 99 | } |
102 | 100 | return n; |
103 | 101 | }; |
104 | | -function hasNumber(str) { |
| 102 | +function hasDigit(str) { |
105 | 103 | return /\d/.test(str); |
106 | 104 | }; |
| 105 | +function isGen(str) { |
| 106 | + return /[_-]\d/.test(str); |
| 107 | +}; |
107 | 108 | function tagName(el) { |
108 | 109 | return el.tagName.toLowerCase(); |
109 | 110 | }; |
|
121 | 122 | var getBestSelector = function(el) { |
122 | 123 | if (!(el instanceof Element)) return; |
123 | 124 | el = turnIntoParentAsNeeded(el); |
124 | | - child_sep = ' > '; |
125 | | - selector_by_id = cssPathById(el); |
126 | | - if (!selector_by_id.includes(child_sep)) return selector_by_id; |
127 | | - child_count_by_id = ssOccurrences(selector_by_id, child_sep); |
| 125 | + sel_by_id = cssPathById(el); |
| 126 | + if (!sel_by_id.includes(' > ') && !isGen(sel_by_id)) return sel_by_id; |
| 127 | + child_count_by_id = ssOccurrences(sel_by_id, ' > '); |
128 | 128 | selector_by_class = cssPathByClass(el); |
129 | 129 | tag_name = tagName(el); |
130 | 130 | non_id_attributes = []; |
|
145 | 145 | non_id_attributes.push('data-cy'); |
146 | 146 | non_id_attributes.push('data-action'); |
147 | 147 | non_id_attributes.push('data-target'); |
148 | | - non_id_attributes.push('data-content'); |
149 | 148 | non_id_attributes.push('data-tooltip'); |
150 | 149 | non_id_attributes.push('alt'); |
151 | 150 | non_id_attributes.push('title'); |
|
157 | 156 | non_id_attributes.push('ng-href'); |
158 | 157 | non_id_attributes.push('href'); |
159 | 158 | non_id_attributes.push('label'); |
| 159 | + non_id_attributes.push('data-content'); |
160 | 160 | non_id_attributes.push('class'); |
161 | 161 | non_id_attributes.push('for'); |
162 | 162 | non_id_attributes.push('placeholder'); |
|
175 | 175 | else selector_by_attr[i] = cssPathByAttribute(el, n_i_attr); |
176 | 176 | all_by_attr[i] = document.querySelectorAll(selector_by_attr[i]); |
177 | 177 | num_by_attr[i] = all_by_attr[i].length; |
178 | | - if (!selector_by_attr[i].includes(child_sep) && |
| 178 | + if (!selector_by_attr[i].includes(' > ') && |
179 | 179 | ((num_by_attr[i] == 1) || (el == all_by_attr[i][0]))) |
180 | 180 | { |
181 | 181 | if (n_i_attr == 'aria-label' || n_i_attr == 'for') |
182 | | - if (hasNumber(selector_by_attr[i])) |
| 182 | + if (hasDigit(selector_by_attr[i])) |
183 | 183 | continue; |
184 | 184 | return selector_by_attr[i]; |
185 | 185 | } |
186 | | - child_count_by_attr[i] = ssOccurrences(selector_by_attr[i], child_sep); |
| 186 | + child_count_by_attr[i] = ssOccurrences(selector_by_attr[i], ' > '); |
187 | 187 | } |
188 | 188 | basic_tags = []; |
189 | 189 | basic_tags.push('h1'); |
|
201 | 201 | contains_tags = []; |
202 | 202 | contains_tags.push('a'); |
203 | 203 | contains_tags.push('b'); |
204 | | - contains_tags.push('i'); |
205 | 204 | contains_tags.push('h1'); |
206 | 205 | contains_tags.push('h2'); |
207 | 206 | contains_tags.push('h3'); |
208 | 207 | contains_tags.push('h4'); |
209 | 208 | contains_tags.push('h5'); |
210 | | - contains_tags.push('li'); |
211 | | - contains_tags.push('td'); |
212 | | - contains_tags.push('th'); |
213 | 209 | contains_tags.push('code'); |
214 | 210 | contains_tags.push('mark'); |
215 | | - contains_tags.push('label'); |
216 | | - contains_tags.push('small'); |
217 | 211 | contains_tags.push('button'); |
| 212 | + contains_tags.push('label'); |
218 | 213 | contains_tags.push('legend'); |
| 214 | + contains_tags.push('li'); |
| 215 | + contains_tags.push('td'); |
| 216 | + contains_tags.push('th'); |
| 217 | + contains_tags.push('i'); |
| 218 | + contains_tags.push('small'); |
219 | 219 | contains_tags.push('strong'); |
220 | 220 | contains_tags.push('summary'); |
| 221 | + contains_tags.push('span'); |
221 | 222 | all_by_tag = []; |
222 | 223 | text_content = ''; |
223 | 224 | if (el.textContent) |
|
239 | 240 | } |
240 | 241 | } |
241 | 242 | } |
242 | | - best_selector = selector_by_id; |
| 243 | + best_selector = sel_by_id; |
243 | 244 | lowest_child_count = child_count_by_id; |
244 | | - child_count_by_class = ssOccurrences(selector_by_class, child_sep); |
| 245 | + child_count_by_class = ssOccurrences(selector_by_class, ' > '); |
245 | 246 | if (child_count_by_class < lowest_child_count) { |
246 | 247 | best_selector = selector_by_class; |
247 | 248 | lowest_child_count = child_count_by_class; |
|
622 | 623 | href = grand_el.href; origin = grand_el.origin; |
623 | 624 | } |
624 | 625 | document.recorded_actions.pop(); |
625 | | - child_sep = ' > '; |
626 | | - child_count = ssOccurrences(selector, child_sep); |
| 626 | + child_count = ssOccurrences(selector, ' > '); |
627 | 627 | if ((tag_name === "a" && !el.hasAttribute('onclick') && |
628 | 628 | child_count > 0 && href.length > 0) || |
629 | 629 | (parent_tag_name === "a" && href.length > 0 && |
|
0 commit comments