function to parse LESS variables to Javascript
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* getLessVars parses your LESS variables to Javascript (provided you make a dummy node in LESS) | |
* @param {String} id The CSS-id your variables are listed under. | |
* @param {Boolean} [parseNumbers=true] Try to parse units as numbers. | |
* @return {Object} A value object containing your LESS variables. | |
* @example | |
* LESS: | |
* @myLessVariable: 123px; | |
* #dummyLessId { width: @myLessVariable; } | |
* Javascript: | |
* getLessVars('dummyLessId'); | |
* returns: | |
* {myLessVariable:123} | |
*/ | |
function getLessVars(id,parseNumbers) { | |
var bNumbers = parseNumbers===undefined?true:parseNumbers | |
,oLess = {} | |
,rgId = /\#[\w-]+/ | |
,rgKey = /\.([\w-]+)/ | |
,rgUnit = /[a-z]+$/ | |
,aUnits = 'em,ex,ch,rem,vw,vh,vmin,cm,mm,in,pt,pc,px,deg,grad,rad,turn,s,ms,Hz,kHz,dpi,dpcm,dppx'.split(',') | |
,rgValue = /:\s?(.*)\s?;\s?\}/ | |
,rgStr = /^'([^']+)'$/ | |
,sId = '#'+id | |
,oStyles = document.styleSheets; | |
for (var i=0,l=oStyles.length;i<l;i++) { | |
var oRules; | |
try{ oRules = oStyles[i].cssRules; } | |
catch (e) { continue; } | |
if (oRules) { | |
for (var j=0,k=oRules.length;j<k;j++) { | |
try { var sRule = oRules[j].cssText; } | |
catch (e) { continue; } | |
var aMatchId = sRule.match(rgId); | |
if (aMatchId&&aMatchId[0]==sId) { | |
var aKey = sRule.match(rgKey) | |
,aVal = sRule.match(rgValue); | |
if (aKey&&aVal) { | |
var sKey = aKey[1] | |
,oVal = aVal[1] | |
,aUnit | |
,aStr; | |
if (bNumbers&&(aUnit=oVal.match(rgUnit))&&aUnits.indexOf(aUnit[0])!==-1) { | |
oVal = parseFloat(oVal); | |
} else if (aStr=oVal.match(rgStr)) { | |
oVal = aStr[1]; | |
} | |
oLess[sKey] = oVal; | |
} | |
} | |
} | |
} | |
} | |
return oLess; | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{ | |
"name": "getLessVars", | |
"version": "0.1.0" | |
} |
This doesn't allow for hyphenated variables names. Modifying these two lines seems to work:
,rgId = /\#[\w-]+/
,rgKey = /\.([\w-]+)/
Also this function fails in Firefix when including external stylesheets (such as Google Web Fonts) because of a security exception. To step around that change:
var oRules = oStyles[i].cssRules;
to:
try{ var oRules = oStyles[i].cssRules; }
catch (e) { continue; }
This thing is great! Thank you!
nathanpbell's comment is true for IE11 too.
There's an error in IE10 when the LESS variables include a keyframe rule—the cssText attribute isn't available (see http://jsbin.com/jituquqewe/edit?html,output for an example).
Same workound approach as above: change
var sRule = oRules[j].cssText,
aMatchId = sRule.match(rgId);
to
try {
var sRule = oRules[j].cssText;
}
catch (e) { continue; }
var aMatchId = sRule.match(rgId);
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Examples:
http://jsfiddle.net/Sjeiti/VHQ8x/
http://jsfiddle.net/Sjeiti/WdyGy/