mh-two-thousand-and-two
2024-04-12 7fc6dbf547b8899d949b67cdec36b96a7d1701c7
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
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
var Class = require('./Class');
var trim = require('./trim');
var repeat = require('./repeat');
var defaults = require('./defaults');
var camelCase = require('./camelCase');
 
exports = {
    parse: function(css) {
        return new Parser(css).parse();
    },
    stringify: function(stylesheet, options) {
        return new Compiler(stylesheet, options).compile();
    }
};
var regComments = /(\/\*[\s\S]*?\*\/)/gi;
var regOpen = /^{\s*/;
var regClose = /^}/;
var regWhitespace = /^\s*/;
var regProperty = /^(\*?[-#/*\\\w]+(\[[0-9a-z_-]+\])?)\s*/;
var regValue = /^((?:'(?:\\'|.)*?'|"(?:\\"|.)*?"|\([^)]*?\)|[^};])+)/;
var regSelector = /^([^{]+)/;
var regSemicolon = /^[;\s]*/;
var regColon = /^:\s*/;
var regMedia = /^@media *([^{]+)/;
var regKeyframes = /^@([-\w]+)?keyframes\s*/;
var regFontFace = /^@font-face\s*/;
var regSupports = /^@supports *([^{]+)/;
var regIdentifier = /^([-\w]+)\s*/;
var regKeyframeSelector = /^((\d+\.\d+|\.\d+|\d+)%?|[a-z]+)\s*/;
var regComma = /^,\s*/;
var Parser = Class({
    initialize: function Parser(css) {
        this.input = stripCmt(css);
        this.open = this._createMatcher(regOpen);
        this.close = this._createMatcher(regClose);
        this.whitespace = this._createMatcher(regWhitespace);
        this.atImport = this._createAtRule('import');
        this.atCharset = this._createAtRule('charset');
        this.atNamespace = this._createAtRule('namespace');
    },
    parse: function() {
        return this.stylesheet();
    },
    stylesheet: function() {
        return {
            type: 'stylesheet',
            rules: this.rules()
        };
    },
    rules: function() {
        var rule;
        var rules = [];
        this.whitespace();
        while (
            this.input.length &&
            this.input[0] !== '}' &&
            (rule = this.atRule() || this.rule())
        ) {
            rules.push(rule);
            this.whitespace();
        }
        return rules;
    },
    atRule: function() {
        if (this.input[0] !== '@') return;
        return (
            this.atKeyframes() ||
            this.atMedia() ||
            this.atSupports() ||
            this.atImport() ||
            this.atCharset() ||
            this.atNamespace() ||
            this.atFontFace()
        );
    },
    atKeyframes: function() {
        var matched = this.match(regKeyframes);
        if (!matched) return;
        var vendor = matched[1] || '';
        matched = this.match(regIdentifier);
        if (!matched) throw Error('@keyframes missing name');
        var name = matched[1];
        if (!this.open()) throw Error("@keyframes missing '{'");
        var keyframes = [];
        var keyframe;
        while ((keyframe = this.keyframe())) {
            keyframes.push(keyframe);
        }
        if (!this.close()) throw Error("@keyframes missing '}'");
        return {
            type: 'keyframes',
            name: name,
            vendor: vendor,
            keyframes: keyframes
        };
    },
    keyframe: function() {
        var selector = [];
        var matched;
        while ((matched = this.match(regKeyframeSelector))) {
            selector.push(matched[1]);
            this.match(regComma);
        }
        if (!selector.length) return;
        this.whitespace();
        return {
            type: 'keyframe',
            selector: selector.join(', '),
            declarations: this.declarations()
        };
    },
    atSupports: function() {
        var matched = this.match(regSupports);
        if (!matched) return;
        var supports = trim(matched[1]);
        if (!this.open()) throw Error("@supports missing '{'");
        var rules = this.rules();
        if (!this.close()) throw Error("@supports missing '}'");
        return {
            type: 'supports',
            supports: supports,
            rules: rules
        };
    },
    atFontFace: function() {
        var matched = this.match(regFontFace);
        if (!matched) return;
        if (!this.open()) throw Error("@font-face missing '{'");
        var declaration;
        var declarations = [];
        while ((declaration = this.declaration())) {
            declarations.push(declaration);
        }
        if (!this.close()) throw Error("@font-face missing '}'");
        return {
            type: 'font-face',
            declarations: declarations
        };
    },
    atMedia: function() {
        var matched = this.match(regMedia);
        if (!matched) return;
        var media = trim(matched[1]);
        if (!this.open()) throw Error("@media missing '{'");
        this.whitespace();
        var rules = this.rules();
        if (!this.close()) throw Error("@media missing '}'");
        return {
            type: 'media',
            media: media,
            rules: rules
        };
    },
    rule: function() {
        var selector = this.selector();
        if (!selector) throw Error('missing selector');
        return {
            type: 'rule',
            selector: selector,
            declarations: this.declarations()
        };
    },
    declarations: function() {
        var declarations = [];
        if (!this.open()) throw Error("missing '{'");
        this.whitespace();
        var declaration;
        while ((declaration = this.declaration())) {
            declarations.push(declaration);
        }
        if (!this.close()) throw Error("missing '}'");
        this.whitespace();
        return declarations;
    },
    declaration: function() {
        var property = this.match(regProperty);
        if (!property) return;
        property = trim(property[0]);
        if (!this.match(regColon)) throw Error("property missing ':'");
        var value = this.match(regValue);
        this.match(regSemicolon);
        this.whitespace();
        return {
            type: 'declaration',
            property: property,
            value: value ? trim(value[0]) : ''
        };
    },
    selector: function() {
        var matched = this.match(regSelector);
        if (!matched) return;
        return trim(matched[0]);
    },
    match: function(reg) {
        var matched = reg.exec(this.input);
        if (!matched) return;
        this.input = this.input.slice(matched[0].length);
        return matched;
    },
    _createMatcher: function(reg) {
        var _this = this;
        return function() {
            return _this.match(reg);
        };
    },
    _createAtRule: function(name) {
        var reg = new RegExp('^@' + name + '\\s*([^;]+);');
        return function() {
            var matched = this.match(reg);
            if (!matched) return;
            var ret = {
                type: name
            };
            ret[name] = trim(matched[1]);
            return ret;
        };
    }
});
var Compiler = Class({
    initialize: function Compiler(input) {
        var options =
            arguments.length > 1 && arguments[1] !== undefined
                ? arguments[1]
                : {};
        defaults(options, {
            indent: '  '
        });
        this.input = input;
        this.indentLevel = 0;
        this.indentation = options.indent;
    },
    compile: function() {
        return this.stylesheet(this.input);
    },
    stylesheet: function(node) {
        return this.mapVisit(node.rules, '\n\n');
    },
    media: function(node) {
        return (
            '@media ' +
            node.media +
            ' {\n' +
            this.indent(1) +
            this.mapVisit(node.rules, '\n\n') +
            this.indent(-1) +
            '\n}'
        );
    },
    keyframes: function(node) {
        return (
            '@'.concat(node.vendor, 'keyframes ') +
            node.name +
            ' {\n' +
            this.indent(1) +
            this.mapVisit(node.keyframes, '\n') +
            this.indent(-1) +
            '\n}'
        );
    },
    supports: function(node) {
        return (
            '@supports ' +
            node.supports +
            ' {\n' +
            this.indent(1) +
            this.mapVisit(node.rules, '\n\n') +
            this.indent(-1) +
            '\n}'
        );
    },
    keyframe: function(node) {
        return this.rule(node);
    },
    mapVisit: function(nodes, delimiter) {
        var str = '';
        for (var i = 0, len = nodes.length; i < len; i++) {
            var node = nodes[i];
            str += this[camelCase(node.type)](node);
            if (delimiter && i < len - 1) str += delimiter;
        }
        return str;
    },
    fontFace: function(node) {
        return (
            '@font-face {\n' +
            this.indent(1) +
            this.mapVisit(node.declarations, '\n') +
            this.indent(-1) +
            '\n}'
        );
    },
    rule: function(node) {
        return (
            this.indent() +
            node.selector +
            ' {\n' +
            this.indent(1) +
            this.mapVisit(node.declarations, '\n') +
            this.indent(-1) +
            '\n' +
            this.indent() +
            '}'
        );
    },
    declaration: function(node) {
        return this.indent() + node.property + ': ' + node.value + ';';
    },
    import: function(node) {
        return '@import '.concat(node.import, ';');
    },
    charset: function(node) {
        return '@charset '.concat(node.charset, ';');
    },
    namespace: function(node) {
        return '@namespace '.concat(node.namespace, ';');
    },
    indent: function(level) {
        if (level) {
            this.indentLevel += level;
            return '';
        }
        return repeat(this.indentation, this.indentLevel);
    }
});
var stripCmt = function(str) {
    return str.replace(regComments, '');
};
 
module.exports = exports;