@asamuzakjp/dom-selector
Advanced tools
Comparing version 2.0.3-a.8 to 2.1.0-b.1
@@ -1,2 +0,2 @@ | ||
var j=Object.create;var W=Object.defineProperty;var B=Object.getOwnPropertyDescriptor;var G=Object.getOwnPropertyNames;var q=Object.getPrototypeOf,V=Object.prototype.hasOwnProperty;var X=(A,a)=>{for(var e in a)W(A,e,{get:a[e],enumerable:!0})},H=(A,a,e,r)=>{if(a&&typeof a=="object"||typeof a=="function")for(let i of G(a))!V.call(A,i)&&i!==e&&W(A,i,{get:()=>a[i],enumerable:!(r=B(a,i))||r.enumerable});return A};var Y=(A,a,e)=>(e=A!=null?j(q(A)):{},H(a||!A||!A.__esModule?W(e,"default",{value:A,enumerable:!0}):e,A)),K=A=>H(W({},"__esModule",{value:!0}),A);var Q={};X(Q,{Finder:()=>J});module.exports=K(Q);var z=Y(require("is-potential-custom-element-name"),1),x=require("./dom-util.js"),D=require("./matcher.js"),v=require("./parser.js"),o=require("./constant.js");const C="next",O="prev",L="all",$="first",R="lineal",P="self",I=o.SHOW_DOCUMENT|o.SHOW_DOCUMENT_FRAGMENT|o.SHOW_ELEMENT;class J{#a;#l;#t;#r;#e;#n;#b;#c;#s;#u;#h;#o;#d;#i;#f;constructor(){this.#l=new WeakMap}_onError(a){if(!this.#b)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{noexcept:i,warn:t}=r;return this.#b=!!i,this.#i=!!t,[this.#f,this.#t,this.#s]=(0,x.prepareDOMObjects)(e),this.#e=e,this.#h=(0,x.isInShadowTree)(e),this.#u=a,[this.#a,this.#n]=this._correspond(a),this.#c=new WeakMap,this.#e}_correspond(a){const e=[];let r,i=this.#t&&this.#l.get(this.#t);if(i&&i.has(`${a}`)&&(r=i.get(a)),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,v.parseSelector)(a)}catch(h){this._onError(h)}const n=(0,v.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 new DOMException(d,o.SYNTAX_ERR)}c.push({combo:s,leaves:(0,v.sortAST)(l)}),l.clear()}else s&&l.add(s);if(h.length)s=h.shift();else{c.push({combo:null,leaves:(0,v.sortAST)(l)}),l.clear();break}}}r.push({branch:c,dir:null,filtered:!1,find:!1}),e[f]=[],f++}this.#t&&(i||(i=new Map),i.set(`${a}`,r),this.#l.set(this.#t,i))}return[r,e]}_prepareTreeWalkers(){return this.#d=this.#t.createTreeWalker(this.#s,I),this.#r=this.#t.createTreeWalker(this.#e,I),this.#o=!1,[this.#d,this.#r]}_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,v.walkAST)(f),this.#l.set(f,s))),h){const l=this.#t.createTreeWalker(h,I);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 _ of s)if(m=this._matchLeaves(_,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 _=0,k=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)&&(k===m&&(c.add(d),m+=i),i>0?k++:k--):_===m&&(f||c.add(d),m+=i),n?d=l.previousSibling():d=l.nextSibling(),_++}}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.#t.createTreeWalker(f,I);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:_}=u;if(m===n&&_===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,v.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,v.unescapeSelector)(a.name),i=(0,x.getDirectionality)(e);let t;return r===i&&(t=e),t??null}_matchLanguagePseudoClass(a,e){const r=(0,v.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 _=new Set([e]);for(let k=g-1;k>=0;k--){const N=d[k],b=[];r.dir=O;for(const w of _){const p=this._matchCombinator(N,w,r);p.size&&b.push(...p)}if(b.length)k===0?l=!0:_=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=this.#i}=r,c=(0,v.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,v.walkAST)(a),g=[],m=[];for(const[..._]of d){for(const w of _){const p=(0,v.generateCSS)(w);g.push(p)}const k=[],N=new Set;let b=_.shift();for(;b;)if(b.type===o.COMBINATOR?(k.push({combo:b,leaves:[...N]}),N.clear()):b&&N.add(b),_.length)b=_.shift();else{k.push({combo:null,leaves:[...N]}),N.clear();break}m.push(k)}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)$/,_=/^(?:date(?:time-local)?|month|time|week)$/,k=/(?:(?:rang|tim)e|date(?:time-local)?|month|number|week)$/,N=/^(?:(?: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.#t.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.#t.URL);e.id&&b===`#${e.id}`&&this.#t.contains(e)&&s.add(e);break}case"target-within":{const{hash:b}=new URL(this.#t.URL);if(b){const w=b.replace(/^#/,"");let p=this.#t.getElementById(w);for(;p;){if(p===e){s.add(e);break}p=p.parentNode}}break}case"scope":{this.#e.nodeType===o.ELEMENT_NODE?e===this.#e&&s.add(e):e===this.#t.documentElement&&s.add(e);break}case"focus":{e===this.#t.activeElement&&s.add(e);break}case"focus-within":{let b=this.#t.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,z.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,z.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||_.test(e.type)||N.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||_.test(e.type)||N.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")?N.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.#t.documentElement);let p;const y=w.getElementsByTagName("input"),E=y.length;if(E)for(let T=0;T<E;T++){const S=y[T];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.#t.createTreeWalker(p,o.SHOW_ELEMENT);let E=y.firstChild();for(;E;){const T=E.localName;let S;if(T==="button"?S=!(E.hasAttribute("type")&&b.test(E.getAttribute("type"))):T==="input"&&(S=E.hasAttribute("type")&&w.test(E.getAttribute("type"))),S){E===e&&s.add(e);break}E=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 E=new Set,T=this.#t.createTreeWalker(n,o.SHOW_ELEMENT);let S=T.firstChild();for(;S;){if(S.selected||S.hasAttribute("selected")){E.add(S);break}S=T.nextSibling()}E.size&&E.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.#t.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.#t.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")&&k.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")&&k.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)||_.test(w)||N.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)||_.test(w)||N.test(w))&&(b=e)}else b=e;b&&!(e.required||e.hasAttribute("required"))&&s.add(e);break}case"root":{e===this.#t.documentElement&&s.add(e);break}case"empty":{if(e.hasChildNodes()){let b;const w=this.#t.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,v.unescapeSelector)(a.name);let t;if(Array.isArray(r)){const[n]=(0,v.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,v.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,v.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,v.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,v.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.#t.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.#t.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.#t.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.#t.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.#t.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.#e.nodeType===o.ELEMENT_NODE?t===this.#e?n=!0:n=this.#e.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.#e)&&(e.push(this.#e),r=!0),[e,r]}_findLineal(a,e={}){const{complex:r,compound:i}=e,t=[];let n=!1,f=this._matchLeaves([a],this.#e);if(f&&!r&&!i)t.push(this.#e),n=!0;else{f&&(t.push(this.#e),i||(n=!0));let h=this.#e.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.#e});return i&&(e.push(i),r=!0),[e,r]}_findFromHTMLCollection(a,e={}){const{compound:r,filterLeaves:i}=e;let t=[],n=!1;const f=a.length;if(f)if(this.#e.nodeType===o.ELEMENT_NODE)for(let h=0;h<f;h++){const c=a[h];(c===this.#e||(0,x.isInclusive)(c,this.#e))&&(r?this._matchLeaves(i,c,{warn:this.#i})&&(t.push(c),n=!0):(t.push(c),n=!0))}else t=[].slice.call(a),r||(n=!0);return[t,n]}_findEntryNodes(a,e,r){const{leaves:i}=a,[t,...n]=i,{type:f}=t,h=(0,v.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===$&&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===$)[s,l]=this._findFirst(i);else if(this.#s.nodeType===o.DOCUMENT_NODE){const d=this.#s.getElementsByClassName(h);[s,l]=this._findFromHTMLCollection(d,{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.#t.contentType==="text/html"&&this.#s.nodeType===o.DOCUMENT_NODE&&!/[*|]/.test(h)){const d=this.#s.getElementsByTagName(h);[s,l]=this._findFromHTMLCollection(d,{compound:c,filterLeaves:n})}else u=!0;break}default:if(e!==R&&o.REG_SHADOW_HOST.test(h)){if(this.#h&&this.#e.nodeType===o.DOCUMENT_FRAGMENT_NODE){const d=this._matchShadowHostPseudoClass(t,this.#e);d&&(s.push(d),l=!0)}}else e===P?[s,l]=this._matchSelf(i):e===R?[s,l]=this._findLineal(t,{complex:r,compound:c}):e===$?[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=O,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=O,f=l;else if(u==="*"&&d===o.SELECTOR_TYPE)n=C,f=t;else if(r===2){const{name:g}=h;/^[+~]$/.test(g)?(n=O,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=O,f=l;else{let g,m;for(const{combo:_,leaves:[k]}of a){const{type:N}=k,b=(0,v.unescapeSelector)(k.name);if(N===o.SELECTOR_PSEUDO_CLASS&&b==="dir"){g=!1;break}if(_&&!m){const{name:w}=_;/^[+~]$/.test(w)&&(g=!0,m=!0)}}g?(n=C,f=t):(n=O,f=l)}}else n=O,f=t;return{complex:i,dir:n,twig:f}}_collectNodes(a){const e=this.#a.values();if(a===L||a===$){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.#e!==this.#s&&this.#e.nodeType===o.ELEMENT_NODE?(t=this.#e,n=this.#r):(t=this.#s,n=this.#d);let f=this._traverse(t,n);for(;f;){let h=!1;if(this.#e.nodeType===o.ELEMENT_NODE?f===this.#e?h=!0:h=this.#e.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=O,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===$)&&this.#e.nodeType===o.ELEMENT_NODE)for(let m=0;m<u;m++){const _=l[m];if(_!==this.#e&&this.#e.contains(_)&&(i.add(_),a!==L))break}else if(g.length)for(let m=0;m<u;m++){const _=l[m];if((h||this._matchLeaves(g,_,{warn:this.#i}))&&(i.add(_),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[,..._]=m;let k;for(let N=0;N<u;N++){const b=l[N];if(h||this._matchLeaves(_,b,{warn:this.#i})){let p=new Set([b]);for(let y=1;y<s;y++){const{combo:E,leaves:T}=n[y],S=[];for(const M of p){const U={combo:g,leaves:T},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)}k=!0}else g=E,p=new Set(S),k=!1;else{k=!1;break}}}else k=!1;if(k&&a!==L)break}if(!k&&a===$){const[N]=[...l];let b=this._findNode(m,{node:N});for(;b;){let w=new Set([b]);for(let p=1;p<s;p++){const{combo:y,leaves:E}=n[p],T=[];for(const S of w){const M={combo:g,leaves:E},U=this._matchCombinator(M,S,{dir:f,warn:this.#i});U.size&&T.push(...U)}if(T.length)if(p===d){const[S]=(0,x.sortNodes)(T);i.add(S),k=!0}else g=y,w=new Set(T),k=!1;else{k=!1;break}}if(k)break;b=this._findNode(m,{node:b}),w=new Set([b])}}}else{const{leaves:g}=n[d];let m;for(let _=0;_<u;_++){const k=l[_];let N=new Set([k]);for(let b=d-1;b>=0;b--){const w=n[b],p=[];for(const y of N){const E=this._matchCombinator(w,y,{dir:f,warn:this.#i});E.size&&p.push(...E)}if(p.length)b===0?(i.add(k),m=!0,a===L&&s>1&&i.size>1&&(this.#o=!0)):(N=new Set(p),m=!1);else{m=!1;break}}if(m&&a!==L)break}if(!m&&a===$){const[_]=[...l];let k=this._findNode(g,{node:_});for(;k;){let N=new Set([k]);for(let b=d-1;b>=0;b--){const w=n[b],p=[];for(const y of N){const E=this._matchCombinator(w,y,{dir:f,warn:this.#i});E.size&&p.push(...E)}if(p.length)b===0?(i.add(k),m=!0):(N=new Set(p),m=!1);else{m=!1;break}}if(m)break;k=this._findNode(g,{node:k}),N=new Set([k])}}}}}return i}_find(a){return this._collectNodes(a),this._matchNodes(a)}matches(a,e,r){let i;try{if(this._setup(a,e,r),e.nodeType!==o.ELEMENT_NODE){const n=`Unexpected node ${e.nodeName}`;throw new TypeError(n)}const t=this._find(P);t.size&&(i=t.has(this.#e))}catch(t){this._onError(t)}return!!i}closest(a,e,r){let i;try{if(this._setup(a,e,r),e.nodeType!==o.ELEMENT_NODE){const f=`Unexpected node ${e.nodeName}`;throw new TypeError(f)}const t=this._find(R);let n=this.#e;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{this._setup(a,e,r),this._prepareTreeWalkers();const t=this._find($);t.delete(this.#e),t.size&&([i]=(0,x.sortNodes)(t))}catch(t){this._onError(t)}return i??null}querySelectorAll(a,e,r){let i;try{this._setup(a,e,r),this._prepareTreeWalkers();const t=this._find(L);t.delete(this.#e),t.size&&(this.#o?i=(0,x.sortNodes)(t):i=[...t])}catch(t){this._onError(t)}return i??[]}}0&&(module.exports={Finder}); | ||
var j=Object.create;var W=Object.defineProperty;var B=Object.getOwnPropertyDescriptor;var G=Object.getOwnPropertyNames;var q=Object.getPrototypeOf,V=Object.prototype.hasOwnProperty;var X=(A,a)=>{for(var e in a)W(A,e,{get:a[e],enumerable:!0})},H=(A,a,e,n)=>{if(a&&typeof a=="object"||typeof a=="function")for(let i of G(a))!V.call(A,i)&&i!==e&&W(A,i,{get:()=>a[i],enumerable:!(n=B(a,i))||n.enumerable});return A};var Y=(A,a,e)=>(e=A!=null?j(q(A)):{},H(a||!A||!A.__esModule?W(e,"default",{value:A,enumerable:!0}):e,A)),K=A=>H(W({},"__esModule",{value:!0}),A);var Q={};X(Q,{Finder:()=>J});module.exports=K(Q);var z=Y(require("is-potential-custom-element-name"),1),x=require("./dom-util.js"),D=require("./matcher.js"),v=require("./parser.js"),o=require("./constant.js");const C="next",O="prev",L="all",$="first",R="lineal",P="self",I=o.SHOW_DOCUMENT|o.SHOW_DOCUMENT_FRAGMENT|o.SHOW_ELEMENT;class J{#a;#l;#t;#r;#e;#n;#b;#c;#s;#u;#h;#o;#d;#i;#f;constructor(){this.#l=new WeakMap}_onError(a){if(!this.#b)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,n={}){const{noexcept:i,warn:t}=n;return this.#b=!!i,this.#i=!!t,[this.#f,this.#t,this.#s]=(0,x.prepareDOMObjects)(e),this.#e=e,this.#h=(0,x.isInShadowTree)(e),this.#u=a,[this.#a,this.#n]=this._correspond(a),this.#c=new WeakMap,this.#e}_correspond(a){const e=[];let n,i=this.#t&&this.#l.get(this.#t);if(i&&i.has(`${a}`)&&(n=i.get(a)),n){const t=n.length;for(let c=0;c<t;c++)n[c].dir=null,n[c].filtered=!1,n[c].find=!1,e[c]=[]}else{let t;try{t=(0,v.parseSelector)(a)}catch(h){this._onError(h)}const c=(0,v.walkAST)(t);n=[];let f=0;for(const[...h]of c){const r=[];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 new DOMException(d,o.SYNTAX_ERR)}r.push({combo:s,leaves:(0,v.sortAST)(l)}),l.clear()}else s&&l.add(s);if(h.length)s=h.shift();else{r.push({combo:null,leaves:(0,v.sortAST)(l)}),l.clear();break}}}n.push({branch:r,dir:null,filtered:!1,find:!1}),e[f]=[],f++}this.#t&&(i||(i=new Map),i.set(`${a}`,n),this.#l.set(this.#t,i))}return[n,e]}_prepareTreeWalkers(){return this.#d=this.#t.createTreeWalker(this.#s,I),this.#r=this.#t.createTreeWalker(this.#e,I),this.#o=!1,[this.#d,this.#r]}_traverse(a={},e=this.#d){let n,i=e.currentNode;if(a.nodeType===o.ELEMENT_NODE&&i===a)n=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){n=i;break}i=e.nextNode()}else n=i}return n??null}_collectNthChild(a,e,n){const{a:i,b:t,reverse:c,selector:f}=a,{parentNode:h}=e;let r=new Set,s;if(f&&(this.#l.has(f)?s=this.#l.get(f):(s=(0,v.walkAST)(f),this.#l.set(f,s))),h){const l=this.#t.createTreeWalker(h,I);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 _ of s)if(m=this._matchLeaves(_,d,n),!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),c?d=l.lastChild():d=l.firstChild();d;){if(g.has(d)){if(m===t-1){r.add(d);break}m++}c?d=l.previousSibling():d=l.nextSibling()}}else if(!f){let m=0;for(d=this._traverse(h,l),c?d=l.lastChild():d=l.firstChild();d;){if(m===t-1){r.add(d);break}c?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 _=0,k=i>0?0:t-1;for(d=this._traverse(h,l),c?d=l.lastChild():d=l.firstChild();d&&(d&&m>=0&&m<u);)g.size?g.has(d)&&(k===m&&(r.add(d),m+=i),i>0?k++:k--):_===m&&(f||r.add(d),m+=i),c?d=l.previousSibling():d=l.nextSibling(),_++}}if(c&&r.size>1){const m=[...r];r=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,n),l)break;l&&r.add(e)}else r.add(e);return r}_collectNthOfType(a,e){const{a:n,b:i,reverse:t}=a,{localName:c,parentNode:f,prefix:h}=e;let r=new Set;if(f){const s=this.#t.createTreeWalker(f,I);let l=0,u=s.firstChild();for(;u;)l++,u=s.nextSibling();if(n===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===c&&m===h){if(d===i-1){r.add(u);break}d++}t?u=s.previousSibling():u=s.nextSibling()}}}else{let d=i-1;if(n>0)for(;d<0;)d+=n;if(d>=0&&d<l){let g=n>0?0:i-1;for(u=this._traverse(f,s),t?u=s.lastChild():u=s.firstChild();u;){const{localName:m,prefix:_}=u;if(m===c&&_===h){if(g===d&&(r.add(u),d+=n),d<0||d>=l)break;n>0?g++:g--}t?u=s.previousSibling():u=s.nextSibling()}}}if(t&&r.size>1){const d=[...r];r=new Set(d.reverse())}}else e===this.#s&&this.#s.nodeType===o.ELEMENT_NODE&&n+i===1&&r.add(e);return r}_matchAnPlusB(a,e,n,i){const{nth:{a:t,b:c,name:f},selector:h}=a,r=(0,v.unescapeSelector)(f),s=new Map;r?(r==="even"?(s.set("a",2),s.set("b",0)):r==="odd"&&(s.set("a",2),s.set("b",1)),n.indexOf("last")>-1&&s.set("reverse",!0)):(typeof t=="string"&&/-?\d+/.test(t)?s.set("a",t*1):s.set("a",0),typeof c=="string"&&/-?\d+/.test(c)?s.set("b",c*1):s.set("b",0),n.indexOf("last")>-1&&s.set("reverse",!0));let l=new Set;if(s.has("a")&&s.has("b")){if(/^nth-(?:last-)?child$/.test(n)){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(n)){const u=Object.fromEntries(s),d=this._collectNthOfType(u,e);d.size&&(l=d)}}return l}_matchDirectionPseudoClass(a,e){const n=(0,v.unescapeSelector)(a.name),i=(0,x.getDirectionality)(e);let t;return n===i&&(t=e),t??null}_matchLanguagePseudoClass(a,e){const n=(0,v.unescapeSelector)(a.name);let i;if(n==="*")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(n){const t=`(?:-${o.ALPHA_NUM})*`;if(new RegExp(`^(?:\\*-)?${o.ALPHA_NUM}${t}$`,"i").test(n)){let f;if(n.indexOf("-")>-1){const[h,r,...s]=n.split("-");let l;h==="*"?l=`${o.ALPHA_NUM}${t}`:l=`${h}${t}`;const u=`-${r}${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(`^${n}${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 r=h.getAttribute("lang");f.test(r)&&(i=e);break}h=h.parentNode}}}}return i??null}_matchHasPseudoFunc(a,e,n={}){let i;if(Array.isArray(a)&&a.length){const[t]=a,{type:c}=t;let f;c===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 r={combo:f,leaves:h};n.dir=C;const s=this._matchCombinator(r,e,n);if(s.size)if(a.length){for(const l of s)if(i=this._matchHasPseudoFunc(Object.assign([],a),l,n),i)break}else i=!0}return!!i}_matchLogicalPseudoFunc(a,e,n={}){const{astName:i="",branches:t=[],selector:c="",twigBranches:f=[]}=a;let h;if(i==="has")if(c.includes(":has("))h=null;else{let r;for(const s of t)if(r=this._matchHasPseudoFunc(Object.assign([],s),e,n),r)break;r&&(h=e)}else{const r=/^(?:is|where)$/.test(i);n.forgive=r;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,n),l&&g>0){let _=new Set([e]);for(let k=g-1;k>=0;k--){const N=d[k],b=[];n.dir=O;for(const w of _){const p=this._matchCombinator(N,w,n);p.size&&b.push(...p)}if(b.length)k===0?l=!0:_=new Set(b);else{l=!1;break}}}if(l)break}i==="not"?l||(h=e):l&&(h=e)}return h??null}_matchPseudoClassSelector(a,e,n={}){const{children:i}=a,{localName:t,parentNode:c}=e,{forgive:f,warn:h=this.#i}=n,r=(0,v.unescapeSelector)(a.name);let s=new Set;if(o.REG_LOGICAL_PSEUDO.test(r)){let l;if(this.#l.has(a))l=this.#l.get(a);else{const d=(0,v.walkAST)(a),g=[],m=[];for(const[..._]of d){for(const w of _){const p=(0,v.generateCSS)(w);g.push(p)}const k=[],N=new Set;let b=_.shift();for(;b;)if(b.type===o.COMBINATOR?(k.push({combo:b,leaves:[...N]}),N.clear()):b&&N.add(b),_.length)b=_.shift();else{k.push({combo:null,leaves:[...N]}),N.clear();break}m.push(k)}l={astName:r,branches:d,twigBranches:m,selector:g.join(",")},this.#l.set(a,l)}const u=this._matchLogicalPseudoFunc(l,e,n);u&&s.add(u)}else if(Array.isArray(i)){const[l]=i;if(/^nth-(?:last-)?(?:child|of-type)$/.test(r)){const u=this._matchAnPlusB(l,e,r,n);u.size&&(s=u)}else if(r==="dir"){const u=this._matchDirectionPseudoClass(l,e);u&&s.add(u)}else if(r==="lang"){const u=this._matchLanguagePseudoClass(l,e);u&&s.add(u)}else switch(r){case"current":case"nth-col":case"nth-last-col":{if(h){const u=`Unsupported pseudo-class :${r}()`;throw new DOMException(u,o.NOT_SUPPORTED_ERR)}break}case"host":case"host-context":break;default:if(!f){const u=`Unknown pseudo-class :${r}()`;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)$/,_=/^(?:date(?:time-local)?|month|time|week)$/,k=/(?:(?:rang|tim)e|date(?:time-local)?|month|number|week)$/,N=/^(?:(?:emai|te|ur)l|number|password|search|text)$/;switch(r){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.#t.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.#t.URL);e.id&&b===`#${e.id}`&&this.#t.contains(e)&&s.add(e);break}case"target-within":{const{hash:b}=new URL(this.#t.URL);if(b){const w=b.replace(/^#/,"");let p=this.#t.getElementById(w);for(;p;){if(p===e){s.add(e);break}p=p.parentNode}}break}case"scope":{this.#e.nodeType===o.ELEMENT_NODE?e===this.#e&&s.add(e):e===this.#t.documentElement&&s.add(e);break}case"focus":{e===this.#t.activeElement&&s.add(e);break}case"focus-within":{let b=this.#t.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,z.default)(t))if(e.disabled||e.hasAttribute("disabled"))s.add(e);else{let b=c;for(;b&&b.localName!=="fieldset";)b=b.parentNode;b&&c.localName!=="legend"&&b.hasAttribute("disabled")&&s.add(e)}break}case"enabled":{(u.test(t)||(0,z.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||_.test(e.type)||N.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||_.test(e.type)||N.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")?N.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.#t.documentElement);let p;const y=w.getElementsByTagName("input"),E=y.length;if(E)for(let T=0;T<E;T++){const S=y[T];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.#t.createTreeWalker(p,o.SHOW_ELEMENT);let E=y.firstChild();for(;E;){const T=E.localName;let S;if(T==="button"?S=!(E.hasAttribute("type")&&b.test(E.getAttribute("type"))):T==="input"&&(S=E.hasAttribute("type")&&w.test(E.getAttribute("type"))),S){E===e&&s.add(e);break}E=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=c;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 E=new Set,T=this.#t.createTreeWalker(c,o.SHOW_ELEMENT);let S=T.firstChild();for(;S;){if(S.selected||S.hasAttribute("selected")){E.add(S);break}S=T.nextSibling()}E.size&&E.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.#t.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.#t.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")&&k.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")&&k.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)||_.test(w)||N.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)||_.test(w)||N.test(w))&&(b=e)}else b=e;b&&!(e.required||e.hasAttribute("required"))&&s.add(e);break}case"root":{e===this.#t.documentElement&&s.add(e);break}case"empty":{if(e.hasChildNodes()){let b;const w=this.#t.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":{(c&&e===c.firstElementChild||e===this.#s&&this.#s.nodeType===o.ELEMENT_NODE)&&s.add(e);break}case"last-child":{(c&&e===c.lastElementChild||e===this.#s&&this.#s.nodeType===o.ELEMENT_NODE)&&s.add(e);break}case"only-child":{(c&&e===c.firstElementChild&&e===c.lastElementChild||e===this.#s&&this.#s.nodeType===o.ELEMENT_NODE)&&s.add(e);break}case"first-of-type":{if(c){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(c){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(c){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 ::${r}`;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 :${r}`;throw new DOMException(b,o.NOT_SUPPORTED_ERR)}break}default:if(r.startsWith("-webkit-")){if(h){const b=`Unsupported pseudo-class :${r}`;throw new DOMException(b,o.NOT_SUPPORTED_ERR)}}else if(!f){const b=`Unknown pseudo-class :${r}`;throw new DOMException(b,o.SYNTAX_ERR)}}}return s}_matchShadowHostPseudoClass(a,e){const{children:n}=a,i=(0,v.unescapeSelector)(a.name);let t;if(Array.isArray(n)){const[c]=(0,v.walkAST)(n[0]),[...f]=c,{host:h}=e;if(i==="host"){let r;for(const s of f){const{type:l}=s;if(l===o.COMBINATOR){const d=`Invalid selector ${(0,v.generateCSS)(a)}`;throw new DOMException(d,o.SYNTAX_ERR)}if(r=this._matchSelector(s,h).has(h),!r)break}r&&(t=e)}else if(i==="host-context"){let r,s=h;for(;s;){for(const l of f){const{type:u}=l;if(u===o.COMBINATOR){const g=`Invalid selector ${(0,v.generateCSS)(a)}`;throw new DOMException(g,o.SYNTAX_ERR)}if(r=this._matchSelector(l,s).has(s),!r)break}if(r)break;s=s.parentNode}r&&(t=e)}}else if(i==="host")t=e;else{const c=`Invalid selector :${i}`;throw new DOMException(c,o.SYNTAX_ERR)}return t??null}_matchSelector(a,e,n){const{type:i}=a,t=(0,v.unescapeSelector)(a.name);let c=new Set;if(e.nodeType===o.ELEMENT_NODE)switch(i){case o.SELECTOR_PSEUDO_CLASS:{const f=this._matchPseudoClassSelector(a,e,n);f.size&&(c=f);break}case o.SELECTOR_PSEUDO_ELEMENT:{(0,D.matchPseudoElementSelector)(t,n);break}default:{const f=(0,D.matchSelector)(a,e,n);f&&c.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,n);f.size&&(c=f)}else if(o.REG_SHADOW_HOST.test(t)){const f=this._matchShadowHostPseudoClass(a,e,n);f&&c.add(f)}}return c}_matchLeaves(a,e,n){let i;if(this.#c.has(a)){const t=this.#c.get(a);if(t.has(e))i=t.get(e);else{for(const c of a)if(i=this._matchSelector(c,e,n).has(e),!i)break;t.set(e,i),this.#c.set(a,t)}}else{for(const c of a)if(i=this._matchSelector(c,e,n).has(e),!i)break;const t=new WeakMap;t.set(e,i),this.#c.set(a,t)}return!!i}_matchHTMLCollection(a,e={}){const{compound:n,filterLeaves:i}=e;let t=new Set;const c=a.length;if(c)if(n)for(let f=0;f<c;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,n){const[i,...t]=a,{type:c}=i,f=(0,v.unescapeSelector)(i.name),h=t.length>0;let r=new Set,s=!1;if(this.#h)s=!0;else switch(c){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,n)&&r.add(l):r.add(l))}break}case o.SELECTOR_CLASS:{const l=e.getElementsByClassName(f);r=this._matchHTMLCollection(l,{compound:h,filterLeaves:t});break}case o.SELECTOR_TYPE:{if(this.#t.contentType==="text/html"&&!/[*|]/.test(f)){const l=e.getElementsByTagName(f);r=this._matchHTMLCollection(l,{compound:h,filterLeaves:t})}else s=!0;break}case o.SELECTOR_PSEUDO_ELEMENT:{(0,D.matchPseudoElementSelector)(f,n);break}default:s=!0}return{nodes:r,pending:s}}_matchCombinator(a,e,n={}){const{combo:i,leaves:t}=a,{name:c}=i,{dir:f}=n;let h=new Set;if(f===C)switch(c){case"+":{const r=e.nextElementSibling;r&&this._matchLeaves(t,r,n)&&h.add(r);break}case"~":{const{parentNode:r}=e;if(r){const s=this.#t.createTreeWalker(r,o.SHOW_ELEMENT);let l=this._traverse(e,s);for(l===e&&(l=s.nextSibling());l;)this._matchLeaves(t,l,n)&&h.add(l),l=s.nextSibling()}break}case">":{const r=this.#t.createTreeWalker(e,o.SHOW_ELEMENT);let s=r.firstChild();for(;s;)this._matchLeaves(t,s,n)&&h.add(s),s=r.nextSibling();break}case" ":default:{const{nodes:r,pending:s}=this._findDescendantNodes(t,e);if(r.size)h=r;else if(s){const l=this.#t.createTreeWalker(e,o.SHOW_ELEMENT);let u=l.nextNode();for(;u;)this._matchLeaves(t,u,n)&&h.add(u),u=l.nextNode()}}}else switch(c){case"+":{const r=e.previousElementSibling;r&&this._matchLeaves(t,r,n)&&h.add(r);break}case"~":{const r=this.#t.createTreeWalker(e.parentNode,o.SHOW_ELEMENT);let s=r.firstChild();for(;s&&s!==e;)this._matchLeaves(t,s,n)&&h.add(s),s=r.nextSibling();break}case">":{const r=e.parentNode;r&&this._matchLeaves(t,r,n)&&h.add(r);break}case" ":default:{const r=[];let s=e.parentNode;for(;s;)this._matchLeaves(t,s,n)&&r.push(s),s=s.parentNode;r.length&&(h=new Set(r.reverse()))}}return h}_findNode(a,e={}){const{node:n}=e;let i,t=this._traverse(n,this.#r);if(t)for(t.nodeType!==o.ELEMENT_NODE?t=this.#r.nextNode():t===n&&t!==this.#s&&(t=this.#r.nextNode());t;){let c;if(this.#e.nodeType===o.ELEMENT_NODE?t===this.#e?c=!0:c=this.#e.contains(t):c=!0,c&&this._matchLeaves(a,t,{warn:this.#i})){i=t;break}t=this.#r.nextNode()}return i??null}_matchSelf(a){const e=[];let n=!1;return this._matchLeaves(a,this.#e)&&(e.push(this.#e),n=!0),[e,n]}_findLineal(a,e={}){const{complex:n,compound:i,filterLeaves:t}=e,c=[];let f=!1,h=this._matchLeaves([a],this.#e);if(h&&!i&&!n?(c.push(this.#e),f=!0):h&&i&&!n&&(h=this._matchLeaves(t,this.#e),h&&(c.push(this.#e),f=!0)),!h||n){h&&(c.push(this.#e),i||(f=!0));let r=this.#e.parentNode;for(;r&&(h=this._matchLeaves([a],r),h&&(c.push(r),i||(f=!0)),r.parentNode);)r=r.parentNode}return[c,f]}_findFirst(a){const e=[];let n=!1;const i=this._findNode(a,{node:this.#e});return i&&(e.push(i),n=!0),[e,n]}_findFromHTMLCollection(a,e={}){const{compound:n,filterLeaves:i}=e;let t=[],c=!1;const f=a.length;if(f)if(this.#e.nodeType===o.ELEMENT_NODE)for(let h=0;h<f;h++){const r=a[h];(r===this.#e||(0,x.isInclusive)(r,this.#e))&&(n?this._matchLeaves(i,r,{warn:this.#i})&&(t.push(r),c=!0):(t.push(r),c=!0))}else t=[].slice.call(a),n||(c=!0);return[t,c]}_findEntryNodes(a,e,n){const{leaves:i}=a,[t,...c]=i,{type:f}=t,h=(0,v.unescapeSelector)(t.name),r=c.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:n,compound:r,filterLeaves:c});else if(e===$&&this.#s.nodeType!==o.ELEMENT_NODE){const d=this.#s.getElementById(h);d&&(r?this._matchLeaves(c,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:n,compound:r,filterLeaves:c});else if(e===$)[s,l]=this._findFirst(i);else if(this.#s.nodeType===o.DOCUMENT_NODE){const d=this.#s.getElementsByClassName(h);[s,l]=this._findFromHTMLCollection(d,{compound:r,filterLeaves:c})}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:n,compound:r,filterLeaves:c});else if(this.#t.contentType==="text/html"&&this.#s.nodeType===o.DOCUMENT_NODE&&!/[*|]/.test(h)){const d=this.#s.getElementsByTagName(h);[s,l]=this._findFromHTMLCollection(d,{compound:r,filterLeaves:c})}else u=!0;break}default:if(e!==R&&o.REG_SHADOW_HOST.test(h)){if(this.#h&&this.#e.nodeType===o.DOCUMENT_FRAGMENT_NODE){const d=this._matchShadowHostPseudoClass(t,this.#e);d&&(s.push(d),l=!0)}}else e===P?[s,l]=this._matchSelf(i):e===R?[s,l]=this._findLineal(t,{complex:n,compound:r,filterLeaves:c}):e===$?[s,l]=this._findFirst(i):u=!0}return{compound:r,filtered:l,nodes:s,pending:u}}_getEntryTwig(a,e){const n=a.length,i=n>1,t=a[0];let c,f;if(i){const{combo:h,leaves:[{name:r,type:s}]}=t,l=a[n-1],{leaves:[{name:u,type:d}]}=l;if(d===o.SELECTOR_PSEUDO_ELEMENT||d===o.SELECTOR_ID)c=O,f=l;else if(s===o.SELECTOR_PSEUDO_ELEMENT||s===o.SELECTOR_ID)c=C,f=t;else if(e===L)if(r==="*"&&s===o.SELECTOR_TYPE)c=O,f=l;else if(u==="*"&&d===o.SELECTOR_TYPE)c=C,f=t;else if(n===2){const{name:g}=h;/^[+~]$/.test(g)?(c=O,f=l):(c=C,f=t)}else c=C,f=t;else if(u==="*"&&d===o.SELECTOR_TYPE)c=C,f=t;else if(r==="*"&&s===o.SELECTOR_TYPE)c=O,f=l;else{let g,m;for(const{combo:_,leaves:[k]}of a){const{type:N}=k,b=(0,v.unescapeSelector)(k.name);if(N===o.SELECTOR_PSEUDO_CLASS&&b==="dir"){g=!1;break}if(_&&!m){const{name:w}=_;/^[+~]$/.test(w)&&(g=!0,m=!0)}}g?(c=C,f=t):(c=O,f=l)}}else c=O,f=t;return{complex:i,dir:c,twig:f}}_collectNodes(a){const e=this.#a.values();if(a===L||a===$){const n=new Set;let i=0;for(const{branch:t}of e){const{complex:c,dir:f,twig:h}=this._getEntryTwig(t,a),{compound:r,filtered:s,nodes:l,pending:u}=this._findEntryNodes(h,a,c);l.length?(this.#a[i].find=!0,this.#n[i]=l):u&&n.add(new Map([["index",i],["twig",h]])),this.#a[i].dir=f,this.#a[i].filtered=s||!r,i++}if(n.size){let t,c;this.#e!==this.#s&&this.#e.nodeType===o.ELEMENT_NODE?(t=this.#e,c=this.#r):(t=this.#s,c=this.#d);let f=this._traverse(t,c);for(;f;){let h=!1;if(this.#e.nodeType===o.ELEMENT_NODE?f===this.#e?h=!0:h=this.#e.contains(f):h=!0,h)for(const r of n){const{leaves:s}=r.get("twig");if(this._matchLeaves(s,f,{warn:this.#i})){const u=r.get("index");this.#a[u].filtered=!0,this.#a[u].find=!0,this.#n[u].push(f)}}f=c.nextNode()}}}else{let n=0;for(const{branch:i}of e){const t=i[i.length-1],c=i.length>1,{compound:f,filtered:h,nodes:r}=this._findEntryNodes(t,a,c);r.length&&(this.#a[n].find=!0,this.#n[n]=r),this.#a[n].dir=O,this.#a[n].filtered=h||!f,n++}}return[this.#a,this.#n]}_matchNodes(a){const[...e]=this.#a,n=e.length;let i=new Set;for(let t=0;t<n;t++){const{branch:c,dir:f,filtered:h,find:r}=e[t],s=c.length;if(s&&r){const l=this.#n[t],u=l.length,d=s-1;if(d===0){const{leaves:[,...g]}=c[0];if((a===L||a===$)&&this.#e.nodeType===o.ELEMENT_NODE)for(let m=0;m<u;m++){const _=l[m];if(_!==this.#e&&this.#e.contains(_)&&(i.add(_),a!==L))break}else if(g.length)for(let m=0;m<u;m++){const _=l[m];if((h||this._matchLeaves(g,_,{warn:this.#i}))&&(i.add(_),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}=c[0];const[,..._]=m;let k;for(let N=0;N<u;N++){const b=l[N];if(h||this._matchLeaves(_,b,{warn:this.#i})){let p=new Set([b]);for(let y=1;y<s;y++){const{combo:E,leaves:T}=c[y],S=[];for(const M of p){const U={combo:g,leaves:T},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)}k=!0}else g=E,p=new Set(S),k=!1;else{k=!1;break}}}else k=!1;if(k&&a!==L)break}if(!k&&a===$){const[N]=[...l];let b=this._findNode(m,{node:N});for(;b;){let w=new Set([b]);for(let p=1;p<s;p++){const{combo:y,leaves:E}=c[p],T=[];for(const S of w){const M={combo:g,leaves:E},U=this._matchCombinator(M,S,{dir:f,warn:this.#i});U.size&&T.push(...U)}if(T.length)if(p===d){const[S]=(0,x.sortNodes)(T);i.add(S),k=!0}else g=y,w=new Set(T),k=!1;else{k=!1;break}}if(k)break;b=this._findNode(m,{node:b}),w=new Set([b])}}}else{const{leaves:g}=c[d];let m;for(let _=0;_<u;_++){const k=l[_];let N=new Set([k]);for(let b=d-1;b>=0;b--){const w=c[b],p=[];for(const y of N){const E=this._matchCombinator(w,y,{dir:f,warn:this.#i});E.size&&p.push(...E)}if(p.length)b===0?(i.add(k),m=!0,a===L&&s>1&&i.size>1&&(this.#o=!0)):(N=new Set(p),m=!1);else{m=!1;break}}if(m&&a!==L)break}if(!m&&a===$){const[_]=[...l];let k=this._findNode(g,{node:_});for(;k;){let N=new Set([k]);for(let b=d-1;b>=0;b--){const w=c[b],p=[];for(const y of N){const E=this._matchCombinator(w,y,{dir:f,warn:this.#i});E.size&&p.push(...E)}if(p.length)b===0?(i.add(k),m=!0):(N=new Set(p),m=!1);else{m=!1;break}}if(m)break;k=this._findNode(g,{node:k}),N=new Set([k])}}}}}return i}_find(a){return this._collectNodes(a),this._matchNodes(a)}matches(a,e,n){let i;try{if(this._setup(a,e,n),e.nodeType!==o.ELEMENT_NODE){const c=`Unexpected node ${e.nodeName}`;throw new TypeError(c)}const t=this._find(P);t.size&&(i=t.has(this.#e))}catch(t){this._onError(t)}return!!i}closest(a,e,n){let i;try{if(this._setup(a,e,n),e.nodeType!==o.ELEMENT_NODE){const f=`Unexpected node ${e.nodeName}`;throw new TypeError(f)}const t=this._find(R);let c=this.#e;for(;c;){if(t.has(c)){i=c;break}if(c.parentNode)c=c.parentNode;else break}}catch(t){this._onError(t)}return i??null}querySelector(a,e,n){let i;try{this._setup(a,e,n),this._prepareTreeWalkers();const t=this._find($);t.delete(this.#e),t.size&&([i]=(0,x.sortNodes)(t))}catch(t){this._onError(t)}return i??null}querySelectorAll(a,e,n){let i;try{this._setup(a,e,n),this._prepareTreeWalkers();const t=this._find(L);t.delete(this.#e),t.size&&(this.#o?i=(0,x.sortNodes)(t):i=[...t])}catch(t){this._onError(t)}return i??[]}}0&&(module.exports={Finder}); | ||
//# sourceMappingURL=finder.js.map |
@@ -1,2 +0,2 @@ | ||
var T=Object.defineProperty;var $=Object.getOwnPropertyDescriptor;var D=Object.getOwnPropertyNames;var C=Object.prototype.hasOwnProperty;var N=(t,e)=>{for(var c in e)T(t,c,{get:e[c],enumerable:!0})},_=(t,e,c,r)=>{if(e&&typeof e=="object"||typeof e=="function")for(let s of D(e))!C.call(t,s)&&s!==c&&T(t,s,{get:()=>e[s],enumerable:!(r=$(e,s))||r.enumerable});return t};var v=t=>_(T({},"__esModule",{value:!0}),t);var P={};N(P,{_matchAttributeSelector:()=>O,_matchClassSelector:()=>S,_matchIDSelector:()=>k,_matchTypeSelector:()=>L,matchPseudoElementSelector:()=>U,matchSelector:()=>R});module.exports=v(P);var E=require("./dom-util.js"),d=require("./parser.js"),i=require("./constant.js");const O=(t,e)=>{const{flags:c,matcher:r,name:s,value:g}=t;if(typeof c=="string"&&!/^[is]$/i.test(c)){const n=`Invalid selector ${(0,d.generateCSS)(t)}`;throw new DOMException(n,i.SYNTAX_ERR)}const{attributes:x}=e;let w;if(x&&x.length){const u=e.ownerDocument.contentType;let n;u==="text/html"?typeof c=="string"&&/^s$/i.test(c)?n=!1:n=!0:typeof c=="string"&&/^i$/i.test(c)?n=!0:n=!1;let h=(0,d.unescapeSelector)(s.name);n&&(h=h.toLowerCase());const p=new Set;if(h.indexOf("|")>-1){const{prefix:m,localName:l}=(0,d.parseAstName)(h);for(const o of x){let{name:a,value:f}=o;switch(n&&(a=a.toLowerCase(),f=f.toLowerCase()),m){case"":{l===a&&p.add(f);break}case"*":{a.indexOf(":")>-1?a.endsWith(`:${l}`)&&p.add(f):l===a&&p.add(f);break}default:if(a.indexOf(":")>-1){const[y,b]=a.split(":");if(y==="xml"&&b==="lang")continue;m===y&&l===b&&(0,E.isNamespaceDeclared)(y,e)&&p.add(f)}}}}else for(let{name:m,value:l}of x)if(n&&(m=m.toLowerCase(),l=l.toLowerCase()),m.indexOf(":")>-1){const[o,a]=m.split(":");if(o==="xml"&&a==="lang")continue;h===a&&p.add(l)}else h===m&&p.add(l);if(p.size){const{name:m,value:l}=g??{};let o;switch(m?n?o=m.toLowerCase():o=m:l?n?o=l.toLowerCase():o=l:l===""&&(o=l),r){case"=":{typeof o=="string"&&p.has(o)&&(w=e);break}case"~=":{if(o&&typeof o=="string"){for(const a of p)if(new Set(a.split(/\s+/)).has(o)){w=e;break}}break}case"|=":{if(o&&typeof o=="string"){let a;for(const f of p)if(f===o||f.startsWith(`${o}-`)){a=f;break}a&&(w=e)}break}case"^=":{if(o&&typeof o=="string"){let a;for(const f of p)if(f.startsWith(`${o}`)){a=f;break}a&&(w=e)}break}case"$=":{if(o&&typeof o=="string"){let a;for(const f of p)if(f.endsWith(`${o}`)){a=f;break}a&&(w=e)}break}case"*=":{if(o&&typeof o=="string"){let a;for(const f of p)if(f.includes(`${o}`)){a=f;break}a&&(w=e)}break}case null:default:w=e}}}return w??null},S=(t,e)=>{const c=(0,d.unescapeSelector)(t.name),{classList:r}=e;let s;return r.contains(c)&&(s=e),s??null},k=(t,e)=>{const c=(0,d.unescapeSelector)(t.name),{id:r}=e;let s;return c===r&&(s=e),s??null},L=(t,e,c={})=>{const r=(0,d.unescapeSelector)(t.name),{localName:s,namespaceURI:g,prefix:x}=e,{forgive:w}=c;let{prefix:u,localName:n}=(0,d.parseAstName)(r,e);e.ownerDocument.contentType==="text/html"&&(u=u.toLowerCase(),n=n.toLowerCase());let p,m;s.indexOf(":")>-1?[p,m]=s.split(":"):(p=x||"",m=s);let l;const o=(0,E.isNamespaceDeclared)(u,e);if(u===""&&p==="")g===null&&(n==="*"||n===m)&&(l=e);else if(u==="*")(n==="*"||n===m)&&(l=e);else if(u&&p){if(u===p){if(o)(n==="*"||n===m)&&(l=e);else if(!w){const a=`Undeclared namespace ${u}`;throw new DOMException(a,i.SYNTAX_ERR)}}}else if(u&&!w&&!o){const a=`Undeclared namespace ${u}`;throw new DOMException(a,i.SYNTAX_ERR)}return l??null},R=(t,e,c)=>{if(!t||!t.type){const g=`Unexpected node ${Object.prototype.toString.call(t).slice(i.TYPE_FROM,i.TYPE_TO)}`;throw new TypeError(g)}else if(!e||!e.nodeType){const g=`Unexpected node ${Object.prototype.toString.call(e).slice(i.TYPE_FROM,i.TYPE_TO)}`;throw new TypeError(g)}else if(e.nodeType!==i.ELEMENT_NODE){const s=`Unexpected node ${e.nodeName}`;throw new TypeError(s)}let r;switch(t.type){case i.SELECTOR_ATTR:{r=O(t,e);break}case i.SELECTOR_CLASS:{r=S(t,e);break}case i.SELECTOR_ID:{r=k(t,e);break}case i.SELECTOR_TYPE:default:r=L(t,e,c)}return r},U=(t,e={})=>{if(!t||typeof t!="string"){const g=`Unexpected type ${Object.prototype.toString.call(t).slice(i.TYPE_FROM,i.TYPE_TO)}`;throw new TypeError(g)}const{forgive:c,warn:r}=e;switch(t){case"after":case"backdrop":case"before":case"cue":case"cue-region":case"first-letter":case"first-line":case"file-selector-button":case"marker":case"placeholder":case"selection":case"target-text":{if(r){const s=`Unsupported pseudo-element ::${t}`;throw new DOMException(s,i.NOT_SUPPORTED_ERR)}break}case"part":case"slotted":{if(r){const s=`Unsupported pseudo-element ::${t}()`;throw new DOMException(s,i.NOT_SUPPORTED_ERR)}break}default:if(t.startsWith("-webkit-")){if(r){const s=`Unsupported pseudo-element ::${t}`;throw new DOMException(s,i.NOT_SUPPORTED_ERR)}}else if(!c){const s=`Unknown pseudo-element ::${t}`;throw new DOMException(s,i.SYNTAX_ERR)}}};0&&(module.exports={_matchAttributeSelector,_matchClassSelector,_matchIDSelector,_matchTypeSelector,matchPseudoElementSelector,matchSelector}); | ||
var T=Object.defineProperty;var $=Object.getOwnPropertyDescriptor;var C=Object.getOwnPropertyNames;var D=Object.prototype.hasOwnProperty;var N=(t,e)=>{for(var c in e)T(t,c,{get:e[c],enumerable:!0})},_=(t,e,c,r)=>{if(e&&typeof e=="object"||typeof e=="function")for(let i of C(e))!D.call(t,i)&&i!==c&&T(t,i,{get:()=>e[i],enumerable:!(r=$(e,i))||r.enumerable});return t};var v=t=>_(T({},"__esModule",{value:!0}),t);var U={};N(U,{_matchAttributeSelector:()=>O,_matchClassSelector:()=>S,_matchIDSelector:()=>k,_matchTypeSelector:()=>L,matchPseudoElementSelector:()=>P,matchSelector:()=>R});module.exports=v(U);var E=require("./dom-util.js"),d=require("./parser.js"),a=require("./constant.js");const O=(t,e)=>{const{flags:c,matcher:r,name:i,value:g}=t;if(typeof c=="string"&&!/^[is]$/i.test(c)){const n=`Invalid selector ${(0,d.generateCSS)(t)}`;throw new DOMException(n,a.SYNTAX_ERR)}const{attributes:x}=e;let w;if(x&&x.length){const h=e.ownerDocument.contentType;let n;h==="text/html"?typeof c=="string"&&/^s$/i.test(c)?n=!1:n=!0:typeof c=="string"&&/^i$/i.test(c)?n=!0:n=!1;let u=(0,d.unescapeSelector)(i.name);n&&(u=u.toLowerCase());const f=new Set;if(u.indexOf("|")>-1){const{prefix:p,localName:m}=(0,d.parseAstName)(u);for(const s of x){let{name:o,value:l}=s;switch(n&&(o=o.toLowerCase(),l=l.toLowerCase()),p){case"":{m===o&&f.add(l);break}case"*":{o.indexOf(":")>-1?o.endsWith(`:${m}`)&&f.add(l):m===o&&f.add(l);break}default:if(o.indexOf(":")>-1){const[y,b]=o.split(":");if(y==="xml"&&b==="lang")continue;p===y&&m===b&&(0,E.isNamespaceDeclared)(y,e)&&f.add(l)}}}}else for(let{name:p,value:m}of x)if(n&&(p=p.toLowerCase(),m=m.toLowerCase()),p.indexOf(":")>-1){const[s,o]=p.split(":");if(s==="xml"&&o==="lang")continue;u===o&&f.add(m)}else u===p&&f.add(m);if(f.size){const{name:p,value:m}=g??{};let s;switch(p?n?s=p.toLowerCase():s=p:m?n?s=m.toLowerCase():s=m:m===""&&(s=m),r){case"=":{typeof s=="string"&&f.has(s)&&(w=e);break}case"~=":{if(s&&typeof s=="string"){for(const o of f)if(new Set(o.split(/\s+/)).has(s)){w=e;break}}break}case"|=":{if(s&&typeof s=="string"){let o;for(const l of f)if(l===s||l.startsWith(`${s}-`)){o=l;break}o&&(w=e)}break}case"^=":{if(s&&typeof s=="string"){let o;for(const l of f)if(l.startsWith(`${s}`)){o=l;break}o&&(w=e)}break}case"$=":{if(s&&typeof s=="string"){let o;for(const l of f)if(l.endsWith(`${s}`)){o=l;break}o&&(w=e)}break}case"*=":{if(s&&typeof s=="string"){let o;for(const l of f)if(l.includes(`${s}`)){o=l;break}o&&(w=e)}break}case null:default:w=e}}}return w??null},S=(t,e)=>{const c=(0,d.unescapeSelector)(t.name);let r;return e.classList.contains(c)&&(r=e),r??null},k=(t,e)=>{const c=(0,d.unescapeSelector)(t.name);let r;return c===e.id&&(r=e),r??null},L=(t,e,c={})=>{const r=(0,d.unescapeSelector)(t.name),{localName:i,namespaceURI:g,prefix:x}=e,{forgive:w}=c;let{prefix:h,localName:n}=(0,d.parseAstName)(r,e);e.ownerDocument.contentType==="text/html"&&(h=h.toLowerCase(),n=n.toLowerCase());let u,f;i.indexOf(":")>-1?[u,f]=i.split(":"):(u=x||"",f=i);let p;const m=(0,E.isNamespaceDeclared)(u,e);if(h===""&&u==="")g===null&&(n==="*"||n===f)&&(p=e);else if(h==="*")(n==="*"||n===f)&&(p=e);else if(h===u){if(m)(n==="*"||n===f)&&(p=e);else if(!w){const s=`Undeclared namespace ${h}`;throw new DOMException(s,a.SYNTAX_ERR)}}return p??null},R=(t,e,c)=>{if(!t||!t.type){const g=`Unexpected node ${Object.prototype.toString.call(t).slice(a.TYPE_FROM,a.TYPE_TO)}`;throw new TypeError(g)}else if(!e||!e.nodeType){const g=`Unexpected node ${Object.prototype.toString.call(e).slice(a.TYPE_FROM,a.TYPE_TO)}`;throw new TypeError(g)}else if(e.nodeType!==a.ELEMENT_NODE){const i=`Unexpected node ${e.nodeName}`;throw new TypeError(i)}let r;switch(t.type){case a.SELECTOR_ATTR:{r=O(t,e);break}case a.SELECTOR_CLASS:{r=S(t,e);break}case a.SELECTOR_ID:{r=k(t,e);break}case a.SELECTOR_TYPE:default:r=L(t,e,c)}return r},P=(t,e={})=>{if(!t||typeof t!="string"){const g=`Unexpected type ${Object.prototype.toString.call(t).slice(a.TYPE_FROM,a.TYPE_TO)}`;throw new TypeError(g)}const{forgive:c,warn:r}=e;switch(t){case"after":case"backdrop":case"before":case"cue":case"cue-region":case"first-letter":case"first-line":case"file-selector-button":case"marker":case"placeholder":case"selection":case"target-text":{if(r){const i=`Unsupported pseudo-element ::${t}`;throw new DOMException(i,a.NOT_SUPPORTED_ERR)}break}case"part":case"slotted":{if(r){const i=`Unsupported pseudo-element ::${t}()`;throw new DOMException(i,a.NOT_SUPPORTED_ERR)}break}default:if(t.startsWith("-webkit-")){if(r){const i=`Unsupported pseudo-element ::${t}`;throw new DOMException(i,a.NOT_SUPPORTED_ERR)}}else if(!c){const i=`Unknown pseudo-element ::${t}`;throw new DOMException(i,a.SYNTAX_ERR)}}};0&&(module.exports={_matchAttributeSelector,_matchClassSelector,_matchIDSelector,_matchTypeSelector,matchPseudoElementSelector,matchSelector}); | ||
//# sourceMappingURL=matcher.js.map |
@@ -31,5 +31,5 @@ { | ||
"devDependencies": { | ||
"@types/css-tree": "^2.3.4", | ||
"@types/css-tree": "^2.3.5", | ||
"benchmark": "^2.1.4", | ||
"c8": "^9.0.0", | ||
"c8": "^9.1.0", | ||
"chai": "^5.0.0", | ||
@@ -42,7 +42,7 @@ "commander": "^11.1.0", | ||
"eslint-plugin-jsdoc": "^48.0.2", | ||
"eslint-plugin-regexp": "^2.1.2", | ||
"eslint-plugin-regexp": "^2.2.0", | ||
"eslint-plugin-unicorn": "^50.0.1", | ||
"happy-dom": "^12.10.3", | ||
"jsdom": "23.1.0", | ||
"linkedom": "^0.16.6", | ||
"happy-dom": "^13.2.1", | ||
"jsdom": "24.0.0", | ||
"linkedom": "^0.16.8", | ||
"mocha": "^10.2.0", | ||
@@ -64,3 +64,3 @@ "sinon": "^17.0.1", | ||
}, | ||
"version": "2.0.3-a.8" | ||
"version": "2.1.0-b.1" | ||
} |
113
README.md
@@ -8,3 +8,2 @@ # DOM Selector | ||
A CSS selector engine. | ||
Used in jsdom since [jsdom v23.2.0](https://github.com/jsdom/jsdom/releases/tag/23.2.0). | ||
@@ -36,2 +35,3 @@ ## Install | ||
- `opt` **[object][60]?** options | ||
- `opt.noexcept` **[boolean][61]?** no exception | ||
- `opt.warn` **[boolean][61]?** console warn e.g. unsupported pseudo-class | ||
@@ -51,2 +51,3 @@ | ||
- `opt` **[object][60]?** options | ||
- `opt.noexcept` **[boolean][61]?** no exception | ||
- `opt.warn` **[boolean][61]?** console warn e.g. unsupported pseudo-class | ||
@@ -66,2 +67,3 @@ | ||
- `opt` **[object][60]?** options | ||
- `opt.noexcept` **[boolean][61]?** no exception | ||
- `opt.warn` **[boolean][61]?** console warn e.g. unsupported pseudo-class | ||
@@ -82,2 +84,3 @@ | ||
- `opt` **[object][60]?** options | ||
- `opt.noexcept` **[boolean][61]?** no exception | ||
- `opt.warn` **[boolean][61]?** console warn e.g. unsupported pseudo-class | ||
@@ -165,9 +168,109 @@ | ||
<!-- | ||
### Performance | ||
## Monkey patch jsdom | ||
TODO: rewrite benchmark table | ||
--> | ||
``` javascript | ||
import { JSDOM } from 'jsdom'; | ||
import { | ||
closest, matches, querySelector, querySelectorAll | ||
} from '@asamuzakjp/dom-selector'; | ||
const dom = new JSDOM('', { | ||
runScripts: 'dangerously', | ||
url: 'http://localhost/', | ||
beforeParse: window => { | ||
window.Element.prototype.matches = function (...args) { | ||
if (!args.length) { | ||
throw new window.TypeError('1 argument required, but only 0 present.'); | ||
} | ||
const [selector] = args; | ||
return matches(selector, this); | ||
}; | ||
window.Element.prototype.closest = function (...args) { | ||
if (!args.length) { | ||
throw new window.TypeError('1 argument required, but only 0 present.'); | ||
} | ||
const [selector] = args; | ||
return closest(selector, this); | ||
}; | ||
window.Document.prototype.querySelector = function (...args) { | ||
if (!args.length) { | ||
throw new window.TypeError('1 argument required, but only 0 present.'); | ||
} | ||
const [selector] = args; | ||
return querySelector(selector, this); | ||
}; | ||
window.DocumentFragment.prototype.querySelector = function (...args) { | ||
if (!args.length) { | ||
throw new window.TypeError('1 argument required, but only 0 present.'); | ||
} | ||
const [selector] = args; | ||
return querySelector(selector, this); | ||
}; | ||
window.Element.prototype.querySelector = function (...args) { | ||
if (!args.length) { | ||
throw new window.TypeError('1 argument required, but only 0 present.'); | ||
} | ||
const [selector] = args; | ||
return querySelector(selector, this); | ||
}; | ||
window.Document.prototype.querySelectorAll = function (...args) { | ||
if (!args.length) { | ||
throw new window.TypeError('1 argument required, but only 0 present.'); | ||
} | ||
const [selector] = args; | ||
return querySelectorAll(selector, this); | ||
}; | ||
window.DocumentFragment.prototype.querySelectorAll = function (...args) { | ||
if (!args.length) { | ||
throw new window.TypeError('1 argument required, but only 0 present.'); | ||
} | ||
const [selector] = args; | ||
return querySelectorAll(selector, this); | ||
}; | ||
window.Element.prototype.querySelectorAll = function (...args) { | ||
if (!args.length) { | ||
throw new window.TypeError('1 argument required, but only 0 present.'); | ||
} | ||
const [selector] = args; | ||
return querySelectorAll(selector, this); | ||
}; | ||
} | ||
}); | ||
``` | ||
## Performance | ||
### matches() | ||
|Selector|jsdom v24.0.0 (nwsapi)|happy-dom|linkeDom|patched-jsdom (dom-selector)|Result| | ||
|:-----------|:-----------|:-----------|:-----------|:-----------|:-----------| | ||
|simple selector:<br>`matches('.content')`|2,730,719 ops/sec ±0.80%|1,669,966 ops/sec ±0.28%|657,226 ops/sec ±1.30%|191,661 ops/sec ±0.17%|jsdom is the fastest and 14.2 times faster than patched-jsdom.| | ||
|compound selector:<br>`matches('p.content[id]:only-child')`|1,153,187 ops/sec ±0.26%|470,767 ops/sec ±0.85%|309,344 ops/sec ±0.18%|89,231 ops/sec ±0.70%|jsdom is the fastest and 12.9 times faster than patched-jsdom.| | ||
|complex selector:<br>`matches('.box:first-child ~ .box:nth-of-type(4n+1) + .box[id] .block.inner > .content')`|165,982 ops/sec ±0.46%|N/A|72,727 ops/sec ±0.25%|21,674 ops/sec ±1.35%|jsdom is the fastest and 7.7 times faster than patched-jsdom.| | ||
### closest() | ||
|Selector|jsdom v24.0.0 (nwsapi)|happy-dom|linkeDom|patched-jsdom (dom-selector)|Result| | ||
|:-----------|:-----------|:-----------|:-----------|:-----------|:-----------| | ||
|simple selector:<br>`closest('.container')`|578,462 ops/sec ±1.64%|361,946 ops/sec ±2.15%|397,660 ops/sec ±1.70%|66,355 ops/sec ±1.68%|jsdom is the fastest and 8.7 times faster than patched-jsdom.| | ||
|compound selector:<br>`closest('div.container[id]:not(.box)')`|216,725 ops/sec ±1.66%|79,273 ops/sec ±1.88%|169,185 ops/sec ±1.81%|33,135 ops/sec ±0.98%|jsdom is the fastest and 6.5 times faster than patched-jsdom.| | ||
|complex selector:<br>`closest('.box:first-child ~ .box:nth-of-type(4n+1) + .box[id] .block.inner > .content')`|159,848 ops/sec ±0.29%|N/A|72,070 ops/sec ±1.68%|17,559 ops/sec ±2.09%|jsdom is the fastest and 9.1 times faster than patched-jsdom.| | ||
### querySelector() | ||
|Selector|jsdom v24.0.0 (nwsapi)|happy-dom|linkeDom|patched-jsdom (dom-selector)|Result| | ||
|:-----------|:-----------|:-----------|:-----------|:-----------|:-----------| | ||
|simple selector:<br>`querySelector('.content')`|3,326 ops/sec ±1.31%|342,846 ops/sec ±1.70%|322,199 ops/sec ±2.38%|70,933 ops/sec ±1.88%|happydom is the fastest and 4.8 times faster than patched-jsdom.| | ||
|compound selector:<br>`querySelector('p.content[id]:only-child')`|1,309 ops/sec ±1.19%|382,636 ops/sec ±1.56%|252,823 ops/sec ±2.03%|47,759 ops/sec ±1.44%|happydom is the fastest and 8.0 times faster than patched-jsdom.| | ||
|complex selector:<br>`querySelector('.box:first-child ~ .box:nth-of-type(4n+1) + .box[id] .block.inner > .content')`|242 ops/sec ±0.16%|N/A|1,342 ops/sec ±0.37%|911 ops/sec ±1.80%|linkedom is the fastest and 1.5 times faster than patched-jsdom.| | ||
### querySelectorAll() | ||
|Selector|jsdom v24.0.0 (nwsapi)|happy-dom|linkeDom|patched-jsdom (dom-selector)|Result| | ||
|:-----------|:-----------|:-----------|:-----------|:-----------|:-----------| | ||
|simple selector:<br>`querySelectorAll('.content')`|3,090 ops/sec ±1.08%|778 ops/sec ±1.56%|1,203 ops/sec ±1.37%|2,483 ops/sec ±0.90%|jsdom is the fastest and 1.2 times faster than patched-jsdom.| | ||
|compound selector:<br>`querySelectorAll('p.content[id]:only-child')`|1,280 ops/sec ±0.96%|915 ops/sec ±1.57%|1,029 ops/sec ±0.89%|151 ops/sec ±0.91%|jsdom is the fastest and 8.5 times faster than patched-jsdom.| | ||
|complex selector:<br>`querySelectorAll('.box:first-child ~ .box:nth-of-type(4n+1) + .box[id] .block.inner > .content')`|245 ops/sec ±0.26%|N/A|438 ops/sec ±1.42%|904 ops/sec ±1.49%|patched-jsdom is the fastest.| | ||
## Acknowledgments | ||
@@ -174,0 +277,0 @@ |
@@ -235,5 +235,4 @@ /** | ||
const astName = unescapeSelector(ast.name); | ||
const { classList } = node; | ||
let res; | ||
if (classList.contains(astName)) { | ||
if (node.classList.contains(astName)) { | ||
res = node; | ||
@@ -253,5 +252,4 @@ } | ||
const astName = unescapeSelector(ast.name); | ||
const { id } = node; | ||
let res; | ||
if (astName === id) { | ||
if (astName === node.id) { | ||
res = node; | ||
@@ -278,4 +276,3 @@ } | ||
} = parseAstName(astName, node); | ||
const contentType = node.ownerDocument.contentType; | ||
if (contentType === 'text/html') { | ||
if (node.ownerDocument.contentType === 'text/html') { | ||
astPrefix = astPrefix.toLowerCase(); | ||
@@ -294,3 +291,3 @@ astLocalName = astLocalName.toLowerCase(); | ||
let res; | ||
const namespaceDeclared = isNamespaceDeclared(astPrefix, node); | ||
const namespaceDeclared = isNamespaceDeclared(nodePrefix, node); | ||
if (astPrefix === '' && nodePrefix === '') { | ||
@@ -305,16 +302,11 @@ if (namespaceURI === null && | ||
} | ||
} else if (astPrefix && nodePrefix) { | ||
if (astPrefix === nodePrefix) { | ||
if (namespaceDeclared) { | ||
if (astLocalName === '*' || astLocalName === nodeLocalName) { | ||
res = node; | ||
} | ||
} else if (!forgive) { | ||
const msg = `Undeclared namespace ${astPrefix}`; | ||
throw new DOMException(msg, SYNTAX_ERR); | ||
} else if (astPrefix === nodePrefix) { | ||
if (namespaceDeclared) { | ||
if (astLocalName === '*' || astLocalName === nodeLocalName) { | ||
res = node; | ||
} | ||
} else if (!forgive) { | ||
const msg = `Undeclared namespace ${astPrefix}`; | ||
throw new DOMException(msg, SYNTAX_ERR); | ||
} | ||
} else if (astPrefix && !forgive && !namespaceDeclared) { | ||
const msg = `Undeclared namespace ${astPrefix}`; | ||
throw new DOMException(msg, SYNTAX_ERR); | ||
} | ||
@@ -321,0 +313,0 @@ return res ?? null; |
@@ -5,3 +5,3 @@ export class Finder { | ||
private _correspond; | ||
_prepareTreeWalkers(): any[]; | ||
private _prepareTreeWalkers; | ||
private _traverse; | ||
@@ -19,3 +19,3 @@ private _collectNthChild; | ||
private _matchLeaves; | ||
_matchHTMLCollection(items: object, opt?: object): Set<object>; | ||
private _matchHTMLCollection; | ||
private _findDescendantNodes; | ||
@@ -22,0 +22,0 @@ private _matchCombinator; |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is too big to display
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
368913
4284
304