add evalEnv class and strictMaths option

This commit is contained in:
Luke Page
2013-01-25 20:05:37 +00:00
parent 531195c92d
commit 988d378922
9 changed files with 62 additions and 44 deletions

View File

@@ -55,7 +55,7 @@ function initRunningMode(){
if (less.watchMode) {
loadStyleSheets(function (e, root, _, sheet, env) {
if (root) {
createCSS(root.toCSS(), sheet, env.lastModified);
createCSS(root.toCSS(less), sheet, env.lastModified);
}
});
}
@@ -104,7 +104,7 @@ less.modifyVars = function(record) {
((record[name].slice(-1) === ';')? record[name] : record[name] +';');
}
new(less.Parser)(new less.tree.parseEnv(less)).parse(str, function (e, root) {
createCSS(root.toCSS(), less.sheets[less.sheets.length - 1]);
createCSS(root.toCSS(less), less.sheets[less.sheets.length - 1]);
});
};
@@ -117,7 +117,7 @@ less.refresh = function (reload) {
log("loading " + sheet.href + " from cache.");
} else {
log("parsed " + sheet.href + " successfully.");
createCSS(root.toCSS(), sheet, env.lastModified);
createCSS(root.toCSS(less), sheet, env.lastModified);
}
log("css for " + sheet.href + " generated in " + (new(Date) - endTime) + 'ms');
(env.remaining === 0) && log("css generated in " + (new(Date) - startTime) + 'ms');
@@ -138,7 +138,7 @@ function loadStyles() {
env.filename = document.location.href.replace(/#.*$/, '');
new(less.Parser)(env).parse(styles[i].innerHTML || '', function (e, cssAST) {
var css = cssAST.toCSS();
var css = cssAST.toCSS(less);
var style = styles[i];
style.type = 'text/css';
if (style.styleSheet) {

View File

@@ -31,25 +31,33 @@
return env;
};
//todo - do the same for the eval env and the toCSS env
//tree.evalEnv = function(options) {
//};
var evalCopyProperties = [
'compress', // whether to compress
'strictMaths' // whether maths has to be within parenthesis
];
//tree.evalEnv.prototype.inParenthesis = function () {
// if (!this.parensStack) {
// this.parensStack = [];
// }
// this.parensStack.push(true);
//};
tree.evalEnv = function(options, frames) {
copyFromOriginal(options, this, evalCopyProperties);
//tree.evalEnv.prototype.outOfParenthesis = function () {
// this.parensStack.pop();
//};
this.frames = frames || [];
};
//tree.evalEnv.prototype.isMathsOn = function () {
// return this.parensStack && this.parensStack.length;
//};
tree.evalEnv.prototype.inParenthesis = function () {
if (!this.parensStack) {
this.parensStack = [];
}
this.parensStack.push(true);
};
tree.evalEnv.prototype.outOfParenthesis = function () {
this.parensStack.pop();
};
tree.evalEnv.prototype.isMathsOn = function () {
return this.strictMaths === false ? true : (this.parensStack && this.parensStack.length);
};
//todo - do the same for the toCSS env
//tree.toCSSEnv = function (options) {
//};

View File

@@ -51,6 +51,9 @@ var lessc_helper = {
sys.puts(" -rp, --rootpath Set rootpath for url rewriting in relative imports and urls.");
sys.puts(" Works with or withour the relative-urls option.");
sys.puts(" -ru, --relative-urls re-write relative urls to the base less file.");
sys.puts(" -sm, --strict-maths-off Make maths not require brackets, which is similar behaviour");
sys.puts(" to before 1.4. This option is for compatability and will be");
sys.puts(" removed in the future.");
sys.puts("");
sys.puts("Report bugs to: http://github.com/cloudhead/less.js/issues");
sys.puts("Home page: <http://lesscss.org/>");

View File

@@ -381,7 +381,8 @@ less.Parser = function Parser(env) {
var line, lines, column;
return function (options, variables) {
var frames = [], importError;
var importError,
evalEnv = new tree.evalEnv(options);
options = options || {};
//
@@ -409,11 +410,11 @@ less.Parser = function Parser(env) {
}
return new(tree.Rule)('@' + k, value, false, 0);
});
frames = [new(tree.Ruleset)(null, variables)];
evalEnv.frames = [new(tree.Ruleset)(null, variables)];
}
try {
var css = evaluate.call(this, { frames: frames, compress: options.compress || false })
var css = evaluate.call(this, evalEnv)
.toCSS([], { compress: options.compress || false, dumpLineNumbers: env.dumpLineNumbers });
} catch (e) {
throw new(LessError)(e, env);

View File

@@ -1,14 +1,12 @@
(function (tree) {
tree.Expression = function (value) { this.value = value };
tree.Expression = function (value) { this.value = value; };
tree.Expression.prototype = {
eval: function (env) {
var returnValue;
if (this.parens && !this.parensInOp) {
if (!env.parensStack) {
env.parensStack = [];
}
env.parensStack.push(true);
var returnValue,
inParenthesis = this.parens && !this.parensInOp;
if (inParenthesis) {
env.inParenthesis();
}
if (this.value.length > 1) {
returnValue = new(tree.Expression)(this.value.map(function (e) {
@@ -19,10 +17,10 @@ tree.Expression.prototype = {
} else {
returnValue = this;
}
if (this.parens && !this.parensInOp) {
env.parensStack.pop();
if (inParenthesis) {
env.outOfParenthesis();
}
if (this.parens && this.parensInOp && !(env.parensStack && env.parensStack.length)) {
if (this.parens && this.parensInOp && !(env.isMathsOn())) {
returnValue = new(tree.Paren)(returnValue);
}
return returnValue;

View File

@@ -167,7 +167,7 @@ tree.mixin.Definition.prototype = {
eval: function (env, args, important) {
var _arguments = [],
mixinFrames = this.frames.concat(env.frames),
frame = this.evalParams(env, {frames: mixinFrames}, args, _arguments),
frame = this.evalParams(env, new(tree.evalEnv)(env, mixinFrames), args, _arguments),
context, rules, start, ruleset;
frame.rules.unshift(new(tree.Rule)('@arguments', new(tree.Expression)(_arguments).eval(env)));
@@ -175,17 +175,19 @@ tree.mixin.Definition.prototype = {
rules = important ?
this.parent.makeImportant.apply(this).rules : this.rules.slice(0);
ruleset = new(tree.Ruleset)(null, rules).eval({
frames: [this, frame].concat(mixinFrames),
compress: env.compress
});
ruleset = new(tree.Ruleset)(null, rules).eval(new(tree.evalEnv)(env,
[this, frame].concat(mixinFrames)));
ruleset.originalRuleset = this;
return ruleset;
},
matchCondition: function (args, env) {
if (this.condition && !this.condition.eval({
frames: [this.evalParams(env, {frames: this.frames.concat(env.frames)}, args, [])].concat(env.frames)
})) { return false }
if (this.condition && !this.condition.eval(
new(tree.evalEnv)(env,
[this.evalParams(env, new(tree.evalEnv)(env, this.frames.concat(env.frames)), args, [])]
.concat(env.frames)))) {
return false;
}
return true;
},
matchArgs: function (args, env) {

View File

@@ -8,7 +8,7 @@ tree.Negative.prototype = {
return '-' + this.value.toCSS(env);
},
eval: function (env) {
if (env.parensStack && env.parensStack.length) {
if (env.isMathsOn()) {
return (new(tree.Operation)('*', [new(tree.Dimension)(-1), this.value])).eval(env);
}
return new(tree.Negative)(this.value.eval(env));

View File

@@ -9,7 +9,7 @@ tree.Operation.prototype.eval = function (env) {
b = this.operands[1].eval(env),
temp;
if (env.parensStack && env.parensStack.length) {
if (env.isMathsOn()) {
if (a instanceof tree.Dimension && b instanceof tree.Color) {
if (this.op === '*' || this.op === '+') {
temp = b, b = a, a = temp;