Add import inline option. Fixes #1209

This commit is contained in:
Luke Page
2013-03-19 15:20:46 +00:00
parent 08fca7a7cc
commit 6fc6dc2301
7 changed files with 41 additions and 32 deletions

View File

@@ -339,6 +339,8 @@ function loadFile(originalHref, currentFileInfo, callback, env) {
});
}
less.Parser.fileLoader = loadFile;
function extractId(href) {
return href.replace(/^[a-z-]+:\/+?[^\/]+/, '' ) // Remove protocol & domain
.replace(/^\//, '' ) // Remove root /

View File

@@ -27,9 +27,10 @@
},
visitImport: function (importNode, visitArgs) {
var importVisitor = this,
evaldImportNode;
evaldImportNode,
inlineCSS = importNode.options.inline;
if (!importNode.css) {
if (!importNode.css || inlineCSS) {
try {
evaldImportNode = importNode.evalForImport(this.env);
@@ -41,11 +42,12 @@
importNode.error = e;
}
if (evaldImportNode && !evaldImportNode.css) {
if (evaldImportNode && (!evaldImportNode.css || inlineCSS)) {
importNode = evaldImportNode;
this.importCount++;
var env = new tree.evalEnv(this.env, this.env.frames.slice(0));
this._importer.push(importNode.getPath(), importNode.currentFileInfo, function (e, root, imported) {
this._importer.push(importNode.getPath(), importNode.currentFileInfo, inlineCSS, function (e, root, imported) {
if (e && !e.filename) { e.index = importNode.index; e.filename = importNode.currentFileInfo.filename; }
if (imported && !importNode.options.multiple) { importNode.skip = imported; }
@@ -59,11 +61,14 @@
if (root) {
importNode.root = root;
new(tree.importVisitor)(importVisitor._importer, subFinish, env)
.run(root);
} else {
subFinish();
if (!inlineCSS) {
new(tree.importVisitor)(importVisitor._importer, subFinish, env)
.run(root);
return;
}
}
subFinish();
});
}
}

View File

@@ -78,20 +78,20 @@ less.Parser = function Parser(env) {
contents: env.contents, // Holds the imported file contents
mime: env.mime, // MIME type of .less files
error: null, // Error in parsing/evaluating an import
push: function (path, currentFileInfo, callback) {
push: function (path, currentFileInfo, inlineCSS, callback) {
var parserImports = this;
this.queue.push(path);
var fileParsedFunc = function (e, root, fullPath) {
parserImports.queue.splice(parserImports.queue.indexOf(path), 1); // Remove the path from the queue
var imported = fullPath in parserImports.files;
var importedPreviously = fullPath in parserImports.files;
parserImports.files[fullPath] = root; // Store the root
if (e && !parserImports.error) { parserImports.error = e; }
callback(e, root, imported);
callback(e, root, importedPreviously);
};
if (less.Parser.importer) {
@@ -106,9 +106,13 @@ less.Parser = function Parser(env) {
newEnv.processImports = false;
newEnv.contents[fullPath] = contents;
new(less.Parser)(newEnv).parse(contents, function (e, root) {
fileParsedFunc(e, root, fullPath);
});
if (inlineCSS) {
fileParsedFunc(null, contents, fullPath);
} else {
new(less.Parser)(newEnv).parse(contents, function (e, root) {
fileParsedFunc(e, root, fullPath);
});
}
}, env)
}
}
@@ -1303,7 +1307,7 @@ less.Parser = function Parser(env) {
},
importOption: function() {
var opt = $(/^(less|css|multiple|once)/);
var opt = $(/^(less|css|multiple|once|inline)/);
if (opt) {
return opt[1];
}
@@ -1602,16 +1606,3 @@ less.Parser = function Parser(env) {
};
};
if (less.mode === 'browser' || less.mode === 'rhino') {
//
// Used by `@import` directives
//
less.Parser.fileLoader = function (path, currentFileInfo, callback, env) {
loadFile(path, currentFileInfo,
function (e, data, path, newFileInfo) {
callback(e, data, path, newFileInfo);
}, env);
};
}

View File

@@ -20,8 +20,8 @@ tree.Import = function (path, features, options, index, currentFileInfo) {
this.features = features;
this.currentFileInfo = currentFileInfo;
if (this.options.less !== undefined) {
this.css = !this.options.less;
if (this.options.less !== undefined || this.options.inline) {
this.css = !this.options.less || this.options.inline;
} else {
var pathValue = this.getPath();
if (pathValue && /css([\?;].*)?$/.test(pathValue)) {
@@ -44,7 +44,9 @@ tree.Import.prototype = {
accept: function (visitor) {
this.features = visitor.visit(this.features);
this.path = visitor.visit(this.path);
this.root = visitor.visit(this.root);
if (!this.options.inline) {
this.root = visitor.visit(this.root);
}
},
toCSS: function (env) {
var features = this.features ? ' ' + this.features.toCSS(env) : '';
@@ -84,7 +86,10 @@ tree.Import.prototype = {
if (this.skip) { return []; }
if (this.css) {
if (this.options.inline) {
var contents = new(tree.Anonymous)(this.root);
return this.features ? new(tree.Media)([contents], this.features.value) : [contents];
} else if (this.css) {
var newImport = new(tree.Import)(this.evalPath(env), features, this.options, this.index);
if (!newImport.css && this.error) {
throw this.error;

View File

@@ -0,0 +1,3 @@
this isn't very valid CSS. @media (min-width: 600px) {
#css { color: yellow; }
}

View File

@@ -0,0 +1,2 @@
@import (inline) url("import/import-test-d.css") (min-width:600px);
@import (inline, css) url("import/invalid-css.less");

View File

@@ -0,0 +1 @@
this isn't very valid CSS.