strip querystring when importing, and caching. Allow relative @import urls

This commit is contained in:
cloudhead 2010-06-20 12:47:33 -04:00
parent e3a84c3819
commit 87b428bf2f
2 changed files with 23 additions and 15 deletions

View File

@ -86,9 +86,10 @@ function loadStyleSheets(callback, reload) {
}
}
function loadStyleSheet(sheet, callback, reload) {
var css = cache && cache.getItem(sheet.href);
var timestamp = cache && cache.getItem(sheet.href + ':timestamp');
function loadStyleSheet(sheet, callback, reload, remaining) {
var href = sheet.href.replace(/\?.*$/, '');
var css = cache && cache.getItem(href);
var timestamp = cache && cache.getItem(href + ':timestamp');
var styles = { css: css, timestamp: timestamp };
xhr(sheet.href, function (data, lastModified) {
@ -101,27 +102,31 @@ function loadStyleSheet(sheet, callback, reload) {
} else {
// Use remote copy (re-parse)
new(less.Parser)({
optimization: less.optimization
optimization: less.optimization,
paths: [href.replace(/[\w\.-]+$/, '')]
}).parse(data, function (e, root) {
if (e) { return error(e, sheet.href) }
if (e) { return error(e, href) }
try {
callback(root, sheet, { local: false, lastModified: lastModified });
removeNode(document.getElementById('less-error-message:' + sheet.href.replace(/[^a-z]+/gi, '-')));
callback(root, sheet, { local: false, lastModified: lastModified, remaining: remaining });
removeNode(document.getElementById('less-error-message:' + href.replace(/[^a-z]+/gi, '-')));
} catch (e) {
error(e, sheet.href);
error(e, href);
}
});
}
}, function (status) {
throw new(Error)("Couldn't load " + sheet.href + " (" + status + ")");
}, function (status, url) {
throw new(Error)("Couldn't load " + url+ " (" + status + ")");
});
}
function createCSS(styles, sheet, lastModified) {
var css;
// Strip the query-string
var href = sheet.href.replace(/\?.*$/, '');
// If there is no title set, use the filename, minus the extension
var id = 'less:' + (sheet.title || sheet.href.match(/(?:^|\/)([-\w]+)\.[a-z]+$/i)[1]);
var id = 'less:' + (sheet.title || href.match(/(?:^|\/)([-\w]+)\.[a-z]+$/i)[1]);
// If the stylesheet doesn't exist, create a new node
if ((css = document.getElementById(id)) === null) {
@ -152,9 +157,9 @@ function createCSS(styles, sheet, lastModified) {
// Don't update the local store if the file wasn't modified
if (lastModified && cache) {
log('saving ' + sheet.href + ' to cache.');
cache.setItem(sheet.href, styles);
cache.setItem(sheet.href + ':timestamp', lastModified);
log('saving ' + href + ' to cache.');
cache.setItem(href, styles);
cache.setItem(href + ':timestamp', lastModified);
}
}
@ -186,7 +191,7 @@ function xhr(url, callback, errback) {
callback(xhr.responseText,
xhr.getResponseHeader("Last-Modified"));
} else if (typeof(errback) === 'function') {
errback(xhr.status);
errback(xhr.status, url);
}
}
}

View File

@ -932,6 +932,9 @@ if (typeof(window) !== 'undefined') {
// Used by `@import` directives
//
less.Parser.importer = function (path, paths, callback) {
if (path[0] !== '/' && paths.length > 0) {
path = paths[0] + path;
}
loadStyleSheet({ href: path, title: path }, function (root) {
callback(root);
});