change data-uri to look at the file relative to the root or current less file. Fixes #1186

This commit is contained in:
Luke Page
2013-02-16 22:11:01 +00:00
parent 21cc34b608
commit f68337e450
8 changed files with 40 additions and 26 deletions

View File

@@ -59,6 +59,10 @@
return this.strictMaths === false ? true : (this.parensStack && this.parensStack.length);
};
tree.evalEnv.prototype.isPathRelative = function (path) {
return !/^(?:[a-z-]+:|\/)/.test(path);
};
//todo - do the same for the toCSS env
//tree.toCSSEnv = function (options) {
//};

View File

@@ -373,17 +373,26 @@ tree.functions = {
return values.value[index];
},
"data-uri": function(mimetype, path) {
"data-uri": function(mimetype, filePath) {
if (typeof window !== 'undefined') {
return new less.tree.URL(path || mimetype, this.rootpath).eval(this.env);
return new less.tree.URL(filePath || mimetype, this.rootpath).eval(this.env);
}
mimetype = mimetype.value;
path = (path && path.value);
filePath = (filePath && filePath.value);
var fs = require('fs');
var useBase64 = false;
var fs = require("fs"),
path = require("path"),
useBase64 = false;
if (arguments.length < 2) {
filePath = mimetype;
}
if (this.relativePath && this.env.isPathRelative(filePath)) {
filePath = path.join(this.relativePath, filePath);
}
// detect the mimetype if not given
if (arguments.length < 2) {
@@ -394,8 +403,7 @@ tree.functions = {
mime = tree._mime;
}
path = mimetype;
mimetype = mime.lookup(path);
mimetype = mime.lookup(filePath);
// use base 64 unless it's an ASCII or UTF-8 format
var charset = mime.charsets.lookup(mimetype);
@@ -406,12 +414,12 @@ tree.functions = {
useBase64 = /;base64$/.test(mimetype)
}
var buf = fs.readFileSync(path);
var buf = fs.readFileSync(filePath);
buf = useBase64 ? buf.toString('base64')
: encodeURIComponent(buf);
var uri = "'data:"+mimetype+','+buf+"'";
var uri = "'data:" + mimetype + ',' + buf + "'";
return new(tree.URL)(new(tree.Anonymous)(uri));
}
};
@@ -489,9 +497,10 @@ function clamp(val) {
return Math.min(1, Math.max(0, val));
}
tree.functionCall = function(env, rootpath) {
tree.functionCall = function(env, rootpath, relativePath) {
this.env = env;
this.rootpath = rootpath;
this.relativePath = relativePath;
};
tree.functionCall.prototype = tree.functions;

View File

@@ -630,7 +630,7 @@ less.Parser = function Parser(env) {
return;
}
if (name) { return new(tree.Call)(name, args, index, env.filename, env.rootpath) }
if (name) { return new(tree.Call)(name, args, index, env.filename, env.rootpath, env.rootpath || env.paths[0]); }
},
arguments: function () {
var args = [], arg;

View File

@@ -3,12 +3,13 @@
//
// A function call node.
//
tree.Call = function (name, args, index, filename, rootpath) {
tree.Call = function (name, args, index, filename, rootpath, relativePath) {
this.name = name;
this.args = args;
this.index = index;
this.filename = filename;
this.rootpath = rootpath;
this.relativePath = relativePath;
};
tree.Call.prototype = {
//
@@ -31,7 +32,7 @@ tree.Call.prototype = {
if (nameLC in tree.functions) { // 1.
try {
func = new tree.functionCall(env, this.rootpath);
func = new tree.functionCall(env, this.rootpath, this.relativePath);
result = func[nameLC].apply(func, args);
if (result != null) {
return result;

View File

@@ -12,7 +12,7 @@ tree.URL.prototype = {
var val = this.value.eval(ctx), rootpath;
// Add the base path if the URL is relative
if (this.rootpath && typeof val.value === "string" && !/^(?:[a-z-]+:|\/)/.test(val.value)) {
if (this.rootpath && typeof val.value === "string" && ctx.isPathRelative(val.value)) {
rootpath = this.rootpath;
if (!val.quote) {
rootpath = rootpath.replace(/[\(\)'"\s]/g, function(match) { return "\\"+match; });