site/node_modules/@asamuzakjp/dom-selector/dist/cjs/js/matcher.js

3 lines
32 KiB
JavaScript
Raw Normal View History

2024-10-14 06:09:33 +00:00
var W=Object.create;var R=Object.defineProperty;var z=Object.getOwnPropertyDescriptor;var B=Object.getOwnPropertyNames;var j=Object.getPrototypeOf,V=Object.prototype.hasOwnProperty;var F=(T,s)=>{for(var e in s)R(T,e,{get:s[e],enumerable:!0})},I=(T,s,e,f)=>{if(s&&typeof s=="object"||typeof s=="function")for(let n of B(s))!V.call(T,n)&&n!==e&&R(T,n,{get:()=>s[n],enumerable:!(f=z(s,n))||f.enumerable});return T};var H=(T,s,e)=>(e=T!=null?W(j(T)):{},I(s||!T||!T.__esModule?R(e,"default",{value:T,enumerable:!0}):e,T)),G=T=>I(R({},"__esModule",{value:!0}),T);var Y={};F(Y,{Matcher:()=>q});module.exports=G(Y);var U=H(require("is-potential-custom-element-name"),1),S=require("./dom-util.js"),v=require("./parser.js"),i=require("./constant.js");const C="next",O="prev",A="all",L="first",$="lineal",M="self";class q{#i;#h;#r;#s;#a;#e;#o;#t;#c;#f;#l;#n;constructor(s,e,f={}){const{warn:n}=f;this.#h=new Map([[i.SELECTOR_PSEUDO_ELEMENT,i.BIT_01],[i.SELECTOR_ID,i.BIT_02],[i.SELECTOR_CLASS,i.BIT_04],[i.SELECTOR_TYPE,i.BIT_08],[i.SELECTOR_ATTR,i.BIT_16],[i.SELECTOR_PSEUDO_CLASS,i.BIT_32]]),this.#r=new WeakMap,this.#e=e,[this.#n,this.#s,this.#t,this.#f]=this._setup(e),this.#c=(0,S.isInShadowTree)(e),[this.#i,this.#o]=this._correspond(s),this.#l=!!n}_onError(s){if((s instanceof DOMException||s instanceof this.#n.DOMException)&&s.name===i.NOT_SUPPORTED_ERR)this.#l&&console.warn(s.message);else throw s instanceof DOMException?new this.#n.DOMException(s.message,s.name):s instanceof TypeError?new this.#n.TypeError(s.message):s}_setup(s){let e,f;switch(s?.nodeType){case i.DOCUMENT_NODE:{e=s,f=s;break}case i.DOCUMENT_FRAGMENT_NODE:{e=s.ownerDocument,f=s;break}case i.ELEMENT_NODE:{if(s.ownerDocument.contains(s))e=s.ownerDocument,f=s.ownerDocument;else{let c=s;for(;c&&c.parentNode;)c=c.parentNode;e=c.ownerDocument,f=c}break}default:{let c;throw s?.nodeName?c=`Unexpected node ${s.nodeName}`:c=`Unexpected node ${Object.prototype.toString.call(s).slice(i.TYPE_FROM,i.TYPE_TO)}`,new TypeError(c)}}const n=i.SHOW_DOCUMENT|i.SHOW_DOCUMENT_FRAGMENT|i.SHOW_ELEMENT,l=e.createTreeWalker(f,n);return[e.defaultView,e,f,l]}_sortLeaves(s){const e=[...s];return e.length>1&&e.sort((f,n)=>{const{type:l}=f,{type:b}=n,c=this.#h.get(l),d=this.#h.get(b);let t;return c===d?t=0:c>d?t=1:t=-1,t}),e}_correspond(s){let e;try{e=(0,v.parseSelector)(s)}catch(c){this._onError(c)}const f=(0,v.walkAST)(e),n=[],l=[];let b=0;for(const[...c]of f){const d=[];let t=c.shift();if(t&&t.type!==i.COMBINATOR){const r=new Set;for(;t;){if(t.type===i.COMBINATOR){const[a]=c;if(a.type===i.COMBINATOR){const o=`Invalid combinator ${t.name}${a.name}`;throw new this.#n.DOMException(o,i.SYNTAX_ERR)}d.push({combo:t,leaves:this._sortLeaves(r)}),r.clear()}else t&&r.add(t);if(c.length)t=c.shift();else{d.push({combo:null,leaves:this._sortLeaves(r)}),r.clear();break}}}n.push({branch:d,dir:null,filtered:!1,find:!1}),l[b]=new Set,b++}return[n,l]}_traverse(s={},e=this.#f){let f,n=e.currentNode;if(s.nodeType===i.ELEMENT_NODE&&n===s)f=n;else{if(n!==e.root)for(;n&&!(n===e.root||s.nodeType===i.ELEMENT_NODE&&n===s);)n=e.parentNode();if(s.nodeType===i.ELEMENT_NODE)for(;n;){if(n===s){f=n;break}n=e.nextNode()}else f=n}return f??null}_collectNthChild(s,e){const{a:f,b:n,reverse:l,selector:b}=s,{parentNode:c}=e;let d=new Set,t;if(b&&(this.#r.has(b)?t=this.#r.get(b):(t=(0,v.walkAST)(b),this.#r.set(b,t))),c){const r=i.SHOW_DOCUMENT|i.SHOW_DOCUMENT_FRAGMENT|i.SHOW_ELEMENT,a=this.#s.createTreeWalker(c,r);let o=0,h=a.firstChild();for(;h;)o++,h=a.nextSibling();h=this._traverse(c,a);const m=new Set;if(t)for(h=this._traverse(c,a),h=a.firstChild();h;){let p;for(const g of t)if(p=this._matchLeaves(g,h),!p)break;p&&m.add(h),h=a.nextSibling()}if(f===0){if(n>0&&n<=o){if(m.size){let p=0;for(h=this._traverse(c,a),l?h=a.lastChild():h=a.firstChild();h;){if(m.has(h)){if(p===n-1){d.add(h);break}p++}l?h=a.previousSibling():h=a.nextSibling()}}else if(!b){let p=0;for(h=this._traverse(c,a),l?h=a.lastChild():h=a.firstChild();h;){if(p===n-1){d.add(h);break}l?h=a.previousSibling():h=a.nextSibling(),p++}}}}else{let p=n-1;if(f>0)for(;p<0;)p+=f;if(p>=0&&p<o
//# sourceMappingURL=matcher.js.map