-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
executable file
·53 lines (40 loc) · 2.48 KB
/
index.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
#!/usr/bin/env node
var fs = require('fs'),
_ = require('lodash'),
css = require('css');
var filename = 'proof-of-concept',
file = fs.readFileSync('./' + filename +'.css', { encoding: 'utf8' }),
parsed = css.parse(file),
o = {
positioning: [ 'position', 'top', 'right', 'bottom', 'left', 'z-index', 'overflow', 'cursor', 'clip', 'float', 'clear' ],
sizing: [ 'display', 'box-sizing', 'width', 'height', 'margin', 'margin-top', 'margin-right', 'margin-bottom', 'margin-left', 'padding', 'padding-top', 'padding-right', 'padding-bottom', 'padding-left', 'max-height', 'max-width', 'min-height', 'min-width' ],
border: [ 'border', 'border-radius', 'border-bottom', 'border-bottom-color', 'border-bottom-style', 'border-bottom-width', 'border-color', 'border-left', 'border-left-color', 'border-left-style', 'border-left-width', 'border-right', 'border-right-color', 'border-right-style', 'border-right-width', 'border-style', 'border-top', 'border-top-color', 'border-top-style', 'border-top-width', 'border-width' ],
background: [ 'background', 'background-attachment', 'background-color', 'background-image', 'background-position', 'background-repeat', 'background-size' ],
text: [ 'color', 'direction', 'letter-spacing', 'line-height', 'text-align', 'text-align-last', 'text-decoration', 'text-indent', 'text-shadow', 'text-transform', 'unicode-bidi', 'vertical-align', 'white-space', 'word-spacing', 'word-wrap', 'text-underline-position'],
font: [ 'font' , 'font-size-adjust' , 'font-stretch' , 'font-family' , 'font-size' , 'font-style' , 'font-variant' , 'font-weight' ],
list: [ 'list-style', 'list-style-image', 'list-style-position', 'list-style-type' ],
outline: [ 'outline', 'outline-color', 'outline-style', 'outline-width' ]
};
_.each(parsed.stylesheet.rules, function (rule, idx) {
var grouped = _.groupBy(rule.declarations, function (dec) {
return _.findKey(o, function (props) {
return _.contains(props, dec.property);
}) || 'unsorted';
});
var groupOrder = _.keys(o);
var newDecs = [];
_.each(groupOrder, function (group) {
if(grouped[group]) {
newDecs.push({
'type': 'comment',
'comment': ' ' + group + ' '
});
}
_.each(grouped[group], function (d) {
newDecs.push(d);
});
});
rule.declarations = newDecs;
parsed.stylesheet.rules[idx] = rule;
});
fs.writeFile('./' + filename + '.parsed' + '.css', css.stringify(parsed).replace(/;(\s*)(\/\*)/g,'; \n\n /*'));