From bf88f660f2eed95baaffff2788f36fd003167489 Mon Sep 17 00:00:00 2001 From: Byron Wong Date: Fri, 10 May 2013 15:07:56 -0400 Subject: [PATCH] initial fix in browser.js and a small test addition --- lib/less/browser.js | 47 +++++++++++++++++++++++------------- test/browser-test-prepare.js | 7 +++--- 2 files changed, 34 insertions(+), 20 deletions(-) diff --git a/lib/less/browser.js b/lib/less/browser.js index edb9c244..e69ed45e 100644 --- a/lib/less/browser.js +++ b/lib/less/browser.js @@ -43,7 +43,7 @@ less.watch = function () { less.env = 'development'; initRunningMode(); } - return this.watchMode = true + return this.watchMode = true }; less.unwatch = function () {clearInterval(less.watchTimer); return this.watchMode = false; }; @@ -134,9 +134,18 @@ less.refreshStyles = loadStyles; less.refresh(less.env === 'development'); function loadStyles(newVars) { - var styles = document.getElementsByTagName('style'); - for (var i = 0; i < styles.length; i++) { + // returned list of style tags gets updated automatically. because we track + // indexes, we need a non-mutating local copy to work with. + var nodeList = document.getElementsByTagName('style'); + var styles = new Array(nodeList.length); + var i; + // Array.slice not guaranteed to work on host objects, forcing a manual loop + for (i = 0; i < nodeList.length; i++) { + styles[i] = nodeList[i]; + } + for (i = 0; i < styles.length; i++) { if (styles[i].type.match(typePattern)) { + console.log(i); var env = new less.tree.parseEnv(less), lessText = styles[i].innerHTML || ''; env.filename = document.location.href.replace(/#.*$/, ''); @@ -145,19 +154,23 @@ function loadStyles(newVars) { lessText += "\n" + newVars; } - new(less.Parser)(env).parse(lessText, function (e, cssAST) { - if (e) { - return error(e, "inline"); + // use closure to store current value of i + var callback = (function(i) { + return function (e, cssAST) { + if (e) { + return error(e, "inline"); + } + var css = cssAST.toCSS(less); + var style = styles[i]; + style.type = 'text/css'; + if (style.styleSheet) { + style.styleSheet.cssText = css; + } else { + style.innerHTML = css; + } } - var css = cssAST.toCSS(less); - var style = styles[i]; - style.type = 'text/css'; - if (style.styleSheet) { - style.styleSheet.cssText = css; - } else { - style.innerHTML = css; - } - }); + })(i); + new(less.Parser)(env).parse(lessText, callback); } } } @@ -207,7 +220,7 @@ function extractUrlParts(url, baseUrl) { throw new Error("Could not parse sheet href - '"+url+"'"); } - // Stylesheets in IE don't always return the full path + // Stylesheets in IE don't always return the full path if (!urlParts[1] || urlParts[2]) { baseUrlParts = baseUrl.match(urlPartsRegex); if (!baseUrlParts) { @@ -218,7 +231,7 @@ function extractUrlParts(url, baseUrl) { urlParts[3] = baseUrlParts[3] + urlParts[3]; } } - + if (urlParts[3]) { directories = urlParts[3].replace("\\", "/").split("/"); diff --git a/test/browser-test-prepare.js b/test/browser-test-prepare.js index 31d3eaa8..f9c81343 100644 --- a/test/browser-test-prepare.js +++ b/test/browser-test-prepare.js @@ -15,11 +15,12 @@ var createTestRunnerPage = function(dir, exclude, testSuiteName, dir2) { readDirFilesSync(path.join("test", dir, 'less', dir2 || ""), /\.less$/, function (file) { var name = path.basename(file, '.less'), id = (dir ? dir + '-' : "") + 'less-' + (dir2 ? dir2 + "-" : "") + name; - + if (exclude && name.match(exclude)) { return; } - + output += '\n'; output += '\n'; + output += '\n'; }); output += String(fs.readFileSync(path.join('test/browser', 'template.htm'))).replace("{runner-name}", testSuiteName); @@ -44,4 +45,4 @@ createTestRunnerPage("browser", null, "relative-urls", "relative-urls"); createTestRunnerPage("browser", null, "rootpath", "rootpath"); createTestRunnerPage("browser", null, "rootpath-relative", "rootpath-relative"); createTestRunnerPage("browser", null, "production"); -createTestRunnerPage("browser", null, "modify-vars", "modify-vars"); \ No newline at end of file +createTestRunnerPage("browser", null, "modify-vars", "modify-vars");