This repository has been archived by the owner on May 19, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 5
/
css-slap-chop.js
175 lines (149 loc) · 4.82 KB
/
css-slap-chop.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
var fs = require('fs')
, cssom = require('cssom')
, jsdom = require('jsdom')
, async = require('async')
, _ = require('underscore')
//
// private helpers
//
function readFile(path, cb) {
fs.readFile(path, function(err, data) {
if (err) cb(err)
else cb(null, data.toString('utf8'))
})
}
//
// the public api
//
module.exports = {
// USAGE
//
// panda.parse({html:path_to_html, css:path_to_css}, function(err, data) {
// console.log(data.used)
// console.log(data.unused)
// })
parse: function(options, cb) {
var ignore = options.ignore ? require(options.ignore).selectors : []
function readCSS(cb) {
readFile(options.css, cb)
}
function readHTML(cb) {
readFile(options.html, cb)
}
function css(cb) {
readCSS(function(err, css){
if (err) cb(err)
parseCSS(css, cb)
})
}
function html(cb) {
readHTML(function(err, html){
if (err) cb(err)
parseHTML(html, cb)
})
}
function parseCSS(str, cb) {
var rules = cssom.parse(str).cssRules.map(function(r) {
return r.selectorText
})
cb(null, rules)
}
function parseHTML(str, cb) {
var window = jsdom.jsdom(str).createWindow()
jsdom.jQueryify(window, 'jquery.js', function(window, $) {
cb(null, $)
})
}
// read in the css rules and html at the same time
async.parallel([css, html], function(err, results){
if (err) cb(err)
var rules = results[0]
, $ = results[1]
, r = { used:ignore, unused:[] }
// then loop thru the css selectors using jquery to test for their existance in the html doc
for (var i = 0, l = rules.length; i < l; i++) {
var rule = rules[i]
, query = rule.replace(/:[\w]*/g,'')
, found = !!($(query).length >= 1)
if (found) {
r.used.push(rule)
}
else {
r.unused.push(rule)
}
}
cb(null, r)
})
}
,
//
// returns used selectors as array
//
used: function(options, callback) {
this.parse(options, function(err, result) {
callback(err, result.used)
})
}
,
//
// returns unused selectors as array
//
unused: function(options, callback) {
this.parse(options, function(err, result) {
callback(err, result.unused)
})
}
,
//
// returns tidy css with unused selectors removed
//
tidy: function(options, callback) {
this.used(options, function(err, selectors) {
readFile(options.css, function(err, str) {
var rules = cssom.parse(str).cssRules
, usedRules = []
, css = ''
rules.forEach(function(rule) {
var hasSelector = _.indexOf(selectors, rule.selectorText)
if (hasSelector >= 0) usedRules.push(rule)
})
usedRules.forEach(function(rule) {
css += rule.selectorText + " {\n"
var allkeys = _.keys(rule.style)
, offset = rule.style.length
, len = allkeys.length
, rest = len - offset
, keys = _.rest(allkeys, rest)
_.each(keys, function(k, i) {
css += " " + k + ":" + rule.style[k] + ";\n"
})
css += "}\n\n"
})
callback(null, css)
})
})
}
,
cli: function() {
var program = require('commander')
program
.version('0.1.0')
.description('Reads a CSS file and an HTML file and returns a CSS file with unused selectors removed.')
.option('-h, --html <path>', 'REQUIRED! The markup to analyze for selectors.')
.option('-c, --css <path>', 'REQUIRED! The css file to analyze.')
.option('-i, --ignore <path>', 'Optional JSON file of selectors to ignore.')
.parse(process.argv)
if (process.argv.length === 2) {
console.log('Try running --help for all the options.')
}
else {
var opts = { html:program.html, css:program.css }
if (program.ignore) opts.ignore = program.ignore
this.tidy(opts, function(err, css) {
if (err) console.error(err)
console.log(css)
})
}
}
// end of exports
}