Skip to content

Instantly share code, notes, and snippets.

@Sjeiti
Last active September 22, 2020 04:51
Show Gist options
  • Star 6 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Sjeiti/2948738 to your computer and use it in GitHub Desktop.
Save Sjeiti/2948738 to your computer and use it in GitHub Desktop.
function to parse LESS variables to Javascript
/**
* 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;
}
{
"name": "getLessVars",
"version": "0.1.0"
}
@Sjeiti
Copy link
Author

Sjeiti commented Jun 19, 2012

@nathanpbell
Copy link

This doesn't allow for hyphenated variables names. Modifying these two lines seems to work:

    ,rgId = /\#[\w-]+/
    ,rgKey = /\.([\w-]+)/

@nathanpbell
Copy link

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; }

@malbert69
Copy link

This thing is great! Thank you!

@jankrogh
Copy link

nathanpbell's comment is true for IE11 too.

@danblaker
Copy link

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