Skip to content

Commit

Permalink
mixins: don't duplicate rules with the same name (fixes less#49)
Browse files Browse the repository at this point in the history
 - 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
  • Loading branch information
neonstalwart committed Oct 24, 2011
1 parent 4990b2f commit 58d6769
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 5 deletions.
11 changes: 9 additions & 2 deletions lib/less/tree/mixin.js
Expand Up @@ -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) {
Expand All @@ -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() + '(' +
Expand Down
13 changes: 11 additions & 2 deletions 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';
}
1 change: 1 addition & 0 deletions test/less/import/mixins-import-vars.less
@@ -1,3 +1,4 @@
.mixin() {
mixed: 20;
foo: 'bar';
}
11 changes: 10 additions & 1 deletion test/less/mixins-import.less
@@ -1,2 +1,11 @@
@import "import/mixins-import-a";
@import "import/mixins-import-b";
@import "import/mixins-import-b";

.mixin() {
mixed: 25;
main: 'yes';
}

#main {
.mixin
}

0 comments on commit 58d6769

Please sign in to comment.