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

Data uri support for include-path #2387

Merged
merged 4 commits into from
Jan 13, 2015
Merged
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
1 change: 1 addition & 0 deletions .gitattributes
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
*.js text eol=lf
*.svg text eol=lf
lessc text eol=lf
*.less text eol=lf
*.css text eol=lf
Expand Down
67 changes: 38 additions & 29 deletions lib/less-node/file-manager.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,36 +23,16 @@ FileManager.prototype.loadFile = function(filename, currentDirectory, options, e

options = options || {};

var paths = isAbsoluteFilename ? [""] : [currentDirectory];
if (options.paths) paths.push.apply(paths, options.paths);
if (!isAbsoluteFilename && paths.indexOf('.') === -1) { paths.push('.'); }

if (options.syncImport) {
var err, result;
for (var i = 0; i < paths.length; i++) {
try {
fullFilename = filename;
if (paths[i]) {
fullFilename = path.join(paths[i], fullFilename);
}
filenamesTried.push(fullFilename);
fs.statSync(fullFilename);
break;
} catch (e) {
fullFilename = null;
}
}

if (!fullFilename) {
err = { type: 'File', message: "'" + filename + "' wasn't found. Tried - " + filenamesTried.join(",") };
} else {
data = fs.readFileSync(fullFilename, 'utf-8');
result = { contents: data, filename: fullFilename};
}
callback(err, result);
data = this.loadFileSync(filename, currentDirectory, options, environment, 'utf-8');
callback(data.error, data);
return;
}

var paths = isAbsoluteFilename ? [""] : [currentDirectory];
if (options.paths) paths.push.apply(paths, options.paths);
if (!isAbsoluteFilename && paths.indexOf('.') === -1) { paths.push('.'); }

// promise is guarenteed to be asyncronous
// which helps as it allows the file handle
// to be closed before it continues with the next file
Expand Down Expand Up @@ -82,9 +62,38 @@ FileManager.prototype.loadFile = function(filename, currentDirectory, options, e
});
};

FileManager.prototype.loadFileSync = function(filename, currentDirectory, options, environment) {
filename = path.join(currentDirectory, filename);
return { contents: fs.readFileSync(filename), filename: filename };
FileManager.prototype.loadFileSync = function(filename, currentDirectory, options, environment, encoding) {
var fullFilename, paths, filenamesTried=[], isAbsoluteFilename = this.isPathAbsolute(filename),data;
options = options || {};

paths = isAbsoluteFilename ? [""] : [currentDirectory];
if (options.paths) paths.push.apply(paths, options.paths);
if (!isAbsoluteFilename && paths.indexOf('.') === -1) { paths.push('.'); }

var err, result;
for (var i = 0; i < paths.length; i++) {
try {
fullFilename = filename;
if (paths[i]) {
fullFilename = path.join(paths[i], fullFilename);
}
filenamesTried.push(fullFilename);
fs.statSync(fullFilename);
break;
} catch (e) {
fullFilename = null;
}
}

if (!fullFilename) {
err = { type: 'File', message: "'" + filename + "' wasn't found. Tried - " + filenamesTried.join(",") };
result = { error: err };
} else {
data = fs.readFileSync(fullFilename, encoding);
result = { contents: data, filename: fullFilename};
}

return result;
};

module.exports = FileManager;
1 change: 1 addition & 0 deletions lib/less/contexts.js
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ contexts.Parse = function(options) {
};

var evalCopyProperties = [
'paths', // additional include paths
'compress', // whether to compress
'ieCompat', // whether to enforce IE compatibility (IE8 data-uri)
'strictMath', // whether math has to be within parenthesis
Expand Down
2 changes: 1 addition & 1 deletion lib/less/tree/expression.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ var Node = require("./node"),
var Expression = function (value) {
this.value = value;
if (!value) {
throw new Error("Expression requires a array parameter");
throw new Error("Expression requires an array parameter");
}
};
Expression.prototype = new Node();
Expand Down
6 changes: 6 additions & 0 deletions test/css/include-path/include-path.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
body {
width: 100%;
}
data-uri {
property: url("data:image/svg+xml,%3Csvg%20height%3D%22100%22%20width%3D%22100%22%3E%0A%20%20%3Ccircle%20cx%3D%2250%22%20cy%3D%2250%22%20r%3D%2240%22%20stroke%3D%22black%22%20stroke-width%3D%221%22%20fill%3D%22blue%22%20%2F%3E%0A%3C%2Fsvg%3E");
}
1 change: 1 addition & 0 deletions test/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ lessTester.runTestSet({globalVars: true, banner: "/**\n * Test\n */\n"}, "glob
lessTester.runTestSet({modifyVars: true}, "modifyVars/",
null, null, null, function(name) { return path.join('test/less/', name) + '.json'; });
lessTester.runTestSet({urlArgs: '424242'}, "url-args/");
lessTester.runTestSet({paths: ['test/data/','test/less/import/']}, "include-path/");
lessTester.testSyncronous({syncImport: true}, "import");
lessTester.testSyncronous({syncImport: true}, "css");
lessTester.testNoOptions();
Expand Down
19 changes: 16 additions & 3 deletions test/less-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -280,11 +280,24 @@ module.exports = function() {
}
}

function toCSS(options, path, callback) {
function contains(fullArray, obj) {
for (var i = 0; i < fullArray.length; i++) {
if (fullArray[i] === obj) {
return true;
}
}
return false;
}


function toCSS(options, path, callback) {
options = options || {};
var str = fs.readFileSync(path, 'utf8');
var str = fs.readFileSync(path, 'utf8'), addPath = require('path').dirname(path);

options.paths = [require('path').dirname(path)];
options.paths = options.paths || [];
if (!contains(options.paths, addPath)) {
options.paths.push(addPath);
}
options.filename = require('path').resolve(process.cwd(), path);
options.optimization = options.optimization || 0;

Expand Down
6 changes: 6 additions & 0 deletions test/less/include-path/include-path.less
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
@import "import-test-e";

data-uri {
property: data-uri('image.svg');
}