form-cheatsheet/js/polyfills/Dom/CSS_selector_engine.ielt8.js

25 lines
9.2 KiB
JavaScript

/* qS[A]/matches polyfill | @version 1.0 | MIT License | github.com/termi */
;(function(){
var d=void 0,j=!0,s=null,u=!1,w=window;function C(){throw Error("SYNTAX_ERR");}
function aa(a,c,e,i,g,k,t,z,f){var P=e||[],q=!!g,x=!q&&c&&"number"==typeof c.length&&c.nodeType===d,n=q&&(c={})||(!c?document:x?c[0]:c),Q,B=0,b,y,l,J=ba[(a[1]||"").replace(ca,"")]||0,W=2<J,E=a[2],R=!!E,F=a[3],L=!!F,G=a[4],M=!!G,ea="*"===E,r,X,A,v,h,m,D,fa,H,ga,p,N;if(R)if(E=ea?s:E.replace("|",":")){if(A=n)A=(A=n&&(n.ownerDocument||n).documentElement)?"HTML"!==A.nodeName:u;(m=A)||(E=E.toUpperCase())}else R=u;if(M&&(G=G.replace(da," "),!I||1!==J))fa=RegExp(G.replace(K,O));if(A=a[5]){A=ha.call(A,"][");
for(r=-1;v=A[++r];)if((A[r]=v=v.match(ia))||C(),v[0]=v.input=v.index=s,v[2]=ja[v[2]],(p=v[3])&&" i"==p.substr(p.length-2))v[3]=p.substr(0,p.length-2),v[4]=j}if(a=a[6]){a=ha.call(a,":");for(r=-1;h=a[++r];)if((a[r]=h=h.match(ka))||C(),h[0]=h.input=h.index=s,p=h[1]=la[h[1]],2>p&&h[2])ma.test(h[2])?"even"===h[2]?h[2]=[s,2]:"odd"===h[2]?h[2]=[s,2,"%",1]:(h[2]=h[2].match(na),h[2][0]=s):h[2]=[s,0,"%",h[2]],h[3]=p?"nodeIndexLast":"nodeIndex",h[4]=p?"lastChild":"firstChild",h[5]=p?"previousSibling":"nextSibling";
else if(17===p)f||C(),q=j,g=x?c:[c],a[r]=s;else if(12==p&&(m=h[2]))oa.test(m.charAt(0))&&oa.test(m.charAt(m.length-1))&&(h[2]=m.substr(1,m.length-2));1==a.length&&a[0]===s&&(a=s)}q&&(J=0);if(1==J)if(L){L=9===n.nodeType?n:n.ownerDocument;if(pa){g=L.getElementsByName(F);H=[];for(r=-1;b=g[++r];)b.id==F&&H.push(b)}else{H=[];for(g=[];b=L.getElementById(F);)g.push(b),m="id"in b?b.id:b.getAttribute("id"),m==F&&H.push(b),b.setAttribute("id",F+" _");for(r=-1;b=g[++r];)b.setAttribute("id",F)}g=s;L=u}else M=
M&&!I,R=R&&I&&!!G;q=(!x||1===c.length)&&!t&&!i&&!A&&!a&&!R&&!M&&!L&&!z;do{switch(J){case 0:b=g[0];break;case 1:if(F)if(g=[],H.length)if(1<H.length)for(r=-1;b=H[++r];){if(9===n.nodeType||n.contains(b))g.push(b),H.splice(r--,1)}else g=H;else return P;else"BODY"===E&&9===n.nodeType?(g=[n.body],M=!!G,q=q&&!M):g=!G||!I?n.getElementsByTagName(E||"*"):"getElementsByClassName"in n?n.getElementsByClassName(G):I.call(n,G);b=g[0];break;case 2:g=n.children;b=g[0];break;case 3:Q=c[B+1];case 4:if(!(b=S(n)))continue}if(q)return g;
x=0;if(b){do if((!ea||1===b.nodeType)&&!(i&&(X=b.sourceIndex)in e)){if(f=!(R&&(y=b.nodeName.toUpperCase())!==E||L&&b.id!==F||M&&(!b.className||!fa.test(b.className)))){if(A)for(r=-1;f&&(v=A[++r]);)if(m=v[2],l=qa(b,v[1]),l===s)f=8===m;else switch(v[4]&&(l=l.toUpperCase()),D=v[3],m){case 1:f=!!l||""===l;break;case 2:f=l===D;break;case 3:case 8:f=RegExp("(^| +)"+D+"($| +)").test(l);8===m&&(f=!f);break;case 4:case 5:case 6:p=l.indexOf(D);f=6===m?!!~p:5===m?p==l.length-D.length:!p;break;case 7:f=l===D||
!!~l.indexOf(D+"-");break;case 9:f=!!~(" "+l.replace(ra," ")+" ").indexOf(" "+D+" ")}if(f&&a)for(r=-1;f&&(h=a[++r]);)switch(m=h[1],m){case 0:case 1:if(!m&&!h[3])break;D=h[3];l=h[4];ga=h[5];h=h[2];N=b[D]||0;m=h[3]?("%"===h[2]?-1:1)*h[3]:0;p=h[1];if(N)f=!p?!(N+m):!((N+m)%p);else{f=u;l=b.parentNode[l];do if(1==l.nodeType&&(l[D]=++N)&&b===l&&(!p?!(N+m):!((N+m)%p)))f=j;while(!f&&(l=l[ga]))}break;case 2:case 3:for(l=b;(l=l.previousSibling)&&1!==l.nodeType;);f=!l;if(!f||3==m){for(;(l=W?4===J?s:b===Q?s:S(b):
g[++x])&&l.parentNode==b.parentNode;);!W&&4!==J&&--x;break}case 4:f=!S(b);break;case 5:f="HTML"==(y||b.nodeName.toUpperCase());break;case 6:f=!b.firstChild;break;case 7:f=!!b.checked;break;case 8:f=b.lang==h||b.ownerDocument.documentElement.lang==h;break;case 9:case 10:f="disabled"in b&&"form"in b&&(10==m?b.disabled===j&&"hidden"!==b.type:b.disabled===u);break;case 11:f=b.parentNode.selectedIndex&&!!b.selected;break;case 12:f=!!~(b.textContent||b.data||b.innerText||b.nodeValue||b.value||"").indexOf(h[2]);
break;case 13:case 14:f=T.call(b,h[2]);13==m&&(f=!f);break;case 15:case 16:y||(y=b.nodeName.toUpperCase());f=("INPUT"==y||"TEXTAREA"==y||sa.call(b.__getAttribute__||b.getAttribute,b,"contenteditable")!==s)&&!b.a;16==m&&(f=!f);break;case 18:f=b==n.ownerDocument.activeElement;break;default:C()}}if(f){if(k)return[b];if(z){P[B]=b;break}else i?P[X]=b:(t&&(t[X]=j),P.push(b))}y=s}while(b=W?4===J?s:b===Q?s:S(b):g[++x])}b=s}while(n=c[++B]);return P}
function ta(a,c,e){var i=!!e&&9===this.nodeType,g=[],k,t,z,f,e=i?e:this;if(ua)try{if(i&&va)return U(ua.call(this,a,e));z=(t="number"==typeof e.length&&e.nodeType===d)?e[0]:e;f=0;do{if(c)return[(9===z.nodeType?wa:xa).call(z,a)];k=(9===z.nodeType?ua:ya).call(z,a);k.length&&(g=t?g.concat(za.call(k)):k)}while(t&&(z=e[++f]));return U(g)}catch(P){g=[]}var a=Aa.call(a.replace(Ba,"$1")),q,x=j;z=u;var n;f=!!document.querySelector.__noorder__||!!document.querySelectorAll.__noorder__;var Q=a.replace(Ca,"@=").replace(Da,
Ea).match(Fa),B,b,y;for(k=e;q=Q.shift();){i=Q[0];t=!i||","===i.charAt(0);if(!z)if(x&&"nodeType"in e&&9===e.nodeType&&"BODY"===q.toUpperCase())k=[e.body],t&&(g=k);else if(x&&":root"===q)k=[(9===e.nodeType?e:e.ownerDocument).documentElement],t&&(g=k);else if(k&&(!(e=k)||0===k.length))k=s,z=j;else{n=t&&(n||!!i||1<e.length);k=!f&&n;k=(q=q.match(Ga))?aa(q,e,t&&!B?g:[],k,s,c&&t&&!B,u,B,x):[];if(B){for(y=b.length;0<y--;)k[y]||(k.splice(y,1),b.splice(y,1));if(t&&b.length){if(c)return b[0];g=g.concat(b)}}if(q&&
q[7]!==d&&k.length&&!t){B&&C();B=j;b=[];x=-1;for(y=k.length;++x<y;)b.push(k[x])}(","===a||!q)&&C()}if(c&&t&&k.length)return k[0];if(x=t)!g.length&&k&&(n=u,g=U(k)),k=s,e=this,z=u,B&&(b=B=d);if(!i||","===i)break}return n?U(g):g}w.Element||((w.Element=ActiveXObject).prototype.ie=j);w.HTMLElement||(w.HTMLElement=w.Element);w.Node||(w.Node=w.Element);
var V=w.Element.prototype,K=/\s*(\S+)\s*/g,O="(?=(^|.*\\s)$1(\\s|$))",Ha=/^([\w\-\|]+)?((?:\.(?:[\w-]+))+)?$|^#([\w-]+$)/,Ba=/\s*([,>+~\s])\s*/g,Ca=/~=/g,Fa=/(^|,|>|\+|~|\s).*?(?=[,>+~\s]|$)/g,da=/\./g,ra=/\s/g,Ga=/^([,>+~\s])?([\w\-\|\*]*)#?([\w-]*)((?:\.?[\w-])*)(\[.+\])?(?::([^!]+))?(!)?$/,ia=/^\[?['"]?(.*?)['"]?(?:([\*~&\^\$@!]?=)['"]?(.*?)['"]?)?\]?$/,ka=/^([^(]+)(?:\((.+)\))?$/,Da=/\-child\((\dn)\+(\d)\)/g,Ea="-child\\($1%$2\\)",na=/(?:([-]?\d*)n)?(?:(%|-)(\d*))?/,Ia=/([,>+~\s])/,Ja=/^\s+/,
ca=/\s/,ma=/\D/,oa=/['"]/,ba={"":1,",":1,">":2,"~":3,"+":4},ja={"":1,"=":2,"&=":3,"^=":4,"$=":5,"*=":6,"|=":7,"!=":8,"@=":9},la={"nth-child":0,"nth-last-child":1,"only-child":2,"first-child":3,"last-child":4,root:5,empty:6,checked:7,lang:8,enabled:9,disabled:10,selected:11,contains:12,not:13,matches:14,"read-only":15,"read-write":16,scope:17,focus:18,"nth-match":19,column:20,"nth-column":21},Ka={checked:s,disabled:s,ismap:s,multiple:s,readonly:s,selected:s},La={value:"defaultValue",checked:"defaultChecked",
selected:"defaultSelected"},Ma={action:s,cite:s,codebase:s,data:s,href:s,longdesc:s,lowsrc:s,src:s,usemap:s},Y=document.documentElement;function U(a){for(var c=a.length>>>0,e=new Z,i=0;i<c;++i)i in a&&e.push(a[i]);return e}
var pa=j,S="nextElementSibling"in Y?function(a){return a.nextElementSibling}:function(a){for(;(a=a.nextSibling)&&1!=a.nodeType;);return a},Aa=String.prototype.trim||function(){for(var a=this.replace(Ja,""),c=a.length;ca.test(a.charAt(--c)););return a.slice(0,c+1)},ha=String.prototype.split,za=Array.prototype.slice,Na,$,I=s,Z,T,qa,sa=Function.prototype.call,xa=Y.querySelector,ya=Y.querySelectorAll,wa=document.querySelector,ua=document.querySelectorAll,va=u,Oa,Pa=document.createElement("input");
Pa.setAttribute("value",5);Oa=5!=Pa.defaultValue;Z=function(){};Z.prototype=[];$=new Z;$.push(1);!$.length&&!("NodeList"in w)&&($=document.createElement("iframe"),$.style.display="none",document.body.appendChild($),$.contentWindow.document.write("<script>parent.NodeList=Array;<\/script>"),Z=w.NodeList);I||(Na=function(a){var c=[],e=this.all,i,g=-1;if(e.length)for(a=RegExp((a+"").replace(K,O));i=e[++g];)i.className&&a.test(i.className)&&c.push(i);return c});
qa=Oa?function(a,c){c=c.toLowerCase();return La[c]!==d?a[La[c]]||"":Ma[c]!==d?a.getAttribute(c,2)||"":Ka[c]!==d?a.getAttribute(c)?c:"":(a=a.getAttributeNode(c))&&a.value||""}:function(a,c){return a.getAttribute(c)};
T=Y.matches||Y.webkitMatchesSelector||Y.mozMatchesSelector||Y.msMatchesSelector||Y.oMatchesSelector?function(a){if(!a)return u;if("*"===a||":root"===a&&this===Y||"body"===a&&this===document.body)return j;var c,e,i=u;if(!Ia.test(a)&&(c=this.parentNode)&&"querySelector"in c)i=c.querySelector(a),i!==s&&(i=i===this);if(!i&&i!==s&&(c=this.ownerDocument))for(e in a=c.querySelectorAll(a),a)if(Object.prototype.hasOwnProperty.call(a,e)&&(i=a[e]===this))return j;return!!i}:function(a){if(!a)return u;if("*"===
a||this===Y&&":root"===a||this===document.body&&"BODY"===a.toUpperCase())return j;var c,e,i=u,g,a=Aa.call(a);if(c=a.match(Ha))switch(a.charAt(0)){case "#":return this.id===a.slice(1);default:return i=!(e=c[2])||this.className&&RegExp(e.replace(da," ").replace(K,O)).test(this.className),!!(i&&!(e=c[1])||this.tagName&&this.tagName.toUpperCase()===e.toUpperCase())}else{if(Ia.test(a)){e=ta.call(this.ownerDocument,a);for(g in e)if(Object.prototype.hasOwnProperty.call(e,g)&&(i=e[g]===this))return j;return u}e=
aa(a.match(Ga),s,s,u,this,j);return e[0]===this}};$="matchesSelector";Y[$]||(V.matches=Y.matches=V[$]=Y[$]=T);$="matches";$ in V||(V[$]=document.documentElement[$]=V.matchesSelector);$="querySelectorAll";V[$]=Y[$]=document[$]=function(a,c){return ta.call(this,a,u,c)};$="querySelector";V[$]=Y[$]=document[$]=function(a,c){return ta.call(this,a,j,c)||s};$="getElementsByClassName";document[$]||(V[$]=Y[$]=document[$]=Na);V=$=s;
})();