Sha256: f6b406e859e779e638d0971f315c00f5fe1c850245692888cff00e7d6e34df61

Contents?: true

Size: 1.54 KB

Versions: 8

Compression:

Stored size: 1.54 KB

Contents

(function (tree) {

tree.JavaScript = function (string, index, escaped) {
    this.escaped = escaped;
    this.expression = string;
    this.index = index;
};
tree.JavaScript.prototype = {
    eval: function (env) {
        var result,
            that = this,
            context = {};

        var expression = this.expression.replace(/@\{([\w-]+)\}/g, function (_, name) {
            return tree.jsify(new(tree.Variable)('@' + name, that.index).eval(env));
        });

        try {
            expression = new(Function)('return (' + expression + ')');
        } catch (e) {
            throw { message: "JavaScript evaluation error: `" + expression + "`" ,
                    index: this.index };
        }

        for (var k in env.frames[0].variables()) {
            context[k.slice(1)] = {
                value: env.frames[0].variables()[k].value,
                toJS: function () {
                    return this.value.eval(env).toCSS();
                }
            };
        }

        try {
            result = expression.call(context);
        } catch (e) {
            throw { message: "JavaScript evaluation error: '" + e.name + ': ' + e.message + "'" ,
                    index: this.index };
        }
        if (typeof(result) === 'string') {
            return new(tree.Quoted)('"' + result + '"', result, this.escaped, this.index);
        } else if (Array.isArray(result)) {
            return new(tree.Anonymous)(result.join(', '));
        } else {
            return new(tree.Anonymous)(result);
        }
    }
};

})(require('less/tree'));

Version data entries

8 entries across 8 versions & 1 rubygems

Version Path
less-2.0.7 lib/less/js/lib/less/tree/javascript.js
less-2.0.6 lib/less/js/lib/less/tree/javascript.js
less-2.0.5 lib/less/js/lib/less/tree/javascript.js
less-2.0.4 lib/less/js/lib/less/tree/javascript.js
less-2.0.3 lib/less/js/lib/less/tree/javascript.js
less-2.0.2 lib/less/js/lib/less/tree/javascript.js
less-2.0.1 lib/less/js/lib/less/tree/javascript.js
less-2.0.0 lib/less/js/lib/less/tree/javascript.js