๐Ÿ“ฆ EdwonLim / node-less

๐Ÿ“„ quoted.js ยท 52 lines
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(function (module) {

    var Variable = require('./variable.js'),
        JavaScript = require('./javascript.js');

    var Quoted = function (str, content, escaped, index, currentFileInfo) {
        this.escaped = escaped;
        this.value = content || '';
        this.quote = str.charAt(0);
        this.index = index;
        this.currentFileInfo = currentFileInfo;
    };

    Quoted.prototype = {
        type: "Quoted",
        toCSS: function () {
            if (this.escaped) {
                return this.value;
            } else {
                return this.quote + this.value + this.quote;
            }
        },
        eval: function (env) {
            var that = this;
            var value = this.value.replace(/`([^`]+)`/g, function (_, exp) {
                return new JavaScript(exp, that.index, true).eval(env).value;
            }).replace(/@\{([\w-]+)\}/g, function (_, name) {
                var v = new Variable('@' + name, that.index, that.currentFileInfo).eval(env, true);
                return (v instanceof Quoted) ? v.value : v.toCSS();
            });
            return new Quoted(this.quote + value + this.quote, value, this.escaped, this.index);
        },
        compare: function (x) {
            if (!x.toCSS) {
                return -1;
            }

            var left = this.toCSS(),
                right = x.toCSS();

            if (left === right) {
                return 0;
            }

            return left < right ? -1 : 1;
        }
    };

    module.exports = Quoted;

})(module);