Skip to content

Instantly share code, notes, and snippets.

@demun
Created November 25, 2012 05:25
Show Gist options
  • Save demun/4142494 to your computer and use it in GitHub Desktop.
Save demun/4142494 to your computer and use it in GitHub Desktop.
css3-mediaqueries.min.js
if(typeof Object.create!=="function"){Object.create=function(b){function a(){}a.prototype=b;return new a()}}var ua={toString:function(){return navigator.userAgent},test:function(a){return this.toString().toLowerCase().indexOf(a.toLowerCase())>-1}};ua.version=(ua.toString().toLowerCase().match(/[\s\S]+(?:rv|it|ra|ie)[\/: ]([\d.]+)/)||[])[1];ua.webkit=ua.test("webkit");ua.gecko=ua.test("gecko")&&!ua.webkit;ua.opera=ua.test("opera");ua.ie=ua.test("msie")&&!ua.opera;ua.ie6=ua.ie&&document.compatMode&&typeof document.documentElement.style.maxHeight==="undefined";ua.ie7=ua.ie&&document.documentElement&&typeof document.documentElement.style.maxHeight!=="undefined"&&typeof XDomainRequest==="undefined";ua.ie8=ua.ie&&typeof XDomainRequest!=="undefined";var domReady=function(){var b=[];var a=function(){if(!arguments.callee.done){arguments.callee.done=true;for(var c=0;c<b.length;c++){b[c]()}}};if(document.addEventListener){document.addEventListener("DOMContentLoaded",a,false)}if(ua.ie){(function(){try{document.documentElement.doScroll("left")}catch(c){setTimeout(arguments.callee,50);return}a()})();document.onreadystatechange=function(){if(document.readyState==="complete"){document.onreadystatechange=null;a()}}}if(ua.webkit&&document.readyState){(function(){if(document.readyState!=="loading"){a()}else{setTimeout(arguments.callee,10)}})()}window.onload=a;return function(c){if(typeof c==="function"){b[b.length]=c}return c}}();var cssHelper=function(){var n={BLOCKS:/[^\s{][^{]*\{(?:[^{}]*\{[^{}]*\}[^{}]*|[^{}]*)*\}/g,BLOCKS_INSIDE:/[^\s{][^{]*\{[^{}]*\}/g,DECLARATIONS:/[a-zA-Z\-]+[^;]*:[^;]+;/g,RELATIVE_URLS:/url\(['"]?([^\/\)'"][^:\)'"]+)['"]?\)/g,REDUNDANT_COMPONENTS:/(?:\/\*([^*\\\\]|\*(?!\/))+\*\/|@import[^;]+;)/g,REDUNDANT_WHITESPACE:/\s*(,|:|;|\{|\})\s*/g,MORE_WHITESPACE:/\s{2,}/g,FINAL_SEMICOLONS:/;\}/g,NOT_WHITESPACE:/\S+/g};var l,k=false;var i=[];var h=function(u){if(typeof u==="function"){i[i.length]=u}};var f=function(){for(var u=0;u<i.length;u++){i[u](l)}};var d={};var o=function(y,u){if(d[y]){var w=d[y].listeners;if(w){for(var x=0;x<w.length;x++){w[x](u)}}}};var m=function(v,u,z){if(ua.ie&&!window.XMLHttpRequest){window.XMLHttpRequest=function(){return new ActiveXObject("Microsoft.XMLHTTP")}}if(!XMLHttpRequest){return""}var w=new XMLHttpRequest();try{w.open("get",v,true);w.setRequestHeader("X_REQUESTED_WITH","XMLHttpRequest")}catch(x){z();return}var y=false;setTimeout(function(){y=true},5000);document.documentElement.style.cursor="progress";w.onreadystatechange=function(){if(w.readyState===4&&!y){if(!w.status&&location.protocol==="file:"||(w.status>=200&&w.status<300)||w.status===304||navigator.userAgent.indexOf("Safari")>-1&&typeof w.status==="undefined"){u(w.responseText)}else{z()}document.documentElement.style.cursor="";w=null}};w.send("")};var j=function(u){u=u.replace(n.REDUNDANT_COMPONENTS,"");u=u.replace(n.REDUNDANT_WHITESPACE,"$1");u=u.replace(n.MORE_WHITESPACE," ");u=u.replace(n.FINAL_SEMICOLONS,"}");return u};var g={mediaQueryList:function(C){var u={};var B=C.indexOf("{");var y=C.substring(0,B);C=C.substring(B+1,C.length-1);var x=[],w=[];var v=y.toLowerCase().substring(7).split(",");for(var A=0;A<v.length;A++){x[x.length]=g.mediaQuery(v[A],u)}var z=C.match(n.BLOCKS_INSIDE);if(z!==null){for(A=0;A<z.length;A++){w[w.length]=g.rule(z[A],u)}}u.getMediaQueries=function(){return x};u.getRules=function(){return w};u.getListText=function(){return y};u.getCssText=function(){return C};return u},mediaQuery:function(D,C){D=D||"";var w=false,A;var z=[];var y=true;var x=D.match(n.NOT_WHITESPACE);for(var B=0;B<x.length;B++){var v=x[B];if(!A&&(v==="not"||v==="only")){if(v==="not"){w=true}}else{if(!A){A=v}else{if(v.charAt(0)==="("){var u=v.substring(1,v.length-1).split(":");z[z.length]={mediaFeature:u[0],value:u[1]||null}}}}}return{getList:function(){return C||null},getValid:function(){return y},getNot:function(){return w},getMediaType:function(){return A},getExpressions:function(){return z}}},rule:function(B,y){var u={};var z=B.indexOf("{");var A=B.substring(0,z);var C=A.split(",");var v=[];var w=B.substring(z+1,B.length-1).split(";");for(var x=0;x<w.length;x++){v[v.length]=g.declaration(w[x],u)}u.getMediaQueryList=function(){return y||null};u.getSelectors=function(){return C};u.getSelectorText=function(){return A};u.getDeclarations=function(){return v};u.getPropertyValue=function(E){for(var D=0;D<v.length;D++){if(v[D].getProperty()===E){return v[D].getValue()}}return null};return u},declaration:function(x,y){var u=x.indexOf(":");var z=x.substring(0,u);var w=x.substring(u+1);return{getRule:function(){return y||null},getProperty:function(){return z},getValue:function(){return w}}}};var a=function(x){if(typeof x.cssHelperText!=="string"){return}var w={mediaQueryLists:[],rules:[],selectors:{},declarations:[],properties:{}};var C=w.mediaQueryLists;var E=w.rules;var B=x.cssHelperText.match(n.BLOCKS);if(B!==null){for(var D=0;D<B.length;D++){if(B[D].substring(0,7)==="@media "){C[C.length]=g.mediaQueryList(B[D]);E=w.rules=E.concat(C[C.length-1].getRules())}else{E[E.length]=g.rule(B[D])}}}var u=w.selectors;var z=function(H){var G=H.getSelectors();for(var F=0;F<G.length;F++){var I=G[F];if(!u[I]){u[I]=[]}u[I][u[I].length]=H}};for(D=0;D<E.length;D++){z(E[D])}var A=w.declarations;for(D=0;D<E.length;D++){A=w.declarations=A.concat(E[D].getDeclarations())}var v=w.properties;for(D=0;D<A.length;D++){var y=A[D].getProperty();if(!v[y]){v[y]=[]}v[y][v[y].length]=A[D]}x.cssHelperParsed=w;l[l.length]=x;return w};var c=function(v,u){v.cssHelperText=j(u||v.innerHTML);return a(v)};var b=function(){k=true;l=[];var u=[];var v=function(){for(var D=0;D<u.length;D++){a(u[D])}var C=document.getElementsByTagName("style");for(D=0;D<C.length;D++){c(C[D])}k=false;f()};var B=document.getElementsByTagName("link");for(var w=0;w<B.length;w++){var z=B[w];if(z.getAttribute("rel").indexOf("style")>-1&&z.href&&z.href.length!==0&&!z.disabled){u[u.length]=z}}if(u.length>0){var A=0;var y=function(){A++;if(A===u.length){v()}};var x=function(D){var C=D.href;m(C,function(E){E=j(E).replace(n.RELATIVE_URLS,"url("+C.substring(0,C.lastIndexOf("/"))+"/$1)");D.cssHelperText=E;y()},y)};for(w=0;w<u.length;w++){x(u[w])}}else{v()}};var p={mediaQueryLists:"array",rules:"array",selectors:"object",declarations:"array",properties:"object"};var t={mediaQueryLists:null,rules:null,selectors:null,declarations:null,properties:null};var s=function(w,u){if(t[w]!==null){if(p[w]==="array"){return(t[w]=t[w].concat(u))}else{var y=t[w];for(var x in u){if(u.hasOwnProperty(x)){if(!y[x]){y[x]=u[x]}else{y[x]=y[x].concat(u[x])}}}return y}}};var r=function(u){t[u]=(p[u]==="array")?[]:{};for(var v=0;v<l.length;v++){s(u,l[v].cssHelperParsed[u])}return t[u]};domReady(function(){var v=document.body.getElementsByTagName("*");for(var u=0;u<v.length;u++){v[u].checkedByCssHelper=true}if(document.implementation.hasFeature("MutationEvents","2.0")||window.MutationEvent){document.body.addEventListener("DOMNodeInserted",function(x){var w=x.target;if(w.nodeType===1){o("DOMElementInserted",w);w.checkedByCssHelper=true}},false)}else{setInterval(function(){var x=document.body.getElementsByTagName("*");for(var w=0;w<x.length;w++){if(!x[w].checkedByCssHelper){o("DOMElementInserted",x[w]);x[w].checkedByCssHelper=true}}},1000)}});var q=function(u){if(typeof window.innerWidth!="undefined"){return window["inner"+u]}else{if(typeof document.documentElement!="undefined"&&typeof document.documentElement.clientWidth!="undefined"&&document.documentElement.clientWidth!=0){return document.documentElement["client"+u]}}};return{addStyle:function(w,u){var v=document.createElement("style");v.setAttribute("type","text/css");document.getElementsByTagName("head")[0].appendChild(v);if(v.styleSheet){v.styleSheet.cssText=w}else{v.appendChild(document.createTextNode(w))}v.addedWithCssHelper=true;if(typeof u==="undefined"||u===true){cssHelper.parsed(function(x){var y=c(v,w);for(var z in y){if(y.hasOwnProperty(z)){s(z,y[z])}}o("newStyleParsed",v)})}else{v.parsingDisallowed=true}return v},removeStyle:function(u){return u.parentNode.removeChild(u)},parsed:function(u){if(k){h(u)}else{if(typeof l!=="undefined"){if(typeof u==="function"){u(l)}}else{h(u);b()}}},mediaQueryLists:function(u){cssHelper.parsed(function(v){u(t.mediaQueryLists||r("mediaQueryLists"))})},rules:function(u){cssHelper.parsed(function(v){u(t.rules||r("rules"))})},selectors:function(u){cssHelper.parsed(function(v){u(t.selectors||r("selectors"))})},declarations:function(u){cssHelper.parsed(function(v){u(t.declarations||r("declarations"))})},properties:function(u){cssHelper.parsed(function(v){u(t.properties||r("properties"))})},broadcast:o,addListener:function(v,u){if(typeof u==="function"){if(!d[v]){d[v]={listeners:[]}}d[v].listeners[d[v].listeners.length]=u}},removeListener:function(x,w){if(typeof w==="function"&&d[x]){var u=d[x].listeners;for(var v=0;v<u.length;v++){if(u[v]===w){u.splice(v,1);v-=1}}}},getViewportWidth:function(){return q("Width")},getViewportHeight:function(){return q("Height")}}}();domReady(function enableCssMediaQueries(){var k;var i={LENGTH_UNIT:/[0-9]+(em|ex|px|in|cm|mm|pt|pc)$/,RESOLUTION_UNIT:/[0-9]+(dpi|dpcm)$/,ASPECT_RATIO:/^[0-9]+\/[0-9]+$/,ABSOLUTE_VALUE:/^[0-9]*(\.[0-9]+)*$/};var h=[];var n=function(){var s="css3-mediaqueries-test";var q=document.createElement("div");q.id=s;var r=cssHelper.addStyle("@media all and (width) { #"+s+" { width: 1px !important; } }",false);document.body.appendChild(q);var p=q.offsetWidth===1;r.parentNode.removeChild(r);q.parentNode.removeChild(q);n=function(){return p};return p};var m=function(){k=document.createElement("div");k.style.cssText="position:absolute;top:-9999em;left:-9999em;margin:0;border:none;padding:0;width:1em;font-size:1em;";document.body.appendChild(k);if(k.offsetWidth!==16){k.style.fontSize=16/k.offsetWidth+"em"}k.style.width=""};var l=function(q){k.style.width=q;var p=k.offsetWidth;k.style.width="";return p};var j=function(z,y){var u=z.length;var x=(z.substring(0,4)==="min-");var B=(!x&&z.substring(0,4)==="max-");if(y!==null){var w;var v;if(i.LENGTH_UNIT.exec(y)){w="length";v=l(y)}else{if(i.RESOLUTION_UNIT.exec(y)){w="resolution";v=parseInt(y,10);var t=y.substring((v+"").length)}else{if(i.ASPECT_RATIO.exec(y)){w="aspect-ratio";v=y.split("/")}else{if(i.ABSOLUTE_VALUE){w="absolute";v=y}else{w="unknown"}}}}}var s,r;if("device-width"===z.substring(u-12,u)){s=screen.width;if(y!==null){if(w==="length"){return((x&&s>=v)||(B&&s<v)||(!x&&!B&&s===v))}else{return false}}else{return s>0}}else{if("device-height"===z.substring(u-13,u)){r=screen.height;if(y!==null){if(w==="length"){return((x&&r>=v)||(B&&r<v)||(!x&&!B&&r===v))}else{return false}}else{return r>0}}else{if("width"===z.substring(u-5,u)){s=document.documentElement.clientWidth||document.body.clientWidth;if(y!==null){if(w==="length"){return((x&&s>=v)||(B&&s<v)||(!x&&!B&&s===v))}else{return false}}else{return s>0}}else{if("height"===z.substring(u-6,u)){r=document.documentElement.clientHeight||document.body.clientHeight;if(y!==null){if(w==="length"){return((x&&r>=v)||(B&&r<v)||(!x&&!B&&r===v))}else{return false}}else{return r>0}}else{if("device-aspect-ratio"===z.substring(u-19,u)){return w==="aspect-ratio"&&screen.width*v[1]===screen.height*v[0]}else{if("color-index"===z.substring(u-11,u)){var q=Math.pow(2,screen.colorDepth);if(y!==null){if(w==="absolute"){return((x&&q>=v)||(B&&q<v)||(!x&&!B&&q===v))}else{return false}}else{return q>0}}else{if("color"===z.substring(u-5,u)){var p=screen.colorDepth;if(y!==null){if(w==="absolute"){return((x&&p>=v)||(B&&p<v)||(!x&&!B&&p===v))}else{return false}}else{return p>0}}else{if("resolution"===z.substring(u-10,u)){var A;if(t==="dpcm"){A=l("1cm")}else{A=l("1in")}if(y!==null){if(w==="resolution"){return((x&&A>=v)||(B&&A<v)||(!x&&!B&&A===v))}else{return false}}else{return A>0}}else{return false}}}}}}}}};var g=function(r){var u=r.getValid();var t=r.getExpressions();var p=t.length;if(p>0){for(var q=0;q<p&&u;q++){u=j(t[q].mediaFeature,t[q].value)}var s=r.getNot();return(u&&!s||s&&!u)}};var f=function(p){var u=p.getMediaQueries();var r={};for(var q=0;q<u.length;q++){if(g(u[q])){r[u[q].getMediaType()]=true}}var v=[],x=0;for(var w in r){if(r.hasOwnProperty(w)){if(x>0){v[x++]=","}v[x++]=w}}if(v.length>0){h[h.length]=cssHelper.addStyle("@media "+v.join("")+"{"+p.getCssText()+"}",false)}};var d=function(q){for(var p=0;p<q.length;p++){f(q[p])}if(ua.ie){document.documentElement.style.display="block";setTimeout(function(){document.documentElement.style.display=""},0);setTimeout(function(){cssHelper.broadcast("cssMediaQueriesTested")},100)}else{cssHelper.broadcast("cssMediaQueriesTested")}};var c=function(){for(var p=0;p<h.length;p++){cssHelper.removeStyle(h[p])}h=[];cssHelper.mediaQueryLists(d)};var b=0;var a=function(){var s=cssHelper.getViewportWidth();var r=cssHelper.getViewportHeight();if(ua.ie){var t=document.createElement("div");t.style.position="absolute";t.style.top="-9999em";t.style.overflow="scroll";document.body.appendChild(t);b=t.offsetWidth-t.clientWidth;document.body.removeChild(t)}var q;var p=function(){var u=cssHelper.getViewportWidth();var v=cssHelper.getViewportHeight();if(Math.abs(u-s)>b||Math.abs(v-r)>b){s=u;r=v;clearTimeout(q);q=setTimeout(function(){if(!n()){c()}else{cssHelper.broadcast("cssMediaQueriesTested")}},500)}};window.onresize=function(){var u=window.onresize||function(){};return function(){u();p()}}()};var o=document.documentElement;o.style.marginLeft="-32767px";setTimeout(function(){o.style.marginTop=""},20000);return function(){if(!n()){cssHelper.addListener("newStyleParsed",function(p){d(p.cssHelperParsed.mediaQueryLists)});cssHelper.addListener("cssMediaQueriesTested",function(){if(ua.ie){o.style.width="1px"}setTimeout(function(){o.style.width="";o.style.marginLeft=""},0);cssHelper.removeListener("cssMediaQueriesTested",arguments.callee)});m();c()}else{o.style.marginLeft=""}a()}}());try{document.execCommand("BackgroundImageCache",false,true)}catch(e){};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment