initial fix in browser.js and a small test addition

This commit is contained in:
Byron Wong
2013-05-10 15:07:56 -04:00
committed by Luke Page
parent 11197b34e3
commit bf88f660f2
2 changed files with 34 additions and 20 deletions

View File

@@ -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("/");

View File

@@ -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 += '<link id="original-less:' + id + '" rel="stylesheet/less" type="text/css" href="/' + path.join(dir, 'less', dir2 || "", name) + '.less' +'">\n';
output += '<link id="expected-less:' + id + '" rel="stylesheet" type="text/css" href="/' + path.join(dir, 'css', dir2 || "", name) + '.css' + '">\n';
output += '<style id="inline-less:' + id + '" type="text/less">@import "/' + path.join(dir, 'less', dir2 || "", name) + '.less' + '";</style>\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");
createTestRunnerPage("browser", null, "modify-vars", "modify-vars");