[Pkg-javascript-commits] [less.js] 219/285: improve support to read options from script tag data attr

Jonas Smedegaard dr at jones.dk
Mon Oct 26 23:23:56 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 0681e59d3da5d4a439c98c851752dfddfb72500d
Author: Moez Bouhlel <bmoez.j at gmail.com>
Date:   Wed Oct 22 18:00:33 2014 +0100

    improve support to read options from script tag data attr
---
 lib/less-browser/index.js |  9 ++-------
 lib/less-browser/utils.js | 22 ++++++++++++++++++++++
 2 files changed, 24 insertions(+), 7 deletions(-)

diff --git a/lib/less-browser/index.js b/lib/less-browser/index.js
index a69ee5c..36cda97 100644
--- a/lib/less-browser/index.js
+++ b/lib/less-browser/index.js
@@ -4,6 +4,7 @@
 /*global window, document, location */
 
 var less;
+var addDataAttr = require("./utils").addDataAttr;
 
 /*
   TODO - options is now hidden - we should expose it on the less object, but not have it "as" the less object
@@ -15,13 +16,7 @@ var isFileProtocol = /^(file|chrome(-extension)?|resource|qrc|app):/.test(window
     options = window.less || {};
 
 // use options from the current script tag data attribues
-var script = document.currentScript || (function() {
-    var scripts = document.getElementsByTagName("script");
-    return scripts[scripts.length - 1];
-})();
-for (var opt in script.dataset)
-    if(script.dataset.hasOwnProperty(opt))
-        options[opt] = script.dataset[opt];
+options = addDataAttr(options);
 
 // shim Promise if required
 require('promise/polyfill.js');
diff --git a/lib/less-browser/utils.js b/lib/less-browser/utils.js
index 8d6644f..7fbfba3 100644
--- a/lib/less-browser/utils.js
+++ b/lib/less-browser/utils.js
@@ -1,3 +1,4 @@
+/* global document */
 module.exports = {
     extractId: function(href) {
         return href.replace(/^[a-z-]+:\/+?[^\/]+/, '' )  // Remove protocol & domain
@@ -5,5 +6,26 @@ module.exports = {
             .replace(/\.[a-zA-Z]+$/,        '' )  // Remove simple extension
             .replace(/[^\.\w-]+/g,          '-')  // Replace illegal characters
             .replace(/\./g,                 ':'); // Replace dots with colons(for valid id)
+    },
+    addDataAttr: function(options) {
+        function reformatOptionName(option) {
+            return String.replace(option , /_(.)/g, function( match, p1) {
+                return p1.toUpperCase();
+            });
+        }
+
+        var script = document.currentScript || (function() {
+            var scripts = document.getElementsByTagName("script");
+            return scripts[scripts.length - 1];
+        })();
+        for (var opt in script.dataset)
+            if (script.dataset.hasOwnProperty(opt)) {
+                opt = reformatOptionName(opt);
+                if (opt === "env" || opt === "dumpLineNumbers" || opt === "rootpath" || opt === "errorReporting")
+                    options[opt] = script.dataset[opt];
+                else
+                    options[opt] = JSON.parse(script.dataset[opt]);
+            }
+        return 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