Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added support for default variables #1104

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 8 additions & 2 deletions lib/less/parser.js
Original file line number Diff line number Diff line change
Expand Up @@ -1155,7 +1155,7 @@ less.Parser = function Parser(env) {
}
},
rule: function () {
var name, value, c = input.charAt(i), important, match;
var name, value, c = input.charAt(i), important, isDefault, match;
save();

if (c === '.' || c === '#' || c === '&') { return }
Expand All @@ -1170,9 +1170,10 @@ less.Parser = function Parser(env) {
value = $(this.value);
}
important = $(this.important);
isDefault = $(this.isDefault);

if (value && $(this.end)) {
return new(tree.Rule)(name, value, important, memo);
return new(tree.Rule)(name, value, important, memo, null, isDefault);
} else {
furthest = i;
restore();
Expand Down Expand Up @@ -1391,6 +1392,11 @@ less.Parser = function Parser(env) {
return $(/^! *important/);
}
},
isDefault: function () {
if (input.charAt(i) === '!') {
return $(/^! *default/);
}
},
sub: function () {
var e;

Expand Down
15 changes: 11 additions & 4 deletions lib/less/tree/rule.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
(function (tree) {

tree.Rule = function (name, value, important, index, inline) {
tree.Rule = function (name, value, important, index, inline, isDefault) {
this.name = name;
this.value = (value instanceof tree.Value) ? value : new(tree.Value)([value]);
this.important = important ? ' ' + important.trim() : '';
Expand All @@ -9,7 +9,10 @@ tree.Rule = function (name, value, important, index, inline) {

if (name.charAt(0) === '@') {
this.variable = true;
} else { this.variable = false }
this.isDefault = isDefault;
} else {
this.variable = false;
}
};
tree.Rule.prototype.toCSS = function (env) {
if (this.variable) { return "" }
Expand All @@ -24,14 +27,18 @@ tree.Rule.prototype.eval = function (context) {
return new(tree.Rule)(this.name,
this.value.eval(context),
this.important,
this.index, this.inline);
this.index,
this.inline,
this.isDefault);
};

tree.Rule.prototype.makeImportant = function () {
return new(tree.Rule)(this.name,
this.value,
"!important",
this.index, this.inline);
this.index,
this.inline,
this.isDefault);
};

tree.Shorthand = function (a, b) {
Expand Down
4 changes: 3 additions & 1 deletion lib/less/tree/ruleset.js
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,9 @@ tree.Ruleset.prototype = {
else {
return this._variables = this.rules.reduce(function (hash, r) {
if (r instanceof tree.Rule && r.variable === true) {
hash[r.name] = r;
if (!r.isDefault || !(r.name in hash)) {
hash[r.name] = r;
}
}
return hash;
}, {});
Expand Down
4 changes: 4 additions & 0 deletions test/css/variables.css
Original file line number Diff line number Diff line change
Expand Up @@ -31,3 +31,7 @@ a:nth-child(2) {
.testPollution {
a: 'pollution';
}
.defaults {
font-size: 14px;
line-height: 14px;
}
9 changes: 9 additions & 0 deletions test/less/variables.less
Original file line number Diff line number Diff line change
Expand Up @@ -66,3 +66,12 @@ a:nth-child(@a) {
.polluteMixin();
a: @a;
}

@baseFontSize: 14px;
@baseFontSize: 12px !default;
@baseLineHeight: 14px;

.defaults {
font-size: @baseFontSize;
line-height: @baseLineHeight;
}