From 4ce7a44bf2870eda269e2c26e805f4e18147cddc Mon Sep 17 00:00:00 2001 From: cloudhead Date: Wed, 30 Jun 2010 13:20:11 +0200 Subject: [PATCH] ability to pass some variables to toCSS in the form of a hash --- lib/less/parser.js | 44 +++++++++++++++++++++++++++++++++++++------- 1 file changed, 37 insertions(+), 7 deletions(-) diff --git a/lib/less/parser.js b/lib/less/parser.js index 14b7fba..f6d9f5c 100644 --- a/lib/less/parser.js +++ b/lib/less/parser.js @@ -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;