mirror of
https://github.com/less/less.js.git
synced 2026-04-09 03:00:20 -04:00
This adds an additional vars parameter to the parse method. It allows a user to inject variables into a less string before compiling.
This commit is contained in:
@@ -595,16 +595,6 @@ function initRunningMode(){
|
||||
}
|
||||
}
|
||||
|
||||
function serializeVars(vars) {
|
||||
var s = "";
|
||||
|
||||
for (var name in vars) {
|
||||
s += ((name.slice(0,1) === '@')? '' : '@') + name +': '+
|
||||
((vars[name].slice(-1) === ';')? vars[name] : vars[name] +';');
|
||||
}
|
||||
|
||||
return s;
|
||||
}
|
||||
|
||||
|
||||
//
|
||||
@@ -649,7 +639,7 @@ for (var i = 0; i < links.length; i++) {
|
||||
// CSS without reloading less-files
|
||||
//
|
||||
less.modifyVars = function(record) {
|
||||
less.refresh(false, serializeVars(record));
|
||||
less.refresh(false, less.Parser.serializeVars(record));
|
||||
};
|
||||
|
||||
less.refresh = function (reload, newVars) {
|
||||
@@ -677,7 +667,7 @@ less.refresh = function (reload, newVars) {
|
||||
};
|
||||
|
||||
if (less.globalVars) {
|
||||
varsPre = serializeVars(less.globalVars) + "\n";
|
||||
varsPre = less.Parser.serializeVars(less.globalVars) + "\n";
|
||||
}
|
||||
|
||||
less.refreshStyles = loadStyles;
|
||||
|
||||
@@ -295,13 +295,17 @@ less.Parser = function Parser(env) {
|
||||
imports: imports,
|
||||
//
|
||||
// Parse an input string into an abstract syntax tree,
|
||||
// call `callback` when done.
|
||||
// @param str A string containing 'less' markup
|
||||
// @param callback call `callback` when done.
|
||||
// @param [vars] An optional map (key, value) of variables to apply
|
||||
//
|
||||
parse: function (str, callback) {
|
||||
parse: function (str, callback, vars) {
|
||||
var root, line, lines, error = null;
|
||||
|
||||
i = j = current = furthest = 0;
|
||||
input = str.replace(/\r\n/g, '\n');
|
||||
|
||||
vars = vars ? less.Parser.serializeVars(vars) + '\n' : '';
|
||||
input = (vars + str).replace(/\r\n/g, '\n');
|
||||
|
||||
// Remove potential UTF Byte Order Mark
|
||||
input = input.replace(/^\uFEFF/, '');
|
||||
@@ -1726,4 +1730,17 @@ less.Parser = function Parser(env) {
|
||||
}
|
||||
};
|
||||
};
|
||||
less.Parser.serializeVars = function(vars) {
|
||||
var s = '';
|
||||
|
||||
for (var name in vars) {
|
||||
if (Object.hasOwnProperty.call(vars, name)) {
|
||||
var value = vars[name];
|
||||
s += ((name[0] === '@') ? '' : '@') + name +': '+ value +
|
||||
((('' + value).slice(-1) === ';') ? '' : ';');
|
||||
}
|
||||
}
|
||||
|
||||
return s;
|
||||
};
|
||||
|
||||
|
||||
9
test/css/parse/extended.css
Normal file
9
test/css/parse/extended.css
Normal file
@@ -0,0 +1,9 @@
|
||||
#header {
|
||||
color: #333333;
|
||||
border-left: 1px;
|
||||
border-right: 2px;
|
||||
}
|
||||
#footer {
|
||||
color: #114411;
|
||||
border-color: #7d2717;
|
||||
}
|
||||
3
test/css/parse/simple.css
Normal file
3
test/css/parse/simple.css
Normal file
@@ -0,0 +1,3 @@
|
||||
.class {
|
||||
color: #ff0000;
|
||||
}
|
||||
@@ -29,6 +29,8 @@ less.tree.functions._color = function (str) {
|
||||
console.log("\n" + stylize("LESS", 'underline') + "\n");
|
||||
|
||||
runTestSet({strictMath: true, relativeUrls: true, silent: true});
|
||||
runTestSet({vars: true}, "parse/",
|
||||
null, null, null, function(name) { return path.join('test/less/', name) + '.json'; });
|
||||
runTestSet({strictMath: true, strictUnits: true}, "errors/",
|
||||
testErrors, null, getErrorPathReplacementFunction("errors"));
|
||||
runTestSet({strictMath: true, strictUnits: true, javascriptEnabled: false}, "no-js-errors/",
|
||||
@@ -122,10 +124,14 @@ function runTestSet(options, foldername, verifyFunction, nameModifier, doReplace
|
||||
doReplacements = globalReplacements;
|
||||
}
|
||||
|
||||
function getBasename(file) {
|
||||
return foldername + path.basename(file, '.less');
|
||||
}
|
||||
|
||||
fs.readdirSync(path.join('test/less/', foldername)).forEach(function (file) {
|
||||
if (! /\.less/.test(file)) { return; }
|
||||
|
||||
var name = foldername + path.basename(file, '.less');
|
||||
var name = getBasename(file);
|
||||
|
||||
if (oneTestOnly && name !== oneTestOnly) {
|
||||
return;
|
||||
@@ -143,6 +149,12 @@ function runTestSet(options, foldername, verifyFunction, nameModifier, doReplace
|
||||
options.sourceMapRootpath = "testweb/";
|
||||
}
|
||||
|
||||
if (options.vars) {
|
||||
options.getVars = function(file) {
|
||||
return JSON.parse(fs.readFileSync(getFilename(getBasename(file)), 'utf8'));
|
||||
};
|
||||
}
|
||||
|
||||
toCSS(options, path.join('test/less/', foldername + file), function (err, less) {
|
||||
|
||||
if (verifyFunction) {
|
||||
@@ -232,7 +244,9 @@ function toCSS(options, path, callback) {
|
||||
options.filename = require('path').resolve(process.cwd(), path);
|
||||
options.optimization = options.optimization || 0;
|
||||
|
||||
new(less.Parser)(options).parse(str, function (err, tree) {
|
||||
var parser = new(less.Parser)(options);
|
||||
var args = [str];
|
||||
args.push(function (err, tree) {
|
||||
if (err) {
|
||||
callback(err);
|
||||
} else {
|
||||
@@ -244,6 +258,10 @@ function toCSS(options, path, callback) {
|
||||
}
|
||||
}
|
||||
});
|
||||
if (options.vars) {
|
||||
args.push(options.getVars(path));
|
||||
}
|
||||
parser.parse.apply(parser, args);
|
||||
});
|
||||
}
|
||||
|
||||
|
||||
5
test/less/parse/extended.json
Normal file
5
test/less/parse/extended.json
Normal file
@@ -0,0 +1,5 @@
|
||||
{
|
||||
"the-border": "1px",
|
||||
"base-color": "#111",
|
||||
"red": "#842210"
|
||||
}
|
||||
9
test/less/parse/extended.less
Normal file
9
test/less/parse/extended.less
Normal file
@@ -0,0 +1,9 @@
|
||||
#header {
|
||||
color: (@base-color * 3);
|
||||
border-left: @the-border;
|
||||
border-right: (@the-border * 2);
|
||||
}
|
||||
#footer {
|
||||
color: (@base-color + #003300);
|
||||
border-color: desaturate(@red, 10%);
|
||||
}
|
||||
3
test/less/parse/simple.json
Normal file
3
test/less/parse/simple.json
Normal file
@@ -0,0 +1,3 @@
|
||||
{
|
||||
"my-color": "red"
|
||||
}
|
||||
3
test/less/parse/simple.less
Normal file
3
test/less/parse/simple.less
Normal file
@@ -0,0 +1,3 @@
|
||||
.class {
|
||||
color: @my-color;
|
||||
}
|
||||
Reference in New Issue
Block a user