refactored and fixed css node creation, for watch mode

This commit is contained in:
cloudhead
2010-06-11 18:18:40 -04:00
parent 0f1f776e43
commit f0ce0533f7

View File

@@ -91,26 +91,39 @@ function loadStyleSheet(sheet, callback) {
}
function createCSS(styles, sheet, lastModified) {
var css = document.createElement('style');
css.type = 'text/css';
css.media = 'screen';
css.title = 'less-sheet';
var css, title, id;
if (sheet) {
css.title = sheet.title || sheet.href.match(/(?:^|\/)([-\w]+)\.[a-z]+$/i)[1];
// If there is no title set, use the filename, minus the extension
title = sheet.title || sheet.href.match(/(?:^|\/)([-\w]+)\.[a-z]+$/i)[1];
id = '-less-' + title;
// Don't update the local store if the file wasn't modified
if (lastModified && typeof(localStorage) !== "undefined") {
localStorage.setItem(sheet.href, JSON.stringify({ timestamp: lastModified, css: styles }));
}
// If the stylesheet doesn't exist, create a new node
if ((css = document.getElementById(id)) === null) {
css = document.createElement('style');
css.type = 'text/css';
css.media = 'screen';
css.title = title;
css.id = id;
document.getElementsByTagName('head')[0].appendChild(css);
}
if (css.styleSheet) {
css.styleSheet.cssText = styles;
if (css.styleSheet) { // IE
try {
css.styleSheet.cssText = styles;
} catch (e) {
throw new(Error)("Couldn't reassign styleSheet.cssText.");
}
} else {
if (css.childNodes.length > 0) {
css.removeChild(css.childNodes[0]);
}
css.appendChild(document.createTextNode(styles));
}
document.getElementsByTagName('head')[0].appendChild(css);
// Don't update the local store if the file wasn't modified
if (lastModified && typeof(localStorage) !== "undefined") {
localStorage.setItem(sheet.href, JSON.stringify({ timestamp: lastModified, css: styles }));
}
}
function xhr(url, callback, errback) {
@@ -158,6 +171,8 @@ function log(str) {
}
function error(e, href) {
if (document.getElementById('less-error-message')) { return }
var template = ['<div>',
'<pre class="ctx"><span>[-1]</span>{0}</pre>',
'<pre><span>[0]</span>{current}</pre>',
@@ -203,7 +218,7 @@ function error(e, href) {
'padding-bottom: 2px;',
'border-bottom: 1px dashed red;',
'}'
].join(''));
].join(''), { title: 'error-message' });
elem.style.cssText = [
"font-family: Arial, sans-serif",