Files
discord-clone/node_modules/ultrahtml/dist/selector.js
2023-01-03 09:29:04 -06:00

2 lines
7.8 KiB
JavaScript

import{ELEMENT_NODE as h,TEXT_NODE as _,walkSync as j}from"./index.js";var b={attribute:/\[\s*(?:(?<namespace>\*|[-\w]*)\|)?(?<name>[-\w\u{0080}-\u{FFFF}]+)\s*(?:(?<operator>\W?=)\s*(?<value>.+?)\s*(?<caseSensitive>[iIsS])?\s*)?\]/gu,id:/#(?<name>(?:[-\w\u{0080}-\u{FFFF}]|\\.)+)/gu,class:/\.(?<name>(?:[-\w\u{0080}-\u{FFFF}]|\\.)+)/gu,comma:/\s*,\s*/g,combinator:/\s*[\s>+~]\s*/g,"pseudo-element":/::(?<name>[-\w\u{0080}-\u{FFFF}]+)(?:\((?<argument>¶+)\))?/gu,"pseudo-class":/:(?<name>[-\w\u{0080}-\u{FFFF}]+)(?:\((?<argument>¶+)\))?/gu,type:/(?:(?<namespace>\*|[-\w]*)\|)?(?<name>[-\w\u{0080}-\u{FFFF}]+)|\*/gu},T=new Set(["pseudo-class","pseudo-element"]),C=new Set([...T,"attribute"]),I=new Set(["combinator","comma"]),E=new Set(["not","is","where","has","matches","-moz-any","-webkit-any","nth-child","nth-last-child"]),M={"nth-child":/(?<index>[\dn+-]+)\s+of\s+(?<subtree>.+)/};E["nth-last-child"]=M["nth-child"];var w=Object.assign({},b);function R(t,r){let e="",n=[];for(;r<t.length;r++){let s=t[r];if(s==="(")n.push(s);else if(s===")"){if(!(n.length>0))throw new Error("Closing paren without opening paren at "+r);n.pop()}if(e+=s,n.length===0)return e}throw new Error("Opening paren without closing paren")}function k(t,r){if(!t)return[];var e=[t];for(var n in r){let u=r[n];for(var s=0;s<e.length;s++){var o=e[s];if(typeof o=="string"){u.lastIndex=0;var a=u.exec(o);if(a){let l=a.index-1,c=[],f=a[0],m=o.slice(0,l+1);m&&c.push(m),c.push({type:n,content:f,...a.groups});let g=o.slice(l+f.length+1);g&&c.push(g),e.splice(s,1,...c)}}}}let i=0;for(let u=0;u<e.length;u++){let l=e[u],c=l.length||l.content.length;typeof l=="object"&&(l.pos=[i,i+c],I.has(l.type)&&(l.content=l.content.trim()||" ")),i+=c}return e}function A(t){if(!t)return null;t=t.trim();let r=[];t=t.replace(/(['"])(\\\1|.)+?\1/g,(i,u,l,c)=>(r.push({str:i,start:c}),u+"\xA7".repeat(l.length)+u));let e,n=[],s=0;for(;(e=t.indexOf("(",s))>-1;){let i=R(t,e);n.push({str:i,start:e}),t=t.substring(0,e)+"("+"\xB6".repeat(i.length-2)+")"+t.substring(e+i.length),s=e+i.length}let o=k(t,b);function a(i,u,l){for(let c of i)for(let f of o)if(l.has(f.type)&&f.pos[0]<c.start&&c.start<f.pos[1]){let m=f.content;if(f.content=f.content.replace(u,c.str),f.content!==m){w[f.type].lastIndex=0;let g=w[f.type].exec(f.content).groups;Object.assign(f,g)}}}return a(n,/\(¶+\)/,T),a(r,/(['"])§+?\1/,C),o}function N(t,{list:r=!0}={}){if(r&&t.find(e=>e.type==="comma")){let e=[],n=[];for(let s=0;s<t.length;s++)if(t[s].type==="comma"){if(n.length===0)throw new Error("Incorrect comma at "+s);e.push(N(n,{list:!1})),n.length=0}else n.push(t[s]);if(n.length===0)throw new Error("Trailing comma");return e.push(N(n,{list:!1})),{type:"list",list:e}}for(let e=t.length-1;e>=0;e--){let n=t[e];if(n.type==="combinator"){let s=t.slice(0,e),o=t.slice(e+1);return{type:"complex",combinator:n.content,left:N(s),right:N(o)}}}return t.length===0?null:t.length===1?t[0]:{type:"compound",list:[...t]}}function p(t,r,e,n){if(t){if(t.type==="complex")p(t.left,r,e,t),p(t.right,r,e,t);else if(t.type==="compound")for(let s of t.list)p(s,r,e,t);else t.subtree&&e&&e.subtree&&p(t.subtree,r,e,t);r(t,n)}}function y(t,{recursive:r=!0,list:e=!0}={}){let n=A(t);if(!n)return null;let s=N(n,{list:e});return r&&p(s,o=>{if(o.type==="pseudo-class"&&o.argument&&E.has(o.name)){let a=o.argument,i=M[o.name];if(i){let u=i.exec(a);if(!u)return;Object.assign(o,u.groups),a=u.groups.subtree}a&&(o.subtree=y(a,{recursive:!0,list:!0}))}}),s}function F(t,r){return r=r||Math.max(...t)+1,t[0]*r**2+t[1]*r+t[2]}function x(t,{format:r="array"}={}){let e=typeof t=="object"?t:y(t,{recursive:!0});if(!e)return null;if(e.type==="list"){let s=10,o=e.list.map(i=>{let u=x(i);return s=Math.max(s,...u),u}),a=o.map(i=>F(i,s));return o[function(i){let u=i[0],l=0;for(let c=0;c<i.length;c++)i[c]>u&&(l=c,u=i[c]);return i.length===0?-1:l}(a)]}let n=[0,0,0];return p(e,s=>{s.type==="id"?n[0]++:s.type==="class"||s.type==="attribute"?n[1]++:s.type==="type"&&s.content!=="*"||s.type==="pseudo-element"?n[2]++:s.type==="pseudo-class"&&s.name!=="where"&&(E.has(s.name)&&s.subtree?x(s.subtree).forEach((o,a)=>n[a]+=o):n[1]++)}),n}w["pseudo-element"]=RegExp(b["pseudo-element"].source.replace("(?<argument>\xB6+)","(?<argument>.+?)"),"gu"),w["pseudo-class"]=RegExp(b["pseudo-class"].source.replace("(?<argument>\xB6+)","(?<argument>.+)"),"gu");function B(t){return F(x(t))}function K(t,r){return d(r)(t,t.parent,v(t,t.parent))}function G(t,r){let e=d(r);try{return O(t,(n,s,o)=>{let a=e(n,s,o);return a||!1},{single:!0})[0]}catch(n){if(n instanceof Error)throw n;return n}}function U(t,r){let e=d(r);return O(t,(n,s,o)=>{let a=e(n,s,o);return a||!1})}var J=U;function O(t,r,e={single:!1}){let n=[];return j(t,(s,o,a)=>{if(!(s&&s.type!==h)&&r(s,o,a)){if(e.single)throw s;n.push(s)}}),n}var L=t=>{let{operator:r="="}=t;switch(r){case"=":return(e,n)=>e===n;case"~=":return(e,n)=>e.split(/\s+/g).includes(n);case"|=":return(e,n)=>e.startsWith(n+"-");case"*=":return(e,n)=>e.indexOf(n)>-1;case"$=":return(e,n)=>e.endsWith(n);case"^=":return(e,n)=>e.startsWith(n)}return(e,n)=>!1},v=(t,r)=>r==null?void 0:r.children.filter(e=>e.type===h).findIndex(e=>e===t),W=t=>{let[r,e="1",n="0"]=/^\s*(?:(-?(?:\d+)?)n)?\s*\+?\s*(\d+)?\s*$/gm.exec(t)??[];e.length===0&&(e="1");let s=Number.parseInt(e==="-"?"-1":e),o=Number.parseInt(n);return a=>s*a+o},D=(t,r)=>(r==null?void 0:r.children.filter(e=>e.type===h).pop())===t,$=(t,r)=>(r==null?void 0:r.children.filter(e=>e.type===h).shift())===t,z=(t,r)=>(r==null?void 0:r.children.filter(e=>e.type===h).length)===1,S=t=>{switch(t.type){case"type":return r=>t.content==="*"?!0:r.name===t.name;case"class":return r=>{var e,n;return(n=(e=r.attributes)==null?void 0:e.class)==null?void 0:n.split(/\s+/g).includes(t.name)};case"id":return r=>{var e;return((e=r.attributes)==null?void 0:e.id)===t.name};case"pseudo-class":switch(t.name){case"global":return(...r)=>d(y(t.argument))(...r);case"not":return(...r)=>!S(t.subtree)(...r);case"is":return(...r)=>d(t.subtree)(...r);case"where":return(...r)=>d(t.subtree)(...r);case"root":return(r,e)=>r.type===h&&r.name==="html";case"empty":return r=>r.type===h&&(r.children.length===0||r.children.every(e=>e.type===_&&e.value.trim()===""));case"first-child":return(r,e)=>$(r,e);case"last-child":return(r,e)=>D(r,e);case"only-child":return(r,e)=>z(r,e);case"nth-child":return(r,e)=>{let n=v(r,e)+1;if(Number.isNaN(Number(t.argument)))switch(t.argument){case"odd":return Math.abs(n%2)==1;case"even":return n%2===0;default:{if(!t.argument)throw new Error("Unsupported empty nth-child selector!");let s=W(t.argument),o=e==null?void 0:e.children.filter(i=>i.type===h),a=v(r,e)+1;for(let i=0;i<o.length;i++){let u=s(i);if(u>o.length)return!1;if(u===a)return!0}return!1}}return n===Number(t.argument)};default:throw new Error(`Unhandled pseudo-class: ${t.name}!`)}case"attribute":return r=>{let{caseSensitive:e,name:n,value:s}=t;if(!r.attributes)return!1;let o=Object.entries(r.attributes);for(let[a,i]of o)if(e==="i"&&(s=n.toLowerCase(),i=a.toLowerCase()),a===n){if(!s)return!0;if((s[0]==='"'||s[0]==="'")&&s[0]===s[s.length-1]&&(s=JSON.parse(s)),s)return L(t)(i,s)}return!1};default:throw new Error(`Unhandled selector: ${t.type}`)}},d=t=>{let r=typeof t=="string"?y(t):t;switch(r.type){case"list":{let e=r.list.map(n=>S(n));return(n,s,o)=>{for(let a of e)if(a(n,s))return!0;return!1}}case"compound":{let e=r.list.map(n=>S(n));return(n,s,o)=>{for(let a of e)if(!a(n,s))return!1;return!0}}case"complex":{let{left:e,right:n,combinator:s}=r,o=d(e),a=d(n),i=new WeakSet;return(u,l,c=0)=>{if((o(u)||l&&i.has(l)&&s===" ")&&i.add(u),!a(u))return!1;switch(s){case" ":case">":return l?i.has(l):!1;case"~":{if(!l)return!1;for(let f of l.children.slice(0,c))if(i.has(f))return!0;return!1}case"+":{if(!l)return!1;let f=l.children.slice(0,c).filter(g=>g.type===h);if(f.length===0)return!1;let m=f[f.length-1];if(!m)return!1;if(i.has(m))return!0}default:return!1}}}default:return S(r)}};export{J as default,K as matches,G as querySelector,U as querySelectorAll,B as specificity};