diff --git a/lib/less/browser.js b/lib/less/browser.js index e2547352..0b4c6885 100644 --- a/lib/less/browser.js +++ b/lib/less/browser.js @@ -312,37 +312,56 @@ function removeError(path) { } } +function clone(obj) { + var cloned = {}; + for(var prop in obj) { + if (obj.hasOwnProperty(prop)) { + cloned[prop] = obj[prop]; + } + } + return cloned; +} + +// only really needed for phantom +function bind(func, thisArg) { + var curryArgs = Array.prototype.slice.call(arguments, 2); + return function() { + var args = curryArgs.concat(Array.prototype.slice.call(arguments, 0)); + return func.apply(thisArg, args); + }; +} + function loadStyles(modifyVars) { 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.contexts.parseEnv(options), - lessText = style.innerHTML || ''; - env.filename = document.location.href.replace(/#.*$/, ''); + var instanceOptions = clone(options); + instanceOptions.modifyVars = modifyVars; + var lessText = style.innerHTML || ''; + instanceOptions.filename = document.location.href.replace(/#.*$/, ''); - if (modifyVars || options.globalVars) { - env.useFileCache = true; + if (modifyVars || instanceOptions.globalVars) { + instanceOptions.useFileCache = true; } /*jshint loopfunc:true */ - // use closure to store current value of i - var callback = (function(style) { - return function (e, cssAST) { - if (e) { - return error(e, "inline"); - } - var css = cssAST.toCSS(options); + // use closure to store current style + less.render(lessText, instanceOptions) + .then( + bind(function(style, css) { style.type = 'text/css'; if (style.styleSheet) { style.styleSheet.cssText = css; } else { style.innerHTML = css; } - }; - })(style); - new(less.Parser)(env).parse(lessText, callback, {globalVars: options.globalVars, modifyVars: modifyVars}); + }, null, style), + function(e) { + error(e, "inline"); + }); } } } @@ -417,7 +436,7 @@ function initRunningMode(){ if (e) { error(e, sheet.href); } else if (root) { - var styles = root.toCSS(less); + var styles = root.toCSS(options); styles = postProcessCSS(styles); createCSS(styles, sheet, env.lastModified); } @@ -477,20 +496,20 @@ less.refresh = function (reload, modifyVars) { var startTime, endTime; startTime = endTime = new Date(); - loadStyleSheets(function (e, root, _, sheet, env) { + loadStyleSheets(function (e, root, _, sheet, webInfo) { if (e) { return error(e, sheet.href); } - if (env.local) { + if (webInfo.local) { log("loading " + sheet.href + " from cache.", logLevel.info); } else { log("parsed " + sheet.href + " successfully.", logLevel.debug); var styles = root.toCSS(options); styles = postProcessCSS(styles); - createCSS(styles, sheet, env.lastModified); + createCSS(styles, sheet, webInfo.lastModified); } log("css for " + sheet.href + " generated in " + (new Date() - endTime) + 'ms', logLevel.info); - if (env.remaining === 0) { + if (webInfo.remaining === 0) { log("less has finished. css generated in " + (new Date() - startTime) + 'ms', logLevel.info); } endTime = new Date(); diff --git a/lib/less/index.js b/lib/less/index.js index f764f66e..cb4719f3 100644 --- a/lib/less/index.js +++ b/lib/less/index.js @@ -1,37 +1,6 @@ -var PromiseConstructor = typeof Promise === 'undefined' ? require('promise') : Promise; var environment = require("./environment/node"); var less = require("./non-node-index.js")(environment); -less.render = function (input, options, callback) { - options = options || {}; - - if (typeof(options) === 'function') { - callback = options; - options = {}; - } - - var parser = new(less.Parser)(options); - - if (callback) { - parser.parse(input, function (e, root) { - if (e) { callback(e); return; } - var css; - try { - css = root && root.toCSS && root.toCSS(options); - } - catch (err) { callback(err); return; } - callback(null, css); - }, options); - } else { - return new PromiseConstructor(function (resolve, reject) { - parser.parse(input, function (e, root) { - if (e) { return reject(e); } - try { resolve(root.toCSS(options)); } - catch (err) { reject( err); } - }, options); - }); - } -}; less.formatError = function(ctx, options) { options = options || {}; diff --git a/lib/less/non-node-index.js b/lib/less/non-node-index.js index 0a618c3e..0e43bc0b 100644 --- a/lib/less/non-node-index.js +++ b/lib/less/non-node-index.js @@ -9,6 +9,7 @@ module.exports = function(environment) { contexts: require("./contexts.js"), environment: environment }; + less.render = require("./render.js")(less.Parser); return less; }; diff --git a/lib/less/render.js b/lib/less/render.js new file mode 100644 index 00000000..d4ea7cc5 --- /dev/null +++ b/lib/less/render.js @@ -0,0 +1,34 @@ +var PromiseConstructor = typeof Promise === 'undefined' ? require('promise') : Promise; + +module.exports = function(Parser) { + return function (input, options, callback) { + options = options || {}; + + if (typeof(options) === 'function') { + callback = options; + options = {}; + } + + var parser = new(Parser)(options); + + if (callback) { + parser.parse(input, function (e, root) { + if (e) { callback(e); return; } + var css; + try { + css = root && root.toCSS && root.toCSS(options); + } + catch (err) { callback(err); return; } + callback(null, css); + }, options); + } else { + return new PromiseConstructor(function (resolve, reject) { + parser.parse(input, function (e, root) { + if (e) { return reject(e); } + try { resolve(root.toCSS(options)); } + catch (err) { reject( err); } + }, options); + }); + } + }; +};