Skip to content

Commit

Permalink
support '!important' after mixin calls
Browse files Browse the repository at this point in the history
ex: `.mixin(4) !important;`
  • Loading branch information
cloudhead committed Jan 5, 2012
1 parent 1078c8b commit aab66a4
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 7 deletions.
8 changes: 6 additions & 2 deletions lib/less/parser.js
Original file line number Diff line number Diff line change
Expand Up @@ -722,7 +722,7 @@ less.Parser = function Parser(env) {
// selector for now.
//
call: function () {
var elements = [], e, c, args, index = i, s = input.charAt(i);
var elements = [], e, c, args, index = i, s = input.charAt(i), important = false;

if (s !== '.' && s !== '#') { return }

Expand All @@ -732,8 +732,12 @@ less.Parser = function Parser(env) {
}
$('(') && (args = $(this.entities.arguments)) && $(')');

if ($(this.important)) {
important = true;
}

if (elements.length > 0 && ($(';') || peek('}'))) {
return new(tree.mixin.Call)(elements, args, index);
return new(tree.mixin.Call)(elements, args, index, important);
}
},

Expand Down
16 changes: 11 additions & 5 deletions lib/less/tree/mixin.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
(function (tree) {

tree.mixin = {};
tree.mixin.Call = function (elements, args, index) {
tree.mixin.Call = function (elements, args, index, important) {
this.selector = new(tree.Selector)(elements);
this.arguments = args;
this.index = index;
this.important = important;
};
tree.mixin.Call.prototype = {
eval: function (env) {
Expand All @@ -17,7 +18,7 @@ tree.mixin.Call.prototype = {
if (mixins[m].match(args, env)) {
try {
Array.prototype.push.apply(
rules, mixins[m].eval(env, this.arguments).rules);
rules, mixins[m].eval(env, this.arguments, this.important).rules);
match = true;
} catch (e) {
throw { message: e.message, index: e.index, stack: e.stack, call: this.index };
Expand Down Expand Up @@ -80,15 +81,20 @@ tree.mixin.Definition.prototype = {
}
return frame;
},
eval: function (env, args) {
var frame = this.evalParams(env, args), context, _arguments = [];
eval: function (env, args, important) {
var frame = this.evalParams(env, args), context, _arguments = [], rules;

for (var i = 0; i < Math.max(this.params.length, args && args.length); i++) {
_arguments.push(args[i] || this.params[i].value);
}
frame.rules.unshift(new(tree.Rule)('@arguments', new(tree.Expression)(_arguments).eval(env)));

return new(tree.Ruleset)(null, this.rules.slice(0)).eval({
rules = important ?
this.rules.map(function (r) {
return new(tree.Rule)(r.name, r.value, '!important', r.index);
}) : this.rules.slice(0);

return new(tree.Ruleset)(null, rules).eval({
frames: [this, frame].concat(this.frames, env.frames)
});
},
Expand Down

0 comments on commit aab66a4

Please sign in to comment.