From 7155f44f4383dc1555593d16529305d7ea891b4b Mon Sep 17 00:00:00 2001 From: cloudhead Date: Tue, 4 May 2010 20:37:06 -0400 Subject: [PATCH] be more flexible, pattern matching --- lib/less/parser.js | 5 ++++- lib/less/tree/mixin.js | 12 +++++++----- test/css/mixins-pattern.css | 22 ++++++++++++++++++++++ test/less/mixins-pattern.less | 32 +++++++++++++++++++++++++++++--- 4 files changed, 62 insertions(+), 9 deletions(-) diff --git a/lib/less/parser.js b/lib/less/parser.js index 8480f90..25c6da5 100644 --- a/lib/less/parser.js +++ b/lib/less/parser.js @@ -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 } } diff --git a/lib/less/tree/mixin.js b/lib/less/tree/mixin.js index 30fb842..82d6f44 100644 --- a/lib/less/tree/mixin.js +++ b/lib/less/tree/mixin.js @@ -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; } } diff --git a/test/css/mixins-pattern.css b/test/css/mixins-pattern.css index 9672bc5..2d68bfa 100644 --- a/test/css/mixins-pattern.css +++ b/test/css/mixins-pattern.css @@ -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; +} diff --git a/test/less/mixins-pattern.less b/test/less/mixins-pattern.less index 57d0f06..445fa0e 100644 --- a/test/less/mixins-pattern.less +++ b/test/less/mixins-pattern.less @@ -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); +}