From e5b18c08cefbf432bab8c8cc6382b09cdd1547cc Mon Sep 17 00:00:00 2001 From: Moez Bouhlel Date: Wed, 22 Oct 2014 16:08:38 +0100 Subject: [PATCH 1/4] add support to read options from script tag data attr --- lib/less-browser/index.js | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/lib/less-browser/index.js b/lib/less-browser/index.js index 0553155a..a69ee5c1 100644 --- a/lib/less-browser/index.js +++ b/lib/less-browser/index.js @@ -14,6 +14,15 @@ var less; var isFileProtocol = /^(file|chrome(-extension)?|resource|qrc|app):/.test(window.location.protocol), 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]; + // shim Promise if required require('promise/polyfill.js'); From 0681e59d3da5d4a439c98c851752dfddfb72500d Mon Sep 17 00:00:00 2001 From: Moez Bouhlel Date: Wed, 22 Oct 2014 18:00:33 +0100 Subject: [PATCH 2/4] 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 a69ee5c1..36cda978 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 8d6644fc..7fbfba30 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; } }; From e2b30eb1b35ccdbca87cfb0e2b5b67d4c622db7a Mon Sep 17 00:00:00 2001 From: Moez Bouhlel Date: Wed, 22 Oct 2014 18:57:29 +0100 Subject: [PATCH 3/4] improve support to read options from script tag data attr - part 2 --- lib/less-browser/utils.js | 7 ------- 1 file changed, 7 deletions(-) diff --git a/lib/less-browser/utils.js b/lib/less-browser/utils.js index 7fbfba30..5a881c8d 100644 --- a/lib/less-browser/utils.js +++ b/lib/less-browser/utils.js @@ -8,19 +8,12 @@ module.exports = { .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 From 815e977b0b39c3b990726d904102064abd3aa0d3 Mon Sep 17 00:00:00 2001 From: Moez Bouhlel Date: Wed, 22 Oct 2014 19:20:20 +0100 Subject: [PATCH 4/4] add support to read options from less file link tag data attr --- lib/less-browser/index.js | 8 ++++++-- lib/less-browser/utils.js | 15 +++++---------- 2 files changed, 11 insertions(+), 12 deletions(-) diff --git a/lib/less-browser/index.js b/lib/less-browser/index.js index 36cda978..8d847b1a 100644 --- a/lib/less-browser/index.js +++ b/lib/less-browser/index.js @@ -16,7 +16,11 @@ var isFileProtocol = /^(file|chrome(-extension)?|resource|qrc|app):/.test(window options = window.less || {}; // use options from the current script tag data attribues -options = addDataAttr(options); +var script = document.currentScript || (function() { + var scripts = document.getElementsByTagName("script"); + return scripts[scripts.length - 1]; +})(); +options = addDataAttr(options, script); // shim Promise if required require('promise/polyfill.js'); @@ -127,7 +131,7 @@ function loadStyles(modifyVars) { function loadStyleSheet(sheet, callback, reload, remaining, modifyVars) { - var instanceOptions = clone(options); + var instanceOptions = addDataAttr(clone(options), sheet); instanceOptions.mime = sheet.type; if (modifyVars) { diff --git a/lib/less-browser/utils.js b/lib/less-browser/utils.js index 5a881c8d..5a771088 100644 --- a/lib/less-browser/utils.js +++ b/lib/less-browser/utils.js @@ -1,4 +1,3 @@ -/* global document */ module.exports = { extractId: function(href) { return href.replace(/^[a-z-]+:\/+?[^\/]+/, '' ) // Remove protocol & domain @@ -7,17 +6,13 @@ module.exports = { .replace(/[^\.\w-]+/g, '-') // Replace illegal characters .replace(/\./g, ':'); // Replace dots with colons(for valid id) }, - addDataAttr: function(options) { - 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)) { + addDataAttr: function(options, tag) { + for (var opt in tag.dataset) + if (tag.dataset.hasOwnProperty(opt)) { if (opt === "env" || opt === "dumpLineNumbers" || opt === "rootpath" || opt === "errorReporting") - options[opt] = script.dataset[opt]; + options[opt] = tag.dataset[opt]; else - options[opt] = JSON.parse(script.dataset[opt]); + options[opt] = JSON.parse(tag.dataset[opt]); } return options; }