organise the import node a bit better. eval the node before fetching

This commit is contained in:
Luke Page
2013-02-26 18:52:20 +00:00
parent e63c8c5868
commit 290d7a055c
4 changed files with 41 additions and 21 deletions

View File

@@ -2,13 +2,17 @@
tree.importVisitor = function(root, importer) {
this._visitor = new tree.visitor(this);
this._importer = importer;
this.env = new tree.evalEnv();
// process the contents
this._visitor.visit(root);
};
tree.importVisitor.prototype = {
visitImport: function (importNode, visitArgs) {
if (!importNode.css) {
this._importer.push(importNode.path, function (e, root, imported) {
importNode = importNode.evalForImport(this.env);
this._importer.push(importNode.getPath(), function (e, root, imported) {
if (e) { e.index = importNode.index; }
if (imported && importNode.once) { importNode.skip = imported; }
importNode.root = root || new(tree.Ruleset)([], []);

View File

@@ -16,18 +16,14 @@ tree.Import = function (path, features, once, index, rootpath) {
this.once = once;
this.index = index;
this._path = path;
this.path = path;
this.features = features;
this.rootpath = rootpath;
// The '.less' extension is optional
if (path instanceof tree.Quoted) {
this.path = /(\.[a-z]*$)|([\?;].*)$/.test(path.value) ? path.value : path.value + '.less';
} else {
this.path = path.value.value || path.value;
}
this.css = /css([\?;].*)?$/.test(this.path);
var pathValue = this.getPath();
if (pathValue) {
this.css = /css([\?;].*)?$/.test(pathValue);
}
};
//
@@ -43,28 +39,48 @@ tree.Import.prototype = {
type: "Import",
accept: function (visitor) {
this.features = visitor.visit(this.features);
this._path = visitor.visit(this._path);
this.path = visitor.visit(this.path);
this.root = visitor.visit(this.root);
},
toCSS: function (env) {
var features = this.features ? ' ' + this.features.toCSS(env) : '';
if (this.css) {
// Add the base path if the import is relative
if (typeof this._path.value === "string" && !/^(?:[a-z-]+:|\/)/.test(this._path.value)) {
this._path.value = this.rootpath + this._path.value;
}
return "@import " + this._path.toCSS() + features + ';\n';
return "@import " + this.path.toCSS() + features + ';\n';
} else {
return "";
}
},
getPath: function () {
if (this.path instanceof tree.Quoted) {
var path = this.path.value;
return (this.css || /(\.[a-z]*$)|([\?;].*)$/.test(path)) ? path : path + '.less';
} else if (this.path instanceof tree.URL) {
return this.path.value.value;
}
return null;
},
evalForImport: function (env) {
return new(tree.Import)(this.path.eval(env), this.features, this.once, this.index);
},
evalPath: function (env) {
var path = this.path.eval(env);
if (this.rootpath && !(path instanceof tree.URL)) {
var pathValue = path.value;
// Add the base path if the import is relative
if (pathValue && !/^(?:[a-z\-]+:|\/)/.test(pathValue)) {
path.value = this.rootpath + pathValue;
}
}
return path;
},
eval: function (env) {
var ruleset, features = this.features && this.features.eval(env);
if (this.skip) { return []; }
if (this.css) {
return new(tree.Import)(this._path, features, this.once, this.index, this.rootpath);
return new(tree.Import)(this.evalPath(env), features, this.once, this.index);
} else {
ruleset = new(tree.Ruleset)([], this.root.rules.slice(0));

4
test/css/import.css vendored
View File

@@ -1,8 +1,8 @@
@import url(http://fonts.googleapis.com/css?family=Open+Sans);
@import url(something.css) screen and (color) and (max-width: 600px);
@import url(/absolute/something.css) screen and (color) and (max-width: 600px);
@import url("file.css") (min-width: 100px);
@import url("//ha.com/file.css") (min-width: 100px);
#import-test {
height: 10px;
color: #ff0000;

View File

@@ -1,9 +1,9 @@
@import url(http://fonts.googleapis.com/css?family=Open+Sans);
@import url(something.css) screen and (color) and (max-width: 600px);
@import url(/absolute/something.css) screen and (color) and (max-width: 600px);
@var: 100px;
@import url("file.css") (min-width:@var);
@import url("//ha.com/file.css") (min-width:@var);
#import-test {
.mixin;