Skip to content

Commit

Permalink
- Change the default behavior of "@import" to only import once.
Browse files Browse the repository at this point in the history
- Add @import-multiple if for some insane reason you want multiple imports.
- Continue to support @import-once for backwards compatibility.
- Fixes #212.
  • Loading branch information
ljharb authored and lukeapage committed Nov 27, 2012
1 parent 0bf5835 commit 93562ea
Show file tree
Hide file tree
Showing 4 changed files with 103 additions and 32 deletions.
63 changes: 49 additions & 14 deletions dist/less-1.4.0.js
Expand Up @@ -767,7 +767,8 @@ less.Parser = function Parser(env) {
return $(this.entities.ratio) ||
$(this.entities.dimension) ||
$(this.entities.color) ||
$(this.entities.quoted);
$(this.entities.quoted) ||
$(this.entities.unicodeDescriptor);
},

// Assignments are argument entities for calls.
Expand Down Expand Up @@ -870,6 +871,19 @@ less.Parser = function Parser(env) {
return new(tree.Ratio)(value[1]);
}
},

//
// A unicode descriptor, as is used in unicode-range
//
// U+0?? or U+00A1-00A9
//
unicodeDescriptor: function () {
var ud;

if (ud = $(/^U\+[0-9a-fA-F?]+(\-[0-9a-fA-F?]+)?/)) {
return new(tree.UnicodeDescriptor)(ud[0]);
}
},

