@asamuzakjp/dom-selector
Advanced tools
Comparing version 5.3.1 to 5.3.2
@@ -1,2 +0,2 @@ | ||
var R=Object.defineProperty;var B=Object.getOwnPropertyDescriptor;var j=Object.getOwnPropertyNames;var V=Object.prototype.hasOwnProperty;var Y=(S,r)=>{for(var e in r)R(S,e,{get:r[e],enumerable:!0})},q=(S,r,e,f)=>{if(r&&typeof r=="object"||typeof r=="function")for(let n of j(r))!V.call(S,n)&&n!==e&&R(S,n,{get:()=>r[n],enumerable:!(f=B(r,n))||f.enumerable});return S};var K=S=>q(R({},"__esModule",{value:!0}),S);var Z={};Y(Z,{Finder:()=>J});module.exports=K(Z);var F=require("./matcher.js"),g=require("./parser.js"),k=require("./utility.js"),c=require("./constant.js");const v="next",x="prev",X="Tab",O=/^a(?:rea)?$/,M=/^(?:button|fieldset|input|optgroup|option|select|textarea)$/,T=/^(?:button|form|input|select|textarea)$/,I=/^(?:details|dialog)$/,$=/^host(?:-context)?$/,C=/^(?:checkbox|radio)$/,P=/^(?:date(?:time-local)?|month|number|range|time|week)$/,D=/^(?:button|reset)$/,U=/^(?:image|submit)$/,Q=/^(?:email|number|password|search|tel|text|url)$/;class J{#l;#c;#w;#t;#o;#n;#k;#f;#d;#h;#e;#u;#_;#m;#N;#s;#b;#r;#p;#i;#a;constructor(r){this.#a=r,this.#h=new F.Matcher,this.#c=new WeakMap,this.#o=new WeakMap,this.#d=new WeakMap,this.#N=new WeakMap,this.#n=null,this.#k=null,this._registerEventListeners()}onError(r,e={}){if(!(e.noexcept??this.#_))if(r instanceof DOMException||r instanceof this.#a.DOMException)if(r.name===c.NOT_SUPPORTED_ERR)this.#i&&console.warn(r.message);else throw new this.#a.DOMException(r.message,r.name);else throw r.name in this.#a?new this.#a[r.name](r.message):r}setup(r,e,f={}){const{event:n,noexcept:a,warn:l}=f;return this.#_=!!a,this.#i=!!l,this.#e=e,[this.#t,this.#s,this.#r]=(0,k.resolveContent)(e),this.#b=(0,k.isInShadowTree)(e),[this.#l,this.#u]=this._correspond(r),this.#d=new WeakMap,this.#p=new WeakMap,this._setEvent(n),e}_registerEventListeners(){const r={capture:!0,passive:!0},e=[],f=["mouseover","mousedown","mouseup","mouseout"];for(const a of f)e.push(this.#a.addEventListener(a,l=>{this.#n=l},r));const n=["keydown","keyup"];for(const a of n)e.push(this.#a.addEventListener(a,l=>{l.key===X&&(this.#n=l)},r));return e.push(this.#a.addEventListener("focusin",a=>{this.#k=a},r)),e}_setEvent(r){return(r instanceof this.#a.KeyboardEvent||r instanceof this.#a.MouseEvent)&&(this.#n=r),this.#n}_correspond(r){const e=[];this.#w=!1,this.#f=!1;let f;if(this.#o.has(this.#t)){const n=this.#o.get(this.#t);if(n&&n.has(`${r}`)){const a=n.get(`${r}`);f=a.ast,this.#w=a.descendant,this.#f=a.invalidate}}if(f){const n=f.length;for(let a=0;a<n;a++)f[a].collected=!1,f[a].dir=null,f[a].filtered=!1,f[a].find=!1,e[a]=[]}else{let n;try{n=(0,g.parseSelector)(r)}catch(t){this.onError(t)}const{branches:a,info:{hasHasPseudoFunc:l,hasNthChildOfSelector:o}}=(0,g.walkAST)(n);let u=!!(l||o),d=!1,i=0;f=[];for(const[...t]of a){const h=[];let b=t.shift();if(b&&b.type!==c.COMBINATOR){const m=new Set;for(;b;){if(b.type===c.COMBINATOR){const[p]=t;if(p.type===c.COMBINATOR)throw new DOMException(`Invalid selector ${r}`,c.SYNTAX_ERR);const w=b.name;/^[\s>]$/.test(w)?d=!0:u=!0,h.push({combo:b,leaves:(0,g.sortAST)(m)}),m.clear()}else if(b){let{name:p}=b;p&&typeof p=="string"&&(p=(0,g.unescapeSelector)(p),typeof p=="string"&&p!==b.name&&(b.name=p),/[|:]/.test(p)&&(b.namespace=!0)),m.add(b)}if(t.length)b=t.shift();else{h.push({combo:null,leaves:(0,g.sortAST)(m)}),m.clear();break}}}f.push({branch:h,collected:!1,dir:null,filtered:!1,find:!1}),e[i]=[],i++}let s;this.#o.has(this.#t)?s=this.#o.get(this.#t):s=new Map,s.set(`${r}`,{ast:f,descendant:d,invalidate:u}),this.#o.set(this.#t,s),this.#w=d,this.#f=u}return[f,e]}_createTreeWalker(r){let e;return this.#p.has(r)?e=this.#p.get(r):(e=this.#t.createTreeWalker(r,c.WALKER_FILTER),this.#p.set(r,e)),e}_prepareQuerySelectorWalker(){return this.#m=this._createTreeWalker(this.#e),this.#m}_collectNthChild(r,e,f){const{a:n,b:a,reverse:l,selector:o}=r,{parentNode:u}=e,d=new Set;let i;if(o)if(this.#c.has(o))i=this.#c.get(o);else{const{branches:s}=(0,g.walkAST)(o);i=s,this.#c.set(o,i)}if(u){const s=this.#r;let t=(0,k.traverseNode)(u,s);t=s.firstChild();let h=0;for(;t;)h++,t=s.nextSibling();const b=new Set;if(i)for(t=(0,k.traverseNode)(u,s),t=s.firstChild();t;){if((0,k.isVisible)(t)){let m;for(const p of i)if(m=this._matchLeaves(p,t,f),!m)break;m&&b.add(t)}t=s.nextSibling()}if(n===0){if(a>0&&a<=h){if(b.size){t=(0,k.traverseNode)(u,s),l?t=s.lastChild():t=s.firstChild();let m=0;for(;t;){if(b.has(t)){if(m===a-1){d.add(t);break}m++}l?t=s.previousSibling():t=s.nextSibling()}}else if(!o){t=(0,k.traverseNode)(u,s),l?t=s.lastChild():t=s.firstChild();let m=0;for(;t;){if(m===a-1){d.add(t);break}l?t=s.previousSibling():t=s.nextSibling(),m++}}}}else{let m=a-1;if(n>0)for(;m<0;)m+=n;if(m>=0&&m<h){t=(0,k.traverseNode)(u,s),l?t=s.lastChild():t=s.firstChild();let p=0,w=n>0?0:a-1;for(;t&&(t&&m>=0&&m<h);)b.size?b.has(t)&&(w===m&&(d.add(t),m+=n),n>0?w++:w--):p===m&&(o||d.add(t),m+=n),l?t=s.previousSibling():t=s.nextSibling(),p++}}if(l&&d.size>1){const m=[...d];return new Set(m.reverse())}}else if(e===this.#s&&n+a===1)if(i){let s;for(const t of i)if(s=this._matchLeaves(t,e,f),s)break;s&&d.add(e)}else d.add(e);return d}_collectNthOfType(r,e){const{a:f,b:n,reverse:a}=r,{localName:l,namespaceURI:o,parentNode:u,prefix:d}=e,i=new Set;if(u){const s=this.#r;let t=(0,k.traverseNode)(u,s);t=s.firstChild();let h=0;for(;t;)h++,t=s.nextSibling();if(f===0){if(n>0&&n<=h){t=(0,k.traverseNode)(u,s),a?t=s.lastChild():t=s.firstChild();let b=0;for(;t;){const{localName:m,namespaceURI:p,prefix:w}=t;if(m===l&&w===d&&p===o){if(b===n-1){i.add(t);break}b++}a?t=s.previousSibling():t=s.nextSibling()}}}else{let b=n-1;if(f>0)for(;b<0;)b+=f;if(b>=0&&b<h){t=(0,k.traverseNode)(u,s),a?t=s.lastChild():t=s.firstChild();let m=f>0?0:n-1;for(;t;){const{localName:p,namespaceURI:w,prefix:N}=t;if(p===l&&N===d&&w===o){if(m===b&&(i.add(t),b+=f),b<0||b>=h)break;f>0?m++:m--}a?t=s.previousSibling():t=s.nextSibling()}}}if(a&&i.size>1){const b=[...i];return new Set(b.reverse())}}else e===this.#s&&f+n===1&&i.add(e);return i}_matchAnPlusB(r,e,f,n){const{nth:{a,b:l,name:o},selector:u}=r,d=new Map;if(o?(o==="even"?(d.set("a",2),d.set("b",0)):o==="odd"&&(d.set("a",2),d.set("b",1)),f.indexOf("last")>-1&&d.set("reverse",!0)):(typeof a=="string"&&/-?\d+/.test(a)?d.set("a",a*1):d.set("a",0),typeof l=="string"&&/-?\d+/.test(l)?d.set("b",l*1):d.set("b",0),f.indexOf("last")>-1&&d.set("reverse",!0)),/^nth-(?:last-)?child$/.test(f)){u&&d.set("selector",u);const i=Object.fromEntries(d);return this._collectNthChild(i,e,n)}else if(/^nth-(?:last-)?of-type$/.test(f)){const i=Object.fromEntries(d);return this._collectNthOfType(i,e)}return new Set}_matchHasPseudoFunc(r,e,f={}){let n;if(Array.isArray(r)&&r.length){const a=r.map(t=>t),[l]=a,{type:o}=l;let u;o===c.COMBINATOR?u=a.shift():u={name:" ",type:c.COMBINATOR};const d=[];for(;a.length;){const[t]=a,{type:h}=t;if(h===c.COMBINATOR)break;d.push(a.shift())}const i={combo:u,leaves:d};f.dir=v;const s=this._matchCombinator(i,e,f);if(s.size)if(a.length){for(const t of s)if(n=this._matchHasPseudoFunc(a,t,f),n)break}else n=!0}return!!n}_matchLogicalPseudoFunc(r,e,f={}){const{astName:n="",branches:a=[],selector:l="",twigBranches:o=[]}=r;let u;if(n==="has")if(l.includes(":has("))u=null;else{let d;for(const i of a)if(d=this._matchHasPseudoFunc(i,e,f),d)break;d&&(u=e)}else{const d=/^(?:is|where)$/.test(n);f.forgive=d;const i=o.length;let s;for(let t=0;t<i;t++){const h=o[t],b=h.length-1,{leaves:m}=h[b];if(s=this._matchLeaves(m,e,f),s&&b>0){let p=new Set([e]);for(let w=b-1;w>=0;w--){const N=h[w],_=[];f.dir=x;for(const y of p){const E=this._matchCombinator(N,y,f);E.size&&_.push(...E)}if(_.length)w===0?s=!0:p=new Set(_);else{s=!1;break}}}if(s)break}n==="not"?s||(u=e):s&&(u=e)}return u??null}_matchPseudoClassSelector(r,e,f={}){const{children:n,name:a}=r,{localName:l,parentNode:o}=e,{forgive:u,warn:d=this.#i}=f,i=new Set;if(c.REG_LOGICAL.test(a)){let s;if(this.#c.has(r))s=this.#c.get(r);else{const{branches:h}=(0,g.walkAST)(r),b=[],m=[];for(const[...p]of h){for(const y of p){const E=(0,g.generateCSS)(y);b.push(E)}const w=[],N=new Set;let _=p.shift();for(;_;)if(_.type===c.COMBINATOR?(w.push({combo:_,leaves:[...N]}),N.clear()):_&&N.add(_),p.length)_=p.shift();else{w.push({combo:null,leaves:[...N]}),N.clear();break}m.push(w)}s={astName:a,branches:h,twigBranches:m,selector:b.join(",")},this.#c.set(r,s)}const t=this._matchLogicalPseudoFunc(s,e,f);t&&i.add(t)}else if(Array.isArray(n))if(/^nth-(?:last-)?(?:child|of-type)$/.test(a)){const[s]=n;return this._matchAnPlusB(s,e,a,f)}else switch(a){case"dir":case"lang":{const s=this.#h.matchSelector(r,e,f,!0);s&&i.add(s);break}case"state":{if((0,k.isCustomElement)(e)){const[{value:s}]=n;if(s)if(e[s])i.add(e);else for(const t in e){const h=e[t];if(h instanceof this.#a.ElementInternals){h?.states?.has(s)&&i.add(e);break}}}break}case"current":case"nth-col":case"nth-last-col":{if(d)throw new DOMException(`Unsupported pseudo-class :${a}()`,c.NOT_SUPPORTED_ERR);break}case"host":case"host-context":break;case"contains":{if(d)throw new DOMException(`Unknown pseudo-class :${a}()`,c.NOT_SUPPORTED_ERR);break}default:if(!u)throw new DOMException(`Unknown pseudo-class :${a}()`,c.SYNTAX_ERR)}else switch(a){case"any-link":case"link":{O.test(l)&&e.hasAttribute("href")&&i.add(e);break}case"local-link":{if(O.test(l)&&e.hasAttribute("href")){const{href:s,origin:t,pathname:h}=new URL(this.#t.URL),b=new URL(e.getAttribute("href"),s);b.origin===t&&b.pathname===h&&i.add(e)}break}case"visited":break;case"hover":{const{target:s,type:t}=this.#n??{};/^(?:mouse|pointer)(?:down|over|up)$/.test(t)&&e.contains(s)&&i.add(e);break}case"active":{const{buttons:s,target:t,type:h}=this.#n??{};/(?:mouse|pointer)down/.test(h)&&s&c.BIT_01&&e.contains(t)&&i.add(e);break}case"target":{const{hash:s}=new URL(this.#t.URL);e.id&&s===`#${e.id}`&&this.#t.contains(e)&&i.add(e);break}case"target-within":{const{hash:s}=new URL(this.#t.URL);if(s){const t=s.replace(/^#/,"");let h=this.#t.getElementById(t);for(;h;){if(h===e){i.add(e);break}h=h.parentNode}}break}case"scope":{this.#e.nodeType===c.ELEMENT_NODE?!this.#b&&e===this.#e&&i.add(e):e===this.#t.documentElement&&i.add(e);break}case"focus":{e===this.#t.activeElement&&e.tabIndex>=0&&(0,k.isFocusable)(e)&&i.add(e);break}case"focus-visible":{if(e===this.#t.activeElement&&e.tabIndex>=0){let s;if((0,k.isFocusVisible)(e))s=!0;else{const{target:t,type:h}=this.#n??{},{target:b,relatedTarget:m}=this.#k??{};(/^key(?:down|up)$/.test(h)&&e.contains(t)||m&&(0,k.isFocusVisible)(m)&&e.contains(b))&&(s=!0)}s&&(0,k.isFocusable)(e)&&i.add(e)}break}case"focus-within":{let s,t=this.#t.activeElement;if(t.tabIndex>=0)for(;t;){if(t===e){s=!0;break}t=t.parentNode}s&&(0,k.isFocusable)(e)&&i.add(e);break}case"open":{I.test(l)&&e.hasAttribute("open")&&i.add(e);break}case"closed":{I.test(l)&&!e.hasAttribute("open")&&i.add(e);break}case"disabled":{if(M.test(l)||(0,k.isCustomElement)(e,{formAssociated:!0})){if(e.disabled||e.hasAttribute("disabled"))i.add(e);else if(e.localName==="option")o.localName==="optgroup"&&(o.disabled||o.hasAttribute("disabled"))&&i.add(e);else if(e.localName!=="optgroup"){let s,t=o;for(;t;)if(t.localName==="fieldset"&&(t.disabled||t.hasAttribute("disabled"))){const h=this.#r;let b=(0,k.traverseNode)(t,h);for(b=h.firstChild();b&&b.localName!=="legend";)b=h.nextSibling();b&&b.contains(e)||(s=!0);break}else{if(t.localName==="form")break;if(t.parentNode?.nodeType===c.ELEMENT_NODE){if(t.parentNode.localName==="form")break;t=t.parentNode}else break}s&&i.add(e)}}break}case"enabled":{if((M.test(l)||(0,k.isCustomElement)(e,{formAssociated:!0}))&&!(e.disabled&&e.hasAttribute("disabled")))if(e.localName==="optgroup")i.add(e);else if(e.localName==="option")(o.localName!=="optgroup"||!(o.disabled||o.hasAttribute("disabled")))&&i.add(e);else{let s,t=o;for(;t;)if(t.localName==="fieldset"&&(t.disabled||t.hasAttribute("disabled"))){const h=this.#r;let b=(0,k.traverseNode)(t,h);for(b=h.firstChild();b&&b.localName!=="legend";)b=h.nextSibling();b&&b.contains(e)||(s=!0);break}else{if(t.localName==="form")break;if(t.parentNode?.nodeType===c.ELEMENT_NODE){if(t.parentNode.localName==="form")break;t=t.parentNode}else break}s||i.add(e)}break}case"read-only":{switch(l){case"textarea":{(e.readonly||e.hasAttribute("readonly")||e.disabled||e.hasAttribute("disabled"))&&i.add(e);break}case"input":{(!e.type||c.REG_INPUT_TYPE.test(e.type))&&(e.readonly||e.hasAttribute("readonly")||e.disabled||e.hasAttribute("disabled"))&&i.add(e);break}default:(0,k.isContentEditable)(e)||i.add(e)}break}case"read-write":{switch(l){case"textarea":{e.readonly||e.hasAttribute("readonly")||e.disabled||e.hasAttribute("disabled")||i.add(e);break}case"input":{(!e.type||c.REG_INPUT_TYPE.test(e.type))&&!(e.readonly||e.hasAttribute("readonly")||e.disabled||e.hasAttribute("disabled"))&&i.add(e);break}default:(0,k.isContentEditable)(e)&&i.add(e)}break}case"placeholder-shown":{let s;if(e.placeholder?s=e.placeholder:e.hasAttribute("placeholder")&&(s=e.getAttribute("placeholder")),typeof s=="string"&&!/[\r\n]/.test(s)){let t;l==="textarea"?t=e:l==="input"&&(e.hasAttribute("type")?Q.test(e.getAttribute("type"))&&(t=e):t=e),t&&e.value===""&&i.add(e)}break}case"checked":{(e.checked&&l==="input"&&e.hasAttribute("type")&&C.test(e.getAttribute("type"))||e.selected&&l==="option")&&i.add(e);break}case"indeterminate":{if(e.indeterminate&&l==="input"&&e.type==="checkbox"||l==="progress"&&!e.hasAttribute("value"))i.add(e);else if(l==="input"&&e.type==="radio"&&!e.hasAttribute("checked")){const s=e.name;let t=e.parentNode;for(;t&&t.localName!=="form";)t=t.parentNode;t||(t=this.#t.documentElement);const h=t.getElementsByTagName("input"),b=h.length;let m;for(let p=0;p<b;p++){const w=h[p];if(w.getAttribute("type")==="radio"&&(s?w.getAttribute("name")===s&&(m=!!w.checked):w.hasAttribute("name")||(m=!!w.checked),m))break}m||i.add(e)}break}case"default":{if(l==="button"&&!(e.hasAttribute("type")&&D.test(e.getAttribute("type")))||l==="input"&&e.hasAttribute("type")&&U.test(e.getAttribute("type"))){let s=e.parentNode;for(;s&&s.localName!=="form";)s=s.parentNode;if(s){const t=this.#r;let h=(0,k.traverseNode)(s,t);for(h=t.firstChild();h&&s.contains(h);){const b=h.localName;let m;if(b==="button"?m=!(h.hasAttribute("type")&&D.test(h.getAttribute("type"))):b==="input"&&(m=h.hasAttribute("type")&&U.test(h.getAttribute("type"))),m){h===e&&i.add(e);break}h=t.nextNode()}}}else(l==="input"&&e.hasAttribute("type")&&C.test(e.getAttribute("type"))&&e.hasAttribute("checked")||l==="option"&&e.hasAttribute("selected"))&&i.add(e);break}case"valid":{if(T.test(l))e.checkValidity()&&(e.maxLength>=0?e.maxLength>=e.value.length&&i.add(e):i.add(e));else if(l==="fieldset"){const s=this.#r;let t=(0,k.traverseNode)(e,s);t=s.firstChild();let h;if(!t)h=!0;else for(;t&&e.contains(t)&&!(T.test(t.localName)&&(t.checkValidity()?t.maxLength>=0?h=t.maxLength>=t.value.length:h=!0:h=!1,!h));)t=s.nextNode();h&&i.add(e)}break}case"invalid":{if(T.test(l))e.checkValidity()?e.maxLength>=0&&e.maxLength<e.value.length&&i.add(e):i.add(e);else if(l==="fieldset"){const s=this.#r;let t=(0,k.traverseNode)(e,s);t=s.firstChild();let h;if(!t)h=!0;else for(;t&&e.contains(t)&&!(T.test(t.localName)&&(t.checkValidity()?t.maxLength>=0?h=t.maxLength>=t.value.length:h=!0:h=!1,!h));)t=s.nextNode();h||i.add(e)}break}case"in-range":{l==="input"&&!(e.readonly||e.hasAttribute("readonly"))&&!(e.disabled||e.hasAttribute("disabled"))&&e.hasAttribute("type")&&P.test(e.getAttribute("type"))&&!(e.validity.rangeUnderflow||e.validity.rangeOverflow)&&(e.hasAttribute("min")||e.hasAttribute("max")||e.getAttribute("type")==="range")&&i.add(e);break}case"out-of-range":{l==="input"&&!(e.readonly||e.hasAttribute("readonly"))&&!(e.disabled||e.hasAttribute("disabled"))&&e.hasAttribute("type")&&P.test(e.getAttribute("type"))&&(e.validity.rangeUnderflow||e.validity.rangeOverflow)&&i.add(e);break}case"required":{let s;if(/^(?:select|textarea)$/.test(l))s=e;else if(l==="input")if(e.hasAttribute("type")){const t=e.getAttribute("type");(t==="file"||C.test(t)||c.REG_INPUT_TYPE.test(t))&&(s=e)}else s=e;s&&(e.required||e.hasAttribute("required"))&&i.add(e);break}case"optional":{let s;if(/^(?:select|textarea)$/.test(l))s=e;else if(l==="input")if(e.hasAttribute("type")){const t=e.getAttribute("type");(t==="file"||C.test(t)||c.REG_INPUT_TYPE.test(t))&&(s=e)}else s=e;s&&!(e.required||e.hasAttribute("required"))&&i.add(e);break}case"root":{e===this.#t.documentElement&&i.add(e);break}case"empty":{if(e.hasChildNodes()){const s=this.#t.createTreeWalker(e,c.SHOW_ALL);let t=s.firstChild(),h;for(;t&&(h=t.nodeType!==c.ELEMENT_NODE&&t.nodeType!==c.TEXT_NODE,!!h);)t=s.nextSibling();h&&i.add(e)}else i.add(e);break}case"first-child":{(o&&e===o.firstElementChild||e===this.#s)&&i.add(e);break}case"last-child":{(o&&e===o.lastElementChild||e===this.#s)&&i.add(e);break}case"only-child":{(o&&e===o.firstElementChild&&e===o.lastElementChild||e===this.#s)&&i.add(e);break}case"first-of-type":{if(o){const[s]=this._collectNthOfType({a:0,b:1},e);s&&i.add(s)}else e===this.#s&&i.add(e);break}case"last-of-type":{if(o){const[s]=this._collectNthOfType({a:0,b:1,reverse:!0},e);s&&i.add(s)}else e===this.#s&&i.add(e);break}case"only-of-type":{if(o){const[s]=this._collectNthOfType({a:0,b:1},e);if(s===e){const[t]=this._collectNthOfType({a:0,b:1,reverse:!0},e);t===e&&i.add(e)}}else e===this.#s&&i.add(e);break}case"defined":{e.hasAttribute("is")||l.includes("-")?(0,k.isCustomElement)(e)&&i.add(e):(e instanceof this.#a.HTMLElement||e instanceof this.#a.SVGElement)&&i.add(e);break}case"popover-open":{e.popover&&(0,k.isVisible)(e)&&i.add(e);break}case"host":case"host-context":break;case"after":case"before":case"first-letter":case"first-line":{if(d)throw new DOMException(`Unsupported pseudo-element ::${a}`,c.NOT_SUPPORTED_ERR);break}case"autofill":case"blank":case"buffering":case"current":case"fullscreen":case"future":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(d)throw new DOMException(`Unsupported pseudo-class :${a}`,c.NOT_SUPPORTED_ERR);break}default:if(a.startsWith("-webkit-")){if(d)throw new DOMException(`Unsupported pseudo-class :${a}`,c.NOT_SUPPORTED_ERR)}else if(!u)throw new DOMException(`Unknown pseudo-class :${a}`,c.SYNTAX_ERR)}return i}_matchShadowHostPseudoClass(r,e){const{children:f,name:n}=r;let a;if(Array.isArray(f)){const{branches:l}=(0,g.walkAST)(f[0]),[o]=l,[...u]=o,{host:d}=e;if(n==="host"){let i;for(const s of u){const{type:t}=s;if(t===c.COMBINATOR){const h=(0,g.generateCSS)(r);throw new DOMException(`Invalid selector ${h}`,c.SYNTAX_ERR)}if(i=this._matchSelector(s,d).has(d),!i)break}i&&(a=e)}else if(n==="host-context"){let i=d,s;for(;i;){for(const t of u){const{type:h}=t;if(h===c.COMBINATOR){const b=(0,g.generateCSS)(r);throw new DOMException(`Invalid selector ${b}`,c.SYNTAX_ERR)}if(s=this._matchSelector(t,i).has(i),!s)break}if(s)break;i=i.parentNode}s&&(a=e)}}else if(n==="host")a=e;else throw new DOMException(`Invalid selector :${n}`,c.SYNTAX_ERR);return a??null}_matchSelector(r,e,f){const{type:n}=r,a=new Set;if(r.name===c.EMPTY)return a;const l=(0,g.unescapeSelector)(r.name);if(typeof l=="string"&&l!==r.name&&(r.name=l),e.nodeType===c.ELEMENT_NODE)switch(n){case c.PS_ELEMENT_SELECTOR:{this.#h.matchPseudoElementSelector(l,f);break}case c.ID_SELECTOR:{e.id===l&&a.add(e);break}case c.CLASS_SELECTOR:{e.classList.contains(l)&&a.add(e);break}case c.PS_CLASS_SELECTOR:return this._matchPseudoClassSelector(r,e,f);default:{const o=this.#h.matchSelector(r,e,f,!0);o&&a.add(o)}}else if(this.#b&&n===c.PS_CLASS_SELECTOR&&e.nodeType===c.DOCUMENT_FRAGMENT_NODE){if(l!=="has"&&c.REG_LOGICAL.test(l))return this._matchPseudoClassSelector(r,e,f);if($.test(l)){const o=this._matchShadowHostPseudoClass(r,e,f);o&&a.add(o)}}return a}_matchLeaves(r,e,f){let n,a;if(this.#f?a=this.#d.get(r):a=this.#N.get(r),a&&a.has(e)){const{matched:l}=a.get(e);n=l}if(typeof n!="boolean"){let l=!0;e.nodeType===c.ELEMENT_NODE&&/^(?:button|fieldset|form|input|select|textarea)$/.test(e.localName)&&(l=!1);for(const o of r){switch(o.type){case c.ATTR_SELECTOR:case c.ID_SELECTOR:{l=!1;break}case c.PS_CLASS_SELECTOR:{/^(?:(?:any-)?link|defined|dir)$/.test(o.name)&&(l=!1);break}default:}if(n=this._matchSelector(o,e,f).has(e),!n)break}l&&(a||(a=new WeakMap),a.set(e,{matched:n}),this.#f?this.#d.set(r,a):this.#N.set(r,a))}return!!n}_matchHTMLCollection(r,e={}){const{compound:f,filterLeaves:n}=e,a=new Set,l=r.length;if(l)if(f)for(let o=0;o<l;o++){const u=r[o];this._matchLeaves(n,u,e)&&a.add(u)}else{const o=[].slice.call(r);return new Set(o)}return a}_findDescendantNodes(r,e,f){const[n,...a]=r,l=a.length>0,{type:o}=n,u=(0,g.unescapeSelector)(n.name);typeof u=="string"&&u!==n.name&&(n.name=u);let d=new Set,i=!1;if(this.#b)i=!0;else switch(o){case c.PS_ELEMENT_SELECTOR:{this.#h.matchPseudoElementSelector(u,f);break}case c.ID_SELECTOR:{if(this.#s.nodeType===c.ELEMENT_NODE)i=!0;else{const s=this.#s.getElementById(u);s&&s!==e&&e.contains(s)&&(l?this._matchLeaves(a,s,f)&&d.add(s):d.add(s))}break}case c.CLASS_SELECTOR:{const s=e.getElementsByClassName(u);d=this._matchHTMLCollection(s,{compound:l,filterLeaves:a});break}case c.TYPE_SELECTOR:{if(this.#t.contentType==="text/html"&&!/[*|]/.test(u)){const s=e.getElementsByTagName(u);d=this._matchHTMLCollection(s,{compound:l,filterLeaves:a})}else i=!0;break}default:i=!0}return{nodes:d,pending:i}}_matchCombinator(r,e,f={}){const{combo:n,leaves:a}=r,{name:l}=n,{parentNode:o}=e,{dir:u}=f,d=new Set;if(u===v)switch(l){case"+":{const i=e.nextElementSibling;i&&this._matchLeaves(a,i,f)&&d.add(i);break}case"~":{if(o){const i=this._createTreeWalker(o);let s=(0,k.traverseNode)(e,i);for(s=i.nextSibling();s;)this._matchLeaves(a,s,f)&&d.add(s),s=i.nextSibling()}break}case">":{const i=this._createTreeWalker(e);let s=(0,k.traverseNode)(e,i);for(s=i.firstChild();s;)this._matchLeaves(a,s,f)&&d.add(s),s=i.nextSibling();break}case" ":default:{const{nodes:i,pending:s}=this._findDescendantNodes(a,e);if(i.size)return i;if(s){const t=this._createTreeWalker(e);let h=(0,k.traverseNode)(e,t);for(h=t.nextNode();h&&e.contains(h);)this._matchLeaves(a,h,f)&&d.add(h),h=t.nextNode()}}}else switch(l){case"+":{const i=e.previousElementSibling;i&&this._matchLeaves(a,i,f)&&d.add(i);break}case"~":{if(o){const i=this._createTreeWalker(o);let s=(0,k.traverseNode)(o,i);for(s=i.firstChild();s&&s!==e;)this._matchLeaves(a,s,f)&&d.add(s),s=i.nextSibling()}break}case">":{o&&this._matchLeaves(a,o,f)&&d.add(o);break}case" ":default:{const i=[];let s=o;for(;s;)this._matchLeaves(a,s,f)&&i.push(s),s=s.parentNode;if(i.length)return new Set(i.reverse())}}return d}_findNode(r,e){const{node:f}=e,n=this.#m;let a=(0,k.traverseNode)(f,n),l;if(a)for((a.nodeType!==c.ELEMENT_NODE||a===f&&a!==this.#s)&&(a=n.nextNode());a;){if(this._matchLeaves(r,a,{warn:this.#i})){l=a;break}a=n.nextNode()}return l??null}_matchSelf(r){const e=[],f=this._matchLeaves(r,this.#e,{warn:this.#i});let n=!1;return f&&(e.push(this.#e),n=!0),[e,n]}_findLineal(r,e={}){const{complex:f}=e,n=[];let a=this._matchLeaves(r,this.#e,{warn:this.#i}),l=!1;if(a&&(n.push(this.#e),l=!0),!a||f){let o=this.#e.parentNode;for(;o&&(a=this._matchLeaves(r,o,{warn:this.#i}),a&&(n.push(o),l=!0),o.parentNode);)o=o.parentNode}return[n,l]}_findFirst(r){const e=[],f=this._findNode(r,{node:this.#e});let n=!1;return f&&(e.push(f),n=!0),[e,n]}_findFromHTMLCollection(r,e={}){const{complex:f,compound:n,filterLeaves:a,targetType:l}=e;let o=[],u=!1,d=!1;const i=r.length;if(i)if(this.#e.nodeType===c.ELEMENT_NODE)for(let s=0;s<i;s++){const t=r[s];if(t!==this.#e&&(this.#e.contains(t)||t.contains(this.#e))){if(n){if(this._matchLeaves(a,t,{warn:this.#i})&&(o.push(t),u=!0,l===c.TARGET_FIRST))break}else if(o.push(t),u=!0,l===c.TARGET_FIRST)break}}else if(f)if(n)for(let s=0;s<i;s++){const t=r[s];if(this._matchLeaves(a,t,{warn:this.#i})&&(o.push(t),u=!0,l===c.TARGET_FIRST))break}else o=[].slice.call(r),u=!0,d=!0;else if(n)for(let s=0;s<i;s++){const t=r[s];if(this._matchLeaves(a,t,{warn:this.#i})&&(o.push(t),u=!0,l===c.TARGET_FIRST))break}else o=[].slice.call(r),u=!0,d=!0;return[o,u,d]}_findEntryNodes(r,e,f){const{leaves:n}=r,[a,...l]=n,o=l.length>0,{name:u,type:d}=a;let i=[],s=!1,t=!1,h=!1;switch(d){case c.PS_ELEMENT_SELECTOR:{this.#h.matchPseudoElementSelector(u,{warn:this.#i});break}case c.ID_SELECTOR:{if(e===c.TARGET_SELF)[i,t]=this._matchSelf(n);else if(e===c.TARGET_LINEAL)[i,t]=this._findLineal(n,{complex:f});else if(e===c.TARGET_FIRST&&this.#s.nodeType!==c.ELEMENT_NODE){const b=this.#s.getElementById(u);b&&(o?this._matchLeaves(l,b,{warn:this.#i})&&(i.push(b),t=!0):(i.push(b),t=!0))}else e===c.TARGET_FIRST?[i,t]=this._findFirst(n):h=!0;break}case c.CLASS_SELECTOR:{if(e===c.TARGET_SELF)[i,t]=this._matchSelf(n);else if(e===c.TARGET_LINEAL)[i,t]=this._findLineal(n,{complex:f});else if(this.#s.nodeType===c.DOCUMENT_NODE){const b=this.#s.getElementsByClassName(u);b.length&&([i,t,s]=this._findFromHTMLCollection(b,{complex:f,compound:o,filterLeaves:l,targetType:e}))}else e===c.TARGET_FIRST?[i,t]=this._findFirst(n):h=!0;break}case c.TYPE_SELECTOR:{if(e===c.TARGET_SELF)[i,t]=this._matchSelf(n);else if(e===c.TARGET_LINEAL)[i,t]=this._findLineal(n,{complex:f});else if(this.#t.contentType==="text/html"&&this.#s.nodeType===c.DOCUMENT_NODE&&!/[*|]/.test(u)){const b=this.#s.getElementsByTagName(u);b.length&&([i,t,s]=this._findFromHTMLCollection(b,{complex:f,compound:o,filterLeaves:l,targetType:e}))}else e===c.TARGET_FIRST?[i,t]=this._findFirst(n):h=!0;break}default:if(e!==c.TARGET_LINEAL&&$.test(u)){if(this.#b&&this.#e.nodeType===c.DOCUMENT_FRAGMENT_NODE){const b=this._matchShadowHostPseudoClass(a,this.#e);b&&(i.push(b),t=!0)}}else e===c.TARGET_SELF?[i,t]=this._matchSelf(n):e===c.TARGET_LINEAL?[i,t]=this._findLineal(n,{complex:f}):e===c.TARGET_FIRST?[i,t]=this._findFirst(n):h=!0}return{collected:s,compound:o,filtered:t,nodes:i,pending:h}}_collectNodes(r){const e=this.#l.values();if(r===c.TARGET_ALL||r===c.TARGET_FIRST){const f=new Set;let n=0;for(const{branch:a}of e){const l=a.length,o=l>1,u=a[0];let d,i;if(o){const{combo:p,leaves:[{name:w,type:N}]}=u,_=a[l-1],{leaves:[{name:y,type:E}]}=_;if(E===c.PS_ELEMENT_SELECTOR||E===c.ID_SELECTOR)d=x,i=_;else if(N===c.PS_ELEMENT_SELECTOR||N===c.ID_SELECTOR)d=v,i=u;else if(r===c.TARGET_ALL)if(w==="*"&&N===c.TYPE_SELECTOR)d=x,i=_;else if(y==="*"&&E===c.TYPE_SELECTOR)d=v,i=u;else if(l===2){const{name:A}=p;/^[+~]$/.test(A)?(d=x,i=_):(d=v,i=u)}else d=v,i=u;else if(y==="*"&&E===c.TYPE_SELECTOR)d=v,i=u;else if(w==="*"&&N===c.TYPE_SELECTOR)d=x,i=_;else{let A;for(const{combo:L,leaves:[W]}of a){const{name:G,type:H}=W;if(H===c.PS_CLASS_SELECTOR&&G==="dir"){A=!1;break}if(!A&&L){const{name:z}=L;/^[+~]$/.test(z)&&(A=!0)}}A?(d=v,i=u):(d=x,i=_)}}else d=x,i=u;const{collected:s,compound:t,filtered:h,nodes:b,pending:m}=this._findEntryNodes(i,r,o);b.length?(this.#l[n].find=!0,this.#u[n]=b):m&&f.add(new Map([["index",n],["twig",i]])),this.#l[n].collected=s,this.#l[n].dir=d,this.#l[n].filtered=h||!t,n++}if(f.size){let a,l;this.#e!==this.#s&&this.#e.nodeType===c.ELEMENT_NODE?(a=this.#e,l=this.#m):(a=this.#s,l=this.#r);let o=(0,k.traverseNode)(a,l);for(;o;){let u=!1;if(this.#e.nodeType===c.ELEMENT_NODE?o===this.#e?u=!0:u=this.#e.contains(o):u=!0,u)for(const d of f){const{leaves:i}=d.get("twig");if(this._matchLeaves(i,o,{warn:this.#i})){const t=d.get("index");this.#l[t].filtered=!0,this.#l[t].find=!0,this.#u[t].push(o)}}o!==l.currentNode&&(o=(0,k.traverseNode)(o,l)),o=l.nextNode()}}}else{let f=0;for(const{branch:n}of e){const a=n[n.length-1],l=n.length>1,{compound:o,filtered:u,nodes:d}=this._findEntryNodes(a,r,l);d.length&&(this.#l[f].find=!0,this.#u[f]=d),this.#l[f].dir=x,this.#l[f].filtered=u||!o,f++}}return[this.#l,this.#u]}_getCombinedNodes(r,e,f){const n=[];for(const a of e){const l=this._matchCombinator(r,a,{dir:f,warn:this.#i});l.size&&n.push(...l)}return n.length?new Set(n):new Set}_matchNodeNext(r,e,f){const{combo:n,index:a}=f,{combo:l,leaves:o}=r[a],u={combo:n,leaves:o},d=this._getCombinedNodes(u,e,v);let i;if(d.size)if(a===r.length-1){const[s]=(0,k.sortNodes)(d);i=s}else i=this._matchNodeNext(r,d,{combo:l,index:a+1});return i??null}_matchNodePrev(r,e,f){const{index:n}=f,a=r[n],l=new Set([e]),o=this._getCombinedNodes(a,l,x);let u;if(o.size){if(n===0)u=e;else for(const d of o)if(this._matchNodePrev(r,d,{index:n-1}))return e}return u??null}find(r){(r===c.TARGET_ALL||r===c.TARGET_FIRST)&&this._prepareQuerySelectorWalker();const[[...e],f]=this._collectNodes(r),n=e.length;let a,l=new Set;for(let o=0;o<n;o++){const{branch:u,collected:d,dir:i,find:s}=e[o],t=u.length;if(t&&s){const h=f[o],b=h.length,m=t-1;if(m===0)if((r===c.TARGET_ALL||r===c.TARGET_FIRST)&&this.#e.nodeType===c.ELEMENT_NODE)for(let p=0;p<b;p++){const w=h[p];if(w!==this.#e&&this.#e.contains(w)&&(l.add(w),r!==c.TARGET_ALL))break}else if(r===c.TARGET_ALL)if(l.size){const p=[...l];l=new Set([...p,...h]),a=!0}else l=new Set(h);else{const[p]=h;l.add(p)}else if(r===c.TARGET_ALL)if(i===v){let{combo:p}=u[0];for(const w of h){let N=new Set([w]);for(let _=1;_<t;_++){const{combo:y,leaves:E}=u[_],A={combo:p,leaves:E};if(N=this._getCombinedNodes(A,N,i),N.size)if(_===m)if(l.size){const L=[...l];l=new Set([...L,...N]),a=!0}else l=N;else p=y;else break}}}else for(const p of h){let w=new Set([p]);for(let N=m-1;N>=0;N--){const _=u[N];if(w=this._getCombinedNodes(_,w,i),w.size)N===0&&(l.add(p),t>1&&l.size>1&&(a=!0));else break}}else if(r===c.TARGET_FIRST&&i===v){const{combo:p}=u[0];let w;for(const N of h)if(w=this._matchNodeNext(u,new Set([N]),{combo:p,index:1}),w){l.add(w);break}if(!w&&!d){const{leaves:N}=u[0],[_]=h;let y=this._findNode(N,{node:_});for(;y;){if(w=this._matchNodeNext(u,new Set([y]),{combo:p,index:1}),w){l.add(w);break}y=this._findNode(N,{node:y})}}}else{let p;for(const w of h)if(p=this._matchNodePrev(u,w,{index:m-1}),p){l.add(w);break}if(!p&&!d&&r===c.TARGET_FIRST){const{leaves:w}=u[m],[N]=h;let _=this._findNode(w,{node:N});for(;_;){if(p=this._matchNodePrev(u,_,{index:m-1}),p){l.add(_);break}_=this._findNode(w,{node:_})}}}}}return r===c.TARGET_FIRST?(l.delete(this.#e),l.size>1&&(l=new Set((0,k.sortNodes)(l)))):r===c.TARGET_ALL&&(l.delete(this.#e),a&&l.size>1&&(l=new Set((0,k.sortNodes)(l)))),this.#f&&(this.#d=new WeakMap),l}}0&&(module.exports={Finder}); | ||
var R=Object.defineProperty;var B=Object.getOwnPropertyDescriptor;var j=Object.getOwnPropertyNames;var V=Object.prototype.hasOwnProperty;var Y=(S,r)=>{for(var e in r)R(S,e,{get:r[e],enumerable:!0})},q=(S,r,e,f)=>{if(r&&typeof r=="object"||typeof r=="function")for(let n of j(r))!V.call(S,n)&&n!==e&&R(S,n,{get:()=>r[n],enumerable:!(f=B(r,n))||f.enumerable});return S};var K=S=>q(R({},"__esModule",{value:!0}),S);var Z={};Y(Z,{Finder:()=>J});module.exports=K(Z);var F=require("./matcher.js"),g=require("./parser.js"),k=require("./utility.js"),c=require("./constant.js");const v="next",x="prev",X="Tab",O=/^a(?:rea)?$/,M=/^(?:button|fieldset|input|optgroup|option|select|textarea)$/,T=/^(?:button|form|input|select|textarea)$/,I=/^(?:details|dialog)$/,$=/^host(?:-context)?$/,C=/^(?:checkbox|radio)$/,P=/^(?:date(?:time-local)?|month|number|range|time|week)$/,D=/^(?:button|reset)$/,U=/^(?:image|submit)$/,Q=/^(?:email|number|password|search|tel|text|url)$/;class J{#l;#b;#w;#t;#c;#n;#k;#o;#h;#f;#e;#d;#_;#m;#N;#s;#u;#r;#p;#i;#a;constructor(r){this.#a=r,this.#f=new F.Matcher,this.#b=new WeakMap,this.#c=new WeakMap,this.#h=new WeakMap,this.#N=new WeakMap,this.#n=null,this.#k=null,this._registerEventListeners()}onError(r,e={}){if(!(e.noexcept??this.#_))if(r instanceof DOMException||r instanceof this.#a.DOMException)if(r.name===c.NOT_SUPPORTED_ERR)this.#i&&console.warn(r.message);else throw new this.#a.DOMException(r.message,r.name);else throw r.name in this.#a?new this.#a[r.name](r.message):r}setup(r,e,f={}){const{event:n,noexcept:a,warn:l}=f;return this.#_=!!a,this.#i=!!l,this.#e=e,[this.#t,this.#s,this.#r]=(0,k.resolveContent)(e),this.#u=(0,k.isInShadowTree)(e),[this.#l,this.#d]=this._correspond(r),this.#h=new WeakMap,this.#p=new WeakMap,this._setEvent(n),e}_registerEventListeners(){const r={capture:!0,passive:!0},e=[],f=["mouseover","mousedown","mouseup","mouseout"];for(const a of f)e.push(this.#a.addEventListener(a,l=>{this.#n=l},r));const n=["keydown","keyup"];for(const a of n)e.push(this.#a.addEventListener(a,l=>{l.key===X&&(this.#n=l)},r));return e.push(this.#a.addEventListener("focusin",a=>{this.#k=a},r)),e}_setEvent(r){return(r instanceof this.#a.KeyboardEvent||r instanceof this.#a.MouseEvent)&&(this.#n=r),this.#n}_correspond(r){const e=[];this.#w=!1,this.#o=!1;let f;if(this.#c.has(this.#t)){const n=this.#c.get(this.#t);if(n&&n.has(`${r}`)){const a=n.get(`${r}`);f=a.ast,this.#w=a.descendant,this.#o=a.invalidate}}if(f){const n=f.length;for(let a=0;a<n;a++)f[a].collected=!1,f[a].dir=null,f[a].filtered=!1,f[a].find=!1,e[a]=[]}else{let n;try{n=(0,g.parseSelector)(r)}catch(t){this.onError(t)}const{branches:a,info:{hasHasPseudoFunc:l,hasNthChildOfSelector:o}}=(0,g.walkAST)(n);let u=!!(l||o),d=!1,i=0;f=[];for(const[...t]of a){const h=[];let b=t.shift();if(b&&b.type!==c.COMBINATOR){const m=new Set;for(;b;){if(b.type===c.COMBINATOR){const[p]=t;if(p.type===c.COMBINATOR)throw new DOMException(`Invalid selector ${r}`,c.SYNTAX_ERR);const w=b.name;/^[\s>]$/.test(w)?d=!0:u=!0,h.push({combo:b,leaves:(0,g.sortAST)(m)}),m.clear()}else if(b){let{name:p}=b;p&&typeof p=="string"&&(p=(0,g.unescapeSelector)(p),typeof p=="string"&&p!==b.name&&(b.name=p),/[|:]/.test(p)&&(b.namespace=!0)),m.add(b)}if(t.length)b=t.shift();else{h.push({combo:null,leaves:(0,g.sortAST)(m)}),m.clear();break}}}f.push({branch:h,collected:!1,dir:null,filtered:!1,find:!1}),e[i]=[],i++}let s;this.#c.has(this.#t)?s=this.#c.get(this.#t):s=new Map,s.set(`${r}`,{ast:f,descendant:d,invalidate:u}),this.#c.set(this.#t,s),this.#w=d,this.#o=u}return[f,e]}_createTreeWalker(r){let e;return this.#p.has(r)?e=this.#p.get(r):(e=this.#t.createTreeWalker(r,c.WALKER_FILTER),this.#p.set(r,e)),e}_prepareQuerySelectorWalker(){return this.#m=this._createTreeWalker(this.#e),this.#m}_collectNthChild(r,e,f){const{a:n,b:a,reverse:l,selector:o}=r,{parentNode:u}=e,d=new Set;let i;if(o){const{branches:s}=(0,g.walkAST)(o);i=s}if(u){const s=this.#r;let t=(0,k.traverseNode)(u,s);t=s.firstChild();let h=0;for(;t;)h++,t=s.nextSibling();const b=new Set;if(i)for(t=(0,k.traverseNode)(u,s),t=s.firstChild();t;){if((0,k.isVisible)(t)){let m;for(const p of i)if(m=this._matchLeaves(p,t,f),!m)break;m&&b.add(t)}t=s.nextSibling()}if(n===0){if(a>0&&a<=h){if(b.size){t=(0,k.traverseNode)(u,s),l?t=s.lastChild():t=s.firstChild();let m=0;for(;t;){if(b.has(t)){if(m===a-1){d.add(t);break}m++}l?t=s.previousSibling():t=s.nextSibling()}}else if(!o){t=(0,k.traverseNode)(u,s),l?t=s.lastChild():t=s.firstChild();let m=0;for(;t;){if(m===a-1){d.add(t);break}l?t=s.previousSibling():t=s.nextSibling(),m++}}}}else{let m=a-1;if(n>0)for(;m<0;)m+=n;if(m>=0&&m<h){t=(0,k.traverseNode)(u,s),l?t=s.lastChild():t=s.firstChild();let p=0,w=n>0?0:a-1;for(;t&&(t&&m>=0&&m<h);)b.size?b.has(t)&&(w===m&&(d.add(t),m+=n),n>0?w++:w--):p===m&&(o||d.add(t),m+=n),l?t=s.previousSibling():t=s.nextSibling(),p++}}if(l&&d.size>1){const m=[...d];return new Set(m.reverse())}}else if(e===this.#s&&n+a===1)if(i){let s;for(const t of i)if(s=this._matchLeaves(t,e,f),s)break;s&&d.add(e)}else d.add(e);return d}_collectNthOfType(r,e){const{a:f,b:n,reverse:a}=r,{localName:l,namespaceURI:o,parentNode:u,prefix:d}=e,i=new Set;if(u){const s=this.#r;let t=(0,k.traverseNode)(u,s);t=s.firstChild();let h=0;for(;t;)h++,t=s.nextSibling();if(f===0){if(n>0&&n<=h){t=(0,k.traverseNode)(u,s),a?t=s.lastChild():t=s.firstChild();let b=0;for(;t;){const{localName:m,namespaceURI:p,prefix:w}=t;if(m===l&&w===d&&p===o){if(b===n-1){i.add(t);break}b++}a?t=s.previousSibling():t=s.nextSibling()}}}else{let b=n-1;if(f>0)for(;b<0;)b+=f;if(b>=0&&b<h){t=(0,k.traverseNode)(u,s),a?t=s.lastChild():t=s.firstChild();let m=f>0?0:n-1;for(;t;){const{localName:p,namespaceURI:w,prefix:N}=t;if(p===l&&N===d&&w===o){if(m===b&&(i.add(t),b+=f),b<0||b>=h)break;f>0?m++:m--}a?t=s.previousSibling():t=s.nextSibling()}}}if(a&&i.size>1){const b=[...i];return new Set(b.reverse())}}else e===this.#s&&f+n===1&&i.add(e);return i}_matchAnPlusB(r,e,f,n){const{nth:{a,b:l,name:o},selector:u}=r,d=new Map;if(o?(o==="even"?(d.set("a",2),d.set("b",0)):o==="odd"&&(d.set("a",2),d.set("b",1)),f.indexOf("last")>-1&&d.set("reverse",!0)):(typeof a=="string"&&/-?\d+/.test(a)?d.set("a",a*1):d.set("a",0),typeof l=="string"&&/-?\d+/.test(l)?d.set("b",l*1):d.set("b",0),f.indexOf("last")>-1&&d.set("reverse",!0)),/^nth-(?:last-)?child$/.test(f)){u&&d.set("selector",u);const i=Object.fromEntries(d);return this._collectNthChild(i,e,n)}else if(/^nth-(?:last-)?of-type$/.test(f)){const i=Object.fromEntries(d);return this._collectNthOfType(i,e)}return new Set}_matchHasPseudoFunc(r,e,f={}){let n;if(Array.isArray(r)&&r.length){const a=r.map(t=>t),[l]=a,{type:o}=l;let u;o===c.COMBINATOR?u=a.shift():u={name:" ",type:c.COMBINATOR};const d=[];for(;a.length;){const[t]=a,{type:h}=t;if(h===c.COMBINATOR)break;d.push(a.shift())}const i={combo:u,leaves:d};f.dir=v;const s=this._matchCombinator(i,e,f);if(s.size)if(a.length){for(const t of s)if(n=this._matchHasPseudoFunc(a,t,f),n)break}else n=!0}return!!n}_matchLogicalPseudoFunc(r,e,f={}){const{astName:n="",branches:a=[],selector:l="",twigBranches:o=[]}=r;let u;if(n==="has")if(l.includes(":has("))u=null;else{let d;for(const i of a)if(d=this._matchHasPseudoFunc(i,e,f),d)break;d&&(u=e)}else{const d=/^(?:is|where)$/.test(n);f.forgive=d;const i=o.length;let s;for(let t=0;t<i;t++){const h=o[t],b=h.length-1,{leaves:m}=h[b];if(s=this._matchLeaves(m,e,f),s&&b>0){let p=new Set([e]);for(let w=b-1;w>=0;w--){const N=h[w],_=[];f.dir=x;for(const y of p){const E=this._matchCombinator(N,y,f);E.size&&_.push(...E)}if(_.length)w===0?s=!0:p=new Set(_);else{s=!1;break}}}if(s)break}n==="not"?s||(u=e):s&&(u=e)}return u??null}_matchPseudoClassSelector(r,e,f={}){const{children:n,name:a}=r,{localName:l,parentNode:o}=e,{forgive:u,warn:d=this.#i}=f,i=new Set;if(c.REG_LOGICAL.test(a)){let s;if(this.#b.has(r))s=this.#b.get(r);else{const{branches:h}=(0,g.walkAST)(r),b=[],m=[];for(const[...p]of h){for(const y of p){const E=(0,g.generateCSS)(y);b.push(E)}const w=[],N=new Set;let _=p.shift();for(;_;)if(_.type===c.COMBINATOR?(w.push({combo:_,leaves:[...N]}),N.clear()):_&&N.add(_),p.length)_=p.shift();else{w.push({combo:null,leaves:[...N]}),N.clear();break}m.push(w)}s={astName:a,branches:h,twigBranches:m,selector:b.join(",")},this.#b.set(r,s)}const t=this._matchLogicalPseudoFunc(s,e,f);t&&i.add(t)}else if(Array.isArray(n))if(/^nth-(?:last-)?(?:child|of-type)$/.test(a)){const[s]=n;return this._matchAnPlusB(s,e,a,f)}else switch(a){case"dir":case"lang":{const s=this.#f.matchSelector(r,e,f,!0);s&&i.add(s);break}case"state":{if((0,k.isCustomElement)(e)){const[{value:s}]=n;if(s)if(e[s])i.add(e);else for(const t in e){const h=e[t];if(h instanceof this.#a.ElementInternals){h?.states?.has(s)&&i.add(e);break}}}break}case"current":case"nth-col":case"nth-last-col":{if(d)throw new DOMException(`Unsupported pseudo-class :${a}()`,c.NOT_SUPPORTED_ERR);break}case"host":case"host-context":break;case"contains":{if(d)throw new DOMException(`Unknown pseudo-class :${a}()`,c.NOT_SUPPORTED_ERR);break}default:if(!u)throw new DOMException(`Unknown pseudo-class :${a}()`,c.SYNTAX_ERR)}else switch(a){case"any-link":case"link":{O.test(l)&&e.hasAttribute("href")&&i.add(e);break}case"local-link":{if(O.test(l)&&e.hasAttribute("href")){const{href:s,origin:t,pathname:h}=new URL(this.#t.URL),b=new URL(e.getAttribute("href"),s);b.origin===t&&b.pathname===h&&i.add(e)}break}case"visited":break;case"hover":{const{target:s,type:t}=this.#n??{};/^(?:mouse|pointer)(?:down|over|up)$/.test(t)&&e.contains(s)&&i.add(e);break}case"active":{const{buttons:s,target:t,type:h}=this.#n??{};/(?:mouse|pointer)down/.test(h)&&s&c.BIT_01&&e.contains(t)&&i.add(e);break}case"target":{const{hash:s}=new URL(this.#t.URL);e.id&&s===`#${e.id}`&&this.#t.contains(e)&&i.add(e);break}case"target-within":{const{hash:s}=new URL(this.#t.URL);if(s){const t=s.replace(/^#/,"");let h=this.#t.getElementById(t);for(;h;){if(h===e){i.add(e);break}h=h.parentNode}}break}case"scope":{this.#e.nodeType===c.ELEMENT_NODE?!this.#u&&e===this.#e&&i.add(e):e===this.#t.documentElement&&i.add(e);break}case"focus":{e===this.#t.activeElement&&e.tabIndex>=0&&(0,k.isFocusable)(e)&&i.add(e);break}case"focus-visible":{if(e===this.#t.activeElement&&e.tabIndex>=0){let s;if((0,k.isFocusVisible)(e))s=!0;else{const{target:t,type:h}=this.#n??{},{target:b,relatedTarget:m}=this.#k??{};(/^key(?:down|up)$/.test(h)&&e.contains(t)||m&&(0,k.isFocusVisible)(m)&&e.contains(b))&&(s=!0)}s&&(0,k.isFocusable)(e)&&i.add(e)}break}case"focus-within":{let s,t=this.#t.activeElement;if(t.tabIndex>=0)for(;t;){if(t===e){s=!0;break}t=t.parentNode}s&&(0,k.isFocusable)(e)&&i.add(e);break}case"open":{I.test(l)&&e.hasAttribute("open")&&i.add(e);break}case"closed":{I.test(l)&&!e.hasAttribute("open")&&i.add(e);break}case"disabled":{if(M.test(l)||(0,k.isCustomElement)(e,{formAssociated:!0})){if(e.disabled||e.hasAttribute("disabled"))i.add(e);else if(e.localName==="option")o.localName==="optgroup"&&(o.disabled||o.hasAttribute("disabled"))&&i.add(e);else if(e.localName!=="optgroup"){let s,t=o;for(;t;)if(t.localName==="fieldset"&&(t.disabled||t.hasAttribute("disabled"))){const h=this.#r;let b=(0,k.traverseNode)(t,h);for(b=h.firstChild();b&&b.localName!=="legend";)b=h.nextSibling();b&&b.contains(e)||(s=!0);break}else{if(t.localName==="form")break;if(t.parentNode?.nodeType===c.ELEMENT_NODE){if(t.parentNode.localName==="form")break;t=t.parentNode}else break}s&&i.add(e)}}break}case"enabled":{if((M.test(l)||(0,k.isCustomElement)(e,{formAssociated:!0}))&&!(e.disabled&&e.hasAttribute("disabled")))if(e.localName==="optgroup")i.add(e);else if(e.localName==="option")(o.localName!=="optgroup"||!(o.disabled||o.hasAttribute("disabled")))&&i.add(e);else{let s,t=o;for(;t;)if(t.localName==="fieldset"&&(t.disabled||t.hasAttribute("disabled"))){const h=this.#r;let b=(0,k.traverseNode)(t,h);for(b=h.firstChild();b&&b.localName!=="legend";)b=h.nextSibling();b&&b.contains(e)||(s=!0);break}else{if(t.localName==="form")break;if(t.parentNode?.nodeType===c.ELEMENT_NODE){if(t.parentNode.localName==="form")break;t=t.parentNode}else break}s||i.add(e)}break}case"read-only":{switch(l){case"textarea":{(e.readonly||e.hasAttribute("readonly")||e.disabled||e.hasAttribute("disabled"))&&i.add(e);break}case"input":{(!e.type||c.REG_INPUT_TYPE.test(e.type))&&(e.readonly||e.hasAttribute("readonly")||e.disabled||e.hasAttribute("disabled"))&&i.add(e);break}default:(0,k.isContentEditable)(e)||i.add(e)}break}case"read-write":{switch(l){case"textarea":{e.readonly||e.hasAttribute("readonly")||e.disabled||e.hasAttribute("disabled")||i.add(e);break}case"input":{(!e.type||c.REG_INPUT_TYPE.test(e.type))&&!(e.readonly||e.hasAttribute("readonly")||e.disabled||e.hasAttribute("disabled"))&&i.add(e);break}default:(0,k.isContentEditable)(e)&&i.add(e)}break}case"placeholder-shown":{let s;if(e.placeholder?s=e.placeholder:e.hasAttribute("placeholder")&&(s=e.getAttribute("placeholder")),typeof s=="string"&&!/[\r\n]/.test(s)){let t;l==="textarea"?t=e:l==="input"&&(e.hasAttribute("type")?Q.test(e.getAttribute("type"))&&(t=e):t=e),t&&e.value===""&&i.add(e)}break}case"checked":{(e.checked&&l==="input"&&e.hasAttribute("type")&&C.test(e.getAttribute("type"))||e.selected&&l==="option")&&i.add(e);break}case"indeterminate":{if(e.indeterminate&&l==="input"&&e.type==="checkbox"||l==="progress"&&!e.hasAttribute("value"))i.add(e);else if(l==="input"&&e.type==="radio"&&!e.hasAttribute("checked")){const s=e.name;let t=e.parentNode;for(;t&&t.localName!=="form";)t=t.parentNode;t||(t=this.#t.documentElement);const h=t.getElementsByTagName("input"),b=h.length;let m;for(let p=0;p<b;p++){const w=h[p];if(w.getAttribute("type")==="radio"&&(s?w.getAttribute("name")===s&&(m=!!w.checked):w.hasAttribute("name")||(m=!!w.checked),m))break}m||i.add(e)}break}case"default":{if(l==="button"&&!(e.hasAttribute("type")&&D.test(e.getAttribute("type")))||l==="input"&&e.hasAttribute("type")&&U.test(e.getAttribute("type"))){let s=e.parentNode;for(;s&&s.localName!=="form";)s=s.parentNode;if(s){const t=this.#r;let h=(0,k.traverseNode)(s,t);for(h=t.firstChild();h&&s.contains(h);){const b=h.localName;let m;if(b==="button"?m=!(h.hasAttribute("type")&&D.test(h.getAttribute("type"))):b==="input"&&(m=h.hasAttribute("type")&&U.test(h.getAttribute("type"))),m){h===e&&i.add(e);break}h=t.nextNode()}}}else(l==="input"&&e.hasAttribute("type")&&C.test(e.getAttribute("type"))&&e.hasAttribute("checked")||l==="option"&&e.hasAttribute("selected"))&&i.add(e);break}case"valid":{if(T.test(l))e.checkValidity()&&(e.maxLength>=0?e.maxLength>=e.value.length&&i.add(e):i.add(e));else if(l==="fieldset"){const s=this.#r;let t=(0,k.traverseNode)(e,s);t=s.firstChild();let h;if(!t)h=!0;else for(;t&&e.contains(t)&&!(T.test(t.localName)&&(t.checkValidity()?t.maxLength>=0?h=t.maxLength>=t.value.length:h=!0:h=!1,!h));)t=s.nextNode();h&&i.add(e)}break}case"invalid":{if(T.test(l))e.checkValidity()?e.maxLength>=0&&e.maxLength<e.value.length&&i.add(e):i.add(e);else if(l==="fieldset"){const s=this.#r;let t=(0,k.traverseNode)(e,s);t=s.firstChild();let h;if(!t)h=!0;else for(;t&&e.contains(t)&&!(T.test(t.localName)&&(t.checkValidity()?t.maxLength>=0?h=t.maxLength>=t.value.length:h=!0:h=!1,!h));)t=s.nextNode();h||i.add(e)}break}case"in-range":{l==="input"&&!(e.readonly||e.hasAttribute("readonly"))&&!(e.disabled||e.hasAttribute("disabled"))&&e.hasAttribute("type")&&P.test(e.getAttribute("type"))&&!(e.validity.rangeUnderflow||e.validity.rangeOverflow)&&(e.hasAttribute("min")||e.hasAttribute("max")||e.getAttribute("type")==="range")&&i.add(e);break}case"out-of-range":{l==="input"&&!(e.readonly||e.hasAttribute("readonly"))&&!(e.disabled||e.hasAttribute("disabled"))&&e.hasAttribute("type")&&P.test(e.getAttribute("type"))&&(e.validity.rangeUnderflow||e.validity.rangeOverflow)&&i.add(e);break}case"required":{let s;if(/^(?:select|textarea)$/.test(l))s=e;else if(l==="input")if(e.hasAttribute("type")){const t=e.getAttribute("type");(t==="file"||C.test(t)||c.REG_INPUT_TYPE.test(t))&&(s=e)}else s=e;s&&(e.required||e.hasAttribute("required"))&&i.add(e);break}case"optional":{let s;if(/^(?:select|textarea)$/.test(l))s=e;else if(l==="input")if(e.hasAttribute("type")){const t=e.getAttribute("type");(t==="file"||C.test(t)||c.REG_INPUT_TYPE.test(t))&&(s=e)}else s=e;s&&!(e.required||e.hasAttribute("required"))&&i.add(e);break}case"root":{e===this.#t.documentElement&&i.add(e);break}case"empty":{if(e.hasChildNodes()){const s=this.#t.createTreeWalker(e,c.SHOW_ALL);let t=s.firstChild(),h;for(;t&&(h=t.nodeType!==c.ELEMENT_NODE&&t.nodeType!==c.TEXT_NODE,!!h);)t=s.nextSibling();h&&i.add(e)}else i.add(e);break}case"first-child":{(o&&e===o.firstElementChild||e===this.#s)&&i.add(e);break}case"last-child":{(o&&e===o.lastElementChild||e===this.#s)&&i.add(e);break}case"only-child":{(o&&e===o.firstElementChild&&e===o.lastElementChild||e===this.#s)&&i.add(e);break}case"first-of-type":{if(o){const[s]=this._collectNthOfType({a:0,b:1},e);s&&i.add(s)}else e===this.#s&&i.add(e);break}case"last-of-type":{if(o){const[s]=this._collectNthOfType({a:0,b:1,reverse:!0},e);s&&i.add(s)}else e===this.#s&&i.add(e);break}case"only-of-type":{if(o){const[s]=this._collectNthOfType({a:0,b:1},e);if(s===e){const[t]=this._collectNthOfType({a:0,b:1,reverse:!0},e);t===e&&i.add(e)}}else e===this.#s&&i.add(e);break}case"defined":{e.hasAttribute("is")||l.includes("-")?(0,k.isCustomElement)(e)&&i.add(e):(e instanceof this.#a.HTMLElement||e instanceof this.#a.SVGElement)&&i.add(e);break}case"popover-open":{e.popover&&(0,k.isVisible)(e)&&i.add(e);break}case"host":case"host-context":break;case"after":case"before":case"first-letter":case"first-line":{if(d)throw new DOMException(`Unsupported pseudo-element ::${a}`,c.NOT_SUPPORTED_ERR);break}case"autofill":case"blank":case"buffering":case"current":case"fullscreen":case"future":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(d)throw new DOMException(`Unsupported pseudo-class :${a}`,c.NOT_SUPPORTED_ERR);break}default:if(a.startsWith("-webkit-")){if(d)throw new DOMException(`Unsupported pseudo-class :${a}`,c.NOT_SUPPORTED_ERR)}else if(!u)throw new DOMException(`Unknown pseudo-class :${a}`,c.SYNTAX_ERR)}return i}_matchShadowHostPseudoClass(r,e){const{children:f,name:n}=r;let a;if(Array.isArray(f)){const{branches:l}=(0,g.walkAST)(f[0]),[o]=l,[...u]=o,{host:d}=e;if(n==="host"){let i;for(const s of u){const{type:t}=s;if(t===c.COMBINATOR){const h=(0,g.generateCSS)(r);throw new DOMException(`Invalid selector ${h}`,c.SYNTAX_ERR)}if(i=this._matchSelector(s,d).has(d),!i)break}i&&(a=e)}else if(n==="host-context"){let i=d,s;for(;i;){for(const t of u){const{type:h}=t;if(h===c.COMBINATOR){const b=(0,g.generateCSS)(r);throw new DOMException(`Invalid selector ${b}`,c.SYNTAX_ERR)}if(s=this._matchSelector(t,i).has(i),!s)break}if(s)break;i=i.parentNode}s&&(a=e)}}else if(n==="host")a=e;else throw new DOMException(`Invalid selector :${n}`,c.SYNTAX_ERR);return a??null}_matchSelector(r,e,f){const{type:n}=r,a=new Set;if(r.name===c.EMPTY)return a;const l=(0,g.unescapeSelector)(r.name);if(typeof l=="string"&&l!==r.name&&(r.name=l),e.nodeType===c.ELEMENT_NODE)switch(n){case c.PS_ELEMENT_SELECTOR:{this.#f.matchPseudoElementSelector(l,f);break}case c.ID_SELECTOR:{e.id===l&&a.add(e);break}case c.CLASS_SELECTOR:{e.classList.contains(l)&&a.add(e);break}case c.PS_CLASS_SELECTOR:return this._matchPseudoClassSelector(r,e,f);default:{const o=this.#f.matchSelector(r,e,f,!0);o&&a.add(o)}}else if(this.#u&&n===c.PS_CLASS_SELECTOR&&e.nodeType===c.DOCUMENT_FRAGMENT_NODE){if(l!=="has"&&c.REG_LOGICAL.test(l))return this._matchPseudoClassSelector(r,e,f);if($.test(l)){const o=this._matchShadowHostPseudoClass(r,e,f);o&&a.add(o)}}return a}_matchLeaves(r,e,f){let n,a;if(this.#o?a=this.#h.get(r):a=this.#N.get(r),a&&a.has(e)){const{matched:l}=a.get(e);n=l}if(typeof n!="boolean"){let l=!0;e.nodeType===c.ELEMENT_NODE&&/^(?:button|fieldset|form|input|select|textarea)$/.test(e.localName)&&(l=!1);for(const o of r){switch(o.type){case c.ATTR_SELECTOR:case c.ID_SELECTOR:{l=!1;break}case c.PS_CLASS_SELECTOR:{/^(?:(?:any-)?link|defined|dir)$/.test(o.name)&&(l=!1);break}default:}if(n=this._matchSelector(o,e,f).has(e),!n)break}l&&(a||(a=new WeakMap),a.set(e,{matched:n}),this.#o?this.#h.set(r,a):this.#N.set(r,a))}return!!n}_matchHTMLCollection(r,e={}){const{compound:f,filterLeaves:n}=e,a=new Set,l=r.length;if(l)if(f)for(let o=0;o<l;o++){const u=r[o];this._matchLeaves(n,u,e)&&a.add(u)}else{const o=[].slice.call(r);return new Set(o)}return a}_findDescendantNodes(r,e,f){const[n,...a]=r,l=a.length>0,{type:o}=n,u=(0,g.unescapeSelector)(n.name);typeof u=="string"&&u!==n.name&&(n.name=u);let d=new Set,i=!1;if(this.#u)i=!0;else switch(o){case c.PS_ELEMENT_SELECTOR:{this.#f.matchPseudoElementSelector(u,f);break}case c.ID_SELECTOR:{if(this.#s.nodeType===c.ELEMENT_NODE)i=!0;else{const s=this.#s.getElementById(u);s&&s!==e&&e.contains(s)&&(l?this._matchLeaves(a,s,f)&&d.add(s):d.add(s))}break}case c.CLASS_SELECTOR:{const s=e.getElementsByClassName(u);d=this._matchHTMLCollection(s,{compound:l,filterLeaves:a});break}case c.TYPE_SELECTOR:{if(this.#t.contentType==="text/html"&&!/[*|]/.test(u)){const s=e.getElementsByTagName(u);d=this._matchHTMLCollection(s,{compound:l,filterLeaves:a})}else i=!0;break}default:i=!0}return{nodes:d,pending:i}}_matchCombinator(r,e,f={}){const{combo:n,leaves:a}=r,{name:l}=n,{parentNode:o}=e,{dir:u}=f,d=new Set;if(u===v)switch(l){case"+":{const i=e.nextElementSibling;i&&this._matchLeaves(a,i,f)&&d.add(i);break}case"~":{if(o){const i=this._createTreeWalker(o);let s=(0,k.traverseNode)(e,i);for(s=i.nextSibling();s;)this._matchLeaves(a,s,f)&&d.add(s),s=i.nextSibling()}break}case">":{const i=this._createTreeWalker(e);let s=(0,k.traverseNode)(e,i);for(s=i.firstChild();s;)this._matchLeaves(a,s,f)&&d.add(s),s=i.nextSibling();break}case" ":default:{const{nodes:i,pending:s}=this._findDescendantNodes(a,e);if(i.size)return i;if(s){const t=this._createTreeWalker(e);let h=(0,k.traverseNode)(e,t);for(h=t.nextNode();h&&e.contains(h);)this._matchLeaves(a,h,f)&&d.add(h),h=t.nextNode()}}}else switch(l){case"+":{const i=e.previousElementSibling;i&&this._matchLeaves(a,i,f)&&d.add(i);break}case"~":{if(o){const i=this._createTreeWalker(o);let s=(0,k.traverseNode)(o,i);for(s=i.firstChild();s&&s!==e;)this._matchLeaves(a,s,f)&&d.add(s),s=i.nextSibling()}break}case">":{o&&this._matchLeaves(a,o,f)&&d.add(o);break}case" ":default:{const i=[];let s=o;for(;s;)this._matchLeaves(a,s,f)&&i.push(s),s=s.parentNode;if(i.length)return new Set(i.reverse())}}return d}_findNode(r,e){const{node:f}=e,n=this.#m;let a=(0,k.traverseNode)(f,n),l;if(a)for((a.nodeType!==c.ELEMENT_NODE||a===f&&a!==this.#s)&&(a=n.nextNode());a;){if(this._matchLeaves(r,a,{warn:this.#i})){l=a;break}a=n.nextNode()}return l??null}_matchSelf(r){const e=[],f=this._matchLeaves(r,this.#e,{warn:this.#i});let n=!1;return f&&(e.push(this.#e),n=!0),[e,n]}_findLineal(r,e={}){const{complex:f}=e,n=[];let a=this._matchLeaves(r,this.#e,{warn:this.#i}),l=!1;if(a&&(n.push(this.#e),l=!0),!a||f){let o=this.#e.parentNode;for(;o&&(a=this._matchLeaves(r,o,{warn:this.#i}),a&&(n.push(o),l=!0),o.parentNode);)o=o.parentNode}return[n,l]}_findFirst(r){const e=[],f=this._findNode(r,{node:this.#e});let n=!1;return f&&(e.push(f),n=!0),[e,n]}_findFromHTMLCollection(r,e={}){const{complex:f,compound:n,filterLeaves:a,targetType:l}=e;let o=[],u=!1,d=!1;const i=r.length;if(i)if(this.#e.nodeType===c.ELEMENT_NODE)for(let s=0;s<i;s++){const t=r[s];if(t!==this.#e&&(this.#e.contains(t)||t.contains(this.#e))){if(n){if(this._matchLeaves(a,t,{warn:this.#i})&&(o.push(t),u=!0,l===c.TARGET_FIRST))break}else if(o.push(t),u=!0,l===c.TARGET_FIRST)break}}else if(f)if(n)for(let s=0;s<i;s++){const t=r[s];if(this._matchLeaves(a,t,{warn:this.#i})&&(o.push(t),u=!0,l===c.TARGET_FIRST))break}else o=[].slice.call(r),u=!0,d=!0;else if(n)for(let s=0;s<i;s++){const t=r[s];if(this._matchLeaves(a,t,{warn:this.#i})&&(o.push(t),u=!0,l===c.TARGET_FIRST))break}else o=[].slice.call(r),u=!0,d=!0;return[o,u,d]}_findEntryNodes(r,e,f){const{leaves:n}=r,[a,...l]=n,o=l.length>0,{name:u,type:d}=a;let i=[],s=!1,t=!1,h=!1;switch(d){case c.PS_ELEMENT_SELECTOR:{this.#f.matchPseudoElementSelector(u,{warn:this.#i});break}case c.ID_SELECTOR:{if(e===c.TARGET_SELF)[i,t]=this._matchSelf(n);else if(e===c.TARGET_LINEAL)[i,t]=this._findLineal(n,{complex:f});else if(e===c.TARGET_FIRST&&this.#s.nodeType!==c.ELEMENT_NODE){const b=this.#s.getElementById(u);b&&(o?this._matchLeaves(l,b,{warn:this.#i})&&(i.push(b),t=!0):(i.push(b),t=!0))}else e===c.TARGET_FIRST?[i,t]=this._findFirst(n):h=!0;break}case c.CLASS_SELECTOR:{if(e===c.TARGET_SELF)[i,t]=this._matchSelf(n);else if(e===c.TARGET_LINEAL)[i,t]=this._findLineal(n,{complex:f});else if(this.#s.nodeType===c.DOCUMENT_NODE){const b=this.#s.getElementsByClassName(u);b.length&&([i,t,s]=this._findFromHTMLCollection(b,{complex:f,compound:o,filterLeaves:l,targetType:e}))}else e===c.TARGET_FIRST?[i,t]=this._findFirst(n):h=!0;break}case c.TYPE_SELECTOR:{if(e===c.TARGET_SELF)[i,t]=this._matchSelf(n);else if(e===c.TARGET_LINEAL)[i,t]=this._findLineal(n,{complex:f});else if(this.#t.contentType==="text/html"&&this.#s.nodeType===c.DOCUMENT_NODE&&!/[*|]/.test(u)){const b=this.#s.getElementsByTagName(u);b.length&&([i,t,s]=this._findFromHTMLCollection(b,{complex:f,compound:o,filterLeaves:l,targetType:e}))}else e===c.TARGET_FIRST?[i,t]=this._findFirst(n):h=!0;break}default:if(e!==c.TARGET_LINEAL&&$.test(u)){if(this.#u&&this.#e.nodeType===c.DOCUMENT_FRAGMENT_NODE){const b=this._matchShadowHostPseudoClass(a,this.#e);b&&(i.push(b),t=!0)}}else e===c.TARGET_SELF?[i,t]=this._matchSelf(n):e===c.TARGET_LINEAL?[i,t]=this._findLineal(n,{complex:f}):e===c.TARGET_FIRST?[i,t]=this._findFirst(n):h=!0}return{collected:s,compound:o,filtered:t,nodes:i,pending:h}}_collectNodes(r){const e=this.#l.values();if(r===c.TARGET_ALL||r===c.TARGET_FIRST){const f=new Set;let n=0;for(const{branch:a}of e){const l=a.length,o=l>1,u=a[0];let d,i;if(o){const{combo:p,leaves:[{name:w,type:N}]}=u,_=a[l-1],{leaves:[{name:y,type:E}]}=_;if(E===c.PS_ELEMENT_SELECTOR||E===c.ID_SELECTOR)d=x,i=_;else if(N===c.PS_ELEMENT_SELECTOR||N===c.ID_SELECTOR)d=v,i=u;else if(r===c.TARGET_ALL)if(w==="*"&&N===c.TYPE_SELECTOR)d=x,i=_;else if(y==="*"&&E===c.TYPE_SELECTOR)d=v,i=u;else if(l===2){const{name:A}=p;/^[+~]$/.test(A)?(d=x,i=_):(d=v,i=u)}else d=v,i=u;else if(y==="*"&&E===c.TYPE_SELECTOR)d=v,i=u;else if(w==="*"&&N===c.TYPE_SELECTOR)d=x,i=_;else{let A;for(const{combo:L,leaves:[W]}of a){const{name:G,type:H}=W;if(H===c.PS_CLASS_SELECTOR&&G==="dir"){A=!1;break}if(!A&&L){const{name:z}=L;/^[+~]$/.test(z)&&(A=!0)}}A?(d=v,i=u):(d=x,i=_)}}else d=x,i=u;const{collected:s,compound:t,filtered:h,nodes:b,pending:m}=this._findEntryNodes(i,r,o);b.length?(this.#l[n].find=!0,this.#d[n]=b):m&&f.add(new Map([["index",n],["twig",i]])),this.#l[n].collected=s,this.#l[n].dir=d,this.#l[n].filtered=h||!t,n++}if(f.size){let a,l;this.#e!==this.#s&&this.#e.nodeType===c.ELEMENT_NODE?(a=this.#e,l=this.#m):(a=this.#s,l=this.#r);let o=(0,k.traverseNode)(a,l);for(;o;){let u=!1;if(this.#e.nodeType===c.ELEMENT_NODE?o===this.#e?u=!0:u=this.#e.contains(o):u=!0,u)for(const d of f){const{leaves:i}=d.get("twig");if(this._matchLeaves(i,o,{warn:this.#i})){const t=d.get("index");this.#l[t].filtered=!0,this.#l[t].find=!0,this.#d[t].push(o)}}o!==l.currentNode&&(o=(0,k.traverseNode)(o,l)),o=l.nextNode()}}}else{let f=0;for(const{branch:n}of e){const a=n[n.length-1],l=n.length>1,{compound:o,filtered:u,nodes:d}=this._findEntryNodes(a,r,l);d.length&&(this.#l[f].find=!0,this.#d[f]=d),this.#l[f].dir=x,this.#l[f].filtered=u||!o,f++}}return[this.#l,this.#d]}_getCombinedNodes(r,e,f){const n=[];for(const a of e){const l=this._matchCombinator(r,a,{dir:f,warn:this.#i});l.size&&n.push(...l)}return n.length?new Set(n):new Set}_matchNodeNext(r,e,f){const{combo:n,index:a}=f,{combo:l,leaves:o}=r[a],u={combo:n,leaves:o},d=this._getCombinedNodes(u,e,v);let i;if(d.size)if(a===r.length-1){const[s]=(0,k.sortNodes)(d);i=s}else i=this._matchNodeNext(r,d,{combo:l,index:a+1});return i??null}_matchNodePrev(r,e,f){const{index:n}=f,a=r[n],l=new Set([e]),o=this._getCombinedNodes(a,l,x);let u;if(o.size){if(n===0)u=e;else for(const d of o)if(this._matchNodePrev(r,d,{index:n-1}))return e}return u??null}find(r){(r===c.TARGET_ALL||r===c.TARGET_FIRST)&&this._prepareQuerySelectorWalker();const[[...e],f]=this._collectNodes(r),n=e.length;let a,l=new Set;for(let o=0;o<n;o++){const{branch:u,collected:d,dir:i,find:s}=e[o],t=u.length;if(t&&s){const h=f[o],b=h.length,m=t-1;if(m===0)if((r===c.TARGET_ALL||r===c.TARGET_FIRST)&&this.#e.nodeType===c.ELEMENT_NODE)for(let p=0;p<b;p++){const w=h[p];if(w!==this.#e&&this.#e.contains(w)&&(l.add(w),r!==c.TARGET_ALL))break}else if(r===c.TARGET_ALL)if(l.size){const p=[...l];l=new Set([...p,...h]),a=!0}else l=new Set(h);else{const[p]=h;l.add(p)}else if(r===c.TARGET_ALL)if(i===v){let{combo:p}=u[0];for(const w of h){let N=new Set([w]);for(let _=1;_<t;_++){const{combo:y,leaves:E}=u[_],A={combo:p,leaves:E};if(N=this._getCombinedNodes(A,N,i),N.size)if(_===m)if(l.size){const L=[...l];l=new Set([...L,...N]),a=!0}else l=N;else p=y;else break}}}else for(const p of h){let w=new Set([p]);for(let N=m-1;N>=0;N--){const _=u[N];if(w=this._getCombinedNodes(_,w,i),w.size)N===0&&(l.add(p),t>1&&l.size>1&&(a=!0));else break}}else if(r===c.TARGET_FIRST&&i===v){const{combo:p}=u[0];let w;for(const N of h)if(w=this._matchNodeNext(u,new Set([N]),{combo:p,index:1}),w){l.add(w);break}if(!w&&!d){const{leaves:N}=u[0],[_]=h;let y=this._findNode(N,{node:_});for(;y;){if(w=this._matchNodeNext(u,new Set([y]),{combo:p,index:1}),w){l.add(w);break}y=this._findNode(N,{node:y})}}}else{let p;for(const w of h)if(p=this._matchNodePrev(u,w,{index:m-1}),p){l.add(w);break}if(!p&&!d&&r===c.TARGET_FIRST){const{leaves:w}=u[m],[N]=h;let _=this._findNode(w,{node:N});for(;_;){if(p=this._matchNodePrev(u,_,{index:m-1}),p){l.add(_);break}_=this._findNode(w,{node:_})}}}}}return r===c.TARGET_FIRST?(l.delete(this.#e),l.size>1&&(l=new Set((0,k.sortNodes)(l)))):r===c.TARGET_ALL&&(l.delete(this.#e),a&&l.size>1&&(l=new Set((0,k.sortNodes)(l)))),this.#o&&(this.#h=new WeakMap),l}}0&&(module.exports={Finder}); | ||
//# sourceMappingURL=finder.js.map |
@@ -63,3 +63,3 @@ { | ||
}, | ||
"version": "5.3.1" | ||
"version": "5.3.2" | ||
} |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is too big to display
439212
4959