diff --git a/.gitattributes b/.gitattributes index d2a478b45..f18460238 100644 --- a/.gitattributes +++ b/.gitattributes @@ -1,4 +1,5 @@ *.js text eol=lf +*.svg text eol=lf lessc text eol=lf *.less text eol=lf *.css text eol=lf diff --git a/lib/less-node/file-manager.js b/lib/less-node/file-manager.js index 3d1a185bd..46802504d 100644 --- a/lib/less-node/file-manager.js +++ b/lib/less-node/file-manager.js @@ -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 @@ -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; diff --git a/lib/less/contexts.js b/lib/less/contexts.js index 5c0ef6d51..52b902ef9 100644 --- a/lib/less/contexts.js +++ b/lib/less/contexts.js @@ -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 diff --git a/lib/less/tree/expression.js b/lib/less/tree/expression.js index 00024ceb5..c852d4215 100644 --- a/lib/less/tree/expression.js +++ b/lib/less/tree/expression.js @@ -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(); diff --git a/test/css/include-path/include-path.css b/test/css/include-path/include-path.css new file mode 100644 index 000000000..586cb6174 --- /dev/null +++ b/test/css/include-path/include-path.css @@ -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"); +} diff --git a/test/index.js b/test/index.js index a82bc9a0e..1146ff57a 100644 --- a/test/index.js +++ b/test/index.js @@ -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(); diff --git a/test/less-test.js b/test/less-test.js index 93caf9be5..8a9dc4b6b 100644 --- a/test/less-test.js +++ b/test/less-test.js @@ -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; diff --git a/test/less/include-path/include-path.less b/test/less/include-path/include-path.less new file mode 100644 index 000000000..bd1f1d197 --- /dev/null +++ b/test/less/include-path/include-path.less @@ -0,0 +1,6 @@ +@import "import-test-e"; + +data-uri { + property: data-uri('image.svg'); +} +