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

Add support for default variables using "?" #1705

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
28 changes: 23 additions & 5 deletions lib/less/parser.js
Original file line number Diff line number Diff line change
Expand Up @@ -886,6 +886,19 @@ less.Parser = function Parser(env) {
if (input.charAt(i) === '@' && (name = $(/^(@[\w-]+)\s*:/))) { return name[1]; }
},

//
// The name portion of a default variable definition. Used in the `rule` parser
//
// ?fink:
//
defaultVariable: function () {
var name;

if (input.charAt(i) === '?' && (name = $(/^(\?[\w-]+)\s*:/))) {
return name[1].replace('?', '@');
}
},

//
// extend syntax - used to extend selectors
//
Expand Down Expand Up @@ -1319,27 +1332,32 @@ less.Parser = function Parser(env) {
}
},
rule: function (tryAnonymous) {
var name, value, c = input.charAt(i), important, merge = false;
var name, value, c = input.charAt(i), important, isDefault, isVar, merge = false;
save();

if (c === '.' || c === '#' || c === '&') { return; }

if (name = $(this.variable) || $(this.ruleProperty)) {
name = $(this.defaultVariable);
isDefault = !!name;
name = name || $(this.variable);
isVar = !!name;
name = name || $(this.ruleProperty);

if (name) {
// prefer to try to parse first if its a variable or we are compressing
// but always fallback on the other one
value = !tryAnonymous && (env.compress || (name.charAt(0) === '@')) ?
value = !tryAnonymous && (env.compress || isVar) ?
($(this.value) || $(this.anonymousValue)) :
($(this.anonymousValue) || $(this.value));


important = $(this.important);
if (name[name.length-1] === "+") {
merge = true;
name = name.substr(0, name.length - 1);
}

if (value && $(this.end)) {
return new (tree.Rule)(name, value, important, merge, memo, env.currentFileInfo);
return new (tree.Rule)(name, value, important, merge, memo, env.currentFileInfo, null, isDefault);
} else {
furthest = i;
restore();
Expand Down
5 changes: 3 additions & 2 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, merge, index, currentFileInfo, inline) {
tree.Rule = function (name, value, important, merge, index, currentFileInfo, inline, isDefault) {
this.name = name;
this.value = (value instanceof tree.Value) ? value : new(tree.Value)([value]);
this.important = important ? ' ' + important.trim() : '';
Expand All @@ -9,6 +9,7 @@ tree.Rule = function (name, value, important, merge, index, currentFileInfo, inl
this.currentFileInfo = currentFileInfo;
this.inline = inline || false;
this.variable = (name.charAt(0) === '@');
this.isDefault = isDefault;
};

tree.Rule.prototype = {
Expand Down Expand Up @@ -40,7 +41,7 @@ tree.Rule.prototype = {
this.value.eval(env),
this.important,
this.merge,
this.index, this.currentFileInfo, this.inline);
this.index, this.currentFileInfo, this.inline, this.isDefault);
}
finally {
if (strictMathBypass) {
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 @@ -145,7 +145,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 || !hash[r.name]) {
hash[r.name] = r;
}
}
return hash;
}, {});
Expand Down