-
Notifications
You must be signed in to change notification settings - Fork 4
/
class.query.min.js
12 lines (12 loc) · 2.09 KB
/
class.query.min.js
1
2
3
4
5
6
7
8
9
10
11
12
/*!
* Class Query v0.2.1
*
* Creates media queries from .classquery- classes for elements with data-classquery attributes
*
* Copyright (c) 2013 Matt Stow
*
* http://mattstow.com
*
* Licensed under the MIT license
*/
;(function(b){if(!b.querySelectorAll){return}function F(k,l){var R=k.cssRules?k.cssRules:k.media,Q,S=[],m=R.length;for(var j=0;j<m;j++){Q=R[j];if(l(Q)){S.push(Q)}}return S}function a(i){return F(i,function(j){if(!j.selectorText){return false}else{if(j.selectorText.indexOf(".classquery-")===0){return true}else{return false}}})}function t(j){var k=window.location,i=b.createElement("a");i.href=j;return i.hostname===k.hostname&&i.protocol===k.protocol}function o(i){return !i.ownerNode?false:i.ownerNode.constructor===HTMLStyleElement}function u(i){return i.href&&t(i.href)}function B(){var Q=b.styleSheets,k,m=Q.length,j=0,l=[];for(j;j<m;j++){k=Q[j];if(u(k)||o(k)){l.push(k)}}return l}var A=b.documentElement,w="classquery",q=b.querySelectorAll("[data-"+w+"]"),s=q.length;if(s===0){return}var g=B(),r=g.length,e=[];for(var O=0;O<r;O++){e=e.concat(a(g[O]))}if(e.length===0){return}A.className+=" "+w+"-init";var z=w+"-id",d=e.length,h,P,n,x,H,f,N,c,C,v,y,p,D,E,I,G="";for(var M=0;M<s;M++){h=q[M];h.setAttribute("data-"+z,M);P=h.getAttribute("data-"+w).split(";");n=P.length;x=h.getAttribute("class")?"."+h.getAttribute("class").replace(/\s+/g,"."):"";H=h.getAttribute("id")?"#"+h.getAttribute("id"):"";for(var L=0;L<n;L++){f=P[L].split(",");N=f.length;for(var K=0;K<d;K++){for(var J=0;J<N;J++){f[J]=f[J].trim();if(J%2===1&&e[K].selectorText.indexOf(f[J])>-1){c=e[K].selectorText.replace(/\[/g,"\\[").replace(/\]/g,"\\]").replace(/\(/g,"\\(").replace(/\)/g,"\\)").replace(/\*/g,"\\*").replace(/\+/g,"\\+").replace(/\^/g,"\\^").replace(/\$/g,"\\$")+"\\s*?{";C=new RegExp(c,"g");v="("+f[J]+")(.*?)(,|{)";y=new RegExp(v);p="[data-"+z+'="'+M+'"]'+x+H+c.match(y)[2].replace("s*?","").replace(/\\/g,"");G+="@media "+f[J-1]+"{"+p+" {"+e[K].cssText.replace(C,"")+"}\n"}}}}}I=b.createElement("style");I.appendChild(b.createTextNode(G));b.head.appendChild(I);A.className=A.className.replace(w+"-init",w+"-complete")})(document);