/
charset_normalizer.js
60 lines (43 loc) · 1.4 KB
/
charset_normalizer.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
/**
* class CharsetNormalizer
*
* Some browsers have issues with stylesheets that contain multiple
* `@charset` definitions. The `CharsetNormalizer` processor strips
* out multiple `@charset` definitions.
*
* The current implementation is naive. It picks the first `@charset`
* it sees and strips the others. This works for most people because
* the other definitions are usually `UTF-8`. A more sophisticated
* approach would be to re-encode stylesheets with mixed encodings.
*
* This behavior can be disabled with:
*
* environment.unregisterBundleProcessor('text/css', CharsetNormalizer);
*
*
* ##### SUBCLASS OF
*
* [[Template]]
**/
'use strict';
// internal
var Template = require('../template');
////////////////////////////////////////////////////////////////////////////////
// Class constructor
var CharsetNormalizer = module.exports = function CharsetNormalizer() {
Template.apply(this, arguments);
};
require('util').inherits(CharsetNormalizer, Template);
// Cached version of RegExp used to find and process charset directives
var CHARSET_RE = /^@charset "([^"]+)";$/gm;
// Process data
CharsetNormalizer.prototype.evaluate = function (/*context, locals*/) {
var charset = null;
this.data = this.data.replace(CHARSET_RE, function (m) {
charset = charset || m;
return '';
});
if (charset !== null) {
this.data = charset + '\n' + this.data;
}
};