This repository has been archived by the owner on Mar 13, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 4
/
style-auditor.html
123 lines (103 loc) · 3.2 KB
/
style-auditor.html
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
<script>
addEventListener('WebComponentsReady', function() {
// given a list of elements, produce a report of rules that
// match those elements.
var auditor = {
matchesForDocument: function(elements) {
var info = [];
this.documentSheets.forEach(function(sheet) {
var list = this.matchesForSheet(sheet, elements);
if (list.length) {
info.push({sheet: sheet, rules: list});
}
}, this);
return info;
},
// TODO(sorvell): support stylesheets inside HTMLImports
documentSheets: Array.prototype.filter.call(document.styleSheets,
function(sheet) {
return !sheet.ownerNode.hasAttribute('scope');
}
),
matchesForSheet: function(sheet, elements) {
var info = [];
Array.prototype.forEach.call(sheet.cssRules, function(rule) {
var list = this.matchesForRule(rule, elements);
if (list.length) {
info.push({selector: rule.selectorText, elements: list});
}
}, this);
return info;
},
matchesForRule: function(rule, list) {
var info = [];
list.forEach(function(i) {
var elements = i.elements.filter(function(e) {
return matchesSelector.call(e, rule.selectorText);
});
if (elements.length) {
info.push({host: i.host, elements: elements});
}
});
return info;
}
};
var p = Element.prototype;
var matchesSelector = p.matches || p.matchesSelector ||
p.mozMatchesSelector || p.msMatchesSelector ||
p.oMatchesSelector || p.webkitMatchesSelector;
// crawl the document and return a list of custom elements with shadyRoots
// and their scoped contents
var crawler = {
// list of elements: array of {host, elements}
list: function() {
var list = [];
var elements = this.elementsWithShadyRoot();
elements.forEach(function(e) {
list.push({host: e, elements: this.elementsInsideShadyRoot(e)});
}, this);
return list;
},
elementsWithShadyRoot: function() {
var e$ = Polymer.dom(document).querySelectorAll('*');
return this.filterElementsWithRoots(e$);
},
elementsInsideShadyRoot: function(e) {
var e$ = Polymer.dom(e.root || e).querySelectorAll('*');
var roots = this.filterElementsWithRoots(e$);
roots.forEach(function(e) {
e$ = e$.concat(this.elementsInsideShadyRoot(e));
}, this);
return e$;
},
filterElementsWithRoots: function(elements) {
return elements.filter(function(e) {
return e.shadyRoot;
});
}
};
// dump an auditor report
var logger = {
dump: function(log) {
log.forEach(function(l) {
console.group(l.sheet.ownerNode);
this.dumpRules(l.rules);
console.groupEnd(l.sheet.ownerNode);
}, this);
},
dumpRules: function(rules) {
rules.forEach(function(i) {
console.group(i.selector);
console.log(i.elements);
console.groupEnd(i.selector);
});
}
};
// pruduces a style audit and reports results on the console.
function audit() {
var report = auditor.matchesForDocument(crawler.list());
logger.dump(report);
}
audit();
});
</script>