mq parser
based on respond.js by Scott Jehl
by John Allan
JavaScript
getEmValue = function () {
var ret,
win = window,
doc = win.document,
docElem = doc.documentElement,
div = doc.createElement('div'),
body = doc.body,
fakeUsed = false;
div.style.cssText = "position:absolute;font-size:1em;width:1em";
if (!body) {
body = fakeUsed = doc.createElement("body");
body.style.background = "none";
}
body.appendChild(div);
docElem.insertBefore(body, docElem.firstChild);
ret = div.offsetWidth;
if (fakeUsed) {
docElem.removeChild(body);
} else {
body.removeChild(div);
}
//also update eminpx before returning
ret = eminpx = parseFloat(ret);
return ret;
}
//enable/disable styles
testMq = function (mqObj) {
var name = "clientWidth",
win = window,
doc = win.document,
docElem = doc.documentElement,
docElemProp = docElem[name],
currWidth = doc.compatMode === "CSS1Compat" && docElemProp || doc.body[name] || docElemProp,
min = mqObj.minw,
max = mqObj.maxw,
minnull = min === null,
maxnull = max === null,
em = "em";
if (!!min) {
min = parseFloat(min) * (min.indexOf(em) > -1 ? (eminpx || getEmValue()) : 1);
}
if (!!max) {
max = parseFloat(max) * (max.indexOf(em) > -1 ? (eminpx || getEmValue()) : 1);
}
// if min or max is not null, and if either is present, they're true
if ((!minnull || !maxnull) && (minnull || currWidth >= min) && (maxnull || currWidth <= max)) {
return true;
}
return false;
}
function parseMq(mq) {
var mqObj;
mqObj = {};
mqObj.media = mq.split("(")[0].match(/(only\s+)?([a-zA-Z]+)\s?/) && RegExp.$2 || "all";
mqObj.minw = mq.match(/\(min\-width:[\s]*([\s]*[0-9\.]+)(px|em)[\s]*\)/) &&...