From f0ce0533f75f982cd91101853c9b8703530a4fb7 Mon Sep 17 00:00:00 2001 From: cloudhead Date: Fri, 11 Jun 2010 18:18:40 -0400 Subject: [PATCH] refactored and fixed css node creation, for watch mode --- lib/less/browser.js | 43 +++++++++++++++++++++++++++++-------------- 1 file changed, 29 insertions(+), 14 deletions(-) diff --git a/lib/less/browser.js b/lib/less/browser.js index a1bb3acf..e6937ba0 100644 --- a/lib/less/browser.js +++ b/lib/less/browser.js @@ -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 = ['
', '
[-1]{0}
', '
[0]{current}
', @@ -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",