forked from driverdan/cssess
-
Notifications
You must be signed in to change notification settings - Fork 0
/
cssess.js
executable file
·364 lines (311 loc) · 9.06 KB
/
cssess.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
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
/**
* CSSess
* A bookmarklet for detecting unused CSS selectors and related info.
* Copyright 2010 passive.ly LLC
* @see https://github.com/driverdan/cssess
*
* @author Dan DeFelippi <[email protected]>
* @license MIT license, see LICENSE
*/
var cssess = cssess || {};
cssess.baseUrl = "http://driverdan.github.com/cssess/";
// CHANGE TO RELATIVE PATH FOR TESTING
// At some point this will be changed to switch via URL parameter
//cssess.baseUrl = "../";
// Get site's base URL for checking remote scripts
cssess.siteUrl = window.location.protocol + "//" + window.location.hostname;
// Data format version. Used for Jdrop.
cssess.dataVersion = "1.0.0";
// Save result data. Used for views & Jdrop.
cssess.data = {};
// Namespace for all templates / views
cssess.v = {};
// List of links on the page
cssess.links = [];
/**
* Fetches the styles from a page and checks them.
*/
cssess.checkStyles = function(url, source) {
var style = [];
// Default source is the document
source = source || document;
// Process inline styles
cssess.$("style", source).each(function() {
style = style.concat(cssess.parseCss(this.innerHTML, source));
});
if (style.length) {
cssess.addUnused("inline styles (" + url + ")", style);
}
// Process stylesheets
cssess.$("link[rel='stylesheet'][href!='']:not([href^='file:']):not([href^='chrome://'])", source).each(function() {
// Check for local or external stylesheet
var href = this.href;
if (!href.match(/^https?:\/\//)
|| href.indexOf(cssess.siteUrl) == 0
) {
cssess.$.get(href, function(data, status) {
if (data) {
var selectors = cssess.parseCss(data, source);
if (selectors.length) {
cssess.addUnused(href, selectors);
}
}
});
}
});
};
/**
* Loads page links and builds list for spidering.
*/
cssess.start = function() {
// Start with the current page
cssess.v.addLink(window.location.href);
/**
* Find valid anchor elements. Exclude
* Empty hrefs
* JavaScript
* Hashes
* Emails
* Local files
* Chrome extensions
*/
cssess.$("a[href!='']:not([href^='javascript:']):not([href^='#']):not([href^='mailto:']):not([href^='file:']):not([href^='chrome://'])").each(function() {
var href = this.href;
// Check for external links
if (!href.match(/^https?:\/\//)
|| (
href.indexOf(cssess.siteUrl) == 0
&& href != window.location
)
) {
// Add link to view
cssess.v.addLink(this.href);
}
});
};
/**
* Parses CSS string and checks for unused selectors.
*
* @param string CSS to parse.
* @param object Optional DOM object to check selectors against. Default is document.
* @return array Returns array of unused selectors.
*/
cssess.parseCss = function(data, source) {
// regexes from Helium: https://github.com/geuis/helium-css/
data = data
// Remove comments
.replace(/\/\*[\s\S]*?\*\//gim, "")
// Remove whitespace
.replace(/[\n\t]/g, "");
var selectors = data.match(/[^\}]+[\.\#\-\w]?(?=\{)/gim)
, missing = []
, i;
for (i in selectors) {
// Wrap in try/catch for invalid selectors
try {
if (!cssess.$(selectors[i], source).length) {
missing.push(selectors[i]);
}
} catch (e) {}
}
return missing;
};
/**
* Spiders all same domain links within the page.
*/
cssess.spider = function() {
if (this.urls && this.urls.length) {
var $iframe = cssess.$("#cssesspider")
, src = this.urls.pop();
// Create iframe if it doesn't exist
if (!$iframe.length) {
$iframe = cssess.$('<iframe id="cssesspider" style="display:none"/>');
$iframe.load(function() {
// Run on the new document
// try / catch in case of cross domain issues
try {
cssess.checkStyles(this.src, cssess.$(this).contents());
} catch (e) {}
// Continue spidering
cssess.spider();
});
$iframe.appendTo(cssess.win);
}
// Check if it's the local page
if (src == window.location.href) {
cssess.checkStyles(src);
} else {
$iframe.attr("src", src);
}
} else {
cssess.$("#cssesspider").remove();
}
};
/**
* Loads all checked links to be spidered.
*/
cssess.loadLinks = function() {
cssess.urls = [];
cssess.$("input[name='urls']:checked", cssess.win).each(function() {
cssess.urls.push(this.value);
});
};
/**
* Add unused styles to data array and to view.
*/
cssess.addUnused = function(name, selectors) {
cssess.data[name] = selectors;
cssess.v.addUnused(name, selectors);
};
/**
* Save data to Jdrop
*/
cssess.saveToJdrop = function(summary) {
// create object of parameters to pass to Jdrop
var params = {
appname: "CSSess"
,title: document.title
,version: cssess.dataVersion
,summary: summary
,json: JSON.stringify(cssess.data)
};
// hidden iframe to use as target of form submit
var jdropif = document.createElement("iframe");
jdropif.style.display = "none";
jdropif.name = "jdropiframe";
jdropif.id = "jdropiframe";
document.body.appendChild(jdropif);
// form for posting data
var jdropform = document.createElement("form");
jdropform.method = "post";
jdropform.action = "http://jdrop.org/save";
jdropform.target = "jdropiframe";
jdropform.style.display = "hidden";
// add each param to the form as an input field
for (var key in params) {
var pInput = document.createElement("input");
pInput.setAttribute("name", key);
pInput.setAttribute("value", params[key]);
jdropform.appendChild(pInput);
}
// submit the form and cleanup
document.body.appendChild(jdropform);
jdropif.onload = function() { document.body.removeChild(jdropform); document.body.removeChild(jdropif); };
jdropif.onerror = function() { document.body.removeChild(jdropform); document.body.removeChild(jdropif); };
jdropform.submit();
};
/**
* Creates the modal window.
*/
cssess.v.createWin = function() {
// Remove window if it already exists
if (cssess.win) {
cssess.win.remove();
}
cssess.$("#cssess").remove();
cssess.win = cssess.$('<div id="cssess-overlay"/><div id="cssess"><h2>CSSess</h2><a class="cssess-close" title="close" href="">X</a><div class="cssess-body"><button class="cssess-toggle">Toggle</button><button class="cssess-jdrop">Save to Jdrop</button><ul class="cssess-links"/><ul class="cssess-styles"/></div><button class="cssess-run">find unused selectors</button></div>');
// Add event to run button to run tests
cssess.$(".cssess-run", cssess.win).click(function() {
// Clear any previously detected styles
cssess.$(".cssess-styles", cssess.win).html("");
// Load the checked links and spyder them.
cssess.loadLinks();
cssess.spider();
cssess.$(".cssess-jdrop").show();
});
// Close button removes the win div
cssess.$(".cssess-close", cssess.win).click(function() {
cssess.win.remove();
return false;
});
// Button to toggle checkboxes
cssess.$(".cssess-toggle", cssess.win).click(function() {
cssess.$(":checkbox", cssess.win).each(function() {
this.checked ? this.checked = false : this.checked = "checked";
});
});
// Button to save to Jdrop
// @see http://jdrop.org
cssess.$(".cssess-jdrop", cssess.win).click(function() {
// Get a total count of unused selectors
var count = 0;
for (i in cssess.data) {
count += cssess.data[i].length;
}
// Save it
cssess.saveToJdrop(count + " unused CSS selectors");
});
cssess.win.appendTo("body");
};
/**
* Adds a newly found link to the DOM
*/
cssess.v.addLink = function(href) {
// Make sure the link hasn't already been found
if (cssess.$.inArray(href, cssess.links) == -1) {
cssess.links.push(href);
cssess.$(".cssess-links", cssess.win).append('<li><input type="checkbox" name="urls" value="' + href + '" checked/> ' + href + '</li>');
}
};
/**
* Save additional unused selectors
*/
cssess.v.addUnused = function(name, selectors) {
var data = {}
,i;
if (name && selectors) {
data[name] = selectors;
} else {
data = name;
}
for (i in data) {
name = i;
selectors = data[i];
var $ul = cssess.$(".cssess-styles", cssess.win)
, li = "<li><strong>" + name + " (" + selectors.length + " found)</strong><ul>"
, i;
// Each missing selector
for (i in selectors) {
li += "<li>" + selectors[i] + "</li>";
}
$ul.append(li + "</ul></li>");
}
};
// Initialize everything
(function(d, t) {
var el, s
/**
* Opens the dialog and gets it running.
*/
, init = function($) {
// Create modal window
cssess.v.createWin();
// Don't run start if it's loading Jdrop data
if (!JDROPVIEW) {
cssess.start();
}
};
// Get the stylesheet
el = d.createElement("link");
el.rel = "stylesheet";
el.href = cssess.baseUrl + "cssess.css";
s = d.getElementsByTagName(t)[0];
s.parentNode.insertBefore(el, s);
// Load jQuery
// Intentionally loads even if already on page to avoid version incompatibilities
el = d.createElement(t);
el.src = "//ajax.googleapis.com/ajax/libs/jquery/1.4.4/jquery.min.js";
el.onload = function() {
el.loaded = true;
cssess.$ = jQuery.noConflict(true);
init(cssess.$);
};
el.onreadystatechange = function() {
if ((el.readyState == "loaded" || el.readyState == "complete") && !el.loaded) {
el.loaded = true;
cssess.$ = jQuery.noConflict(true);
init(cssess.$);
}
};
s.parentNode.insertBefore(el, s);
})(document, "script");