-
Notifications
You must be signed in to change notification settings - Fork 324
/
tidy-block.js
26 lines (21 loc) · 935 Bytes
/
tidy-block.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
var SUPPORTED_COMPACT_BLOCK_MATCHER = /^@media\W/;
var SUPPORTED_QUOTE_REMOVAL_MATCHER = /^@(?:keyframes|-moz-keyframes|-o-keyframes|-webkit-keyframes)\W/;
function tidyBlock(values, spaceAfterClosingBrace) {
var withoutSpaceAfterClosingBrace;
var withoutQuotes;
var i;
for (i = values.length - 1; i >= 0; i--) {
withoutSpaceAfterClosingBrace = !spaceAfterClosingBrace && SUPPORTED_COMPACT_BLOCK_MATCHER.test(values[i][1]);
withoutQuotes = SUPPORTED_QUOTE_REMOVAL_MATCHER.test(values[i][1]);
values[i][1] = values[i][1]
.replace(/\n|\r\n/g, ' ')
.replace(/\s+/g, ' ')
.replace(/(,|:|\() /g, '$1')
.replace(/ \)/g, ')')
.replace(withoutQuotes ? /'([a-zA-Z][a-zA-Z\d\-_]+)'/ : null, '$1')
.replace(withoutQuotes ? /"([a-zA-Z][a-zA-Z\d\-_]+)"/ : null, '$1')
.replace(withoutSpaceAfterClosingBrace ? /\) /g : null, ')');
}
return values;
}
module.exports = tidyBlock;