simplify by saving reference to style tag instead of saving index

This commit is contained in:
Byron Wong
2013-06-07 09:20:28 -04:00
committed by Luke Page
parent c4dc89b74b
commit 8ed519fd98

View File

@@ -134,19 +134,13 @@ less.refreshStyles = loadStyles;
less.refresh(less.env === 'development');
function loadStyles(newVars) {
// 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)) {
var styles = document.getElementsByTagName('style'),
style;
for (var i = 0; i < styles.length; i++) {
style = styles[i];
if (style.type.match(typePattern)) {
var env = new less.tree.parseEnv(less),
lessText = styles[i].innerHTML || '';
lessText = style.innerHTML || '';
env.filename = document.location.href.replace(/#.*$/, '');
if (newVars) {
env.useFileCache = true;
@@ -154,13 +148,12 @@ function loadStyles(newVars) {
}
// use closure to store current value of i
var callback = (function(i) {
var callback = (function(style) {
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;
@@ -168,7 +161,7 @@ function loadStyles(newVars) {
style.innerHTML = css;
}
}
})(i);
})(style);
new(less.Parser)(env).parse(lessText, callback);
}
}