Skip to content

Commit

Permalink
Add import inline option. Fixes #1209
Browse files Browse the repository at this point in the history
  • Loading branch information
lukeapage committed May 4, 2013
1 parent 88cd75c commit d375ab8
Show file tree
Hide file tree
Showing 7 changed files with 41 additions and 32 deletions.
2 changes: 2 additions & 0 deletions lib/less/browser.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 /
Expand Down
21 changes: 13 additions & 8 deletions lib/less/import-visitor.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -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; }

Expand All @@ -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();
});
}
}
Expand Down
31 changes: 11 additions & 20 deletions lib/less/parser.js
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand All @@ -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)
}
}
Expand Down Expand Up @@ -1302,7 +1306,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];
}
Expand Down Expand Up @@ -1601,16 +1605,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);
};
}

13 changes: 9 additions & 4 deletions lib/less/tree/import.js
Original file line number Diff line number Diff line change
Expand Up @@ -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)) {
Expand All @@ -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) : '';
Expand Down Expand Up @@ -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;
Expand Down
3 changes: 3 additions & 0 deletions test/css/import-inline.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
this isn't very valid CSS. @media (min-width: 600px) {
#css { color: yellow; }
}
2 changes: 2 additions & 0 deletions test/less/import-inline.less
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
@import (inline) url("import/import-test-d.css") (min-width:600px);
@import (inline, css) url("import/invalid-css.less");
1 change: 1 addition & 0 deletions test/less/import/invalid-css.less
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
this isn't very valid CSS.

0 comments on commit d375ab8

Please sign in to comment.