-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathrulechecker.js
244 lines (194 loc) · 7.61 KB
/
rulechecker.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
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
(function(){
// Global var to allow customization and multiple runs
CSSRuleChecker = window.CSSRuleChecker || {};
var percentil = CSSRuleChecker.percentil;
CSSRuleChecker.checkRules = function(statistics) {
var finalStatistics = CSSRuleChecker.defaultStatistics;
var customStatistics = CSSRuleChecker.customStatistics;
if (_.isArray(customStatistics)) {
finalStatistics = _.union(customStatistics, finalStatistics);
}
if (_.isArray(statistics)) {
finalStatistics = _.union(statistics, finalStatistics);
}
CSSRuleChecker.statistics = _.uniq(finalStatistics, function(elem) { return elem.name });
var stylesheets = document.styleSheets;
var totalCount = 0;
var totalAppliedCount = 0;
var cssResults = {
rules: [],
parts: []
};
for ( var index = 0; stylesheets && index < stylesheets.length; ++index ){
var currentStylesheet = stylesheets[index];
var stylesheetName = "Inline Style";
if (currentStylesheet.ownerNode.tagName.toLowerCase() === "link") {
stylesheetName = currentStylesheet.href;
}
console.info("Stylesheet " + index + ": " + stylesheetName);
var result = analyzeStylesheet(currentStylesheet );
if (result) {
totalAppliedCount += result.appliedSelectorCount;
totalCount += result.totalSelectorCount;
updateIndices(result.rules, cssResults.rules.length, cssResults.parts.length);
cssResults.rules.push.apply(cssResults.rules, result.rules.rules);
cssResults.parts.push.apply(cssResults.parts, result.rules.parts);
console.log(result);
}
}
console.info("There are " + totalCount + " rules");
var notAppliedCount = totalCount - totalAppliedCount;
console.info("Applied: " + totalAppliedCount + ", Not applied: " + notAppliedCount);
CSSRuleChecker.totalCount = totalCount;
CSSRuleChecker.appliedCount = totalAppliedCount;
CSSRuleChecker.rules = cssResults;
CSSRuleChecker.results = {};
for (var j = 0; j < CSSRuleChecker.statistics.length; ++j) {
var statistic = CSSRuleChecker.statistics[j];
CSSRuleChecker.results[statistic.name] = statistic.func(cssResults);
console.log(statistic.name, CSSRuleChecker.results[statistic.name]);
}
};
// returns a tuple containing:
// - appliedSelectorCount
// - totalSelectorCount
// - selectorsPerRule
// - partsPerSelector
function analyzeStylesheet(stylesheet) {
var appliedCount = 0;
var totalCount = 0;
var result = {
rules: [],
parts: []
};
if (!stylesheet) {
return false;
}
var rules = stylesheet.rules;
for ( var j = 0; rules && j < rules.length; ++j ){
var currentRule = rules[j];
// Skip other types of rules like media rules or fontface rules
if (!((currentRule instanceof CSSImportRule) || currentRule instanceof CSSStyleRule)) {
continue;
}
if ( currentRule instanceof CSSImportRule ) {
var importResult = analyzeStylesheet(currentRule.styleSheet);
appliedCount += importResult.appliedSelectorCount;
totalCount += importResult.totalSelectorCount;
updateIndices(importResult.rules, result.rules.length, result.parts.length);
result.rules.push.apply(result.rules, importResult.rules.rules);
result.parts.push.apply(result.parts, importResult.rules.parts);
} else {
totalCount += 1;
var rule = {
selector: currentRule.selectorText,
partIndices: [],
applied: false,
};
var selectorAppliedCount = selectorAppliedTimes(rule.selector);
if (selectorAppliedCount){
appliedCount += 1;
rule.applied = selectorAppliedCount;
}
// Analyze rule composition
if (rule.selector !== void 0 && rule.selector !== null) {
var selectorsInRule = rule.selector.split(',');
// This takes into account all possible ways to create a relationship between 2
// selectors:
// - spaces e.g: "parent children"
// - '>' or '+' within spaces. e.g: "parent > children" "sibling + sibling
// - attribute selector. e.g: "selector[attr="modifier"]
// - status selector. e.g: "selector:status"
var cssRegex = /\s+|\s*?[>+]\s*|\[|\:?\:/;
for (var i = 0; i < selectorsInRule.length; ++i) {
// Trim leading and traling whitespaces
var selector = selectorsInRule[i].replace(/^\s*([\S][\s\S]*[\S])\s*$/, '$1');
// Remove first '.' cause it made difficult to parse rules right
var selectorParts = selector.split('.').filter(notEmpty).join(' ').split(cssRegex);
var part = {
selector: selector,
count: selectorParts.length,
ruleIndex: result.rules.length,
};
rule.partIndices.push(result.parts.length);
result.parts.push(part);
}
result.rules.push(rule);
}
}
}
return {
appliedSelectorCount: appliedCount,
totalSelectorCount: totalCount,
rules: result
};
}
function updateIndices(results, ruleCount, partCount) {
var currentRule,
currentPart,
i;
for (i = 0; i < results.rules.length; ++i) {
currentRule = results.rules[i];
currentRule.partIndices = currentRule.partIndices.map(function(index) {
return index + partCount;
});
}
for (i = 0; i < results.parts.length; ++i) {
currentPart = results.parts[i];
currentRule.ruleIndex += ruleCount;
}
}
function selectorAppliedTimes(selector) {
// Remove all pseudo classes, as some sizzle versions
// break on them
var pseudoRegex = /([^:\s])\:[\w\-]+(?:\(.*\))?([.\s>+\[#,]*?)/gi;
var cleanSelectorText = selector.replace(pseudoRegex, '$1$2');
var matchedElements = jQuery(cleanSelectorText);
return matchedElements.length;
}
function notEmpty(element) {
return element !== '';
}
// algorithm must be a function that returns a number value
function getStatistic(statName, collection, algorithm) {
var result = algorithm(collection);
console.log(statName, result);
}
var loadDependency = function(existenceCheck, scriptSrc) {
var deferred = $.Deferred();
if (!existenceCheck) {
var script = document.createElement('script');
script.src = scriptSrc;
script.onload = function() {
deferred.resolve();
};
document.body.appendChild(script);
} else {
deferred.resolve();
}
return deferred.promise();
};
var loadDependenciesAndRun = function() {
var dependenciesPromise = [
loadDependency(window._ && window._.VERSION > "1.4.4", 'https://raw.github.com/documentcloud/underscore/master/underscore-min.js'),
loadDependency(window.cssExplain, 'https://raw.github.com/josh/css-explain/master/css-explain.js'),
loadDependency(null, 'https://raw.github.com/Juanxo/css-rule-checker/master/statistics.js')
];
if (CSSRuleChecker.dependencies) {
for (var i = 0; i < CSSRuleChecker.dependencies.length; ++i) {
dependenciesPromise.push(loadDependency(null, CSSRuleChecker.dependencies[i]));
}
}
$.when.apply($, dependenciesPromise).then(CSSRuleChecker.checkRules);
};
// Load jQuery if it hasn't be previously loaded
if (!window.jQuery || window.jQuery().jquery < '1.9.0') {
var script = document.createElement( 'script' );
script.src = '//ajax.googleapis.com/ajax/libs/jquery/1/jquery.min.js';
script.onload = loadDependenciesAndRun;
document.body.appendChild(script);
}
else {
loadDependenciesAndRun();
}
})();