@asamuzakjp/dom-selector 2.0.3-a.2 → 2.0.3-a.4
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/cjs/js/finder.js +1 -1
- package/dist/cjs/js/finder.js.map +3 -3
- package/dist/cjs/js/matcher.js +1 -1
- package/dist/cjs/js/matcher.js.map +2 -2
- package/package.json +1 -1
- package/src/js/finder.js +285 -192
- package/src/js/matcher.js +4 -1
- package/types/js/finder.d.ts +5 -0
package/dist/cjs/js/finder.js
CHANGED
|
@@ -1,2 +1,2 @@
|
|
|
1
|
-
var F=Object.create;var P=Object.defineProperty;var H=Object.getOwnPropertyDescriptor;var G=Object.getOwnPropertyNames;var q=Object.getPrototypeOf,V=Object.prototype.hasOwnProperty;var X=(T,r)=>{for(var e in r)P(T,e,{get:r[e],enumerable:!0})},W=(T,r,e,c)=>{if(r&&typeof r=="object"||typeof r=="function")for(let i of G(r))!V.call(T,i)&&i!==e&&P(T,i,{get:()=>r[i],enumerable:!(c=H(r,i))||c.enumerable});return T};var Y=(T,r,e)=>(e=T!=null?F(q(T)):{},W(r||!T||!T.__esModule?P(e,"default",{value:T,enumerable:!0}):e,T)),K=T=>W(P({},"__esModule",{value:!0}),T);var Q={};X(Q,{Finder:()=>J});module.exports=K(Q);var I=Y(require("is-potential-custom-element-name"),1),A=require("./dom-util.js"),D=require("./matcher.js"),S=require("./parser.js"),o=require("./constant.js");const O="next",$="prev",L="all",C="first",j="lineal",B="self",U=o.SHOW_DOCUMENT|o.SHOW_DOCUMENT_FRAGMENT|o.SHOW_ELEMENT;class J{#a;#l;#d;#e;#r;#t;#n;#s;#b;#f;#c;#h;#i;#o;constructor(){this.#l=new WeakMap}_onError(r){if(r instanceof DOMException||this.#o&&r instanceof this.#o.DOMException)if(r.name===o.NOT_SUPPORTED_ERR)this.#i&&console.warn(r.message);else throw this.#o?new this.#o.DOMException(r.message,r.name):r;else throw r}_setup(r,e,c={}){const{warn:i}=c;this.#i=!!i,[this.#o,this.#e,this.#s]=(0,A.prepareDOMObjects)(r),this.#t=r,this.#f=(0,A.isInShadowTree)(r),this.#b=e,[this.#a,this.#n]=this._correspond(e),this.#d=new WeakMap}_correspond(r){const e=[];let c,i=this.#e&&this.#l.get(this.#e);if(i&&i.has(`${r}`)&&(c=i.get(r),typeof c=="string"))throw new DOMException(c,o.SYNTAX_ERR);if(c){const s=c.length;for(let f=0;f<s;f++)c[f].dir=null,c[f].filtered=!1,c[f].find=!1,e[f]=[]}else{let s;try{s=(0,S.parseSelector)(r)}catch(u){this.#e&&(i||(i=new Map),i.set(`${r}`,u.message),this.#l.set(this.#e,i)),this._onError(u)}const f=(0,S.walkAST)(s);c=[];let b=0;for(const[...u]of f){const n=[];let t=u.shift();if(t&&t.type!==o.COMBINATOR){const a=new Set;for(;t;){if(t.type===o.COMBINATOR){const[h]=u;if(h.type===o.COMBINATOR){const l=`Invalid selector ${r}`;throw this.#e&&(i||(i=new Map),i.set(`${r}`,l),this.#l.set(this.#e,i)),new DOMException(l,o.SYNTAX_ERR)}n.push({combo:t,leaves:(0,S.sortAST)(a)}),a.clear()}else t&&a.add(t);if(u.length)t=u.shift();else{n.push({combo:null,leaves:(0,S.sortAST)(a)}),a.clear();break}}}c.push({branch:n,dir:null,filtered:!1,find:!1}),e[b]=[],b++}this.#e&&(i||(i=new Map),i.set(`${r}`,c),this.#l.set(this.#e,i))}return[c,e]}_traverse(r={},e=this.#h){let c,i=e.currentNode;if(r.nodeType===o.ELEMENT_NODE&&i===r)c=i;else{if(i!==e.root)for(;i&&!(i===e.root||r.nodeType===o.ELEMENT_NODE&&i===r);)i=e.parentNode();if(r.nodeType===o.ELEMENT_NODE)for(;i;){if(i===r){c=i;break}i=e.nextNode()}else c=i}return c??null}_collectNthChild(r,e,c){const{a:i,b:s,reverse:f,selector:b}=r,{parentNode:u}=e;let n=new Set,t;if(b&&(this.#l.has(b)?t=this.#l.get(b):(t=(0,S.walkAST)(b),this.#l.set(b,t))),u){const a=this.#e.createTreeWalker(u,U);let h=0,l=a.firstChild();for(;l;)h++,l=a.nextSibling();l=this._traverse(u,a);const p=new Set;if(t)for(l=this._traverse(u,a),l=a.firstChild();l;){let m;for(const g of t)if(m=this._matchLeaves(g,l,c),!m)break;m&&p.add(l),l=a.nextSibling()}if(i===0){if(s>0&&s<=h){if(p.size){let m=0;for(l=this._traverse(u,a),f?l=a.lastChild():l=a.firstChild();l;){if(p.has(l)){if(m===s-1){n.add(l);break}m++}f?l=a.previousSibling():l=a.nextSibling()}}else if(!b){let m=0;for(l=this._traverse(u,a),f?l=a.lastChild():l=a.firstChild();l;){if(m===s-1){n.add(l);break}f?l=a.previousSibling():l=a.nextSibling(),m++}}}}else{let m=s-1;if(i>0)for(;m<0;)m+=i;if(m>=0&&m<h){let g=0,N=i>0?0:s-1;for(l=this._traverse(u,a),f?l=a.lastChild():l=a.firstChild();l&&(l&&m>=0&&m<h);)p.size?p.has(l)&&(N===m&&(n.add(l),m+=i),i>0?N++:N--):g===m&&(b||n.add(l),m+=i),f?l=a.previousSibling():l=a.nextSibling(),g++}}if(f&&n.size>1){const m=[...n];n=new Set(m.reverse())}}else if(e===this.#s&&this.#s.nodeType===o.ELEMENT_NODE&&i+s===1)if(t){let a;for(const h of t)if(a=this._matchLeaves(h,e,c),a)break;a&&n.add(e)}else n.add(e);return n}_collectNthOfType(r,e){const{a:c,b:i,reverse:s}=r,{localName:f,parentNode:b,prefix:u}=e;let n=new Set;if(b){const t=this.#e.createTreeWalker(b,U);let a=0,h=t.firstChild();for(;h;)a++,h=t.nextSibling();if(c===0){if(i>0&&i<=a){let l=0;for(h=this._traverse(b,t),s?h=t.lastChild():h=t.firstChild();h;){const{localName:p,prefix:m}=h;if(p===f&&m===u){if(l===i-1){n.add(h);break}l++}s?h=t.previousSibling():h=t.nextSibling()}}}else{let l=i-1;if(c>0)for(;l<0;)l+=c;if(l>=0&&l<a){let p=c>0?0:i-1;for(h=this._traverse(b,t),s?h=t.lastChild():h=t.firstChild();h;){const{localName:m,prefix:g}=h;if(m===f&&g===u){if(p===l&&(n.add(h),l+=c),l<0||l>=a)break;c>0?p++:p--}s?h=t.previousSibling():h=t.nextSibling()}}}if(s&&n.size>1){const l=[...n];n=new Set(l.reverse())}}else e===this.#s&&this.#s.nodeType===o.ELEMENT_NODE&&c+i===1&&n.add(e);return n}_matchAnPlusB(r,e,c,i){const{nth:{a:s,b:f,name:b},selector:u}=r,n=(0,S.unescapeSelector)(b),t=new Map;n?(n==="even"?(t.set("a",2),t.set("b",0)):n==="odd"&&(t.set("a",2),t.set("b",1)),c.indexOf("last")>-1&&t.set("reverse",!0)):(typeof s=="string"&&/-?\d+/.test(s)?t.set("a",s*1):t.set("a",0),typeof f=="string"&&/-?\d+/.test(f)?t.set("b",f*1):t.set("b",0),c.indexOf("last")>-1&&t.set("reverse",!0));let a=new Set;if(t.has("a")&&t.has("b")){if(/^nth-(?:last-)?child$/.test(c)){u&&t.set("selector",u);const h=Object.fromEntries(t),l=this._collectNthChild(h,e,i);l.size&&(a=l)}else if(/^nth-(?:last-)?of-type$/.test(c)){const h=Object.fromEntries(t),l=this._collectNthOfType(h,e);l.size&&(a=l)}}return a}_matchDirectionPseudoClass(r,e){const c=(0,S.unescapeSelector)(r.name),i=(0,A.getDirectionality)(e);let s;return c===i&&(s=e),s??null}_matchLanguagePseudoClass(r,e){const c=(0,S.unescapeSelector)(r.name);let i;if(c==="*")if(e.hasAttribute("lang"))e.getAttribute("lang")&&(i=e);else{let s=e.parentNode;for(;s&&s.nodeType===o.ELEMENT_NODE;){if(s.hasAttribute("lang")){s.getAttribute("lang")&&(i=e);break}s=s.parentNode}}else if(c){const s=`(?:-${o.ALPHA_NUM})*`;if(new RegExp(`^(?:\\*-)?${o.ALPHA_NUM}${s}$`,"i").test(c)){let b;if(c.indexOf("-")>-1){const[u,n,...t]=c.split("-");let a;u==="*"?a=`${o.ALPHA_NUM}${s}`:a=`${u}${s}`;const h=`-${n}${s}`,l=t.length;let p="";if(l)for(let m=0;m<l;m++)p+=`-${t[m]}${s}`;b=new RegExp(`^${a}${h}${p}$`,"i")}else b=new RegExp(`^${c}${s}$`,"i");if(e.hasAttribute("lang"))b.test(e.getAttribute("lang"))&&(i=e);else{let u=e.parentNode;for(;u&&u.nodeType===o.ELEMENT_NODE;){if(u.hasAttribute("lang")){const n=u.getAttribute("lang");b.test(n)&&(i=e);break}u=u.parentNode}}}}return i??null}_matchHasPseudoFunc(r,e,c={}){let i;if(Array.isArray(r)&&r.length){const[s]=r,{type:f}=s;let b;f===o.COMBINATOR?b=r.shift():b={name:" ",type:o.COMBINATOR};const u=[];for(;r.length;){const[a]=r,{type:h}=a;if(h===o.COMBINATOR)break;u.push(r.shift())}const n={combo:b,leaves:u};c.dir=O;const t=this._matchCombinator(n,e,c);if(t.size)if(r.length){for(const a of t)if(i=this._matchHasPseudoFunc(Object.assign([],r),a,c),i)break}else i=!0}return!!i}_matchLogicalPseudoFunc(r,e,c={}){const{astName:i="",branches:s=[],selector:f="",twigBranches:b=[]}=r;let u;if(i==="has")if(f.includes(":has("))u=null;else{let n;for(const t of s)if(n=this._matchHasPseudoFunc(Object.assign([],t),e,c),n)break;n&&(u=e)}else{const n=/^(?:is|where)$/.test(i);c.forgive=n;const t=b.length;let a;for(let h=0;h<t;h++){const l=b[h],p=l.length-1,{leaves:m}=l[p];if(a=this._matchLeaves(m,e,c),a&&p>0){let g=new Set([e]);for(let N=p-1;N>=0;N--){const y=l[N],d=[];c.dir=$;for(const k of g){const w=this._matchCombinator(y,k,c);w.size&&d.push(...w)}if(d.length)N===0?a=!0:g=new Set(d);else{a=!1;break}}}if(a)break}i==="not"?a||(u=e):a&&(u=e)}return u??null}_matchPseudoClassSelector(r,e,c={}){const{children:i}=r,{localName:s,parentNode:f}=e,{forgive:b,warn:u}=c,n=(0,S.unescapeSelector)(r.name);let t=new Set;if(o.REG_LOGICAL_PSEUDO.test(n)){let a;if(this.#l.has(r))a=this.#l.get(r);else{const l=(0,S.walkAST)(r),p=[],m=[];for(const[...g]of l){for(const k of g){const w=(0,S.generateCSS)(k);p.push(w)}const N=[],y=new Set;let d=g.shift();for(;d;)if(d.type===o.COMBINATOR?(N.push({combo:d,leaves:[...y]}),y.clear()):d&&y.add(d),g.length)d=g.shift();else{N.push({combo:null,leaves:[...y]}),y.clear();break}m.push(N)}a={astName:n,branches:l,twigBranches:m,selector:p.join(",")},this.#l.set(r,a)}const h=this._matchLogicalPseudoFunc(a,e,c);h&&t.add(h)}else if(Array.isArray(i)){const[a]=i;if(/^nth-(?:last-)?(?:child|of-type)$/.test(n)){const h=this._matchAnPlusB(a,e,n,c);h.size&&(t=h)}else if(n==="dir"){const h=this._matchDirectionPseudoClass(a,e);h&&t.add(h)}else if(n==="lang"){const h=this._matchLanguagePseudoClass(a,e);h&&t.add(h)}else switch(n){case"current":case"nth-col":case"nth-last-col":{if(u){const h=`Unsupported pseudo-class :${n}()`;throw new DOMException(h,o.NOT_SUPPORTED_ERR)}break}case"host":case"host-context":break;default:if(!b){const h=`Unknown pseudo-class :${n}()`;throw new DOMException(h,o.SYNTAX_ERR)}}}else{const a=/^a(?:rea)?$/,h=/^(?:(?:fieldse|inpu|selec)t|button|opt(?:group|ion)|textarea)$/,l=/^(?:(?:inpu|selec)t|button|form|textarea)$/,p=/^d(?:etails|ialog)$/,m=/^(?:checkbox|radio)$/,g=/^(?:date(?:time-local)?|month|time|week)$/,N=/(?:(?:rang|tim)e|date(?:time-local)?|month|number|week)$/,y=/^(?:(?:emai|te|ur)l|number|password|search|text)$/;switch(n){case"any-link":case"link":{a.test(s)&&e.hasAttribute("href")&&t.add(e);break}case"local-link":{if(a.test(s)&&e.hasAttribute("href")){const{href:d,origin:k,pathname:w}=new URL(this.#e.URL),_=new URL(e.getAttribute("href"),d);_.origin===k&&_.pathname===w&&t.add(e)}break}case"visited":break;case"target":{const{hash:d}=new URL(this.#e.URL);e.id&&d===`#${e.id}`&&this.#e.contains(e)&&t.add(e);break}case"target-within":{const{hash:d}=new URL(this.#e.URL);if(d){const k=d.replace(/^#/,"");let w=this.#e.getElementById(k);for(;w;){if(w===e){t.add(e);break}w=w.parentNode}}break}case"scope":{this.#t.nodeType===o.ELEMENT_NODE?e===this.#t&&t.add(e):e===this.#e.documentElement&&t.add(e);break}case"focus":{e===this.#e.activeElement&&t.add(e);break}case"focus-within":{let d=this.#e.activeElement;for(;d;){if(d===e){t.add(e);break}d=d.parentNode}break}case"open":{p.test(s)&&e.hasAttribute("open")&&t.add(e);break}case"closed":{p.test(s)&&!e.hasAttribute("open")&&t.add(e);break}case"disabled":{if(h.test(s)||(0,I.default)(s))if(e.disabled||e.hasAttribute("disabled"))t.add(e);else{let d=f;for(;d&&d.localName!=="fieldset";)d=d.parentNode;d&&f.localName!=="legend"&&d.hasAttribute("disabled")&&t.add(e)}break}case"enabled":{(h.test(s)||(0,I.default)(s))&&!(e.disabled&&e.hasAttribute("disabled"))&&t.add(e);break}case"read-only":{switch(s){case"textarea":{(e.readonly||e.hasAttribute("readonly")||e.disabled||e.hasAttribute("disabled"))&&t.add(e);break}case"input":{(!e.type||g.test(e.type)||y.test(e.type))&&(e.readonly||e.hasAttribute("readonly")||e.disabled||e.hasAttribute("disabled"))&&t.add(e);break}default:(0,A.isContentEditable)(e)||t.add(e)}break}case"read-write":{switch(s){case"textarea":{e.readonly||e.hasAttribute("readonly")||e.disabled||e.hasAttribute("disabled")||t.add(e);break}case"input":{(!e.type||g.test(e.type)||y.test(e.type))&&!(e.readonly||e.hasAttribute("readonly")||e.disabled||e.hasAttribute("disabled"))&&t.add(e);break}default:(0,A.isContentEditable)(e)&&t.add(e)}break}case"placeholder-shown":{let d;s==="textarea"?d=e:s==="input"&&(e.hasAttribute("type")?y.test(e.getAttribute("type"))&&(d=e):d=e),d&&e.value===""&&e.hasAttribute("placeholder")&&e.getAttribute("placeholder").trim().length&&t.add(e);break}case"checked":{(e.checked&&s==="input"&&e.hasAttribute("type")&&m.test(e.getAttribute("type"))||e.selected&&s==="option")&&t.add(e);break}case"indeterminate":{if(e.indeterminate&&s==="input"&&e.type==="checkbox"||s==="progress"&&!e.hasAttribute("value"))t.add(e);else if(s==="input"&&e.type==="radio"&&!e.hasAttribute("checked")){const d=e.name;let k=e.parentNode;for(;k&&k.localName!=="form";)k=k.parentNode;k||(k=this.#e.documentElement);let w;const _=k.getElementsByTagName("input"),E=_.length;if(E)for(let x=0;x<E;x++){const v=_[x];if(v.getAttribute("type")==="radio"&&(d?v.getAttribute("name")===d&&(w=!!v.checked):v.hasAttribute("name")||(w=!!v.checked),w))break}w||t.add(e)}break}case"default":{const d=/^(?:button|reset)$/,k=/^(?:image|submit)$/;if(s==="button"&&!(e.hasAttribute("type")&&d.test(e.getAttribute("type")))||s==="input"&&e.hasAttribute("type")&&k.test(e.getAttribute("type"))){let w=e.parentNode;for(;w&&w.localName!=="form";)w=w.parentNode;if(w){const _=this.#e.createTreeWalker(w,o.SHOW_ELEMENT);let E=_.firstChild();for(;E;){const x=E.localName;let v;if(x==="button"?v=!(E.hasAttribute("type")&&d.test(E.getAttribute("type"))):x==="input"&&(v=E.hasAttribute("type")&&k.test(E.getAttribute("type"))),v){E===e&&t.add(e);break}E=_.nextNode()}}}else if(s==="input"&&e.hasAttribute("type")&&m.test(e.getAttribute("type"))&&(e.checked||e.hasAttribute("checked")))t.add(e);else if(s==="option"){let w=!1,_=f;for(;_&&_.localName!=="datalist";){if(_.localName==="select"){(_.multiple||_.hasAttribute("multiple"))&&(w=!0);break}_=_.parentNode}if(w)(e.selected||e.hasAttribute("selected"))&&t.add(e);else{const E=new Set,x=this.#e.createTreeWalker(f,o.SHOW_ELEMENT);let v=x.firstChild();for(;v;){if(v.selected||v.hasAttribute("selected")){E.add(v);break}v=x.nextSibling()}E.size&&E.has(e)&&t.add(e)}}break}case"valid":{if(l.test(s))e.checkValidity()&&t.add(e);else if(s==="fieldset"){let d;const k=this.#e.createTreeWalker(e,o.SHOW_ELEMENT);let w=k.firstChild();for(;w&&!(l.test(w.localName)&&(d=w.checkValidity(),!d));)w=k.nextNode();d&&t.add(e)}break}case"invalid":{if(l.test(s))e.checkValidity()||t.add(e);else if(s==="fieldset"){let d;const k=this.#e.createTreeWalker(e,o.SHOW_ELEMENT);let w=k.firstChild();for(;w&&!(l.test(w.localName)&&(d=w.checkValidity(),!d));)w=k.nextNode();d||t.add(e)}break}case"in-range":{s==="input"&&!(e.readonly||e.hasAttribute("readonly"))&&!(e.disabled||e.hasAttribute("disabled"))&&e.hasAttribute("type")&&N.test(e.getAttribute("type"))&&!(e.validity.rangeUnderflow||e.validity.rangeOverflow)&&(e.hasAttribute("min")||e.hasAttribute("max")||e.getAttribute("type")==="range")&&t.add(e);break}case"out-of-range":{s==="input"&&!(e.readonly||e.hasAttribute("readonly"))&&!(e.disabled||e.hasAttribute("disabled"))&&e.hasAttribute("type")&&N.test(e.getAttribute("type"))&&(e.validity.rangeUnderflow||e.validity.rangeOverflow)&&t.add(e);break}case"required":{let d;if(/^(?:select|textarea)$/.test(s))d=e;else if(s==="input")if(e.hasAttribute("type")){const k=e.getAttribute("type");(k==="file"||m.test(k)||g.test(k)||y.test(k))&&(d=e)}else d=e;d&&(e.required||e.hasAttribute("required"))&&t.add(e);break}case"optional":{let d;if(/^(?:select|textarea)$/.test(s))d=e;else if(s==="input")if(e.hasAttribute("type")){const k=e.getAttribute("type");(k==="file"||m.test(k)||g.test(k)||y.test(k))&&(d=e)}else d=e;d&&!(e.required||e.hasAttribute("required"))&&t.add(e);break}case"root":{e===this.#e.documentElement&&t.add(e);break}case"empty":{if(e.hasChildNodes()){let d;const k=this.#e.createTreeWalker(e,o.SHOW_ALL);let w=k.firstChild();for(;w&&(d=w.nodeType!==o.ELEMENT_NODE&&w.nodeType!==o.TEXT_NODE,!!d);)w=k.nextSibling();d&&t.add(e)}else t.add(e);break}case"first-child":{(f&&e===f.firstElementChild||e===this.#s&&this.#s.nodeType===o.ELEMENT_NODE)&&t.add(e);break}case"last-child":{(f&&e===f.lastElementChild||e===this.#s&&this.#s.nodeType===o.ELEMENT_NODE)&&t.add(e);break}case"only-child":{(f&&e===f.firstElementChild&&e===f.lastElementChild||e===this.#s&&this.#s.nodeType===o.ELEMENT_NODE)&&t.add(e);break}case"first-of-type":{if(f){const[d]=this._collectNthOfType({a:0,b:1},e);d&&t.add(d)}else e===this.#s&&this.#s.nodeType===o.ELEMENT_NODE&&t.add(e);break}case"last-of-type":{if(f){const[d]=this._collectNthOfType({a:0,b:1,reverse:!0},e);d&&t.add(d)}else e===this.#s&&this.#s.nodeType===o.ELEMENT_NODE&&t.add(e);break}case"only-of-type":{if(f){const[d]=this._collectNthOfType({a:0,b:1},e);if(d===e){const[k]=this._collectNthOfType({a:0,b:1,reverse:!0},e);k===e&&t.add(e)}}else e===this.#s&&this.#s.nodeType===o.ELEMENT_NODE&&t.add(e);break}case"host":case"host-context":break;case"after":case"before":case"first-letter":case"first-line":{if(u){const d=`Unsupported pseudo-element ::${n}`;throw new DOMException(d,o.NOT_SUPPORTED_ERR)}break}case"active":case"autofill":case"blank":case"buffering":case"current":case"defined":case"focus-visible":case"fullscreen":case"future":case"hover":case"modal":case"muted":case"past":case"paused":case"picture-in-picture":case"playing":case"seeking":case"stalled":case"user-invalid":case"user-valid":case"volume-locked":case"-webkit-autofill":{if(u){const d=`Unsupported pseudo-class :${n}`;throw new DOMException(d,o.NOT_SUPPORTED_ERR)}break}default:if(n.startsWith("-webkit-")){if(u){const d=`Unsupported pseudo-class :${n}`;throw new DOMException(d,o.NOT_SUPPORTED_ERR)}}else if(!b){const d=`Unknown pseudo-class :${n}`;throw new DOMException(d,o.SYNTAX_ERR)}}}return t}_matchShadowHostPseudoClass(r,e){const{children:c}=r,i=(0,S.unescapeSelector)(r.name);let s;if(Array.isArray(c)){const[f]=(0,S.walkAST)(c[0]),[...b]=f,{host:u}=e;if(i==="host"){let n;for(const t of b){const{type:a}=t;if(a===o.COMBINATOR){const l=`Invalid selector ${(0,S.generateCSS)(r)}`;throw new DOMException(l,o.SYNTAX_ERR)}if(n=this._matchSelector(t,u).has(u),!n)break}n&&(s=e)}else if(i==="host-context"){let n,t=u;for(;t;){for(const a of b){const{type:h}=a;if(h===o.COMBINATOR){const p=`Invalid selector ${(0,S.generateCSS)(r)}`;throw new DOMException(p,o.SYNTAX_ERR)}if(n=this._matchSelector(a,t).has(t),!n)break}if(n)break;t=t.parentNode}n&&(s=e)}}else if(i==="host")s=e;else{const f=`Invalid selector :${i}`;throw new DOMException(f,o.SYNTAX_ERR)}return s??null}_matchSelector(r,e,c){const{type:i}=r,s=(0,S.unescapeSelector)(r.name);let f=new Set;if(e.nodeType===o.ELEMENT_NODE)switch(i){case o.SELECTOR_PSEUDO_CLASS:{const b=this._matchPseudoClassSelector(r,e,c);b.size&&(f=b);break}case o.SELECTOR_PSEUDO_ELEMENT:{(0,D.matchPseudoElementSelector)(s,c);break}default:{const b=(0,D.matchSelector)(r,e,c);b&&f.add(b)}}else if(this.#f&&i===o.SELECTOR_PSEUDO_CLASS&&e.nodeType===o.DOCUMENT_FRAGMENT_NODE){if(s!=="has"&&o.REG_LOGICAL_PSEUDO.test(s)){const b=this._matchPseudoClassSelector(r,e,c);b.size&&(f=b)}else if(o.REG_SHADOW_HOST.test(s)){const b=this._matchShadowHostPseudoClass(r,e,c);b&&f.add(b)}}return f}_matchLeaves(r,e,c){let i;for(const s of r)if(i=this._matchSelector(s,e,c).has(e),!i)break;return!!i}_findDescendantNodes(r,e,c){const[i,...s]=r,{type:f}=i,b=(0,S.unescapeSelector)(i.name),u=s.length>0;let n=new Set,t=!1;if(this.#f)t=!0;else switch(f){case o.SELECTOR_ID:{if(this.#s.nodeType===o.ELEMENT_NODE)t=!0;else{const a=this.#s.getElementById(b);a&&a!==e&&e.contains(a)&&(u?this._matchLeaves(s,a,c)&&n.add(a):n.add(a))}break}case o.SELECTOR_CLASS:{const a=e.getElementsByClassName(b),h=a.length;if(h)if(u)for(let l=0;l<h;l++){const p=a[l];this._matchLeaves(s,p,c)&&n.add(p)}else{const l=[].slice.call(a);n=new Set(l)}break}case o.SELECTOR_TYPE:{if(this.#e.contentType==="text/html"&&!/[*|]/.test(b)){const a=e.getElementsByTagName(b),h=a.length;if(h)if(u)for(let l=0;l<h;l++){const p=a[l];this._matchLeaves(s,p,c)&&n.add(p)}else{const l=[].slice.call(a);n=new Set(l)}}else t=!0;break}case o.SELECTOR_PSEUDO_ELEMENT:{(0,D.matchPseudoElementSelector)(b,c);break}default:t=!0}return{nodes:n,pending:t}}_matchCombinator(r,e,c={}){const{combo:i,leaves:s}=r,{name:f}=i,{dir:b}=c;let u=new Set;if(b===O)switch(f){case"+":{const n=e.nextElementSibling;n&&this._matchLeaves(s,n,c)&&u.add(n);break}case"~":{const{parentNode:n}=e;if(n){const t=this.#e.createTreeWalker(n,o.SHOW_ELEMENT);let a=this._traverse(e,t);for(a===e&&(a=t.nextSibling());a;)this._matchLeaves(s,a,c)&&u.add(a),a=t.nextSibling()}break}case">":{const n=this.#e.createTreeWalker(e,o.SHOW_ELEMENT);let t=n.firstChild();for(;t;)this._matchLeaves(s,t,c)&&u.add(t),t=n.nextSibling();break}case" ":default:{const{nodes:n,pending:t}=this._findDescendantNodes(s,e);if(n.size)u=n;else if(t){const a=this.#e.createTreeWalker(e,o.SHOW_ELEMENT);let h=a.nextNode();for(;h;)this._matchLeaves(s,h,c)&&u.add(h),h=a.nextNode()}}}else switch(f){case"+":{const n=e.previousElementSibling;n&&this._matchLeaves(s,n,c)&&u.add(n);break}case"~":{const n=this.#e.createTreeWalker(e.parentNode,o.SHOW_ELEMENT);let t=n.firstChild();for(;t&&t!==e;)this._matchLeaves(s,t,c)&&u.add(t),t=n.nextSibling();break}case">":{const n=e.parentNode;n&&this._matchLeaves(s,n,c)&&u.add(n);break}case" ":default:{const n=[];let t=e.parentNode;for(;t;)this._matchLeaves(s,t,c)&&n.push(t),t=t.parentNode;n.length&&(u=new Set(n.reverse()))}}return u}_findNode(r,e={}){const{node:c}=e;let i,s=this._traverse(c,this.#r);if(s)for(s.nodeType!==o.ELEMENT_NODE?s=this.#r.nextNode():s===c&&s!==this.#s&&(s=this.#r.nextNode());s;){let f;if(this.#t.nodeType===o.ELEMENT_NODE?s===this.#t?f=!0:f=this.#t.contains(s):f=!0,f&&this._matchLeaves(r,s,{warn:this.#i})){i=s;break}s=this.#r.nextNode()}return i??null}_findEntryNodes(r,e,c){const{leaves:i}=r,[s,...f]=i,{type:b}=s,u=(0,S.unescapeSelector)(s.name),n=f.length>0;let t=[],a=!1,h=!1;if(e===B)this._matchLeaves(i,this.#t,{warn:this.#i})&&(t.push(this.#t),a=!0);else if(e===j){let l=this.#t;for(;l&&!(this._matchLeaves(i,l,{warn:this.#i})&&(t.push(l),a=!0,!c));)l=l.parentNode}else switch(b){case o.SELECTOR_PSEUDO_ELEMENT:{(0,D.matchPseudoElementSelector)(u,{warn:this.#i});break}case o.SELECTOR_ID:{if(e===C&&this.#s.nodeType!==o.ELEMENT_NODE){const l=this.#s.getElementById(u);l&&(n?this._matchLeaves(f,l,{warn:this.#i})&&t.push(l):t.push(l),a=!0)}else h=!0;break}case o.SELECTOR_CLASS:{if(e===C){const l=this._findNode(i,{node:this.#t});l&&(t.push(l),a=!0)}else if(this.#s.nodeType===o.DOCUMENT_NODE){const l=this.#s.getElementsByClassName(u),p=l.length;if(p)if(this.#t.nodeType===o.ELEMENT_NODE)for(let m=0;m<p;m++){const g=l[m];(g===this.#t||(0,A.isInclusive)(g,this.#t))&&(n?this._matchLeaves(f,g,{warn:this.#i})&&(t.push(g),a=!0):(t.push(g),a=!0))}else t=[].slice.call(l),n||(a=!0)}else h=!0;break}case o.SELECTOR_TYPE:{if(e===C){const l=this._findNode(i,{node:this.#t});l&&(t.push(l),a=!0)}else if(this.#e.contentType==="text/html"&&this.#s.nodeType===o.DOCUMENT_NODE&&!/[*|]/.test(u)){const l=this.#s.getElementsByTagName(u),p=l.length;if(p)if(this.#t.nodeType===o.ELEMENT_NODE)for(let m=0;m<p;m++){const g=l[m];(g===this.#t||(0,A.isInclusive)(g,this.#t))&&(n?this._matchLeaves(f,g,{warn:this.#i})&&(t.push(g),a=!0):(t.push(g),a=!0))}else t=[].slice.call(l),n||(a=!0)}else h=!0;break}default:if(o.REG_SHADOW_HOST.test(u)){if(this.#f&&this.#t.nodeType===o.DOCUMENT_FRAGMENT_NODE){const l=this._matchShadowHostPseudoClass(s,this.#t);l&&t.push(l)}}else if(e===C){const l=this._findNode(i,{node:this.#t});l&&(t.push(l),a=!0)}else h=!0}return{compound:n,filtered:a,nodes:t,pending:h}}_getEntryTwig(r,e){const c=r.length,i=c>1,s=r[0];let f,b;if(i){const{combo:u,leaves:[{name:n,type:t}]}=s,a=r[c-1],{leaves:[{name:h,type:l}]}=a;if(l===o.SELECTOR_PSEUDO_ELEMENT||l===o.SELECTOR_ID)f=$,b=a;else if(t===o.SELECTOR_PSEUDO_ELEMENT||t===o.SELECTOR_ID)f=O,b=s;else if(e===L)if(n==="*"&&t===o.SELECTOR_TYPE)f=$,b=a;else if(h==="*"&&l===o.SELECTOR_TYPE)f=O,b=s;else if(c===2){const{name:p}=u;/^[+~]$/.test(p)?(f=$,b=a):(f=O,b=s)}else f=O,b=s;else if(h==="*"&&l===o.SELECTOR_TYPE)f=O,b=s;else if(n==="*"&&t===o.SELECTOR_TYPE)f=$,b=a;else{let p,m;for(const{combo:g,leaves:[N]}of r){const{type:y}=N,d=(0,S.unescapeSelector)(N.name);if(y===o.SELECTOR_PSEUDO_CLASS&&d==="dir"){p=!1;break}if(g&&!m){const{name:k}=g;/^[+~]$/.test(k)&&(p=!0,m=!0)}}p?(f=O,b=s):(f=$,b=a)}}else f=$,b=s;return{complex:i,dir:f,twig:b}}_collectNodes(r){const e=this.#a.values();if(r===L||r===C){const c=new Set;let i=0;for(const{branch:s}of e){const{complex:f,dir:b,twig:u}=this._getEntryTwig(s,r),{compound:n,filtered:t,nodes:a,pending:h}=this._findEntryNodes(u,r,f);a.length?(this.#a[i].find=!0,this.#n[i]=a):h&&c.add(new Map([["index",i],["twig",u]])),this.#a[i].dir=b,this.#a[i].filtered=t||!n,i++}if(c.size){let s,f;this.#t!==this.#s&&this.#t.nodeType===o.ELEMENT_NODE?(s=this.#t,f=this.#r):(s=this.#s,f=this.#h);let b=this._traverse(s,f);for(;b;){let u=!1;if(this.#t.nodeType===o.ELEMENT_NODE?b===this.#t?u=!0:u=this.#t.contains(b):u=!0,u)for(const n of c){const{leaves:t}=n.get("twig");if(this._matchLeaves(t,b,{warn:this.#i})){const h=n.get("index");this.#a[h].filtered=!0,this.#a[h].find=!0,this.#n[h].push(b)}}b=f.nextNode()}}}else{let c=0;for(const{branch:i}of e){const s=i[i.length-1],f=i.length>1,{compound:b,filtered:u,nodes:n}=this._findEntryNodes(s,r,f);n.length&&(this.#a[c].find=!0,this.#n[c]=n),this.#a[c].dir=$,this.#a[c].filtered=u||!b,c++}}return[this.#a,this.#n]}_matchNodes(r){const[...e]=this.#a,c=e.length;let i=new Set;for(let s=0;s<c;s++){const{branch:f,dir:b,filtered:u,find:n}=e[s],t=f.length;if(t&&n){const a=this.#n[s],h=a.length,l=t-1;if(l===0){const{leaves:[,...p]}=f[0];if((r===L||r===C)&&this.#t.nodeType===o.ELEMENT_NODE)for(let m=0;m<h;m++){const g=a[m];if((u||this._matchLeaves(p,g,{warn:this.#i}))&&g!==this.#t&&this.#t.contains(g)&&(i.add(g),r!==L))break}else if(p.length)for(let m=0;m<h;m++){const g=a[m];if((u||this._matchLeaves(p,g,{warn:this.#i}))&&(i.add(g),r!==L))break}else if(r===L)if(i.size){const m=[...i];i=new Set([...m,...a]),this.#c=!0}else i=new Set([...a]);else{const[m]=[...a];i.add(m)}}else if(b===O){let{combo:p,leaves:m}=f[0];const[,...g]=m;let N;for(let y=0;y<h;y++){const d=a[y];if(u||this._matchLeaves(g,d,{warn:this.#i})){let w=new Set([d]);for(let _=1;_<t;_++){const{combo:E,leaves:x}=f[_],v=[];for(const R of w){const M={combo:p,leaves:x},z=this._matchCombinator(M,R,{dir:b,warn:this.#i});z.size&&v.push(...z)}if(v.length)if(_===l){if(r===L){if(i.size){const R=[...i];i=new Set([...R,...v])}else i=new Set([...v]);this.#c=!0}else{const[R]=(0,A.sortNodes)(v);i.add(R)}N=!0}else p=E,w=new Set(v),N=!1;else{N=!1;break}}}else N=!1;if(N&&r!==L)break}if(!N&&r===C){const[y]=[...a];let d=this._findNode(m,{node:y});for(;d;){let k=new Set([d]);for(let w=1;w<t;w++){const{combo:_,leaves:E}=f[w],x=[];for(const v of k){const R={combo:p,leaves:E},M=this._matchCombinator(R,v,{dir:b,warn:this.#i});M.size&&x.push(...M)}if(x.length)if(w===l){const[v]=(0,A.sortNodes)(x);i.add(v),N=!0}else p=_,k=new Set(x),N=!1;else{N=!1;break}}if(N)break;d=this._findNode(m,{node:d}),k=new Set([d])}}}else{const{leaves:p}=f[l],[,...m]=p;let g;for(let N=0;N<h;N++){const y=a[N];if(u||this._matchLeaves(m,y,{warn:this.#i})){let k=new Set([y]);for(let w=l-1;w>=0;w--){const _=f[w],E=[];for(const x of k){const v=this._matchCombinator(_,x,{dir:b,warn:this.#i});v.size&&E.push(...v)}if(E.length)w===0?(i.add(y),g=!0,r===L&&t>1&&i.size>1&&(this.#c=!0)):(k=new Set(E),g=!1);else{g=!1;break}}}if(g&&r!==L)break}if(!g&&r===C){const[N]=[...a];let y=this._findNode(p,{node:N});for(;y;){let d=new Set([y]);for(let k=l-1;k>=0;k--){const w=f[k],_=[];for(const E of d){const x=this._matchCombinator(w,E,{dir:b,warn:this.#i});x.size&&_.push(...x)}if(_.length)k===0?(i.add(y),g=!0):(d=new Set(_),g=!1);else{g=!1;break}}if(g)break;y=this._findNode(p,{node:y}),d=new Set([y])}}}}}return i}_find(r,e,c,i){if(this._setup(e,c,i),r===L||r===C)this.#h=this.#e.createTreeWalker(this.#s,U),this.#r=this.#e.createTreeWalker(this.#t,U),this.#c=!1;else if(e.nodeType!==o.ELEMENT_NODE){const f=`Unexpected node ${e.nodeName}`;throw new TypeError(f)}return this._collectNodes(r),this._matchNodes(r)}matches(r,e,c){let i;try{const s=this._find(B,r,e,c);s.size&&(i=s.has(this.#t))}catch(s){this._onError(s)}return!!i}closest(r,e,c){let i;try{const s=this._find(j,r,e,c);let f=this.#t;for(;f;){if(s.has(f)){i=f;break}f=f.parentNode}}catch(s){this._onError(s)}return i??null}querySelector(r,e,c){let i;try{const s=this._find(C,r,e,c);s.delete(this.#t),s.size&&([i]=(0,A.sortNodes)(s))}catch(s){this._onError(s)}return i??null}querySelectorAll(r,e,c){let i;try{const s=this._find(L,r,e,c);s.delete(this.#t),s.size&&(this.#c?i=(0,A.sortNodes)(s):i=[...s])}catch(s){this._onError(s)}return i??[]}}0&&(module.exports={Finder});
|
|
1
|
+
var j=Object.create;var I=Object.defineProperty;var B=Object.getOwnPropertyDescriptor;var G=Object.getOwnPropertyNames;var q=Object.getPrototypeOf,V=Object.prototype.hasOwnProperty;var X=(T,a)=>{for(var e in a)I(T,e,{get:a[e],enumerable:!0})},H=(T,a,e,r)=>{if(a&&typeof a=="object"||typeof a=="function")for(let i of G(a))!V.call(T,i)&&i!==e&&I(T,i,{get:()=>a[i],enumerable:!(r=B(a,i))||r.enumerable});return T};var Y=(T,a,e)=>(e=T!=null?j(q(T)):{},H(a||!T||!T.__esModule?I(e,"default",{value:T,enumerable:!0}):e,T)),K=T=>H(I({},"__esModule",{value:!0}),T);var Q={};X(Q,{Finder:()=>J});module.exports=K(Q);var W=Y(require("is-potential-custom-element-name"),1),x=require("./dom-util.js"),D=require("./matcher.js"),E=require("./parser.js"),o=require("./constant.js");const C="next",$="prev",L="all",O="first",R="lineal",P="self",z=o.SHOW_DOCUMENT|o.SHOW_DOCUMENT_FRAGMENT|o.SHOW_ELEMENT;class J{#a;#l;#e;#r;#t;#n;#c;#s;#b;#h;#o;#d;#i;#f;constructor(){this.#l=new WeakMap}_onError(a){if(a instanceof DOMException||this.#f&&a instanceof this.#f.DOMException)if(a.name===o.NOT_SUPPORTED_ERR)this.#i&&console.warn(a.message);else throw this.#f?new this.#f.DOMException(a.message,a.name):a;else throw a}_setup(a,e,r={}){const{warn:i}=r;return this.#i=!!i,[this.#f,this.#e,this.#s]=(0,x.prepareDOMObjects)(a),this.#t=a,this.#h=(0,x.isInShadowTree)(a),this.#b=e,[this.#a,this.#n]=this._correspond(e),this.#c=new WeakMap,this.#t}_correspond(a){const e=[];let r,i=this.#e&&this.#l.get(this.#e);if(i&&i.has(`${a}`)&&(r=i.get(a),typeof r=="string"))throw new DOMException(r,o.SYNTAX_ERR);if(r){const t=r.length;for(let n=0;n<t;n++)r[n].dir=null,r[n].filtered=!1,r[n].find=!1,e[n]=[]}else{let t;try{t=(0,E.parseSelector)(a)}catch(h){this.#e&&(i||(i=new Map),i.set(`${a}`,h.message),this.#l.set(this.#e,i)),this._onError(h)}const n=(0,E.walkAST)(t);r=[];let f=0;for(const[...h]of n){const c=[];let s=h.shift();if(s&&s.type!==o.COMBINATOR){const l=new Set;for(;s;){if(s.type===o.COMBINATOR){const[u]=h;if(u.type===o.COMBINATOR){const d=`Invalid selector ${a}`;throw this.#e&&(i||(i=new Map),i.set(`${a}`,d),this.#l.set(this.#e,i)),new DOMException(d,o.SYNTAX_ERR)}c.push({combo:s,leaves:(0,E.sortAST)(l)}),l.clear()}else s&&l.add(s);if(h.length)s=h.shift();else{c.push({combo:null,leaves:(0,E.sortAST)(l)}),l.clear();break}}}r.push({branch:c,dir:null,filtered:!1,find:!1}),e[f]=[],f++}this.#e&&(i||(i=new Map),i.set(`${a}`,r),this.#l.set(this.#e,i))}return[r,e]}_traverse(a={},e=this.#d){let r,i=e.currentNode;if(a.nodeType===o.ELEMENT_NODE&&i===a)r=i;else{if(i!==e.root)for(;i&&!(i===e.root||a.nodeType===o.ELEMENT_NODE&&i===a);)i=e.parentNode();if(a.nodeType===o.ELEMENT_NODE)for(;i;){if(i===a){r=i;break}i=e.nextNode()}else r=i}return r??null}_collectNthChild(a,e,r){const{a:i,b:t,reverse:n,selector:f}=a,{parentNode:h}=e;let c=new Set,s;if(f&&(this.#l.has(f)?s=this.#l.get(f):(s=(0,E.walkAST)(f),this.#l.set(f,s))),h){const l=this.#e.createTreeWalker(h,z);let u=0,d=l.firstChild();for(;d;)u++,d=l.nextSibling();d=this._traverse(h,l);const g=new Set;if(s)for(d=this._traverse(h,l),d=l.firstChild();d;){let m;for(const k of s)if(m=this._matchLeaves(k,d,r),!m)break;m&&g.add(d),d=l.nextSibling()}if(i===0){if(t>0&&t<=u){if(g.size){let m=0;for(d=this._traverse(h,l),n?d=l.lastChild():d=l.firstChild();d;){if(g.has(d)){if(m===t-1){c.add(d);break}m++}n?d=l.previousSibling():d=l.nextSibling()}}else if(!f){let m=0;for(d=this._traverse(h,l),n?d=l.lastChild():d=l.firstChild();d;){if(m===t-1){c.add(d);break}n?d=l.previousSibling():d=l.nextSibling(),m++}}}}else{let m=t-1;if(i>0)for(;m<0;)m+=i;if(m>=0&&m<u){let k=0,N=i>0?0:t-1;for(d=this._traverse(h,l),n?d=l.lastChild():d=l.firstChild();d&&(d&&m>=0&&m<u);)g.size?g.has(d)&&(N===m&&(c.add(d),m+=i),i>0?N++:N--):k===m&&(f||c.add(d),m+=i),n?d=l.previousSibling():d=l.nextSibling(),k++}}if(n&&c.size>1){const m=[...c];c=new Set(m.reverse())}}else if(e===this.#s&&this.#s.nodeType===o.ELEMENT_NODE&&i+t===1)if(s){let l;for(const u of s)if(l=this._matchLeaves(u,e,r),l)break;l&&c.add(e)}else c.add(e);return c}_collectNthOfType(a,e){const{a:r,b:i,reverse:t}=a,{localName:n,parentNode:f,prefix:h}=e;let c=new Set;if(f){const s=this.#e.createTreeWalker(f,z);let l=0,u=s.firstChild();for(;u;)l++,u=s.nextSibling();if(r===0){if(i>0&&i<=l){let d=0;for(u=this._traverse(f,s),t?u=s.lastChild():u=s.firstChild();u;){const{localName:g,prefix:m}=u;if(g===n&&m===h){if(d===i-1){c.add(u);break}d++}t?u=s.previousSibling():u=s.nextSibling()}}}else{let d=i-1;if(r>0)for(;d<0;)d+=r;if(d>=0&&d<l){let g=r>0?0:i-1;for(u=this._traverse(f,s),t?u=s.lastChild():u=s.firstChild();u;){const{localName:m,prefix:k}=u;if(m===n&&k===h){if(g===d&&(c.add(u),d+=r),d<0||d>=l)break;r>0?g++:g--}t?u=s.previousSibling():u=s.nextSibling()}}}if(t&&c.size>1){const d=[...c];c=new Set(d.reverse())}}else e===this.#s&&this.#s.nodeType===o.ELEMENT_NODE&&r+i===1&&c.add(e);return c}_matchAnPlusB(a,e,r,i){const{nth:{a:t,b:n,name:f},selector:h}=a,c=(0,E.unescapeSelector)(f),s=new Map;c?(c==="even"?(s.set("a",2),s.set("b",0)):c==="odd"&&(s.set("a",2),s.set("b",1)),r.indexOf("last")>-1&&s.set("reverse",!0)):(typeof t=="string"&&/-?\d+/.test(t)?s.set("a",t*1):s.set("a",0),typeof n=="string"&&/-?\d+/.test(n)?s.set("b",n*1):s.set("b",0),r.indexOf("last")>-1&&s.set("reverse",!0));let l=new Set;if(s.has("a")&&s.has("b")){if(/^nth-(?:last-)?child$/.test(r)){h&&s.set("selector",h);const u=Object.fromEntries(s),d=this._collectNthChild(u,e,i);d.size&&(l=d)}else if(/^nth-(?:last-)?of-type$/.test(r)){const u=Object.fromEntries(s),d=this._collectNthOfType(u,e);d.size&&(l=d)}}return l}_matchDirectionPseudoClass(a,e){const r=(0,E.unescapeSelector)(a.name),i=(0,x.getDirectionality)(e);let t;return r===i&&(t=e),t??null}_matchLanguagePseudoClass(a,e){const r=(0,E.unescapeSelector)(a.name);let i;if(r==="*")if(e.hasAttribute("lang"))e.getAttribute("lang")&&(i=e);else{let t=e.parentNode;for(;t&&t.nodeType===o.ELEMENT_NODE;){if(t.hasAttribute("lang")){t.getAttribute("lang")&&(i=e);break}t=t.parentNode}}else if(r){const t=`(?:-${o.ALPHA_NUM})*`;if(new RegExp(`^(?:\\*-)?${o.ALPHA_NUM}${t}$`,"i").test(r)){let f;if(r.indexOf("-")>-1){const[h,c,...s]=r.split("-");let l;h==="*"?l=`${o.ALPHA_NUM}${t}`:l=`${h}${t}`;const u=`-${c}${t}`,d=s.length;let g="";if(d)for(let m=0;m<d;m++)g+=`-${s[m]}${t}`;f=new RegExp(`^${l}${u}${g}$`,"i")}else f=new RegExp(`^${r}${t}$`,"i");if(e.hasAttribute("lang"))f.test(e.getAttribute("lang"))&&(i=e);else{let h=e.parentNode;for(;h&&h.nodeType===o.ELEMENT_NODE;){if(h.hasAttribute("lang")){const c=h.getAttribute("lang");f.test(c)&&(i=e);break}h=h.parentNode}}}}return i??null}_matchHasPseudoFunc(a,e,r={}){let i;if(Array.isArray(a)&&a.length){const[t]=a,{type:n}=t;let f;n===o.COMBINATOR?f=a.shift():f={name:" ",type:o.COMBINATOR};const h=[];for(;a.length;){const[l]=a,{type:u}=l;if(u===o.COMBINATOR)break;h.push(a.shift())}const c={combo:f,leaves:h};r.dir=C;const s=this._matchCombinator(c,e,r);if(s.size)if(a.length){for(const l of s)if(i=this._matchHasPseudoFunc(Object.assign([],a),l,r),i)break}else i=!0}return!!i}_matchLogicalPseudoFunc(a,e,r={}){const{astName:i="",branches:t=[],selector:n="",twigBranches:f=[]}=a;let h;if(i==="has")if(n.includes(":has("))h=null;else{let c;for(const s of t)if(c=this._matchHasPseudoFunc(Object.assign([],s),e,r),c)break;c&&(h=e)}else{const c=/^(?:is|where)$/.test(i);r.forgive=c;const s=f.length;let l;for(let u=0;u<s;u++){const d=f[u],g=d.length-1,{leaves:m}=d[g];if(l=this._matchLeaves(m,e,r),l&&g>0){let k=new Set([e]);for(let N=g-1;N>=0;N--){const _=d[N],b=[];r.dir=$;for(const w of k){const p=this._matchCombinator(_,w,r);p.size&&b.push(...p)}if(b.length)N===0?l=!0:k=new Set(b);else{l=!1;break}}}if(l)break}i==="not"?l||(h=e):l&&(h=e)}return h??null}_matchPseudoClassSelector(a,e,r={}){const{children:i}=a,{localName:t,parentNode:n}=e,{forgive:f,warn:h}=r,c=(0,E.unescapeSelector)(a.name);let s=new Set;if(o.REG_LOGICAL_PSEUDO.test(c)){let l;if(this.#l.has(a))l=this.#l.get(a);else{const d=(0,E.walkAST)(a),g=[],m=[];for(const[...k]of d){for(const w of k){const p=(0,E.generateCSS)(w);g.push(p)}const N=[],_=new Set;let b=k.shift();for(;b;)if(b.type===o.COMBINATOR?(N.push({combo:b,leaves:[..._]}),_.clear()):b&&_.add(b),k.length)b=k.shift();else{N.push({combo:null,leaves:[..._]}),_.clear();break}m.push(N)}l={astName:c,branches:d,twigBranches:m,selector:g.join(",")},this.#l.set(a,l)}const u=this._matchLogicalPseudoFunc(l,e,r);u&&s.add(u)}else if(Array.isArray(i)){const[l]=i;if(/^nth-(?:last-)?(?:child|of-type)$/.test(c)){const u=this._matchAnPlusB(l,e,c,r);u.size&&(s=u)}else if(c==="dir"){const u=this._matchDirectionPseudoClass(l,e);u&&s.add(u)}else if(c==="lang"){const u=this._matchLanguagePseudoClass(l,e);u&&s.add(u)}else switch(c){case"current":case"nth-col":case"nth-last-col":{if(h){const u=`Unsupported pseudo-class :${c}()`;throw new DOMException(u,o.NOT_SUPPORTED_ERR)}break}case"host":case"host-context":break;default:if(!f){const u=`Unknown pseudo-class :${c}()`;throw new DOMException(u,o.SYNTAX_ERR)}}}else{const l=/^a(?:rea)?$/,u=/^(?:(?:fieldse|inpu|selec)t|button|opt(?:group|ion)|textarea)$/,d=/^(?:(?:inpu|selec)t|button|form|textarea)$/,g=/^d(?:etails|ialog)$/,m=/^(?:checkbox|radio)$/,k=/^(?:date(?:time-local)?|month|time|week)$/,N=/(?:(?:rang|tim)e|date(?:time-local)?|month|number|week)$/,_=/^(?:(?:emai|te|ur)l|number|password|search|text)$/;switch(c){case"any-link":case"link":{l.test(t)&&e.hasAttribute("href")&&s.add(e);break}case"local-link":{if(l.test(t)&&e.hasAttribute("href")){const{href:b,origin:w,pathname:p}=new URL(this.#e.URL),y=new URL(e.getAttribute("href"),b);y.origin===w&&y.pathname===p&&s.add(e)}break}case"visited":break;case"target":{const{hash:b}=new URL(this.#e.URL);e.id&&b===`#${e.id}`&&this.#e.contains(e)&&s.add(e);break}case"target-within":{const{hash:b}=new URL(this.#e.URL);if(b){const w=b.replace(/^#/,"");let p=this.#e.getElementById(w);for(;p;){if(p===e){s.add(e);break}p=p.parentNode}}break}case"scope":{this.#t.nodeType===o.ELEMENT_NODE?e===this.#t&&s.add(e):e===this.#e.documentElement&&s.add(e);break}case"focus":{e===this.#e.activeElement&&s.add(e);break}case"focus-within":{let b=this.#e.activeElement;for(;b;){if(b===e){s.add(e);break}b=b.parentNode}break}case"open":{g.test(t)&&e.hasAttribute("open")&&s.add(e);break}case"closed":{g.test(t)&&!e.hasAttribute("open")&&s.add(e);break}case"disabled":{if(u.test(t)||(0,W.default)(t))if(e.disabled||e.hasAttribute("disabled"))s.add(e);else{let b=n;for(;b&&b.localName!=="fieldset";)b=b.parentNode;b&&n.localName!=="legend"&&b.hasAttribute("disabled")&&s.add(e)}break}case"enabled":{(u.test(t)||(0,W.default)(t))&&!(e.disabled&&e.hasAttribute("disabled"))&&s.add(e);break}case"read-only":{switch(t){case"textarea":{(e.readonly||e.hasAttribute("readonly")||e.disabled||e.hasAttribute("disabled"))&&s.add(e);break}case"input":{(!e.type||k.test(e.type)||_.test(e.type))&&(e.readonly||e.hasAttribute("readonly")||e.disabled||e.hasAttribute("disabled"))&&s.add(e);break}default:(0,x.isContentEditable)(e)||s.add(e)}break}case"read-write":{switch(t){case"textarea":{e.readonly||e.hasAttribute("readonly")||e.disabled||e.hasAttribute("disabled")||s.add(e);break}case"input":{(!e.type||k.test(e.type)||_.test(e.type))&&!(e.readonly||e.hasAttribute("readonly")||e.disabled||e.hasAttribute("disabled"))&&s.add(e);break}default:(0,x.isContentEditable)(e)&&s.add(e)}break}case"placeholder-shown":{let b;t==="textarea"?b=e:t==="input"&&(e.hasAttribute("type")?_.test(e.getAttribute("type"))&&(b=e):b=e),b&&e.value===""&&e.hasAttribute("placeholder")&&e.getAttribute("placeholder").trim().length&&s.add(e);break}case"checked":{(e.checked&&t==="input"&&e.hasAttribute("type")&&m.test(e.getAttribute("type"))||e.selected&&t==="option")&&s.add(e);break}case"indeterminate":{if(e.indeterminate&&t==="input"&&e.type==="checkbox"||t==="progress"&&!e.hasAttribute("value"))s.add(e);else if(t==="input"&&e.type==="radio"&&!e.hasAttribute("checked")){const b=e.name;let w=e.parentNode;for(;w&&w.localName!=="form";)w=w.parentNode;w||(w=this.#e.documentElement);let p;const y=w.getElementsByTagName("input"),v=y.length;if(v)for(let A=0;A<v;A++){const S=y[A];if(S.getAttribute("type")==="radio"&&(b?S.getAttribute("name")===b&&(p=!!S.checked):S.hasAttribute("name")||(p=!!S.checked),p))break}p||s.add(e)}break}case"default":{const b=/^(?:button|reset)$/,w=/^(?:image|submit)$/;if(t==="button"&&!(e.hasAttribute("type")&&b.test(e.getAttribute("type")))||t==="input"&&e.hasAttribute("type")&&w.test(e.getAttribute("type"))){let p=e.parentNode;for(;p&&p.localName!=="form";)p=p.parentNode;if(p){const y=this.#e.createTreeWalker(p,o.SHOW_ELEMENT);let v=y.firstChild();for(;v;){const A=v.localName;let S;if(A==="button"?S=!(v.hasAttribute("type")&&b.test(v.getAttribute("type"))):A==="input"&&(S=v.hasAttribute("type")&&w.test(v.getAttribute("type"))),S){v===e&&s.add(e);break}v=y.nextNode()}}}else if(t==="input"&&e.hasAttribute("type")&&m.test(e.getAttribute("type"))&&(e.checked||e.hasAttribute("checked")))s.add(e);else if(t==="option"){let p=!1,y=n;for(;y&&y.localName!=="datalist";){if(y.localName==="select"){(y.multiple||y.hasAttribute("multiple"))&&(p=!0);break}y=y.parentNode}if(p)(e.selected||e.hasAttribute("selected"))&&s.add(e);else{const v=new Set,A=this.#e.createTreeWalker(n,o.SHOW_ELEMENT);let S=A.firstChild();for(;S;){if(S.selected||S.hasAttribute("selected")){v.add(S);break}S=A.nextSibling()}v.size&&v.has(e)&&s.add(e)}}break}case"valid":{if(d.test(t))e.checkValidity()&&s.add(e);else if(t==="fieldset"){let b;const w=this.#e.createTreeWalker(e,o.SHOW_ELEMENT);let p=w.firstChild();for(;p&&!(d.test(p.localName)&&(b=p.checkValidity(),!b));)p=w.nextNode();b&&s.add(e)}break}case"invalid":{if(d.test(t))e.checkValidity()||s.add(e);else if(t==="fieldset"){let b;const w=this.#e.createTreeWalker(e,o.SHOW_ELEMENT);let p=w.firstChild();for(;p&&!(d.test(p.localName)&&(b=p.checkValidity(),!b));)p=w.nextNode();b||s.add(e)}break}case"in-range":{t==="input"&&!(e.readonly||e.hasAttribute("readonly"))&&!(e.disabled||e.hasAttribute("disabled"))&&e.hasAttribute("type")&&N.test(e.getAttribute("type"))&&!(e.validity.rangeUnderflow||e.validity.rangeOverflow)&&(e.hasAttribute("min")||e.hasAttribute("max")||e.getAttribute("type")==="range")&&s.add(e);break}case"out-of-range":{t==="input"&&!(e.readonly||e.hasAttribute("readonly"))&&!(e.disabled||e.hasAttribute("disabled"))&&e.hasAttribute("type")&&N.test(e.getAttribute("type"))&&(e.validity.rangeUnderflow||e.validity.rangeOverflow)&&s.add(e);break}case"required":{let b;if(/^(?:select|textarea)$/.test(t))b=e;else if(t==="input")if(e.hasAttribute("type")){const w=e.getAttribute("type");(w==="file"||m.test(w)||k.test(w)||_.test(w))&&(b=e)}else b=e;b&&(e.required||e.hasAttribute("required"))&&s.add(e);break}case"optional":{let b;if(/^(?:select|textarea)$/.test(t))b=e;else if(t==="input")if(e.hasAttribute("type")){const w=e.getAttribute("type");(w==="file"||m.test(w)||k.test(w)||_.test(w))&&(b=e)}else b=e;b&&!(e.required||e.hasAttribute("required"))&&s.add(e);break}case"root":{e===this.#e.documentElement&&s.add(e);break}case"empty":{if(e.hasChildNodes()){let b;const w=this.#e.createTreeWalker(e,o.SHOW_ALL);let p=w.firstChild();for(;p&&(b=p.nodeType!==o.ELEMENT_NODE&&p.nodeType!==o.TEXT_NODE,!!b);)p=w.nextSibling();b&&s.add(e)}else s.add(e);break}case"first-child":{(n&&e===n.firstElementChild||e===this.#s&&this.#s.nodeType===o.ELEMENT_NODE)&&s.add(e);break}case"last-child":{(n&&e===n.lastElementChild||e===this.#s&&this.#s.nodeType===o.ELEMENT_NODE)&&s.add(e);break}case"only-child":{(n&&e===n.firstElementChild&&e===n.lastElementChild||e===this.#s&&this.#s.nodeType===o.ELEMENT_NODE)&&s.add(e);break}case"first-of-type":{if(n){const[b]=this._collectNthOfType({a:0,b:1},e);b&&s.add(b)}else e===this.#s&&this.#s.nodeType===o.ELEMENT_NODE&&s.add(e);break}case"last-of-type":{if(n){const[b]=this._collectNthOfType({a:0,b:1,reverse:!0},e);b&&s.add(b)}else e===this.#s&&this.#s.nodeType===o.ELEMENT_NODE&&s.add(e);break}case"only-of-type":{if(n){const[b]=this._collectNthOfType({a:0,b:1},e);if(b===e){const[w]=this._collectNthOfType({a:0,b:1,reverse:!0},e);w===e&&s.add(e)}}else e===this.#s&&this.#s.nodeType===o.ELEMENT_NODE&&s.add(e);break}case"host":case"host-context":break;case"after":case"before":case"first-letter":case"first-line":{if(h){const b=`Unsupported pseudo-element ::${c}`;throw new DOMException(b,o.NOT_SUPPORTED_ERR)}break}case"active":case"autofill":case"blank":case"buffering":case"current":case"defined":case"focus-visible":case"fullscreen":case"future":case"hover":case"modal":case"muted":case"past":case"paused":case"picture-in-picture":case"playing":case"seeking":case"stalled":case"user-invalid":case"user-valid":case"volume-locked":case"-webkit-autofill":{if(h){const b=`Unsupported pseudo-class :${c}`;throw new DOMException(b,o.NOT_SUPPORTED_ERR)}break}default:if(c.startsWith("-webkit-")){if(h){const b=`Unsupported pseudo-class :${c}`;throw new DOMException(b,o.NOT_SUPPORTED_ERR)}}else if(!f){const b=`Unknown pseudo-class :${c}`;throw new DOMException(b,o.SYNTAX_ERR)}}}return s}_matchShadowHostPseudoClass(a,e){const{children:r}=a,i=(0,E.unescapeSelector)(a.name);let t;if(Array.isArray(r)){const[n]=(0,E.walkAST)(r[0]),[...f]=n,{host:h}=e;if(i==="host"){let c;for(const s of f){const{type:l}=s;if(l===o.COMBINATOR){const d=`Invalid selector ${(0,E.generateCSS)(a)}`;throw new DOMException(d,o.SYNTAX_ERR)}if(c=this._matchSelector(s,h).has(h),!c)break}c&&(t=e)}else if(i==="host-context"){let c,s=h;for(;s;){for(const l of f){const{type:u}=l;if(u===o.COMBINATOR){const g=`Invalid selector ${(0,E.generateCSS)(a)}`;throw new DOMException(g,o.SYNTAX_ERR)}if(c=this._matchSelector(l,s).has(s),!c)break}if(c)break;s=s.parentNode}c&&(t=e)}}else if(i==="host")t=e;else{const n=`Invalid selector :${i}`;throw new DOMException(n,o.SYNTAX_ERR)}return t??null}_matchSelector(a,e,r){const{type:i}=a,t=(0,E.unescapeSelector)(a.name);let n=new Set;if(e.nodeType===o.ELEMENT_NODE)switch(i){case o.SELECTOR_PSEUDO_CLASS:{const f=this._matchPseudoClassSelector(a,e,r);f.size&&(n=f);break}case o.SELECTOR_PSEUDO_ELEMENT:{(0,D.matchPseudoElementSelector)(t,r);break}default:{const f=(0,D.matchSelector)(a,e,r);f&&n.add(f)}}else if(this.#h&&i===o.SELECTOR_PSEUDO_CLASS&&e.nodeType===o.DOCUMENT_FRAGMENT_NODE){if(t!=="has"&&o.REG_LOGICAL_PSEUDO.test(t)){const f=this._matchPseudoClassSelector(a,e,r);f.size&&(n=f)}else if(o.REG_SHADOW_HOST.test(t)){const f=this._matchShadowHostPseudoClass(a,e,r);f&&n.add(f)}}return n}_matchLeaves(a,e,r){let i;if(this.#c.has(a)){const t=this.#c.get(a);if(t.has(e))i=t.get(e);else{for(const n of a)if(i=this._matchSelector(n,e,r).has(e),!i)break;t.set(e,i),this.#c.set(a,t)}}else{for(const n of a)if(i=this._matchSelector(n,e,r).has(e),!i)break;const t=new WeakMap;t.set(e,i),this.#c.set(a,t)}return!!i}_matchHTMLCollection(a,e={}){const{compound:r,filterLeaves:i}=e;let t=new Set;const n=a.length;if(n)if(r)for(let f=0;f<n;f++){const h=a[f];this._matchLeaves(i,h,e)&&t.add(h)}else{const f=[].slice.call(a);t=new Set(f)}return t}_findDescendantNodes(a,e,r){const[i,...t]=a,{type:n}=i,f=(0,E.unescapeSelector)(i.name),h=t.length>0;let c=new Set,s=!1;if(this.#h)s=!0;else switch(n){case o.SELECTOR_ID:{if(this.#s.nodeType===o.ELEMENT_NODE)s=!0;else{const l=this.#s.getElementById(f);l&&l!==e&&e.contains(l)&&(h?this._matchLeaves(t,l,r)&&c.add(l):c.add(l))}break}case o.SELECTOR_CLASS:{const l=e.getElementsByClassName(f);c=this._matchHTMLCollection(l,{compound:h,filterLeaves:t});break}case o.SELECTOR_TYPE:{if(this.#e.contentType==="text/html"&&!/[*|]/.test(f)){const l=e.getElementsByTagName(f);c=this._matchHTMLCollection(l,{compound:h,filterLeaves:t})}else s=!0;break}case o.SELECTOR_PSEUDO_ELEMENT:{(0,D.matchPseudoElementSelector)(f,r);break}default:s=!0}return{nodes:c,pending:s}}_matchCombinator(a,e,r={}){const{combo:i,leaves:t}=a,{name:n}=i,{dir:f}=r;let h=new Set;if(f===C)switch(n){case"+":{const c=e.nextElementSibling;c&&this._matchLeaves(t,c,r)&&h.add(c);break}case"~":{const{parentNode:c}=e;if(c){const s=this.#e.createTreeWalker(c,o.SHOW_ELEMENT);let l=this._traverse(e,s);for(l===e&&(l=s.nextSibling());l;)this._matchLeaves(t,l,r)&&h.add(l),l=s.nextSibling()}break}case">":{const c=this.#e.createTreeWalker(e,o.SHOW_ELEMENT);let s=c.firstChild();for(;s;)this._matchLeaves(t,s,r)&&h.add(s),s=c.nextSibling();break}case" ":default:{const{nodes:c,pending:s}=this._findDescendantNodes(t,e);if(c.size)h=c;else if(s){const l=this.#e.createTreeWalker(e,o.SHOW_ELEMENT);let u=l.nextNode();for(;u;)this._matchLeaves(t,u,r)&&h.add(u),u=l.nextNode()}}}else switch(n){case"+":{const c=e.previousElementSibling;c&&this._matchLeaves(t,c,r)&&h.add(c);break}case"~":{const c=this.#e.createTreeWalker(e.parentNode,o.SHOW_ELEMENT);let s=c.firstChild();for(;s&&s!==e;)this._matchLeaves(t,s,r)&&h.add(s),s=c.nextSibling();break}case">":{const c=e.parentNode;c&&this._matchLeaves(t,c,r)&&h.add(c);break}case" ":default:{const c=[];let s=e.parentNode;for(;s;)this._matchLeaves(t,s,r)&&c.push(s),s=s.parentNode;c.length&&(h=new Set(c.reverse()))}}return h}_findNode(a,e={}){const{node:r}=e;let i,t=this._traverse(r,this.#r);if(t)for(t.nodeType!==o.ELEMENT_NODE?t=this.#r.nextNode():t===r&&t!==this.#s&&(t=this.#r.nextNode());t;){let n;if(this.#t.nodeType===o.ELEMENT_NODE?t===this.#t?n=!0:n=this.#t.contains(t):n=!0,n&&this._matchLeaves(a,t,{warn:this.#i})){i=t;break}t=this.#r.nextNode()}return i??null}_matchSelf(a){const e=[];let r=!1;return this._matchLeaves(a,this.#t)&&(e.push(this.#t),r=!0),[e,r]}_findLineal(a,e={}){const{complex:r,compound:i}=e,t=[];let n=!1,f=this._matchLeaves([a],this.#t);if(f&&!r&&!i)t.push(this.#t),n=!0;else{f&&(t.push(this.#t),i||(n=!0));let h=this.#t.parentNode;for(;h&&(f=this._matchLeaves([a],h),f&&(t.push(h),i||(n=!0)),h.parentNode);)h=h.parentNode}return[t,n]}_findFirst(a){const e=[];let r=!1;const i=this._findNode(a,{node:this.#t});return i&&(e.push(i),r=!0),[e,r]}_findFromHTMLCollection(a,e=[],r={}){const{compound:i,filterLeaves:t}=r;let n=!1;const f=a.length;if(f)if(this.#t.nodeType===o.ELEMENT_NODE)for(let h=0;h<f;h++){const c=a[h];(c===this.#t||(0,x.isInclusive)(c,this.#t))&&(i?this._matchLeaves(t,c,{warn:this.#i})&&(e.push(c),n=!0):(e.push(c),n=!0))}else e=[].slice.call(a),i||(n=!0);return[e,n]}_findEntryNodes(a,e,r){const{leaves:i}=a,[t,...n]=i,{type:f}=t,h=(0,E.unescapeSelector)(t.name),c=n.length>0;let s=[],l=!1,u=!1;switch(f){case o.SELECTOR_PSEUDO_ELEMENT:{(0,D.matchPseudoElementSelector)(h,{warn:this.#i});break}case o.SELECTOR_ID:{if(e===P)[s,l]=this._matchSelf(i);else if(e===R)[s,l]=this._findLineal(t,{complex:r,compound:c});else if(e===O&&this.#s.nodeType!==o.ELEMENT_NODE){const d=this.#s.getElementById(h);d&&(c?this._matchLeaves(n,d,{warn:this.#i})&&(s.push(d),l=!0):(s.push(d),l=!0))}else u=!0;break}case o.SELECTOR_CLASS:{if(e===P)[s,l]=this._matchSelf(i);else if(e===R)[s,l]=this._findLineal(t,{complex:r,compound:c});else if(e===O)[s,l]=this._findFirst(i);else if(this.#s.nodeType===o.DOCUMENT_NODE){const d=this.#s.getElementsByClassName(h);[s,l]=this._findFromHTMLCollection(d,s,{compound:c,filterLeaves:n})}else u=!0;break}case o.SELECTOR_TYPE:{if(e===P)[s,l]=this._matchSelf(i);else if(e===R)[s,l]=this._findLineal(t,{complex:r,compound:c});else if(this.#e.contentType==="text/html"&&this.#s.nodeType===o.DOCUMENT_NODE&&!/[*|]/.test(h)){const d=this.#s.getElementsByTagName(h);[s,l]=this._findFromHTMLCollection(d,s,{compound:c,filterLeaves:n})}else u=!0;break}default:if(e!==R&&o.REG_SHADOW_HOST.test(h)){if(this.#h&&this.#t.nodeType===o.DOCUMENT_FRAGMENT_NODE){const d=this._matchShadowHostPseudoClass(t,this.#t);d&&s.push(d)}}else e===P?[s,l]=this._matchSelf(i):e===R?[s,l]=this._findLineal(t,{complex:r,compound:c}):e===O?[s,l]=this._findFirst(i):u=!0}return{compound:c,filtered:l,nodes:s,pending:u}}_getEntryTwig(a,e){const r=a.length,i=r>1,t=a[0];let n,f;if(i){const{combo:h,leaves:[{name:c,type:s}]}=t,l=a[r-1],{leaves:[{name:u,type:d}]}=l;if(d===o.SELECTOR_PSEUDO_ELEMENT||d===o.SELECTOR_ID)n=$,f=l;else if(s===o.SELECTOR_PSEUDO_ELEMENT||s===o.SELECTOR_ID)n=C,f=t;else if(e===L)if(c==="*"&&s===o.SELECTOR_TYPE)n=$,f=l;else if(u==="*"&&d===o.SELECTOR_TYPE)n=C,f=t;else if(r===2){const{name:g}=h;/^[+~]$/.test(g)?(n=$,f=l):(n=C,f=t)}else n=C,f=t;else if(u==="*"&&d===o.SELECTOR_TYPE)n=C,f=t;else if(c==="*"&&s===o.SELECTOR_TYPE)n=$,f=l;else{let g,m;for(const{combo:k,leaves:[N]}of a){const{type:_}=N,b=(0,E.unescapeSelector)(N.name);if(_===o.SELECTOR_PSEUDO_CLASS&&b==="dir"){g=!1;break}if(k&&!m){const{name:w}=k;/^[+~]$/.test(w)&&(g=!0,m=!0)}}g?(n=C,f=t):(n=$,f=l)}}else n=$,f=t;return{complex:i,dir:n,twig:f}}_collectNodes(a){const e=this.#a.values();if(a===L||a===O){const r=new Set;let i=0;for(const{branch:t}of e){const{complex:n,dir:f,twig:h}=this._getEntryTwig(t,a),{compound:c,filtered:s,nodes:l,pending:u}=this._findEntryNodes(h,a,n);l.length?(this.#a[i].find=!0,this.#n[i]=l):u&&r.add(new Map([["index",i],["twig",h]])),this.#a[i].dir=f,this.#a[i].filtered=s||!c,i++}if(r.size){let t,n;this.#t!==this.#s&&this.#t.nodeType===o.ELEMENT_NODE?(t=this.#t,n=this.#r):(t=this.#s,n=this.#d);let f=this._traverse(t,n);for(;f;){let h=!1;if(this.#t.nodeType===o.ELEMENT_NODE?f===this.#t?h=!0:h=this.#t.contains(f):h=!0,h)for(const c of r){const{leaves:s}=c.get("twig");if(this._matchLeaves(s,f,{warn:this.#i})){const u=c.get("index");this.#a[u].filtered=!0,this.#a[u].find=!0,this.#n[u].push(f)}}f=n.nextNode()}}}else{let r=0;for(const{branch:i}of e){const t=i[i.length-1],n=i.length>1,{compound:f,filtered:h,nodes:c}=this._findEntryNodes(t,a,n);c.length&&(this.#a[r].find=!0,this.#n[r]=c),this.#a[r].dir=$,this.#a[r].filtered=h||!f,r++}}return[this.#a,this.#n]}_matchNodes(a){const[...e]=this.#a,r=e.length;let i=new Set;for(let t=0;t<r;t++){const{branch:n,dir:f,filtered:h,find:c}=e[t],s=n.length;if(s&&c){const l=this.#n[t],u=l.length,d=s-1;if(d===0){const{leaves:[,...g]}=n[0];if((a===L||a===O)&&this.#t.nodeType===o.ELEMENT_NODE)for(let m=0;m<u;m++){const k=l[m];if((h||this._matchLeaves(g,k,{warn:this.#i}))&&k!==this.#t&&this.#t.contains(k)&&(i.add(k),a!==L))break}else if(g.length)for(let m=0;m<u;m++){const k=l[m];if((h||this._matchLeaves(g,k,{warn:this.#i}))&&(i.add(k),a!==L))break}else if(a===L)if(i.size){const m=[...i];i=new Set([...m,...l]),this.#o=!0}else i=new Set([...l]);else{const[m]=[...l];i.add(m)}}else if(f===C){let{combo:g,leaves:m}=n[0];const[,...k]=m;let N;for(let _=0;_<u;_++){const b=l[_];if(h||this._matchLeaves(k,b,{warn:this.#i})){let p=new Set([b]);for(let y=1;y<s;y++){const{combo:v,leaves:A}=n[y],S=[];for(const M of p){const U={combo:g,leaves:A},F=this._matchCombinator(U,M,{dir:f,warn:this.#i});F.size&&S.push(...F)}if(S.length)if(y===d){if(a===L){if(i.size){const M=[...i];i=new Set([...M,...S])}else i=new Set([...S]);this.#o=!0}else{const[M]=(0,x.sortNodes)(S);i.add(M)}N=!0}else g=v,p=new Set(S),N=!1;else{N=!1;break}}}else N=!1;if(N&&a!==L)break}if(!N&&a===O){const[_]=[...l];let b=this._findNode(m,{node:_});for(;b;){let w=new Set([b]);for(let p=1;p<s;p++){const{combo:y,leaves:v}=n[p],A=[];for(const S of w){const M={combo:g,leaves:v},U=this._matchCombinator(M,S,{dir:f,warn:this.#i});U.size&&A.push(...U)}if(A.length)if(p===d){const[S]=(0,x.sortNodes)(A);i.add(S),N=!0}else g=y,w=new Set(A),N=!1;else{N=!1;break}}if(N)break;b=this._findNode(m,{node:b}),w=new Set([b])}}}else{const{leaves:g}=n[d],[,...m]=g;let k;for(let N=0;N<u;N++){const _=l[N];if(h||this._matchLeaves(m,_,{warn:this.#i})){let w=new Set([_]);for(let p=d-1;p>=0;p--){const y=n[p],v=[];for(const A of w){const S=this._matchCombinator(y,A,{dir:f,warn:this.#i});S.size&&v.push(...S)}if(v.length)p===0?(i.add(_),k=!0,a===L&&s>1&&i.size>1&&(this.#o=!0)):(w=new Set(v),k=!1);else{k=!1;break}}}if(k&&a!==L)break}if(!k&&a===O){const[N]=[...l];let _=this._findNode(g,{node:N});for(;_;){let b=new Set([_]);for(let w=d-1;w>=0;w--){const p=n[w],y=[];for(const v of b){const A=this._matchCombinator(p,v,{dir:f,warn:this.#i});A.size&&y.push(...A)}if(y.length)w===0?(i.add(_),k=!0):(b=new Set(y),k=!1);else{k=!1;break}}if(k)break;_=this._findNode(g,{node:_}),b=new Set([_])}}}}}return i}_find(a,e,r,i){switch(e=this._setup(e,r,i),a){case L:case O:{this.#d=this.#e.createTreeWalker(this.#s,z),this.#r=this.#e.createTreeWalker(e,z),this.#o=!1;break}default:if(e.nodeType!==o.ELEMENT_NODE){const n=`Unexpected node ${e.nodeName}`;throw new TypeError(n)}}return this._collectNodes(a),this._matchNodes(a)}matches(a,e,r){let i;try{const t=this._find(P,a,e,r);t.size&&(i=t.has(this.#t))}catch(t){this._onError(t)}return!!i}closest(a,e,r){let i;try{const t=this._find(R,a,e,r);let n=this.#t;for(;n;){if(t.has(n)){i=n;break}if(n.parentNode)n=n.parentNode;else break}}catch(t){this._onError(t)}return i??null}querySelector(a,e,r){let i;try{const t=this._find(O,a,e,r);t.delete(this.#t),t.size&&([i]=(0,x.sortNodes)(t))}catch(t){this._onError(t)}return i??null}querySelectorAll(a,e,r){let i;try{const t=this._find(L,a,e,r);t.delete(this.#t),t.size&&(this.#o?i=(0,x.sortNodes)(t):i=[...t])}catch(t){this._onError(t)}return i??[]}}0&&(module.exports={Finder});
|
|
2
2
|
//# sourceMappingURL=finder.js.map
|