minify-family.js
2.6 KB
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
var stringify = require('postcss-value-parser').stringify;
var uniqs = require('./uniqs')('monospace');
// Note that monospace is missing intentionally from this list; we should not
// remove instances of duplicated monospace keywords, it causes the font to be
// rendered smaller in Chrome.
var keywords = [
'sans-serif',
'serif',
'fantasy',
'cursive'
];
function intersection(haystack, array) {
return array.some(function (v) {
return ~haystack.indexOf(v);
});
};
module.exports = function (nodes, opts) {
var family = [];
var last = null;
var i, max;
nodes.forEach(function (node, index, nodes) {
var value = node.value;
if (node.type === 'string' || node.type === 'function') {
family.push(node);
} else if (node.type === 'word') {
if (!last) {
last = { type: 'word', value: '' };
family.push(last);
}
last.value += node.value;
} else if (node.type === 'space') {
if (last && index !== nodes.length - 1) {
last.value += ' ';
}
} else {
last = null;
}
});
family = family.map(function (node) {
if (node.type === 'string') {
if (
!opts.removeQuotes ||
intersection(node.value, keywords) ||
/[0-9]/.test(node.value.slice(0, 1))
) {
return stringify(node);
}
var escaped = node.value.split(/\s/).map(function (word, index, words) {
var next = words[index + 1];
if (next && /^[^a-z]/i.test(next)) {
return word + '\\';
}
if (!/^[^a-z\d\xa0-\uffff_-]/i.test(word)) {
return word.replace(/([^a-z\d\xa0-\uffff_-])/gi, '\\$1');
}
if (/^[^a-z]/i.test(word) && index < 1) {
return '\\' + word;
}
return word;
}).join(' ');
if (escaped.length < node.value.length + 2) {
return escaped;
}
}
return stringify(node);
});
if (opts.removeAfterKeyword) {
for (i = 0, max = family.length; i < max; i += 1) {
if (~keywords.indexOf(family[i])) {
family = family.slice(0, i + 1);
break;
}
}
}
if (opts.removeDuplicates) {
family = uniqs(family);
}
return [
{
type: 'word',
value: family.join()
}
];
};