From e7cbc4c190de4a0642ae8d6f1a26f8d2cf323b0e Mon Sep 17 00:00:00 2001 From: Luke Page Date: Sun, 17 Feb 2013 12:44:31 +0000 Subject: [PATCH] Fix data-uri relative url to be relative in the same way as normal url's --- lib/less/env.js | 5 +++-- lib/less/functions.js | 8 ++++---- lib/less/index.js | 3 +++ lib/less/parser.js | 7 ++++++- lib/less/tree/call.js | 6 +++--- 5 files changed, 19 insertions(+), 10 deletions(-) diff --git a/lib/less/env.js b/lib/less/env.js index afc747d6..0ad1a849 100644 --- a/lib/less/env.js +++ b/lib/less/env.js @@ -12,8 +12,9 @@ 'dumpLineNumbers', // option - whether to dump line numbers 'compress', // option - whether to compress 'mime', // browser only - mime type for sheet import - 'entryPath', // browser only, path of entry less file - 'rootFilename' // browser only, href of the entry less file + 'entryPath', // browser only - path of entry less file + 'rootFilename', // browser only - href of the entry less file + 'currentDirectory' // node only - the current directory ]; tree.parseEnv = function(options) { diff --git a/lib/less/functions.js b/lib/less/functions.js index ab6fca60..f412d5d1 100644 --- a/lib/less/functions.js +++ b/lib/less/functions.js @@ -390,8 +390,8 @@ tree.functions = { filePath = mimetype; } - if (this.relativePath && this.env.isPathRelative(filePath)) { - filePath = path.join(this.relativePath, filePath); + if (this.currentDirectory && this.env.isPathRelative(filePath)) { + filePath = path.join(this.currentDirectory, filePath); } // detect the mimetype if not given @@ -497,10 +497,10 @@ function clamp(val) { return Math.min(1, Math.max(0, val)); } -tree.functionCall = function(env, rootpath, relativePath) { +tree.functionCall = function(env, rootpath, currentDirectory) { this.env = env; this.rootpath = rootpath; - this.relativePath = relativePath; + this.currentDirectory = currentDirectory; }; tree.functionCall.prototype = tree.functions; diff --git a/lib/less/index.js b/lib/less/index.js index c1dfbb54..9d1418e8 100644 --- a/lib/less/index.js +++ b/lib/less/index.js @@ -124,6 +124,9 @@ less.Parser.importer = function (file, paths, callback, env) { if(env.relativeUrls && !/^(?:[a-z-]+:|\/)/.test(file) && j != -1) { env.rootpath = env.rootpath + file.slice(0, j+1); // append (sub|sup) directory path of imported file } + if (env.relativeUrls) { + env.currentDirectory = pathname.replace(/[^\\\/]*$/, ""); + } env.contents[pathname] = data; // Updating top importing parser content cache. env.paths = [dirname].concat(paths); diff --git a/lib/less/parser.js b/lib/less/parser.js index b854f06e..431eda03 100644 --- a/lib/less/parser.js +++ b/lib/less/parser.js @@ -71,6 +71,11 @@ less.Parser = function Parser(env) { env = new tree.parseEnv(env); } + if (!env.currentDirectory && env.filename) { + // only works for node, only used for node + env.currentDirectory = env.filename.replace(/[^\/\\]*$/, ""); + } + // This function is called after all files // have been imported through `@import`. var finish = function () {}; @@ -630,7 +635,7 @@ less.Parser = function Parser(env) { return; } - if (name) { return new(tree.Call)(name, args, index, env.filename, env.rootpath, env.rootpath || env.paths[0]); } + if (name) { return new(tree.Call)(name, args, index, env.filename, env.rootpath, env.currentDirectory); } }, arguments: function () { var args = [], arg; diff --git a/lib/less/tree/call.js b/lib/less/tree/call.js index 0805f919..15bd8fdc 100644 --- a/lib/less/tree/call.js +++ b/lib/less/tree/call.js @@ -3,13 +3,13 @@ // // A function call node. // -tree.Call = function (name, args, index, filename, rootpath, relativePath) { +tree.Call = function (name, args, index, filename, rootpath, currentDirectory) { this.name = name; this.args = args; this.index = index; this.filename = filename; this.rootpath = rootpath; - this.relativePath = relativePath; + this.currentDirectory = currentDirectory; }; tree.Call.prototype = { // @@ -32,7 +32,7 @@ tree.Call.prototype = { if (nameLC in tree.functions) { // 1. try { - func = new tree.functionCall(env, this.rootpath, this.relativePath); + func = new tree.functionCall(env, this.rootpath, this.currentDirectory); result = func[nameLC].apply(func, args); if (result != null) { return result;