[Pkg-javascript-commits] [less.js] 80/285: move render to be accessible in all environments and start using it in the browser

Jonas Smedegaard dr at jones.dk
Mon Oct 26 23:23:40 UTC 2015


This is an automated email from the git hooks/post-receive script.

js pushed a commit to annotated tag v2.0.0
in repository less.js.

commit a0658a4b1533ab6bdaf2ddea6210134d0cd07dc6
Author: Luke Page <luke.a.page at gmail.com>
Date:   Wed Sep 3 15:07:33 2014 +0100

    move render to be accessible in all environments and start using it in the browser
---
 lib/less/browser.js        | 59 ++++++++++++++++++++++++++++++----------------
 lib/less/index.js          | 31 ------------------------
 lib/less/non-node-index.js |  1 +
 lib/less/render.js         | 34 ++++++++++++++++++++++++++
 4 files changed, 74 insertions(+), 51 deletions(-)

diff --git a/lib/less/browser.js b/lib/less/browser.js
index e254735..0b4c688 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 f764f66..cb4719f 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 0a618c3..0e43bc0 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 0000000..d4ea7cc
--- /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);
+            });
+        }
+    };
+};

-- 
Alioth's /usr/local/bin/git-commit-notice on /srv/git.debian.org/git/pkg-javascript/less.js.git



More information about the Pkg-javascript-commits mailing list