be more flexible, pattern matching

This commit is contained in:
cloudhead 2010-05-04 20:37:06 -04:00
parent 0ec53f74e0
commit 7155f44f43
4 changed files with 62 additions and 9 deletions

View File

@ -374,10 +374,13 @@ less.Parser = function Parser(env) {
if (name) { return new(tree.Call)(name[1], args) }
},
wildcard: function () {
if ($('*')) { return { wildcard: true } }
},
arguments: function () {
var args = [], arg;
while (arg = $(this.expression)) {
while (arg = $(this.entities.wildcard) || $(this.expression)) {
args.push(arg);
if (! $(',')) { break }
}

View File

@ -32,8 +32,8 @@ tree.mixin.Definition = function MixinDefinition(name, params, rules) {
this.rules = rules;
this._lookups = {};
this.required = params.reduce(function (count, p) {
if (p.name && p.value) { return count }
else { return count + 1 }
if (p.name && !p.value) { return count + 1 }
else { return count }
}, 0);
};
tree.mixin.Definition.prototype = {
@ -61,13 +61,15 @@ tree.mixin.Definition.prototype = {
match: function (args, env) {
var argsLength = (args && args.length) || 0;
if (argsLength < this.required || argsLength > this.arity) {
if (argsLength < this.required) {
return false;
}
for (var i = 0; i < argsLength; i++) {
for (var i = 0; i < Math.min(argsLength, this.arity); i++) {
if (!this.params[i].name) {
if (args[i].toCSS(env) != this.params[i].value.toCSS(env)) {
if (args[i].wildcard) { continue }
else if (args[i].toCSS(env) != this.params[i].value.toCSS(env)) {
return false;
}
}

View File

@ -5,16 +5,25 @@
three: 3;
}
.one {
zero: 0;
one: 1;
one-req: 1;
two: 2;
three: 3;
}
.two {
zero: 0;
one: 1;
one-req: 1;
two: 2;
three: 3;
}
.three {
zero: 0;
one: 1;
one-req: 1;
two: 2;
three-req: 3;
three: 3;
}
.left {
@ -31,3 +40,16 @@
color: black;
border-left: 4px;
}
.only-right {
both: 330;
right: 33;
}
.only-left {
both: 330;
left: 33;
}
.left-right {
both: 330;
left: 33;
right: 33;
}

View File

@ -8,7 +8,11 @@
one-req: 1;
}
.mixin (@a: 1px, @b: 2px) {
two: 2;
two: 2;
}
.mixin (@a, @b, @c) {
three-req: 3;
}
.mixin (@a: 1px, @b: 2px, @c: 3px) {
@ -65,9 +69,31 @@
}
.border-right {
.border(right, 4px);
.border(right, 4px);
}
.border-left {
.border(left, 4px);
.border(left, 4px);
}
//
.border-radius (@r) {
both: @r * 10;
}
.border-radius (@r, left) {
left: @r;
}
.border-radius (@r, right) {
right: @r;
}
.only-right {
.border-radius(33, right);
}
.only-left {
.border-radius(33, left);
}
.left-right {
.border-radius(33);
}