From d31be578a3b35c7b0f43738e26f42576bab48ce9 Mon Sep 17 00:00:00 2001 From: Levi Thomason Date: Wed, 15 Oct 2014 22:46:20 -0700 Subject: [PATCH 1/3] Fixes #713, #771, #1161 --- lib/less-browser/index.js | 32 ++++++++++++++++++++++---------- 1 file changed, 22 insertions(+), 10 deletions(-) diff --git a/lib/less-browser/index.js b/lib/less-browser/index.js index fcad374a3..5c69aad7d 100644 --- a/lib/less-browser/index.js +++ b/lib/less-browser/index.js @@ -26,6 +26,7 @@ require("./log-listener")(less, options); var errors = require("./error-reporting")(window, less, options); var browser = require("./browser"); var cache = options.cache || require("./cache")(window, options, less.logger); +var PromiseConstructor = typeof Promise === 'undefined' ? require('promise') : Promise; options.env = options.env || (window.location.hostname == '127.0.0.1' || window.location.hostname == '0.0.0.0' || @@ -222,16 +223,23 @@ if (/!watch/.test(location.hash)) { // // Get all tags with the 'rel' attribute set to "stylesheet/less" // -var links = document.getElementsByTagName('link'); - -less.sheets = []; +less.registerStylesheets = function(callback) { + return new PromiseConstructor(function(resolve, reject) { + var links = document.getElementsByTagName('link'); + less.sheets = []; + + for (var i = 0; i < links.length; i++) { + if (links[i].rel === 'stylesheet/less' || (links[i].rel.match(/stylesheet/) && + (links[i].type.match(typePattern)))) { + less.sheets.push(links[i]); + } -for (var i = 0; i < links.length; i++) { - if (links[i].rel === 'stylesheet/less' || (links[i].rel.match(/stylesheet/) && - (links[i].type.match(typePattern)))) { - less.sheets.push(links[i]); - } -} + if (i === links.length - 1) { + resolve(); + } + } + }); +}; // // With this function, it's possible to alter variables and re-render @@ -269,4 +277,8 @@ less.refresh = function (reload, modifyVars) { less.refreshStyles = loadStyles; -less.refresh(less.env === 'development'); +less.registerStylesheets().then( + function () { + less.refresh(less.env === 'development'); + } +); From 89358faefe8a577cb4adc8cf4c487a5c184a57fe Mon Sep 17 00:00:00 2001 From: Levi Thomason Date: Wed, 15 Oct 2014 23:02:11 -0700 Subject: [PATCH 2/3] more sane resolve --- lib/less-browser/index.js | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) diff --git a/lib/less-browser/index.js b/lib/less-browser/index.js index 5c69aad7d..97a1a9a0c 100644 --- a/lib/less-browser/index.js +++ b/lib/less-browser/index.js @@ -233,11 +233,9 @@ less.registerStylesheets = function(callback) { (links[i].type.match(typePattern)))) { less.sheets.push(links[i]); } - - if (i === links.length - 1) { - resolve(); - } } + + resolve(); }); }; From 0e821f1a4f563f24e4269799d45d223ea31473b9 Mon Sep 17 00:00:00 2001 From: Levi Thomason Date: Wed, 15 Oct 2014 23:03:55 -0700 Subject: [PATCH 3/3] cleanup callback --- lib/less-browser/index.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/less-browser/index.js b/lib/less-browser/index.js index 97a1a9a0c..3855e52ac 100644 --- a/lib/less-browser/index.js +++ b/lib/less-browser/index.js @@ -223,7 +223,7 @@ if (/!watch/.test(location.hash)) { // // Get all tags with the 'rel' attribute set to "stylesheet/less" // -less.registerStylesheets = function(callback) { +less.registerStylesheets = function() { return new PromiseConstructor(function(resolve, reject) { var links = document.getElementsByTagName('link'); less.sheets = [];