index.js
1.04 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
var postcss = require('postcss');
var charset = 'charset';
module.exports = postcss.plugin('postcss-normalize-' + charset, function (opts) {
opts = opts || {};
return function (css) {
var charsetRule;
var nonAsciiNode;
var nonAscii = /[^\x00-\x7F]/;
css.walk(function (node) {
if (node.type === 'atrule' && node.name === charset) {
if (!charsetRule) {
charsetRule = node;
}
node.remove();
} else if (!nonAsciiNode && node.parent === css && nonAscii.test(node)) {
nonAsciiNode = node;
}
});
if (nonAsciiNode) {
if (!charsetRule && opts.add !== false) {
charsetRule = postcss.atRule({
name: charset,
params: '"utf-8"'
});
}
if (charsetRule) {
charsetRule.source = nonAsciiNode.source;
css.prepend(charsetRule);
}
}
};
});