more robust id extraction from href

This commit is contained in:
cloudhead 2010-07-04 21:05:02 +02:00
parent b8e6455d55
commit e85fb82754

View File

@ -133,7 +133,7 @@ function loadStyleSheet(sheet, callback, reload, remaining) {
if (e) { return error(e, href) }
try {
callback(root, sheet, { local: false, lastModified: lastModified, remaining: remaining });
removeNode(document.getElementById('less-error-message:' + href.replace(/[^a-z]+/gi, '-')));
removeNode(document.getElementById('less-error-message:' + extractId(href)));
} catch (e) {
error(e, href);
}
@ -144,6 +144,13 @@ function loadStyleSheet(sheet, callback, reload, remaining) {
});
}
function extractId(href) {
return href.replace(/^[a-z]+:\/\/?/, '') // Remove protocol
.replace(/\?.*$/, '') // Remove query
.replace(/\.[^\/]+$/, '') // Remove file extension
.replace(/[^\w-]+/g, '-'); // Replace illegal characters
}
function createCSS(styles, sheet, lastModified) {
var css;
@ -151,7 +158,7 @@ function createCSS(styles, sheet, lastModified) {
var href = sheet.href ? sheet.href.replace(/\?.*$/, '') : '';
// If there is no title set, use the filename, minus the extension
var id = 'less:' + (sheet.title || href.match(/(?:^|\/)([-\w.]+)\.[a-z]+$/i)[1]);
var id = 'less:' + (sheet.title || extractId(href));
// If the stylesheet doesn't exist, create a new node
if ((css = document.getElementById(id)) === null) {
@ -246,7 +253,7 @@ function log(str) {
}
function error(e, href) {
var id = 'less-error-message:' + href.replace(/[^a-z]+/ig, '-');
var id = 'less-error-message:' + extractId(href);
if (! e.extract) { throw e }