//
// JavaScript code to be evaluated
Expand Down Expand Up @@ -1115,7 +1129,7 @@ less.Parser = function Parser(env) {
//
entity: function () {
return $(this.entities.literal) || $(this.entities.variable) || $(this.entities.url) ||
$(this.entities.call) || $(this.entities.keyword) || $(this.entities.javascript) ||
$(this.entities.call) || $(this.entities.keyword) ||$(this.entities.javascript) ||
$(this.comment);
},

Expand Down Expand Up @@ -1218,9 +1232,6 @@ less.Parser = function Parser(env) {

if (elements.length > 0) { return new(tree.Selector)(elements) }
},
tag: function () {
return $(/^[A-Za-z][A-Za-z-]*[0-9]?/) || $('*');
},
attribute: function () {
var attr = '', key, val, op;

Expand Down Expand Up @@ -1318,12 +1329,13 @@ less.Parser = function Parser(env) {

save();

var dir = $(/^@import(?:-(once))?\s+/);
var dir = $(/^@import(?:-(once|multiple))?\s+/);

if (dir && (path = $(this.entities.quoted) || $(this.entities.url))) {
features = $(this.mediaFeatures);
if ($(';')) {
return new(tree.Import)(path, imports, features, (dir[1] === 'once'), index);
var importOnce = dir[1] !== 'multiple';
return new(tree.Import)(path, imports, features, importOnce, index);
}
}

Expand Down Expand Up @@ -2988,22 +3000,20 @@ tree.Media.prototype = {
env.mediaPath = [];
}

var blockIndex = env.mediaBlocks.length;
env.mediaPath.push(this);
env.mediaBlocks.push(this);

var media = new(tree.Media)([], []);
if(this.debugInfo) {
this.ruleset.debugInfo = this.debugInfo;
media.debugInfo = this.debugInfo;
}
media.features = this.features.eval(env);

env.mediaPath.push(media);
env.mediaBlocks.push(media);

env.frames.unshift(this.ruleset);
media.ruleset = this.ruleset.eval(env);
env.frames.shift();

env.mediaBlocks[blockIndex] = media;
env.mediaPath.pop();

return env.mediaPath.length === 0 ? media.evalTop(env) :
Expand Down Expand Up @@ -3111,7 +3121,7 @@ tree.mixin.Call.prototype = {
for (m = 0; m < mixins.length; m++) {
isRecursive = false;
for(f = 0; f < env.frames.length; f++) {
if (mixins[m] === (env.frames[f].originalRuleset || env.frames[f])) {
if ((!(mixins[m] instanceof tree.mixin.Definition)) && mixins[m] === (env.frames[f].originalRuleset || env.frames[f])) {
isRecursive = true;
break;
}
Expand Down Expand Up @@ -3896,6 +3906,19 @@ tree.Selector.prototype.toCSS = function (env) {
})(require('../tree'));
(function (tree) {

tree.UnicodeDescriptor = function (value) {
this.value = value;
};
tree.UnicodeDescriptor.prototype = {
toCSS: function (env) {
return this.value;
},
eval: function () { return this }
};

})(require('../tree'));
(function (tree) {

tree.URL = function (val, paths) {
this.value = val;
this.paths = paths;
Expand Down Expand Up @@ -3951,12 +3974,24 @@ tree.Variable.prototype = {
if (name.indexOf('@@') == 0) {
name = '@' + new(tree.Variable)(name.slice(1)).eval(env).value;
}

if (this.evaluating) {
throw { type: 'Name',
message: "Recursive variable definition for " + name,
filename: this.file,
index: this.index };
}

this.evaluating = true;

if (variable = tree.find(env.frames, function (frame) {
if (v = frame.variable(name)) {
return v.value.eval(env);
}
})) { return variable }
})) {
this.evaluating = false;
return variable;
}
else {
throw { type: 'Name',
message: "variable " + name + " is undefined",
Expand Down
4 changes: 2 additions & 2 deletions dist/less-1.4.0.min.js

Large diffs are not rendered by default.

63 changes: 49 additions & 14 deletions dist/less-rhino-1.4.0.js
Expand Up @@ -759,7 +759,8 @@ less.Parser = function Parser(env) {
return $(this.entities.ratio) ||
$(this.entities.dimension) ||
$(this.entities.color) ||
$(this.entities.quoted);
$(this.entities.quoted) ||
$(this.entities.unicodeDescriptor);
},

// Assignments are argument entities for calls.
Expand Down Expand Up @@ -862,6 +863,19 @@ less.Parser = function Parser(env) {
return new(tree.Ratio)(value[1]);
}
},

//
// A unicode descriptor, as is used in unicode-range
//
// U+0?? or U+00A1-00A9
//
unicodeDescriptor: function () {
var ud;

if (ud = $(/^U\+[0-9a-fA-F?]+(\-[0-9a-fA-F?]+)?/)) {
return new(tree.UnicodeDescriptor)(ud[0]);
}
},

//
// JavaScript code to be evaluated
Expand Down Expand Up @@ -1107,7 +1121,7 @@ less.Parser = function Parser(env) {
//
entity: function () {
return $(this.entities.literal) || $(this.entities.variable) || $(this.entities.url) ||
$(this.entities.call) || $(this.entities.keyword) || $(this.entities.javascript) ||
$(this.entities.call) || $(this.entities.keyword) ||$(this.entities.javascript) ||
$(this.comment);
},

Expand Down Expand Up @@ -1210,9 +1224,6 @@ less.Parser = function Parser(env) {

if (elements.length > 0) { return new(tree.Selector)(elements) }
},
tag: function () {
return $(/^[A-Za-z][A-Za-z-]*[0-9]?/) || $('*');
},
attribute: function () {
var attr = '', key, val, op;

Expand Down Expand Up @@ -1310,12 +1321,13 @@ less.Parser = function Parser(env) {

save();

var dir = $(/^@import(?:-(once))?\s+/);
var dir = $(/^@import(?:-(once|multiple))?\s+/);

if (dir && (path = $(this.entities.quoted) || $(this.entities.url))) {
features = $(this.mediaFeatures);
if ($(';')) {
return new(tree.Import)(path, imports, features, (dir[1] === 'once'), index);
var importOnce = dir[1] !== 'multiple';
return new(tree.Import)(path, imports, features, importOnce, index);
}
}

Expand Down Expand Up @@ -2980,22 +2992,20 @@ tree.Media.prototype = {
env.mediaPath = [];
}

var blockIndex = env.mediaBlocks.length;
env.mediaPath.push(this);
env.mediaBlocks.push(this);

var media = new(tree.Media)([], []);
if(this.debugInfo) {
this.ruleset.debugInfo = this.debugInfo;
media.debugInfo = this.debugInfo;
}
media.features = this.features.eval(env);

env.mediaPath.push(media);
env.mediaBlocks.push(media);

env.frames.unshift(this.ruleset);
media.ruleset = this.ruleset.eval(env);
env.frames.shift();

env.mediaBlocks[blockIndex] = media;
env.mediaPath.pop();

return env.mediaPath.length === 0 ? media.evalTop(env) :
Expand Down Expand Up @@ -3103,7 +3113,7 @@ tree.mixin.Call.prototype = {
for (m = 0; m < mixins.length; m++) {
isRecursive = false;
for(f = 0; f < env.frames.length; f++) {
if (mixins[m] === (env.frames[f].originalRuleset || env.frames[f])) {
if ((!(mixins[m] instanceof tree.mixin.Definition)) && mixins[m] === (env.frames[f].originalRuleset || env.frames[f])) {
isRecursive = true;
break;
}
Expand Down Expand Up @@ -3888,6 +3898,19 @@ tree.Selector.prototype.toCSS = function (env) {
})(require('../tree'));
(function (tree) {

tree.UnicodeDescriptor = function (value) {
this.value = value;
};
tree.UnicodeDescriptor.prototype = {
toCSS: function (env) {
return this.value;
},
eval: function () { return this }
};

})(require('../tree'));
(function (tree) {

tree.URL = function (val, paths) {
this.value = val;
this.paths = paths;
Expand Down Expand Up @@ -3943,12 +3966,24 @@ tree.Variable.prototype = {
if (name.indexOf('@@') == 0) {
name = '@' + new(tree.Variable)(name.slice(1)).eval(env).value;
}

if (this.evaluating) {
throw { type: 'Name',
message: "Recursive variable definition for " + name,
filename: this.file,
index: this.index };
}

this.evaluating = true;

if (variable = tree.find(env.frames, function (frame) {
if (v = frame.variable(name)) {
return v.value.eval(env);
}
})) { return variable }
})) {
this.evaluating = false;
return variable;
}
else {
throw { type: 'Name',
message: "variable " + name + " is undefined",
Expand Down
5 changes: 3 additions & 2 deletions lib/less/parser.js
Expand Up @@ -1194,12 +1194,13 @@ less.Parser = function Parser(env) {

save();

var dir = $(/^@import(?:-(once))?\s+/);
var dir = $(/^@import(?:-(once|multiple))?\s+/);

if (dir && (path = $(this.entities.quoted) || $(this.entities.url))) {
features = $(this.mediaFeatures);
if ($(';')) {
return new(tree.Import)(path, imports, features, (dir[1] === 'once'), index);
var importOnce = dir[1] !== 'multiple';
return new(tree.Import)(path, imports, features, importOnce, index);
}
}

Expand Down

0 comments on commit 93562ea

Please sign in to comment.