ability to pass some variables to toCSS in the form of a hash

This commit is contained in:
cloudhead 2010-06-30 13:20:11 +02:00
parent 7565305c4d
commit 4ce7a44bf2

View File

@ -254,18 +254,43 @@ less.Parser = function Parser(env) {
root.toCSS = (function (toCSS) {
var line, lines, column;
return function (options) {
return function (options, variables) {
var frames = [];
options = options || {};
//
// Allows setting variables with a hash, so:
//
// `{ color: new(tree.Color)('#f01') }` will become:
//
// new(tree.Rule)('@color',
// new(tree.Value)([
// new(tree.Expression)([
// new(tree.Color)('#f01')
// ])
// ])
// )
//
if (typeof(variables) === 'object' && !Array.isArray(variables)) {
variables = Object.keys(variables).map(function (k) {
var value = variables[k];
if (! (value instanceof tree.Value)) {
if (! (value instanceof tree.Expression)) {
value = new(tree.Expression)([value]);
}
value = new(tree.Value)([value]);
}
return new(tree.Rule)('@' + k, value, false, 0);
});
frames = [new(tree.Ruleset)(null, variables)];
}
try {
var css = toCSS.call(this, [], {
frames: [],
frames: frames,
compress: options.compress || false
});
if (options.compress) {
return css.replace(/(\s)+/g, "$1");
} else {
return css;
}
} catch (e) {
lines = input.split('\n');
line = getLine(e.index);
@ -291,6 +316,11 @@ less.Parser = function Parser(env) {
]
};
}
if (options.compress) {
return css.replace(/(\s)+/g, "$1");
} else {
return css;
}
function getLine(index) {
return (input.slice(0, index).match(/\n/g) || "").length;