forked from meteor/meteor
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathminification.js
206 lines (171 loc) · 4.66 KB
/
minification.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
// http://stackoverflow.com/questions/9906794/internet-explorers-css-rules-limits
var LIMIT = 4095;
// Stringifier based on css-stringify
var emit = function (str) {
return str.toString();
};
var visit = function (node, last) {
return traverse[node.type](node, last);
};
var mapVisit = function (nodes) {
var buf = "";
for (var i = 0, length = nodes.length; i < length; i++) {
buf += visit(nodes[i], i === length - 1);
}
return buf;
};
// returns a list of strings
MinifyAst = function(node) {
// the approach is taken from BlessCSS
var newAsts = [];
var current = {
selectors: 0,
nodes: []
};
var startNewAst = function () {
newAsts.push({
type: 'stylesheet',
stylesheet: {
rules: current.nodes
}
});
current.nodes = [];
current.selectors = 0;
};
_.each(node.stylesheet.rules, function (rule) {
switch (rule.type) {
case 'rule':
if (current.selectors + rule.selectors.length > LIMIT) {
startNewAst();
}
current.selectors += rule.selectors.length;
current.nodes.push(rule);
break;
case 'comment':
// no-op
break;
default:
// nested rules
var nested = 0;
_.each(rule.rules, function (nestedRule) {
if (nestedRule.selectors) {
nested += nestedRule.selectors.length;
}
});
if (current.selectors + nested > LIMIT) {
startNewAst();
}
current.selectors += nested;
current.nodes.push(rule);
break;
}
});
// push the left-over
if (current.nodes.length > 0) {
startNewAst();
}
var stringifyAst = function (node) {
return node.stylesheet
.rules.map(function (rule) { return visit(rule); })
.join('');
};
return newAsts.map(stringifyAst);
};
var traverse = {};
traverse.comment = function(node) {
return emit('', node.position);
};
traverse.import = function(node) {
return emit('@import ' + node.import + ';', node.position);
};
traverse.media = function(node) {
return emit('@media ' + node.media, node.position, true)
+ emit('{')
+ mapVisit(node.rules)
+ emit('}');
};
traverse.document = function(node) {
var doc = '@' + (node.vendor || '') + 'document ' + node.document;
return emit(doc, node.position, true)
+ emit('{')
+ mapVisit(node.rules)
+ emit('}');
};
traverse.charset = function(node) {
return emit('@charset ' + node.charset + ';', node.position);
};
traverse.namespace = function(node) {
return emit('@namespace ' + node.namespace + ';', node.position);
};
traverse.supports = function(node){
return emit('@supports ' + node.supports, node.position, true)
+ emit('{')
+ mapVisit(node.rules)
+ emit('}');
};
traverse.keyframes = function(node) {
return emit('@'
+ (node.vendor || '')
+ 'keyframes '
+ node.name, node.position, true)
+ emit('{')
+ mapVisit(node.keyframes)
+ emit('}');
};
traverse.keyframe = function(node) {
var decls = node.declarations;
return emit(node.values.join(','), node.position, true)
+ emit('{')
+ mapVisit(decls)
+ emit('}');
};
traverse.page = function(node) {
var sel = node.selectors.length
? node.selectors.join(', ')
: '';
return emit('@page ' + sel, node.position, true)
+ emit('{')
+ mapVisit(node.declarations)
+ emit('}');
};
traverse['font-face'] = function(node){
return emit('@font-face', node.position, true)
+ emit('{')
+ mapVisit(node.declarations)
+ emit('}');
};
traverse.rule = function(node) {
var decls = node.declarations;
if (!decls.length) return '';
var selectors = node.selectors.map(function (selector) {
// removes universal selectors like *.class => .class
// removes optional whitespace around '>' and '+'
return selector.replace(/\*\./, '.')
.replace(/\s*>\s*/g, '>')
.replace(/\s*\+\s*/g, '+');
});
return emit(selectors.join(','), node.position, true)
+ emit('{')
+ mapVisit(decls)
+ emit('}');
};
traverse.declaration = function(node, last) {
var value = node.value;
// remove optional quotes around font name
if (node.property === 'font') {
value = value.replace(/\'[^\']+\'/g, function (m) {
if (m.indexOf(' ') !== -1)
return m;
return m.replace(/\'/g, '');
});
value = value.replace(/\"[^\"]+\"/g, function (m) {
if (m.indexOf(' ') !== -1)
return m;
return m.replace(/\"/g, '');
});
}
// remove url quotes if possible
// in case it is the last declaration, we can omit the semicolon
return emit(node.property + ':' + value, node.position)
+ (last ? '' : emit(';'));
};