From 1bee790aee61376287abc83bd96898e5e17ae4eb Mon Sep 17 00:00:00 2001 From: ben hockey Date: Fri, 21 Oct 2011 15:09:51 -0400 Subject: [PATCH] mixins: don't duplicate rules with the same name (fixes #49) - this maintains the behavior where all mixins that matched the call are applied - if the result of applying the mixins produces rules with the same name then the last rule with that name wins - an alternative approach would be to only apply the last mixin that matches the call --- lib/less/tree/mixin.js | 11 +++++++++-- test/css/mixins-import.css | 13 +++++++++++-- test/less/import/mixins-import-vars.less | 1 + test/less/mixins-import.less | 11 ++++++++++- 4 files changed, 31 insertions(+), 5 deletions(-) diff --git a/lib/less/tree/mixin.js b/lib/less/tree/mixin.js index 24cb8e4c0..3376de416 100644 --- a/lib/less/tree/mixin.js +++ b/lib/less/tree/mixin.js @@ -8,7 +8,7 @@ tree.mixin.Call = function (elements, args, index) { }; tree.mixin.Call.prototype = { eval: function (env) { - var mixins, args, rules = [], match = false; + var mixins, args, rules = [], match = false, names = {}; for (var i = 0; i < env.frames.length; i++) { if ((mixins = env.frames[i].find(this.selector)).length > 0) { @@ -25,7 +25,14 @@ tree.mixin.Call.prototype = { } } if (match) { - return rules; + // filter out rules with the same name. let the last one win + return rules.reduceRight(function (reduced, rule) { + if (!names[rule.name]) { + reduced.unshift(rule); + names[rule.name] = 1; + } + return reduced; + }, []); } else { throw { message: 'No matching definition was found for `' + this.selector.toCSS().trim() + '(' + diff --git a/test/css/mixins-import.css b/test/css/mixins-import.css index 708091bfd..3bb5956a1 100644 --- a/test/css/mixins-import.css +++ b/test/css/mixins-import.css @@ -1,6 +1,15 @@ .a { - mixed: 20; + foo: 'bar'; + mixed: 25; + main: 'yes'; } .b { - mixed: 20; + foo: 'bar'; + mixed: 25; + main: 'yes'; +} +#main { + foo: 'bar'; + mixed: 25; + main: 'yes'; } diff --git a/test/less/import/mixins-import-vars.less b/test/less/import/mixins-import-vars.less index d4f51a3ba..a9810a107 100644 --- a/test/less/import/mixins-import-vars.less +++ b/test/less/import/mixins-import-vars.less @@ -1,3 +1,4 @@ .mixin() { mixed: 20; + foo: 'bar'; } \ No newline at end of file diff --git a/test/less/mixins-import.less b/test/less/mixins-import.less index 42971c4b1..cd7fdf75e 100644 --- a/test/less/mixins-import.less +++ b/test/less/mixins-import.less @@ -1,2 +1,11 @@ @import "import/mixins-import-a"; -@import "import/mixins-import-b"; \ No newline at end of file +@import "import/mixins-import-b"; + +.mixin() { + mixed: 25; + main: 'yes'; +} + +#main { + .mixin +} \ No newline at end of file