Merge pull request #2387 from SomMeri/data-uri-1634

Data uri support for include-path
This commit is contained in:
Luke Page
2015-01-13 17:54:31 +01:00
8 changed files with 71 additions and 34 deletions

1
.gitattributes vendored
View File

@@ -1,4 +1,5 @@
*.js text eol=lf
*.svg text eol=lf
lessc text eol=lf
*.less text eol=lf
*.css text eol=lf

View File

@@ -23,36 +23,16 @@ FileManager.prototype.loadFile = function(filename, currentDirectory, options, e
options = options || {};
if (options.syncImport) {
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('.'); }
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);
return;
}
// promise is guarenteed to be asyncronous
// which helps as it allows the file handle
// to be closed before it continues with the next file
@@ -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;

View File

@@ -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

View File

@@ -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();

View File

@@ -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");
}

View File

@@ -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();

View File

@@ -280,11 +280,24 @@ module.exports = function() {
}
}
function toCSS(options, path, callback) {
options = options || {};
var str = fs.readFileSync(path, 'utf8');
function contains(fullArray, obj) {
for (var i = 0; i < fullArray.length; i++) {
if (fullArray[i] === obj) {
return true;
}
}
return false;
}
options.paths = [require('path').dirname(path)];
function toCSS(options, path, callback) {
options = options || {};
var str = fs.readFileSync(path, 'utf8'), addPath = 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;

View File

@@ -0,0 +1,6 @@
@import "import-test-e";
data-uri {
property: data-uri('image.svg');
}