-
Notifications
You must be signed in to change notification settings - Fork 0
/
buildElementSelectorCssBreadcrumbs.js
47 lines (39 loc) · 1.61 KB
/
buildElementSelectorCssBreadcrumbs.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
/*
* @Example of result:
*
* 0: ".container-fluid .row"
* 1: ".container-fluid .row .col-md-12 .conteiner-fluid .row"
*
* @param selector
* @returns {*[]}
*/
function buildElementSelectorCssBreadcrumbs(selector) {
var elements = document.querySelectorAll(selector);
console.log('Elements with same selector:' + elements.length);
if (elements.length > 0) {
var allElementBreadcrumbs = [];
for (var k in elements) {
if (elements.hasOwnProperty(k)) {
var parents = [];
var parents2 = [];
var elem = elements[k];
while (elem.parentNode && elem.parentNode.nodeName.toLowerCase() != 'body') {
elem = elem.parentNode;
parents.push(elem);
var breadcrumbItem = (elem.getAttribute('id') != null ? ('#' + elem.getAttribute('id')) : '') + (elem.getAttribute('class') != null ? ('.' + elem.getAttribute('class').split(' ').join('.')) : '');
if (breadcrumbItem.length > 0) {
parents2.push(breadcrumbItem);
}
}
}
var newBreadcrumb = parents2.reverse().join(' ') + ' ' + selector;
if (allElementBreadcrumbs.indexOf(newBreadcrumb) == -1) {
allElementBreadcrumbs.push(newBreadcrumb);
}
}
console.log('Unique elements with same selector:' + allElementBreadcrumbs.length);
return allElementBreadcrumbs;
}
}
/* Possible run inside web browser console. */
console.log(buildElementSelectorCssBreadcrumbs(".selector"));