Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Tree functions cleanup + CSS Guards default error. #1757

Merged
merged 2 commits into from
Dec 22, 2013
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
101 changes: 67 additions & 34 deletions lib/less/functions.js
Original file line number Diff line number Diff line change
Expand Up @@ -232,12 +232,6 @@ tree.functions = {
str = str.replace(/%%/g, '%');
return new(tree.Quoted)('"' + str + '"', str);
},
default: function () {
if (this.default.enabled) {
return this.default.value
? tree.True : tree.False;
}
},
unit: function (val, unit) {
if(!(val instanceof tree.Dimension)) {
throw { type: "Argument", message: "the first argument to unit must be a number" + (val instanceof tree.Operation ? ". Have you forgotten parenthesis?" : "") };
Expand All @@ -249,7 +243,7 @@ tree.functions = {
},
round: function (n, f) {
var fraction = typeof(f) === "undefined" ? 0 : f.value;
return this._math(function(num) { return num.toFixed(fraction); }, null, n);
return _math(function(num) { return num.toFixed(fraction); }, null, n);
},
pi: function () {
return new(tree.Dimension)(Math.PI);
Expand All @@ -267,15 +261,6 @@ tree.functions = {

return new(tree.Dimension)(Math.pow(x.value, y.value), x.unit);
},
_math: function (fn, unit, n) {
if (n instanceof tree.Dimension) {
return new(tree.Dimension)(fn(parseFloat(n.value)), unit == null ? n.unit : unit);
} else if (typeof(n) === 'number') {
return fn(n);
} else {
throw { type: "Argument", message: "argument must be a number" };
}
},
_minmax: function (isMin, args) {
args = Array.prototype.slice.call(args);
switch(args.length) {
Expand Down Expand Up @@ -565,22 +550,36 @@ tree._mime = {
}
};

var mathFunctions = [{name:"ceil"}, {name:"floor"}, {name: "sqrt"}, {name:"abs"},
{name:"tan", unit: ""}, {name:"sin", unit: ""}, {name:"cos", unit: ""},
{name:"atan", unit: "rad"}, {name:"asin", unit: "rad"}, {name:"acos", unit: "rad"}],
createMathFunction = function(name, unit) {
return function(n) {
if (unit != null) {
n = n.unify();
}
return this._math(Math[name], unit, n);
};
};
// Math

var mathFunctions = {
// name, unit
ceil: null,
floor: null,
sqrt: null,
abs: null,
tan: "",
sin: "",
cos: "",
atan: "rad",
asin: "rad",
acos: "rad"
};

for(var i = 0; i < mathFunctions.length; i++) {
tree.functions[mathFunctions[i].name] = createMathFunction(mathFunctions[i].name, mathFunctions[i].unit);
function _math(fn, unit, n) {
if (!(n instanceof tree.Dimension)) {
throw { type: "Argument", message: "argument must be a number" };
}
if (unit == null) {
unit = n.unit;
} else {
n = n.unify();
}
return new(tree.Dimension)(fn(parseFloat(n.value)), unit);
}

// ~ End of Math

// Color Blending
// ref: http://www.w3.org/TR/compositing-1

Expand Down Expand Up @@ -645,13 +644,47 @@ var colorBlendMode = {
}
};

function colorBlendInit() {
for (var f in colorBlendMode) {
tree.functions[f] = colorBlend.bind(null, colorBlendMode[f]);
// ~ End of Color Blending

tree.defaultFunc = {
eval: function () {
var v = this.value_, e = this.error_;
if (e) {
throw e;
}
if (v != null) {
return v ? tree.True : tree.False;
}
},
value: function (v) {
this.value_ = v;
},
error: function (e) {
this.error_ = e;
},
reset: function () {
this.value_ = this.error_ = null;
}
} colorBlendInit();
};

// ~ End of Color Blending
function initFunctions() {
var f, tf = tree.functions;

// math
for (f in mathFunctions) {
tf[f] = _math.bind(null, Math[f], mathFunctions[f]);
}

// color blending
for (f in colorBlendMode) {
tf[f] = colorBlend.bind(null, colorBlendMode[f]);
}

// default
f = tree.defaultFunc;
tf.default = f.eval.bind(f);

} initFunctions();

function hsla(color) {
return tree.functions.hsla(color.h, color.s, color.l, color.a);
Expand Down
7 changes: 3 additions & 4 deletions lib/less/tree/mixin.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ tree.mixin.Call.prototype = {
},
eval: function (env) {
var mixins, mixin, args, rules = [], match = false, i, m, f, isRecursive, isOneFound, rule;
var candidates = [], candidate, conditionResult = [], defaultFunc = tree.functions.default, defaultUsed = false;
var candidates = [], candidate, conditionResult = [], defaultFunc = tree.defaultFunc, defaultUsed = false;

args = this.arguments && this.arguments.map(function (a) {
return { name: a.name, value: a.value.eval(env) };
Expand All @@ -29,7 +29,6 @@ tree.mixin.Call.prototype = {
for (i = 0; i < env.frames.length; i++) {
if ((mixins = env.frames[i].find(this.selector)).length > 0) {
isOneFound = true;
defaultFunc.enabled = true;

// To make `default()` function independent of definition order we have two "subpasses" here.
// At first we evaluate each guard *twice* (with `default() == true` and `default() == false`),
Expand All @@ -54,7 +53,7 @@ tree.mixin.Call.prototype = {

if (mixin.matchCondition) {
for (f = 0; f < 2; f++) {
defaultFunc.value = f;
defaultFunc.value(f);
conditionResult[f] = mixin.matchCondition(args, env);
}
if (conditionResult[0] || conditionResult[1]) {
Expand Down Expand Up @@ -86,7 +85,7 @@ tree.mixin.Call.prototype = {
}
}

defaultFunc.enabled = false;
defaultFunc.reset();

for (m in candidates) {
candidate = candidates[m];
Expand Down
8 changes: 7 additions & 1 deletion lib/less/tree/ruleset.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,12 +19,18 @@ tree.Ruleset.prototype = {
}
},
eval: function (env) {
var thisSelectors = this.selectors, selectors, selCnt, i;
var thisSelectors = this.selectors, selectors,
selCnt, i, defaultFunc = tree.defaultFunc;
if (thisSelectors && (selCnt = thisSelectors.length)) {
selectors = [];
defaultFunc.error({
type: "Syntax",
message: "it is currently only allowed in parametric mixin guards,"
});
for (i = 0; i < selCnt; i++) {
selectors.push(thisSelectors[i].eval(env));
}
defaultFunc.reset();
}

var rules = this.rules ? this.rules.slice(0) : null,
Expand Down
6 changes: 6 additions & 0 deletions test/css/mixins-guards-default-func.css
Original file line number Diff line number Diff line change
Expand Up @@ -112,3 +112,9 @@ guard-default-multi-4 {
always: 2;
case: 2;
}
guard-default-scopes-3 {
3: when default;
}
guard-default-scopes-1 {
1: no condition;
}
4 changes: 4 additions & 0 deletions test/less/errors/css-guard-default-func.less
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@

selector when (default()) {
color: red;
}
4 changes: 4 additions & 0 deletions test/less/errors/css-guard-default-func.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
SyntaxError: error evaluating function `default`: it is currently only allowed in parametric mixin guards, in {path}css-guard-default-func.less on line 2, column 16:
1
2 selector when (default()) {
3 color: red;
20 changes: 20 additions & 0 deletions test/less/mixins-guards-default-func.less
Original file line number Diff line number Diff line change
Expand Up @@ -157,3 +157,23 @@ guard-default-multi-4 {
.m(1);
.m(2);
}

// default & scope

guard-default-scopes {
.s1() {.m(@v) {1: no condition}}
.s2() {.m(@v) when (@v) {2: when true}}
.s3() {.m(@v) when (default()) {3: when default}}

&-3 {
.s2();
.s3();
.m(false);
}

&-1 {
.s1();
.s3();
.m(false);
}
}