@asamuzakjp/dom-selector 1.1.0 → 1.1.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/cjs/js/constant.js +1 -1
- package/dist/cjs/js/constant.js.map +3 -3
- package/dist/cjs/js/dom-util.js +1 -1
- package/dist/cjs/js/dom-util.js.map +3 -3
- package/dist/cjs/js/matcher.js +1 -1
- package/dist/cjs/js/matcher.js.map +3 -3
- package/dist/cjs/js/parser.js +2 -2
- package/dist/cjs/js/parser.js.map +3 -3
- package/package.json +2 -2
- package/src/js/constant.js +23 -12
- package/src/js/dom-util.js +19 -23
- package/src/js/matcher.js +111 -111
- package/src/js/parser.js +33 -36
- package/types/js/constant.d.ts +21 -12
package/dist/cjs/js/constant.js
CHANGED
|
@@ -1,2 +1,2 @@
|
|
|
1
|
-
var
|
|
1
|
+
var E=Object.defineProperty;var s=Object.getOwnPropertyDescriptor;var c=Object.getOwnPropertyNames;var n=Object.prototype.hasOwnProperty;var T=(o,t)=>{for(var r in t)E(o,r,{get:t[r],enumerable:!0})},_=(o,t,r,x)=>{if(t&&typeof t=="object"||typeof t=="function")for(let e of c(t))!n.call(o,e)&&e!==r&&E(o,e,{get:()=>t[e],enumerable:!(x=s(t,e))||x.enumerable});return o};var p=o=>_(E({},"__esModule",{value:!0}),o);var V={};T(V,{ALPHA_NUM:()=>O,AN_PLUS_B:()=>S,BIT_01:()=>d,BIT_02:()=>i,BIT_04:()=>u,BIT_08:()=>W,BIT_16:()=>Y,BIT_32:()=>h,COMBINATOR:()=>N,DOCUMENT_FRAGMENT_NODE:()=>Z,DOCUMENT_NODE:()=>g,DOCUMENT_POSITION_CONTAINED_BY:()=>q,DOCUMENT_POSITION_CONTAINS:()=>k,DOCUMENT_POSITION_PRECEDING:()=>j,DUO:()=>X,ELEMENT_NODE:()=>y,HEX:()=>$,IDENTIFIER:()=>I,MAX_BIT_16:()=>b,NOT_SUPPORTED_ERR:()=>R,NTH:()=>D,RAW:()=>C,REG_LOGICAL_PSEUDO:()=>z,REG_SHADOW_HOST:()=>J,REG_SHADOW_MODE:()=>K,REG_SHADOW_PSEUDO:()=>Q,SELECTOR:()=>A,SELECTOR_ATTR:()=>L,SELECTOR_CLASS:()=>P,SELECTOR_ID:()=>l,SELECTOR_LIST:()=>M,SELECTOR_PSEUDO_CLASS:()=>U,SELECTOR_PSEUDO_ELEMENT:()=>F,SELECTOR_TYPE:()=>B,SHOW_ELEMENT:()=>v,STRING:()=>G,SYNTAX_ERR:()=>H,TEXT_NODE:()=>f,TYPE_FROM:()=>m,TYPE_TO:()=>w,U_FFFD:()=>a});module.exports=p(V);const O="[A-Z\\d]+",S="AnPlusB",N="Combinator",I="Identifier",R="NotSupportedError",D="Nth",C="Raw",A="Selector",L="AttributeSelector",P="ClassSelector",l="IdSelector",M="SelectorList",U="PseudoClassSelector",F="PseudoElementSelector",B="TypeSelector",G="String",H="SyntaxError",a="\uFFFD",d=1,i=2,u=4,W=8,Y=16,h=32,X=2,$=16,b=65535,m=8,w=-1,y=1,f=3,g=9,Z=11,j=2,k=8,q=16,v=1,z=/^(?:(?:ha|i)s|not|where)$/,J=/^host(?:-context)?$/,K=/^(?:close|open)$/,Q=/^part|slotted$/;0&&(module.exports={ALPHA_NUM,AN_PLUS_B,BIT_01,BIT_02,BIT_04,BIT_08,BIT_16,BIT_32,COMBINATOR,DOCUMENT_FRAGMENT_NODE,DOCUMENT_NODE,DOCUMENT_POSITION_CONTAINED_BY,DOCUMENT_POSITION_CONTAINS,DOCUMENT_POSITION_PRECEDING,DUO,ELEMENT_NODE,HEX,IDENTIFIER,MAX_BIT_16,NOT_SUPPORTED_ERR,NTH,RAW,REG_LOGICAL_PSEUDO,REG_SHADOW_HOST,REG_SHADOW_MODE,REG_SHADOW_PSEUDO,SELECTOR,SELECTOR_ATTR,SELECTOR_CLASS,SELECTOR_ID,SELECTOR_LIST,SELECTOR_PSEUDO_CLASS,SELECTOR_PSEUDO_ELEMENT,SELECTOR_TYPE,SHOW_ELEMENT,STRING,SYNTAX_ERR,TEXT_NODE,TYPE_FROM,TYPE_TO,U_FFFD});
|
|
2
2
|
//# sourceMappingURL=constant.js.map
|
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"version": 3,
|
|
3
3
|
"sources": ["../../../src/js/constant.js"],
|
|
4
|
-
"sourcesContent": ["/**\n * constant.js\n */\n\n/* string */\nexport const AN_PLUS_B = 'AnPlusB';\nexport const
|
|
5
|
-
"mappings": "4ZAAA,IAAAA,EAAA,GAAAC,EAAAD,EAAA,eAAAE,EAAA,
|
|
6
|
-
"names": ["constant_exports", "__export", "
|
|
4
|
+
"sourcesContent": ["/**\n * constant.js\n */\n\n/* string */\nexport const ALPHA_NUM = '[A-Z\\\\d]+';\nexport const AN_PLUS_B = 'AnPlusB';\nexport const COMBINATOR = 'Combinator';\nexport const IDENTIFIER = 'Identifier';\nexport const NOT_SUPPORTED_ERR = 'NotSupportedError';\nexport const NTH = 'Nth';\nexport const RAW = 'Raw';\nexport const SELECTOR = 'Selector';\nexport const SELECTOR_ATTR = 'AttributeSelector';\nexport const SELECTOR_CLASS = 'ClassSelector';\nexport const SELECTOR_ID = 'IdSelector';\nexport const SELECTOR_LIST = 'SelectorList';\nexport const SELECTOR_PSEUDO_CLASS = 'PseudoClassSelector';\nexport const SELECTOR_PSEUDO_ELEMENT = 'PseudoElementSelector';\nexport const SELECTOR_TYPE = 'TypeSelector';\nexport const STRING = 'String';\nexport const SYNTAX_ERR = 'SyntaxError';\nexport const U_FFFD = '\\uFFFD';\n\n/* numeric */\nexport const BIT_01 = 0x1;\nexport const BIT_02 = 0x2;\nexport const BIT_04 = 0x4;\nexport const BIT_08 = 0x8;\nexport const BIT_16 = 0x10;\nexport const BIT_32 = 0x20;\nexport const DUO = 2;\nexport const HEX = 16;\nexport const MAX_BIT_16 = 0xFFFF;\nexport const TYPE_FROM = 8;\nexport const TYPE_TO = -1;\n\n/* Node */\nexport const ELEMENT_NODE = 1;\nexport const TEXT_NODE = 3;\nexport const DOCUMENT_NODE = 9;\nexport const DOCUMENT_FRAGMENT_NODE = 11;\nexport const DOCUMENT_POSITION_PRECEDING = 0x2;\nexport const DOCUMENT_POSITION_CONTAINS = 0x8;\nexport const DOCUMENT_POSITION_CONTAINED_BY = 0x10;\n\n/* NodeFilter */\nexport const SHOW_ELEMENT = 0x1;\n\n/* regexp */\nexport const REG_LOGICAL_PSEUDO = /^(?:(?:ha|i)s|not|where)$/;\nexport const REG_SHADOW_HOST = /^host(?:-context)?$/;\nexport const REG_SHADOW_MODE = /^(?:close|open)$/;\nexport const REG_SHADOW_PSEUDO = /^part|slotted$/;\n"],
|
|
5
|
+
"mappings": "4ZAAA,IAAAA,EAAA,GAAAC,EAAAD,EAAA,eAAAE,EAAA,cAAAC,EAAA,WAAAC,EAAA,WAAAC,EAAA,WAAAC,EAAA,WAAAC,EAAA,WAAAC,EAAA,WAAAC,EAAA,eAAAC,EAAA,2BAAAC,EAAA,kBAAAC,EAAA,mCAAAC,EAAA,+BAAAC,EAAA,gCAAAC,EAAA,QAAAC,EAAA,iBAAAC,EAAA,QAAAC,EAAA,eAAAC,EAAA,eAAAC,EAAA,sBAAAC,EAAA,QAAAC,EAAA,QAAAC,EAAA,uBAAAC,EAAA,oBAAAC,EAAA,oBAAAC,EAAA,sBAAAC,EAAA,aAAAC,EAAA,kBAAAC,EAAA,mBAAAC,EAAA,gBAAAC,EAAA,kBAAAC,EAAA,0BAAAC,EAAA,4BAAAC,EAAA,kBAAAC,EAAA,iBAAAC,EAAA,WAAAC,EAAA,eAAAC,EAAA,cAAAC,EAAA,cAAAC,EAAA,YAAAC,EAAA,WAAAC,IAAA,eAAAC,EAAA3C,GAKO,MAAME,EAAY,YACZC,EAAY,UACZO,EAAa,aACbS,EAAa,aACbE,EAAoB,oBACpBC,EAAM,MACNC,EAAM,MACNK,EAAW,WACXC,EAAgB,oBAChBC,EAAiB,gBACjBC,EAAc,aACdC,EAAgB,eAChBC,EAAwB,sBACxBC,EAA0B,wBAC1BC,EAAgB,eAChBE,EAAS,SACTC,EAAa,cACbI,EAAS,SAGTtC,EAAS,EACTC,EAAS,EACTC,EAAS,EACTC,EAAS,EACTC,EAAS,GACTC,EAAS,GACTO,EAAM,EACNE,EAAM,GACNE,EAAa,MACboB,EAAY,EACZC,EAAU,GAGVxB,EAAe,EACfsB,EAAY,EACZ3B,EAAgB,EAChBD,EAAyB,GACzBI,EAA8B,EAC9BD,EAA6B,EAC7BD,EAAiC,GAGjCuB,EAAe,EAGfZ,EAAqB,4BACrBC,EAAkB,sBAClBC,EAAkB,mBAClBC,EAAoB",
|
|
6
|
+
"names": ["constant_exports", "__export", "ALPHA_NUM", "AN_PLUS_B", "BIT_01", "BIT_02", "BIT_04", "BIT_08", "BIT_16", "BIT_32", "COMBINATOR", "DOCUMENT_FRAGMENT_NODE", "DOCUMENT_NODE", "DOCUMENT_POSITION_CONTAINED_BY", "DOCUMENT_POSITION_CONTAINS", "DOCUMENT_POSITION_PRECEDING", "DUO", "ELEMENT_NODE", "HEX", "IDENTIFIER", "MAX_BIT_16", "NOT_SUPPORTED_ERR", "NTH", "RAW", "REG_LOGICAL_PSEUDO", "REG_SHADOW_HOST", "REG_SHADOW_MODE", "REG_SHADOW_PSEUDO", "SELECTOR", "SELECTOR_ATTR", "SELECTOR_CLASS", "SELECTOR_ID", "SELECTOR_LIST", "SELECTOR_PSEUDO_CLASS", "SELECTOR_PSEUDO_ELEMENT", "SELECTOR_TYPE", "SHOW_ELEMENT", "STRING", "SYNTAX_ERR", "TEXT_NODE", "TYPE_FROM", "TYPE_TO", "U_FFFD", "__toCommonJS"]
|
|
7
7
|
}
|
package/dist/cjs/js/dom-util.js
CHANGED
|
@@ -1,2 +1,2 @@
|
|
|
1
|
-
var y=Object.create;var
|
|
1
|
+
var y=Object.create;var p=Object.defineProperty;var E=Object.getOwnPropertyDescriptor;var x=Object.getOwnPropertyNames;var D=Object.getPrototypeOf,g=Object.prototype.hasOwnProperty;var h=(e,t)=>{for(var i in t)p(e,i,{get:t[i],enumerable:!0})},N=(e,t,i,r)=>{if(t&&typeof t=="object"||typeof t=="function")for(let n of x(t))!g.call(e,n)&&n!==i&&p(e,n,{get:()=>t[n],enumerable:!(r=E(t,n))||r.enumerable});return e};var O=(e,t,i)=>(i=e!=null?y(D(e)):{},N(t||!e||!e.__esModule?p(i,"default",{value:e,enumerable:!0}):i,e)),C=e=>N(p({},"__esModule",{value:!0}),e);var A={};h(A,{getDirectionality:()=>m,getSlottedTextContent:()=>u,isContentEditable:()=>T,isInShadowTree:()=>d,isNamespaceDeclared:()=>w,isSameOrDescendant:()=>_,selectorToNodeProps:()=>v});module.exports=C(A);var b=O(require("bidi-js"),1),s=require("./constant.js");const c=(0,b.default)(),d=(e={})=>{let t;if(e.nodeType===s.ELEMENT_NODE||e.nodeType===s.DOCUMENT_FRAGMENT_NODE){let i=e;for(;i;){const{host:r,mode:n,nodeType:l,parentNode:f}=i;if(r&&n&&l===s.DOCUMENT_FRAGMENT_NODE&&s.REG_SHADOW_MODE.test(n)){t=!0;break}i=f}}return!!t},u=(e={})=>{let t;if(e.localName==="slot"&&d(e)){const i=e.assignedNodes();if(i.length){for(const r of i)if(t=r.textContent.trim(),t)break}else t=e.textContent.trim()}return t??null},m=(e={})=>{let t;if(e.nodeType===s.ELEMENT_NODE){const{dir:i,localName:r,parentNode:n}=e,l="ltr",f="rtl";if(/^(?:ltr|rtl)$/.test(i))t=i;else if(i==="auto"){let o;if(r==="textarea"||r==="input"&&(!e.type||/^(?:(?:butto|hidde)n|(?:emai|te|ur)l|(?:rese|submi|tex)t|password|search)$/.test(e.type))?o=e.value:r==="slot"?o=u(e):o=e.textContent.trim(),o){const{paragraphs:[{level:a}]}=c.getEmbeddingLevels(o);a%2===1?t=f:t=l}if(!t)if(n){const{nodeType:a}=n;a===s.ELEMENT_NODE?t=m(n):(a===s.DOCUMENT_NODE||a===s.DOCUMENT_FRAGMENT_NODE)&&(t=l)}else t=l}else if(r==="bdi"){const o=e.textContent.trim();if(o){const{paragraphs:[{level:a}]}=c.getEmbeddingLevels(o);a%2===1?t=f:t=l}t||n||(t=l)}else if(r==="input"&&e.type==="tel")t=l;else if(n){if(r==="slot"){const o=u(e);if(o){const{paragraphs:[{level:a}]}=c.getEmbeddingLevels(o);a%2===1?t=f:t=l}}if(!t){const{nodeType:o}=n;o===s.ELEMENT_NODE?t=m(n):(o===s.DOCUMENT_NODE||o===s.DOCUMENT_FRAGMENT_NODE)&&(t=l)}}else t=l}return t??null},T=(e={})=>{let t;if(e.nodeType===s.ELEMENT_NODE){if(typeof e.isContentEditable=="boolean")t=e.isContentEditable;else if(e.ownerDocument.designMode==="on")t=!0;else if(e.hasAttribute("contenteditable")){const i=e.getAttribute("contenteditable");if(i===""||/^(?:plaintext-only|true)$/.test(i))t=!0;else if(i==="inherit"){let r=e.parentNode;for(;r;){if(T(r)){t=!0;break}r=r.parentNode}}}}return!!t},w=(e="",t={})=>{let i;if(e&&typeof e=="string"&&t.nodeType===s.ELEMENT_NODE){const r=`xmlns:${e}`,n=t.ownerDocument.documentElement;let l=t;for(;l;){if(typeof l.hasAttribute=="function"&&l.hasAttribute(r)){i=!0;break}else if(l===n)break;l=l.parentNode}}return!!i},_=(e={},t={})=>{let i;return e.ownerDocument&&e.nodeType===s.ELEMENT_NODE&&((!t||t.nodeType!==s.ELEMENT_NODE)&&(t=e.ownerDocument),e===t?i=!0:t&&(i=t.compareDocumentPosition(e)&s.DOCUMENT_POSITION_CONTAINED_BY)),!!i},v=(e,t)=>{let i,r;if(e&&typeof e=="string")/\|/.test(e)?[i,r]=e.split("|"):(i="*",r=e);else throw new DOMException(`invalid selector ${e}`,s.SYNTAX_ERR);return{prefix:i,tagName:r}};0&&(module.exports={getDirectionality,getSlottedTextContent,isContentEditable,isInShadowTree,isNamespaceDeclared,isSameOrDescendant,selectorToNodeProps});
|
|
2
2
|
//# sourceMappingURL=dom-util.js.map
|
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"version": 3,
|
|
3
3
|
"sources": ["../../../src/js/dom-util.js"],
|
|
4
|
-
"sourcesContent": ["/**\n * dom-util.js\n */\n\n/* import */\nimport bidiFactory from 'bidi-js';\n\n/* constants */\nimport {\n
|
|
5
|
-
"mappings": "6iBAAA,IAAAA,EAAA,GAAAC,EAAAD,EAAA,uBAAAE,EAAA,0BAAAC,EAAA,sBAAAC,EAAA,mBAAAC,EAAA,wBAAAC,EAAA,uBAAAC,EAAA,wBAAAC,IAAA,eAAAC,EAAAT,GAKA,IAAAU,EAAwB,wBAGxBC,EAGO,
|
|
6
|
-
"names": ["dom_util_exports", "__export", "getDirectionality", "getSlottedTextContent", "isContentEditable", "isInShadowTree", "isNamespaceDeclared", "isSameOrDescendant", "selectorToNodeProps", "__toCommonJS", "import_bidi_js", "import_constant", "
|
|
4
|
+
"sourcesContent": ["/**\n * dom-util.js\n */\n\n/* import */\nimport bidiFactory from 'bidi-js';\n\n/* constants */\nimport {\n DOCUMENT_FRAGMENT_NODE, DOCUMENT_NODE, DOCUMENT_POSITION_CONTAINED_BY,\n ELEMENT_NODE, REG_SHADOW_MODE, SYNTAX_ERR\n} from './constant.js';\n\n/* bidi */\nconst bidi = bidiFactory();\n\n/**\n * is in shadow tree\n * @param {object} node - node\n * @returns {boolean} - result;\n */\nexport const isInShadowTree = (node = {}) => {\n let bool;\n if (node.nodeType === ELEMENT_NODE ||\n node.nodeType === DOCUMENT_FRAGMENT_NODE) {\n let refNode = node;\n while (refNode) {\n const { host, mode, nodeType, parentNode } = refNode;\n if (host && mode && nodeType === DOCUMENT_FRAGMENT_NODE &&\n REG_SHADOW_MODE.test(mode)) {\n bool = true;\n break;\n }\n refNode = parentNode;\n }\n }\n return !!bool;\n};\n\n/**\n * get slotted text content\n * @param {object} node - Element node\n * @returns {?string} - text content\n */\nexport const getSlottedTextContent = (node = {}) => {\n let res;\n if (node.localName === 'slot' && isInShadowTree(node)) {\n const nodes = node.assignedNodes();\n if (nodes.length) {\n for (const item of nodes) {\n res = item.textContent.trim();\n if (res) {\n break;\n }\n }\n } else {\n res = node.textContent.trim();\n }\n }\n return res ?? null;\n};\n\n/**\n * get directionality of node\n * @see https://html.spec.whatwg.org/multipage/dom.html#the-dir-attribute\n * @param {object} node - Element node\n * @returns {?string} - 'ltr' / 'rtl'\n */\nexport const getDirectionality = (node = {}) => {\n let res;\n if (node.nodeType === ELEMENT_NODE) {\n const { dir: nodeDir, localName, parentNode } = node;\n const ltr = 'ltr';\n const rtl = 'rtl';\n if (/^(?:ltr|rtl)$/.test(nodeDir)) {\n res = nodeDir;\n } else if (nodeDir === 'auto') {\n let text;\n if (localName === 'textarea') {\n text = node.value;\n } else if (localName === 'input' &&\n (!node.type ||\n /^(?:(?:butto|hidde)n|(?:emai|te|ur)l|(?:rese|submi|tex)t|password|search)$/.test(node.type))) {\n text = node.value;\n } else if (localName === 'slot') {\n text = getSlottedTextContent(node);\n } else {\n text = node.textContent.trim();\n }\n if (text) {\n const { paragraphs: [{ level }] } = bidi.getEmbeddingLevels(text);\n if (level % 2 === 1) {\n res = rtl;\n } else {\n res = ltr;\n }\n }\n if (!res) {\n if (parentNode) {\n const { nodeType: parentNodeType } = parentNode;\n if (parentNodeType === ELEMENT_NODE) {\n res = getDirectionality(parentNode);\n } else if (parentNodeType === DOCUMENT_NODE ||\n parentNodeType === DOCUMENT_FRAGMENT_NODE) {\n res = ltr;\n }\n } else {\n res = ltr;\n }\n }\n } else if (localName === 'bdi') {\n const text = node.textContent.trim();\n if (text) {\n const { paragraphs: [{ level }] } = bidi.getEmbeddingLevels(text);\n if (level % 2 === 1) {\n res = rtl;\n } else {\n res = ltr;\n }\n }\n if (!(res || parentNode)) {\n res = ltr;\n }\n } else if (localName === 'input' && node.type === 'tel') {\n res = ltr;\n } else if (parentNode) {\n if (localName === 'slot') {\n const text = getSlottedTextContent(node);\n if (text) {\n const { paragraphs: [{ level }] } = bidi.getEmbeddingLevels(text);\n if (level % 2 === 1) {\n res = rtl;\n } else {\n res = ltr;\n }\n }\n }\n if (!res) {\n const { nodeType: parentNodeType } = parentNode;\n if (parentNodeType === ELEMENT_NODE) {\n res = getDirectionality(parentNode);\n } else if (parentNodeType === DOCUMENT_NODE ||\n parentNodeType === DOCUMENT_FRAGMENT_NODE) {\n res = ltr;\n }\n }\n } else {\n res = ltr;\n }\n }\n return res ?? null;\n};\n\n/**\n * is content editable\n * NOTE: not implemented in jsdom https://github.com/jsdom/jsdom/issues/1670\n * @param {object} node - Element node\n * @returns {boolean} - result\n */\nexport const isContentEditable = (node = {}) => {\n let res;\n if (node.nodeType === ELEMENT_NODE) {\n if (typeof node.isContentEditable === 'boolean') {\n res = node.isContentEditable;\n } else if (node.ownerDocument.designMode === 'on') {\n res = true;\n } else if (node.hasAttribute('contenteditable')) {\n const attr = node.getAttribute('contenteditable');\n if (attr === '' || /^(?:plaintext-only|true)$/.test(attr)) {\n res = true;\n } else if (attr === 'inherit') {\n let parent = node.parentNode;\n while (parent) {\n if (isContentEditable(parent)) {\n res = true;\n break;\n }\n parent = parent.parentNode;\n }\n }\n }\n }\n return !!res;\n};\n\n/**\n * is namespace declared\n * @param {string} ns - namespace\n * @param {object} node - Element node\n * @returns {boolean} - result\n */\nexport const isNamespaceDeclared = (ns = '', node = {}) => {\n let res;\n if (ns && typeof ns === 'string' && node.nodeType === ELEMENT_NODE) {\n const attr = `xmlns:${ns}`;\n const root = node.ownerDocument.documentElement;\n let parent = node;\n while (parent) {\n if (typeof parent.hasAttribute === 'function' &&\n parent.hasAttribute(attr)) {\n res = true;\n break;\n } else if (parent === root) {\n break;\n }\n parent = parent.parentNode;\n }\n }\n return !!res;\n};\n\n/**\n * is node same or descendant of the root node\n * @param {object} node - Element node\n * @param {object} root - Document, DocumentFragment, Element node\n * @returns {boolean} - result\n */\nexport const isSameOrDescendant = (node = {}, root = {}) => {\n let res;\n if (node.ownerDocument && node.nodeType === ELEMENT_NODE) {\n if (!root || root.nodeType !== ELEMENT_NODE) {\n root = node.ownerDocument;\n }\n if (node === root) {\n res = true;\n } else if (root) {\n res = root.compareDocumentPosition(node) & DOCUMENT_POSITION_CONTAINED_BY;\n }\n }\n return !!res;\n};\n\n/**\n * selector to node properties - e.g. ns|E -> { prefix: ns, tagName: E }\n * @param {string} selector - type selector\n * @param {object} [node] - Element node\n * @returns {object} - node properties\n */\nexport const selectorToNodeProps = (selector, node) => {\n let prefix;\n let tagName;\n if (selector && typeof selector === 'string') {\n if (/\\|/.test(selector)) {\n [prefix, tagName] = selector.split('|');\n } else {\n prefix = '*';\n tagName = selector;\n }\n } else {\n throw new DOMException(`invalid selector ${selector}`, SYNTAX_ERR);\n }\n return {\n prefix,\n tagName\n };\n};\n"],
|
|
5
|
+
"mappings": "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",
|
|
6
|
+
"names": ["dom_util_exports", "__export", "getDirectionality", "getSlottedTextContent", "isContentEditable", "isInShadowTree", "isNamespaceDeclared", "isSameOrDescendant", "selectorToNodeProps", "__toCommonJS", "import_bidi_js", "import_constant", "bidi", "bidiFactory", "node", "bool", "refNode", "host", "mode", "nodeType", "parentNode", "res", "nodes", "item", "nodeDir", "localName", "ltr", "rtl", "text", "level", "parentNodeType", "attr", "parent", "ns", "root", "selector", "prefix", "tagName"]
|
|
7
7
|
}
|
package/dist/cjs/js/matcher.js
CHANGED
|
@@ -1,2 +1,2 @@
|
|
|
1
|
-
var W=Object.create;var L=Object.defineProperty;var q=Object.getOwnPropertyDescriptor;var Y=Object.getOwnPropertyNames;var X=Object.getPrototypeOf,K=Object.prototype.hasOwnProperty;var Z=(_,i)=>{for(var e in i)L(_,e,{get:i[e],enumerable:!0})},R=(_,i,e,c)=>{if(i&&typeof i=="object"||typeof i=="function")for(let d of Y(i))!K.call(_,d)&&d!==e&&L(_,d,{get:()=>i[d],enumerable:!(c=q(i,d))||c.enumerable});return _};var J=(_,i,e)=>(e=_!=null?W(X(_)):{},R(i||!_||!_.__esModule?L(e,"default",{value:_,enumerable:!0}):e,_)),Q=_=>R(L({},"__esModule",{value:!0}),_);var ie={};Z(ie,{Matcher:()=>se});module.exports=Q(ie);var P=J(require("is-potential-custom-element-name"),1),k=require("./dom-util.js"),g=require("./parser.js"),h=require("./constant.js");const D="[A-Z\\d]+",A=`(?:-${D})*`,$="all",T="first",S="lineal",v="self",U=/^(?:(?:fieldse|inpu|selec)t|button|opt(?:group|ion)|textarea)$/,C=/^(?:(?:(?:in|out)pu|selec)t|button|form|textarea)$/,B=/^a(?:rea)?$/,z=/^d(?:etails|ialog)$/,O=/^(?:checkbox|radio)$/,x=/^(?:(?:emai|te|ur)l|number|password|search|text)$/,H=/(?:(?:rang|tim)e|date(?:time-local)?|month|number|week)$/,F=/^(?:button|reset)$/,V=/^(?:image|submit)$/,I=/^(?:date(?:time-local)?|month|time|week)$/,j=/^(?:(?:ha|i)s|not|where)$/,ee=/^nth-(?:last-)?(?:child|of-type)$/,G=/^host(?:-context)?$/,te=new RegExp(`^(?:\\*-)?${D}${A}$`,"i");class se{#i;#r;#a;#e;#l;#t;#o;#c;#s;constructor(i,e,c={}){const{sort:d,warn:r}=c;this.#r=new Map([[h.PSEUDO_ELEMENT_SELECTOR,h.BIT_1],[h.ID_SELECTOR,h.BIT_10],[h.CLASS_SELECTOR,h.BIT_100],[h.TYPE_SELECTOR,h.BIT_1000],[h.ATTRIBUTE_SELECTOR,h.BIT_10000],[h.PSEUDO_CLASS_SELECTOR,h.BIT_100000]]),this.#a=new WeakMap,this.#o=i,this.#e=e,this.#c=!!d,this.#s=!!r,[this.#i,this.#l]=this._prepare(i),this.#t=this._getRoot(e)}_onError(i){if(i instanceof DOMException&&i.name===h.NOT_SUPPORTED_ERR)this.#s&&console.warn(i.message);else throw i}_getRoot(i=this.#e){let e,c;switch(i.nodeType){case h.DOCUMENT_NODE:{e=i,c=i;break}case h.DOCUMENT_FRAGMENT_NODE:{e=i.ownerDocument,c=i;break}case h.ELEMENT_NODE:{if((0,k.isSameOrDescendant)(i))e=i.ownerDocument,c=i.ownerDocument;else{let r=i;for(;r&&r.parentNode;)r=r.parentNode;e=r.ownerDocument,c=r}break}default:throw new TypeError(`Unexpected node ${i.nodeName}`)}const d=(0,k.isInShadowTree)(i);return{document:e,root:c,shadow:d}}_sortLeaves(i){const e=[...i];return e.length>1&&e.sort((c,d)=>{const{type:r}=c,{type:b}=d,m=this.#r.get(r),f=this.#r.get(b);let s;return m===f?s=0:m>f?s=1:s=-1,s}),e}_prepare(i=this.#o){const e=(0,g.parseSelector)(i),c=(0,g.walkAST)(e),d=[],r=[];let b=0;for(const[...m]of c){const f=[];let s=m.shift();if(s&&s.type!==h.COMBINATOR){const l=new Set;for(;s;){if(s.type===h.COMBINATOR){const[o]=m;if(o.type===h.COMBINATOR){const u=`Invalid combinator ${s.name}${o.name}`;throw new DOMException(u,h.SYNTAX_ERR)}f.push({combo:s,leaves:this._sortLeaves(l)}),l.clear()}else s&&l.add(s);if(m.length)s=m.shift();else{f.push({combo:null,leaves:this._sortLeaves(l)}),l.clear();break}}}d.push({branch:f,find:null,skip:!1}),r[b]=new Set,b++}return[d,r]}_collectNthChild(i,e){const{a:c,b:d,reverse:r,selector:b}=i,{parentNode:m}=e,f=new Set;let s;if(b&&(this.#a.has(b)?s=this.#a.get(b):(s=(0,g.walkAST)(b),this.#a.set(b,s))),m){const l=[...m.children],o=l.length;if(o){const u=new Set;if(s){const n=s.length;for(const t of l){let a;for(let p=0;p<n;p++){const N=s[p];if(a=this._matchLeaves(N,t),!a)break}a&&u.add(t)}}if(r&&l.reverse(),c===0){if(d>0&&d<=o){if(u.size)for(let n=0;n<o;n++){const t=l[n];if(u.has(t)){f.add(t);break}}else if(!b){const n=l[d-1];f.add(n)}}}else{let n=d-1;if(c>0)for(;n<0;)n+=c;if(n>=0&&n<o){let t=c>0?0:d-1;for(let a=0;a<o&&n>=0&&n<o;a++){const p=l[a];u.size?u.has(p)&&(t===n&&(f.add(p),n+=c),c>0?t++:t--):a===n&&(b||f.add(p),n+=c)}}}}}else{const{root:l}=this.#t;if(e===l&&l.nodeType===h.ELEMENT_NODE&&c+d===1)if(s){const o=s.length;let u;for(let n=0;n<o;n++){const t=s[n];if(u=this._matchLeaves(t,e),u)break}u&&f.add(e)}else f.add(e)}return f}_collectNthOfType(i,e){const{a:c,b:d,reverse:r}=i,{localName:b,parentNode:m,prefix:f}=e,s=new Set;if(m){const l=[...m.children],o=l.length;if(o)if(r&&l.reverse(),c===0){if(d>0&&d<=o){let u=0;for(let n=0;n<o;n++){const t=l[n],{localName:a,prefix:p}=t;if(a===b&&p===f){if(u===d-1){s.add(t);break}u++}}}}else{let u=d-1;if(c>0)for(;u<0;)u+=c;if(u>=0&&u<o){let n=c>0?0:d-1;for(let t=0;t<o;t++){const a=l[t],{localName:p,prefix:N}=a;if(p===b&&N===f){if(n===u&&(s.add(a),u+=c),u<0||u>=o)break;c>0?n++:n--}}}}}else{const{root:l}=this.#t;e===l&&l.nodeType===h.ELEMENT_NODE&&c+d===1&&s.add(e)}return s}_matchAnPlusB(i,e,c){const{nth:{a:d,b:r,name:b},selector:m}=i,f=(0,g.unescapeSelector)(b),s=new Map;f?(f==="even"?(s.set("a",2),s.set("b",0)):f==="odd"&&(s.set("a",2),s.set("b",1)),/last/.test(c)&&s.set("reverse",!0)):(typeof d=="string"&&/-?\d+/.test(d)?s.set("a",d*1):s.set("a",0),typeof r=="string"&&/-?\d+/.test(r)?s.set("b",r*1):s.set("b",0),/last/.test(c)&&s.set("reverse",!0));let l=new Set;if(s.has("a")&&s.has("b")){if(/^nth-(?:last-)?child$/.test(c)){m&&s.set("selector",m);const o=Object.fromEntries(s),u=this._collectNthChild(o,e);u.size&&(l=u)}else if(/^nth-(?:last-)?of-type$/.test(c)){const o=Object.fromEntries(s),u=this._collectNthOfType(o,e);u.size&&(l=u)}}return l}_matchPseudoElementSelector(i,e={}){const{forgive:c}=e;switch(i){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(this.#s)throw new DOMException(`Unsupported pseudo-element ::${i}`,h.NOT_SUPPORTED_ERR);break}case"part":case"slotted":{if(this.#s)throw new DOMException(`Unsupported pseudo-element ::${i}()`,h.NOT_SUPPORTED_ERR);break}default:if(i.startsWith("-webkit-")){if(this.#s)throw new DOMException(`Unsupported pseudo-element ::${i}`,h.NOT_SUPPORTED_ERR)}else if(!c)throw new DOMException(`Unknown pseudo-element ::${i}`,h.SYNTAX_ERR)}}_matchDirectionPseudoClass(i,e){const c=(0,g.unescapeSelector)(i.name),d=(0,k.getDirectionality)(e);let r;return c===d&&(r=e),r??null}_matchLanguagePseudoClass(i,e){const c=(0,g.unescapeSelector)(i.name);let d;if(c){if(c==="*")if(e.hasAttribute("lang"))e.getAttribute("lang")&&(d=e);else{let r=e.parentNode;for(;r;){if(r.hasAttribute("lang")){r.getAttribute("lang")&&(d=e);break}r=r.parentNode}}else if(te.test(c)){let r;if(/-/.test(c)){const[b,m,...f]=c.split("-");let s;b==="*"?s=`${D}${A}`:s=`${b}${A}`;const l=`-${m}${A}`,o=f.length;let u="";if(o)for(let n=0;n<o;n++)u+=`-${f[n]}${A}`;r=new RegExp(`^${s}${l}${u}$`,"i")}else r=new RegExp(`^${c}${A}$`,"i");if(e.hasAttribute("lang"))r.test(e.getAttribute("lang"))&&(d=e);else{let b=e.parentNode;for(;b;){if(b.hasAttribute("lang")){const m=b.getAttribute("lang");r.test(m)&&(d=e);break}b=b.parentNode}}}}return d??null}_matchHasPseudoFunc(i,e){let c;if(Array.isArray(i)&&i.length){const[d]=i,{type:r}=d;let b;r===h.COMBINATOR?b=i.shift():b={name:" ",type:h.COMBINATOR};const m=[];for(;i.length;){const[l]=i,{type:o}=l;if(o===h.COMBINATOR)break;m.push(i.shift())}const f={combo:b,leaves:m},s=this._matchCombinator(f,e,{find:"next"});if(s.size)if(i.length){for(const l of s)if(c=this._matchHasPseudoFunc(Object.assign([],i),l),c)break}else c=!0}return!!c}_matchLogicalPseudoFunc(i,e){const{astName:c="",branches:d=[],selector:r="",twigBranches:b=[]}=i;let m;if(c==="has")if(r.includes(":has("))m=null;else{const f=d.length;let s;for(let l=0;l<f;l++){const o=d[l];if(s=this._matchHasPseudoFunc(Object.assign([],o),e),s)break}s&&(m=e)}else{const f=/^(?:is|where)$/.test(c),s=b.length;let l;for(let o=0;o<s;o++){const u=b[o],n=u.length-1,{leaves:t}=u[n];if(l=this._matchLeaves(t,e,{forgive:f}),l&&n>0){let a=new Set([e]);for(let p=n-1;p>=0;p--){const N=u[p],w=[];for(const y of a){const E=this._matchCombinator(N,y,{forgive:f,find:"prev"});E.size&&w.push(...E)}if(w.length)if(p===0){l=!0;break}else a=new Set(w);else{l=!1;break}}}if(l)break}c==="not"?l||(m=e):l&&(m=e)}return m??null}_matchPseudoClassSelector(i,e,c={}){const{children:d}=i,{localName:r,parentNode:b}=e,{forgive:m}=c,f=(0,g.unescapeSelector)(i.name);let s=new Set;if(j.test(f)){let l;if(this.#a.has(i))l=this.#a.get(i);else{const u=(0,g.walkAST)(i),n=[],t=[];for(const[...a]of u){for(const y of a){const E=(0,g.generateCSS)(y);n.push(E)}const p=[],N=new Set;let w=a.shift();for(;w;)if(w.type===h.COMBINATOR?(p.push({combo:w,leaves:[...N]}),N.clear()):w&&N.add(w),a.length)w=a.shift();else{p.push({combo:null,leaves:[...N]}),N.clear();break}t.push(p)}l={astName:f,branches:u,twigBranches:t,selector:n.join(",")},this.#a.set(i,l)}const o=this._matchLogicalPseudoFunc(l,e);o&&s.add(o)}else if(Array.isArray(d)){const[l]=d;if(ee.test(f)){const o=this._matchAnPlusB(l,e,f);o.size&&(s=o)}else if(f==="dir"){const o=this._matchDirectionPseudoClass(l,e);o&&s.add(o)}else if(f==="lang"){const o=this._matchLanguagePseudoClass(l,e);o&&s.add(o)}else switch(f){case"current":case"nth-col":case"nth-last-col":{if(this.#s)throw new DOMException(`Unsupported pseudo-class :${f}()`,h.NOT_SUPPORTED_ERR);break}default:if(!m)throw new DOMException(`Unknown pseudo-class :${f}()`,h.SYNTAX_ERR)}}else{const{document:l,root:o}=this.#t,{documentElement:u}=l,n=new URL(l.URL);switch(f){case"any-link":case"link":{B.test(r)&&e.hasAttribute("href")&&s.add(e);break}case"local-link":{if(B.test(r)&&e.hasAttribute("href")){const t=new URL(e.getAttribute("href"),n.href);t.origin===n.origin&&t.pathname===n.pathname&&s.add(e)}break}case"visited":break;case"target":{e.id&&n.hash&&n.hash===`#${e.id}`&&(0,k.isSameOrDescendant)(e)&&s.add(e);break}case"target-within":{if(n.hash){const t=n.hash.replace(/^#/,"");let a=l.getElementById(t);for(;a;){if(a===e){s.add(e);break}a=a.parentNode}}break}case"scope":{this.#e.nodeType===h.ELEMENT_NODE?e===this.#e&&s.add(e):e===u&&s.add(e);break}case"focus":{e===l.activeElement&&s.add(e);break}case"focus-within":{let t=l.activeElement;for(;t;){if(t===e){s.add(e);break}t=t.parentNode}break}case"open":{z.test(r)&&e.hasAttribute("open")&&s.add(e);break}case"closed":{z.test(r)&&!e.hasAttribute("open")&&s.add(e);break}case"disabled":{if(U.test(r)||(0,P.default)(r))if(e.disabled||e.hasAttribute("disabled"))s.add(e);else{let t=b;for(;t&&t.localName!=="fieldset";)t=t.parentNode;t&&b.localName!=="legend"&&t.hasAttribute("disabled")&&s.add(e)}break}case"enabled":{(U.test(r)||(0,P.default)(r))&&!(e.disabled&&e.hasAttribute("disabled"))&&s.add(e);break}case"read-only":{switch(r){case"textarea":{(e.readonly||e.hasAttribute("readonly")||e.disabled||e.hasAttribute("disabled"))&&s.add(e);break}case"input":{(!e.type||x.test(e.type)||I.test(e.type))&&(e.readonly||e.hasAttribute("readonly")||e.disabled||e.hasAttribute("disabled"))&&s.add(e);break}default:(0,k.isContentEditable)(e)||s.add(e)}break}case"read-write":{switch(r){case"textarea":{e.readonly||e.hasAttribute("readonly")||e.disabled||e.hasAttribute("disabled")||s.add(e);break}case"input":{(!e.type||x.test(e.type)||I.test(e.type))&&!(e.readonly||e.hasAttribute("readonly")||e.disabled||e.hasAttribute("disabled"))&&s.add(e);break}default:(0,k.isContentEditable)(e)&&s.add(e)}break}case"placeholder-shown":{let t;r==="textarea"?t=e:r==="input"&&(e.hasAttribute("type")?x.test(e.getAttribute("type"))&&(t=e):t=e),t&&e.value===""&&e.hasAttribute("placeholder")&&e.getAttribute("placeholder").trim().length&&s.add(e);break}case"checked":{(e.checked&&r==="input"&&e.hasAttribute("type")&&O.test(e.getAttribute("type"))||e.selected&&r==="option")&&s.add(e);break}case"indeterminate":{if(e.indeterminate&&r==="input"&&e.type==="checkbox"||r==="progress"&&!e.hasAttribute("value"))s.add(e);else if(r==="input"&&e.type==="radio"&&!e.hasAttribute("checked")){const t=e.name;let a=e.parentNode;for(;a&&a.localName!=="form";)a=a.parentNode;a||(a=u);const p=[...a.getElementsByTagName("input")];let N;for(const w of p)if(w.getAttribute("type")==="radio"&&(t?w.getAttribute("name")===t&&(N=!!w.checked):w.hasAttribute("name")||(N=!!w.checked),N))break;N||s.add(e)}break}case"default":{if(r==="button"&&!(e.hasAttribute("type")&&F.test(e.getAttribute("type")))||r==="input"&&e.hasAttribute("type")&&V.test(e.getAttribute("type"))){let t=e.parentNode;for(;t&&t.localName!=="form";)t=t.parentNode;if(t){const a=l.createNodeIterator(t,h.SHOW_ELEMENT);let p=a.nextNode();for(;p;){const N=p.localName;let w;if(N==="button"?w=!(p.hasAttribute("type")&&F.test(p.getAttribute("type"))):N==="input"&&(w=p.hasAttribute("type")&&V.test(p.getAttribute("type"))),w){p===e&&s.add(e);break}p=a.nextNode()}}}else if(r==="input"&&e.hasAttribute("type")&&O.test(e.getAttribute("type"))&&(e.checked||e.hasAttribute("checked")))s.add(e);else if(r==="option"){let t=!1,a=b;for(;a&&a.localName!=="datalist";){if(a.localName==="select"){(a.multiple||a.hasAttribute("multiple"))&&(t=!0);break}a=a.parentNode}if(t)(e.selected||e.hasAttribute("selected"))&&s.add(e);else{const p=b.firstElementChild,N=new Set;let w=p;for(;w;){if(w.selected||w.hasAttribute("selected")){N.add(w);break}w=w.nextElementSibling}N.size||N.add(p),N.has(e)&&s.add(e)}}break}case"valid":{if(C.test(r))e.checkValidity()&&s.add(e);else if(/^fieldset$/.test(r)){const t=l.createNodeIterator(e,h.SHOW_ELEMENT);let a=t.nextNode();a===e&&(a=t.nextNode());let p;for(;a&&!(C.test(a.localName)&&(p=a.checkValidity(),!p));)a=t.nextNode();p&&s.add(e)}break}case"invalid":{if(C.test(r))e.checkValidity()||s.add(e);else if(/^fieldset$/.test(r)){const t=l.createNodeIterator(e,h.SHOW_ELEMENT);let a=t.nextNode();a===e&&(a=t.nextNode());let p;for(;a&&!(C.test(a.localName)&&(p=a.checkValidity(),!p));)a=t.nextNode();p||s.add(e)}break}case"in-range":{r==="input"&&!(e.readonly||e.hasAttribute("readonly"))&&!(e.disabled||e.hasAttribute("disabled"))&&e.hasAttribute("type")&&H.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":{r==="input"&&!(e.readonly||e.hasAttribute("readonly"))&&!(e.disabled||e.hasAttribute("disabled"))&&e.hasAttribute("type")&&H.test(e.getAttribute("type"))&&(e.validity.rangeUnderflow||e.validity.rangeOverflow)&&s.add(e);break}case"required":{let t;if(/^(?:select|textarea)$/.test(r))t=e;else if(r==="input")if(e.hasAttribute("type")){const a=e.getAttribute("type");(a==="file"||x.test(a)||O.test(a)||I.test(a))&&(t=e)}else t=e;t&&(e.required||e.hasAttribute("required"))&&s.add(e);break}case"optional":{let t;if(/^(?:select|textarea)$/.test(r))t=e;else if(r==="input")if(e.hasAttribute("type")){const a=e.getAttribute("type");(a==="file"||x.test(a)||O.test(a)||I.test(a))&&(t=e)}else t=e;t&&!(e.required||e.hasAttribute("required"))&&s.add(e);break}case"root":{e===u&&s.add(e);break}case"empty":{if(e.hasChildNodes()){const t=e.childNodes.values();let a;for(const p of t)if(a=p.nodeType!==h.ELEMENT_NODE&&p.nodeType!==h.TEXT_NODE,!a)break;a&&s.add(e)}else s.add(e);break}case"first-child":{(b&&e===b.firstElementChild||e===o&&o.nodeType===h.ELEMENT_NODE)&&s.add(e);break}case"last-child":{(b&&e===b.lastElementChild||e===o&&o.nodeType===h.ELEMENT_NODE)&&s.add(e);break}case"only-child":{(b&&e===b.firstElementChild&&e===b.lastElementChild||e===o&&o.nodeType===h.ELEMENT_NODE)&&s.add(e);break}case"first-of-type":{if(b){const[t]=this._collectNthOfType({a:0,b:1},e);t&&s.add(t)}else e===o&&o.nodeType===h.ELEMENT_NODE&&s.add(e);break}case"last-of-type":{if(b){const[t]=this._collectNthOfType({a:0,b:1,reverse:!0},e);t&&s.add(t)}else e===o&&o.nodeType===h.ELEMENT_NODE&&s.add(e);break}case"only-of-type":{if(b){const[t]=this._collectNthOfType({a:0,b:1},e);if(t===e){const[a]=this._collectNthOfType({a:0,b:1,reverse:!0},e);a===e&&s.add(e)}}else e===o&&o.nodeType===h.ELEMENT_NODE&&s.add(e);break}case"host":case"host-context":break;case"after":case"before":case"first-letter":case"first-line":{if(this.#s)throw new DOMException(`Unsupported pseudo-element ::${f}`,h.NOT_SUPPORTED_ERR);break}case"active":case"autofill":case"blank":case"buffering":case"current":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(this.#s)throw new DOMException(`Unsupported pseudo-class :${f}`,h.NOT_SUPPORTED_ERR);break}default:if(f.startsWith("-webkit-")){if(this.#s)throw new DOMException(`Unsupported pseudo-class :${f}`,h.NOT_SUPPORTED_ERR)}else if(!m)throw new DOMException(`Unknown pseudo-class :${f}`,h.SYNTAX_ERR)}}return s}_matchAttributeSelector(i,e){const{flags:c,matcher:d,name:r,value:b}=i;if(typeof c=="string"&&!/^[is]$/i.test(c)){const s=(0,g.generateCSS)(i);throw new DOMException(`Invalid selector ${s}`,h.SYNTAX_ERR)}const{attributes:m}=e;let f;if(m&&m.length){const{document:s}=this.#t;let l;s.contentType==="text/html"?typeof c=="string"&&/^s$/i.test(c)?l=!1:l=!0:typeof c=="string"&&/^i$/i.test(c)?l=!0:l=!1;let{name:o}=r;o=(0,g.unescapeSelector)(o),l&&(o=o.toLowerCase());const u=new Set;if(/\|/.test(o)){const{prefix:n,tagName:t}=(0,k.selectorToNodeProps)(o);for(let{name:a,value:p}of m)switch(l&&(a=a.toLowerCase(),p=p.toLowerCase()),n){case"":{t===a&&u.add(p);break}case"*":{/:/.test(a)?a.endsWith(`:${t}`)&&u.add(p):t===a&&u.add(p);break}default:if(/:/.test(a)){const[N,w]=a.split(":");n===N&&t===w&&(0,k.isNamespaceDeclared)(n,e)&&u.add(p)}}}else for(let{name:n,value:t}of m)if(l&&(n=n.toLowerCase(),t=t.toLowerCase()),/:/.test(n)){const[a,p]=n.split(":");if(a==="xml"&&p==="lang")continue;o===p&&u.add(t)}else o===n&&u.add(t);if(u.size){const{name:n,value:t}=b||{};let a;switch(n?l?a=n.toLowerCase():a=n:t?l?a=t.toLowerCase():a=t:t===""&&(a=t),d){case"=":{typeof a=="string"&&u.has(a)&&(f=e);break}case"~=":{if(a&&typeof a=="string"){for(const p of u)if(new Set(p.split(/\s+/)).has(a)){f=e;break}}break}case"|=":{if(a&&typeof a=="string"){let p;for(const N of u)if(N===a||N.startsWith(`${a}-`)){p=N;break}p&&(f=e)}break}case"^=":{if(a&&typeof a=="string"){let p;for(const N of u)if(N.startsWith(`${a}`)){p=N;break}p&&(f=e)}break}case"$=":{if(a&&typeof a=="string"){let p;for(const N of u)if(N.endsWith(`${a}`)){p=N;break}p&&(f=e)}break}case"*=":{if(a&&typeof a=="string"){let p;for(const N of u)if(N.includes(`${a}`)){p=N;break}p&&(f=e)}break}case null:default:f=e}}}return f??null}_matchClassSelector(i,e){const c=(0,g.unescapeSelector)(i.name);let d;return e.classList.contains(c)&&(d=e),d??null}_matchIDSelector(i,e){const c=(0,g.unescapeSelector)(i.name),{id:d}=e;let r;return c===d&&(r=e),r??null}_matchTypeSelector(i,e){const c=(0,g.unescapeSelector)(i.name),{localName:d,prefix:r}=e,{document:b}=this.#t;let{prefix:m,tagName:f}=(0,k.selectorToNodeProps)(c,e);b.contentType==="text/html"&&(m=m.toLowerCase(),f=f.toLowerCase());let s,l;/:/.test(d)?[s,l]=d.split(":"):(s=r||"",l=d);let o;return m===""&&s===""?e.namespaceURI===null&&(f==="*"||f===l)&&(o=e):m==="*"?(f==="*"||f===l)&&(o=e):m===s&&(0,k.isNamespaceDeclared)(m,e)&&(f==="*"||f===l)&&(o=e),o??null}_matchShadowHostPseudoClass(i,e){const{children:c}=i,d=(0,g.unescapeSelector)(i.name);let r;if(Array.isArray(c)){const[b]=(0,g.walkAST)(c[0]),[...m]=b,{host:f}=e;if(d==="host"){let s;for(const l of m){const{type:o}=l;if(o===h.COMBINATOR){const u=(0,g.generateCSS)(i);throw new DOMException(`Invalid selector ${u}`,h.SYNTAX_ERR)}if(s=this._matchSelector(l,f).has(f),!s)break}s&&(r=e)}else if(d==="host-context"){let s=f,l;for(;s;){for(const o of m){const{type:u}=o;if(u===h.COMBINATOR){const n=(0,g.generateCSS)(i);throw new DOMException(`Invalid selector ${n}`,h.SYNTAX_ERR)}if(l=this._matchSelector(o,s).has(s),!l)break}if(l)break;s=s.parentNode}l&&(r=e)}}else d==="host"&&(r=e);return r??null}_matchSelector(i,e,c){const{type:d}=i,r=(0,g.unescapeSelector)(i.name),{shadow:b}=this.#t;let m=new Set;if(e.nodeType===h.ELEMENT_NODE)switch(d){case h.ATTRIBUTE_SELECTOR:{const f=this._matchAttributeSelector(i,e);f&&m.add(f);break}case h.CLASS_SELECTOR:{const f=this._matchClassSelector(i,e);f&&m.add(f);break}case h.ID_SELECTOR:{const f=this._matchIDSelector(i,e);f&&m.add(f);break}case h.PSEUDO_CLASS_SELECTOR:{const f=this._matchPseudoClassSelector(i,e,c);f.size&&(m=f);break}case h.PSEUDO_ELEMENT_SELECTOR:{this._matchPseudoElementSelector(r,c);break}case h.TYPE_SELECTOR:default:{const f=this._matchTypeSelector(i,e);f&&m.add(f)}}else if(b&&d===h.PSEUDO_CLASS_SELECTOR&&e.nodeType===h.DOCUMENT_FRAGMENT_NODE){if(r!=="has"&&j.test(r)){const f=this._matchPseudoClassSelector(i,e,c);f.size&&(m=f)}else if(G.test(r)){const f=this._matchShadowHostPseudoClass(i,e);f&&m.add(f)}}return m}_matchLeaves(i,e,c){let d;for(const r of i)if(d=this._matchSelector(r,e,c).has(e),!d)break;return!!d}_findDescendantNodes(i,e){const[c,...d]=i,{type:r}=c,b=(0,g.unescapeSelector)(c.name),m=d.length>0,f=new Set;let s=!1;switch(r){case h.ID_SELECTOR:{const{root:l}=this.#t;if(l.nodeType===h.ELEMENT_NODE)s=!0;else{const o=l.getElementById(b);if(o&&o!==e){const u=(0,k.isSameOrDescendant)(o,e);let n;u&&(n=o),n&&(m?this._matchLeaves(d,n)&&f.add(n):f.add(n))}}break}case h.PSEUDO_ELEMENT_SELECTOR:{this._matchPseudoElementSelector(b);break}default:s=!0}return{nodes:f,pending:s}}_matchCombinator(i,e,c={}){const{combo:d,leaves:r}=i,{name:b}=d,{find:m,forgive:f}=c;let s=new Set;if(m==="next")switch(b){case"+":{const l=e.nextElementSibling;l&&this._matchLeaves(r,l,{forgive:f})&&s.add(l);break}case"~":{let l=e.nextElementSibling;for(;l;)this._matchLeaves(r,l,{forgive:f})&&s.add(l),l=l.nextElementSibling;break}case">":{const l=[...e.children];for(const o of l)this._matchLeaves(r,o,{forgive:f})&&s.add(o);break}case" ":default:{const{nodes:l,pending:o}=this._findDescendantNodes(r,e);if(l.size)s=l;else if(o){const{document:u}=this.#t,n=u.createNodeIterator(e,h.SHOW_ELEMENT);let t=n.nextNode();for(t===e&&(t=n.nextNode());t;)this._matchLeaves(r,t,{forgive:f})&&s.add(t),t=n.nextNode()}}}else switch(b){case"+":{const l=e.previousElementSibling;l&&this._matchLeaves(r,l,{forgive:f})&&s.add(l);break}case"~":{const l=[];let o=e.previousElementSibling;for(;o;)this._matchLeaves(r,o,{forgive:f})&&l.push(o),o=o.previousElementSibling;l.length&&(s=new Set(l.reverse()));break}case">":{const l=e.parentNode;l&&this._matchLeaves(r,l,{forgive:f})&&s.add(l);break}case" ":default:{const l=[];let o=e.parentNode;for(;o;)this._matchLeaves(r,o,{forgive:f})&&l.push(o),o=o.parentNode;l.length&&(s=new Set(l.reverse()))}}return s}_findNodes(i,e){const{leaves:[c,...d]}=i,{type:r}=c,b=(0,g.unescapeSelector)(c.name),m=d.length>0,{document:f,root:s,shadow:l}=this.#t;let o=new Set,u=!1;switch(r){case h.ID_SELECTOR:{let n;if(e===v)this._matchLeaves([c],this.#e)&&(n=this.#e);else if(e===S){let t=this.#e;for(;t;){if(this._matchLeaves([c],t)){n=t;break}t=t.parentNode}}else s.nodeType===h.ELEMENT_NODE?u=!0:n=s.getElementById(b);n&&(m?this._matchLeaves(d,n)&&o.add(n):o.add(n));break}case h.CLASS_SELECTOR:{const n=[];if(e===v)this.#e.nodeType===h.ELEMENT_NODE&&this.#e.classList.contains(b)&&n.push(this.#e);else if(e===S){let t=this.#e;for(;t&&t.nodeType===h.ELEMENT_NODE;)t.classList.contains(b)&&n.push(t),t=t.parentNode}else if(s.nodeType===h.DOCUMENT_FRAGMENT_NODE){const t=[...s.children];for(const a of t){a.classList.contains(b)&&n.push(a);const p=[...a.getElementsByClassName(b)];n.push(...p)}}else{const t=[...s.getElementsByClassName(b)];n.push(...t)}if(n.length)if(m)for(const t of n)this._matchLeaves(d,t)&&o.add(t);else o=new Set(n);break}case h.TYPE_SELECTOR:{const n=[];if(e===v)this.#e.nodeType===h.ELEMENT_NODE&&this._matchLeaves([c],this.#e)&&n.push(this.#e);else if(e===S){let t=this.#e;for(;t&&t.nodeType===h.ELEMENT_NODE;)this._matchLeaves([c],t)&&n.push(t),t=t.parentNode}else if(f.contentType!=="text/html"||/[*|]/.test(b))u=!0;else if(s.nodeType===h.DOCUMENT_FRAGMENT_NODE){const t=b.toLowerCase(),a=[...s.children];for(const p of a){p.localName===t&&n.push(p);const N=[...p.getElementsByTagName(b)];n.push(...N)}}else{const t=[...s.getElementsByTagName(b)];n.push(...t)}if(n.length)if(m)for(const t of n)this._matchLeaves(d,t)&&o.add(t);else o=new Set(n);break}case h.PSEUDO_ELEMENT_SELECTOR:{this._matchPseudoElementSelector(b);break}default:{const n=[];if(e!==S&&G.test(b)){if(l&&this.#e.nodeType===h.DOCUMENT_FRAGMENT_NODE){const t=this._matchShadowHostPseudoClass(c,this.#e);t&&n.push(t)}}else if(e===v)this._matchLeaves([c],this.#e)&&n.push(this.#e);else if(e===S){let t=this.#e;for(;t;)this._matchLeaves([c],t)&&n.push(t),t=t.parentNode}else u=!0;if(n.length)if(m)for(const t of n)this._matchLeaves(d,t)&&o.add(t);else o=new Set(n)}}return{nodes:o,pending:u}}_getFirstTwig(i){const e=i.length-1,c=i[0];let d,r;if(e){const b=i[e],{leaves:[{type:m}]}=b;m===h.PSEUDO_ELEMENT_SELECTOR||m===h.ID_SELECTOR?(d="prev",r=b):(d="next",r=c)}else d="prev",r=c;return{find:d,twig:r}}_collectNodes(i){const e=this.#i.values();if(i===$||i===T){const c=new Set;let d=0;for(const{branch:r}of e){const{find:b,twig:m}=this._getFirstTwig(r),{nodes:f,pending:s}=this._findNodes(m,i);f.size?this.#l[d]=f:s?c.add(new Map([["index",d],["twig",m]])):this.#i[d].skip=!0,this.#i[d].find=b,d++}if(c.size){const{document:r,root:b}=this.#t,m=r.createNodeIterator(b,h.SHOW_ELEMENT);let f=m.nextNode();for(;f;){let s=!1;if(this.#e.nodeType===h.ELEMENT_NODE?s=(0,k.isSameOrDescendant)(f,this.#e):s=!0,s)for(const l of c){const{leaves:o}=l.get("twig");if(this._matchLeaves(o,f)){const n=l.get("index");this.#l[n].add(f)}}f=m.nextNode()}}}else{let c=0;for(const{branch:d}of e){const r=d[d.length-1],{nodes:b}=this._findNodes(r,i);b.size?this.#l[c]=b:this.#i[c].skip=!0,this.#i[c].find="prev",c++}}return[this.#i,this.#l]}_sortNodes(i){const e=[...i];return e.length>1&&e.sort((c,d)=>{const r=c.compareDocumentPosition(d);let b;return r&h.DOCUMENT_POSITION_PRECEDING||r&h.DOCUMENT_POSITION_CONTAINS?b=1:b=-1,b}),e}_matchNodes(i){const[...e]=this.#i,c=e.length;let d=new Set;for(let r=0;r<c;r++){const{branch:b,find:m,skip:f}=e[r],s=b.length;if(!f&&s){const l=this.#l[r],o=s-1;if(o===0)if((i===$||i===T)&&this.#e.nodeType===h.ELEMENT_NODE){for(const u of l)if(u!==this.#e&&(0,k.isSameOrDescendant)(u,this.#e)&&(d.add(u),i===T))break}else if(i===T){const[u]=this._sortNodes(l);d.add(u)}else{const u=[...d],n=[...l];d=new Set([...u,...n])}else if(m==="next"){let{combo:u}=b[0];for(const n of l){let t=new Set([n]);for(let a=1;a<s;a++){const{combo:p,leaves:N}=b[a],w=[];for(const y of t){const E={combo:u,leaves:N},M=this._matchCombinator(E,y,{find:m});M.size&&w.push(...M)}if(w.length)if(a===o){if(i===T){const[y]=this._sortNodes(w);d.add(y)}else{const y=[...d];d=new Set([...y,...w])}break}else u=p,t=new Set(w);else break}}}else for(const u of l){let n=new Set([u]),t;for(let a=o-1;a>=0;a--){const p=b[a],N=[];for(const w of n){const y=this._matchCombinator(p,w,{find:m});y.size&&N.push(...y)}if(N.length)if(t=!0,a===0){d.add(u);break}else n=new Set(N);else{t=!1;break}}if(t&&i!==$)break}}}return d}_find(i){return this._collectNodes(i),this._matchNodes(i)}matches(){if(this.#e.nodeType!==h.ELEMENT_NODE)throw new TypeError(`Unexpected node ${this.#e.nodeName}`);let i;try{i=this._find(v).has(this.#e)}catch(e){this._onError(e)}return!!i}closest(){if(this.#e.nodeType!==h.ELEMENT_NODE)throw new TypeError(`Unexpected node ${this.#e.nodeName}`);let i;try{const e=this._find(S);let c=this.#e;for(;c;){if(e.has(c)){i=c;break}c=c.parentNode}}catch(e){this._onError(e)}return i??null}querySelector(){let i;try{const e=this._find(T);e.delete(this.#e),e.size>1?[i]=this._sortNodes(e):e.size&&([i]=[...e])}catch(e){this._onError(e)}return i??null}querySelectorAll(){const i=[];try{const e=this._find($);e.delete(this.#e),e.size>1&&this.#c?i.push(...this._sortNodes(e)):e.size&&i.push(...e)}catch(e){this._onError(e)}return i}}0&&(module.exports={Matcher});
|
|
1
|
+
var B=Object.create;var P=Object.defineProperty;var z=Object.getOwnPropertyDescriptor;var F=Object.getOwnPropertyNames;var V=Object.getPrototypeOf,j=Object.prototype.hasOwnProperty;var H=(x,s)=>{for(var e in s)P(x,e,{get:s[e],enumerable:!0})},U=(x,s,e,c)=>{if(s&&typeof s=="object"||typeof s=="function")for(let h of F(s))!j.call(x,h)&&h!==e&&P(x,h,{get:()=>s[h],enumerable:!(c=z(s,h))||c.enumerable});return x};var G=(x,s,e)=>(e=x!=null?B(V(x)):{},U(s||!x||!x.__esModule?P(e,"default",{value:x,enumerable:!0}):e,x)),W=x=>U(P({},"__esModule",{value:!0}),x);var X={};H(X,{Matcher:()=>q});module.exports=W(X);var M=G(require("is-potential-custom-element-name"),1),E=require("./dom-util.js"),y=require("./parser.js"),o=require("./constant.js");const R="all",O="first",D="lineal",I="self";class q{#i;#l;#a;#e;#r;#t;#o;#c;#s;constructor(s,e,c={}){const{sort:h,warn:a}=c;this.#l=new Map([[o.SELECTOR_PSEUDO_ELEMENT,o.BIT_01],[o.SELECTOR_ID,o.BIT_02],[o.SELECTOR_CLASS,o.BIT_04],[o.SELECTOR_TYPE,o.BIT_08],[o.SELECTOR_ATTR,o.BIT_16],[o.SELECTOR_PSEUDO_CLASS,o.BIT_32]]),this.#a=new WeakMap,this.#o=s,this.#e=e,this.#c=!!h,this.#s=!!a,[this.#i,this.#r]=this._prepare(s),this.#t=this._getRoot(e)}_onError(s){if(s instanceof DOMException&&s.name===o.NOT_SUPPORTED_ERR)this.#s&&console.warn(s.message);else throw s}_getRoot(s=this.#e){let e,c;switch(s.nodeType){case o.DOCUMENT_NODE:{e=s,c=s;break}case o.DOCUMENT_FRAGMENT_NODE:{e=s.ownerDocument,c=s;break}case o.ELEMENT_NODE:{if((0,E.isSameOrDescendant)(s))e=s.ownerDocument,c=s.ownerDocument;else{let a=s;for(;a&&a.parentNode;)a=a.parentNode;e=a.ownerDocument,c=a}break}default:throw new TypeError(`Unexpected node ${s.nodeName}`)}const h=(0,E.isInShadowTree)(s);return{document:e,root:c,shadow:h}}_sortLeaves(s){const e=[...s];return e.length>1&&e.sort((c,h)=>{const{type:a}=c,{type:b}=h,p=this.#l.get(a),r=this.#l.get(b);let t;return p===r?t=0:p>r?t=1:t=-1,t}),e}_prepare(s=this.#o){const e=(0,y.parseSelector)(s),c=(0,y.walkAST)(e),h=[],a=[];let b=0;for(const[...p]of c){const r=[];let t=p.shift();if(t&&t.type!==o.COMBINATOR){const i=new Set;for(;t;){if(t.type===o.COMBINATOR){const[l]=p;if(l.type===o.COMBINATOR){const d=`Invalid combinator ${t.name}${l.name}`;throw new DOMException(d,o.SYNTAX_ERR)}r.push({combo:t,leaves:this._sortLeaves(i)}),i.clear()}else t&&i.add(t);if(p.length)t=p.shift();else{r.push({combo:null,leaves:this._sortLeaves(i)}),i.clear();break}}}h.push({branch:r,find:null,skip:!1}),a[b]=new Set,b++}return[h,a]}_collectNthChild(s,e){const{a:c,b:h,reverse:a,selector:b}=s,{parentNode:p}=e,r=new Set;let t;if(b&&(this.#a.has(b)?t=this.#a.get(b):(t=(0,y.walkAST)(b),this.#a.set(b,t))),p){const i=[...p.children],l=i.length;if(l){const d=new Set;if(t){const n=t.length;for(const f of i){let u;for(let m=0;m<n;m++){const N=t[m];if(u=this._matchLeaves(N,f),!u)break}u&&d.add(f)}}if(a&&i.reverse(),c===0){if(h>0&&h<=l){if(d.size)for(let n=0;n<l;n++){const f=i[n];if(d.has(f)){r.add(f);break}}else if(!b){const n=i[h-1];r.add(n)}}}else{let n=h-1;if(c>0)for(;n<0;)n+=c;if(n>=0&&n<l){let f=c>0?0:h-1;for(let u=0;u<l&&n>=0&&n<l;u++){const m=i[u];d.size?d.has(m)&&(f===n&&(r.add(m),n+=c),c>0?f++:f--):u===n&&(b||r.add(m),n+=c)}}}}}else{const{root:i}=this.#t;if(e===i&&i.nodeType===o.ELEMENT_NODE&&c+h===1)if(t){const l=t.length;let d;for(let n=0;n<l;n++){const f=t[n];if(d=this._matchLeaves(f,e),d)break}d&&r.add(e)}else r.add(e)}return r}_collectNthOfType(s,e){const{a:c,b:h,reverse:a}=s,{localName:b,parentNode:p,prefix:r}=e,t=new Set;if(p){const i=[...p.children],l=i.length;if(l)if(a&&i.reverse(),c===0){if(h>0&&h<=l){let d=0;for(let n=0;n<l;n++){const f=i[n],{localName:u,prefix:m}=f;if(u===b&&m===r){if(d===h-1){t.add(f);break}d++}}}}else{let d=h-1;if(c>0)for(;d<0;)d+=c;if(d>=0&&d<l){let n=c>0?0:h-1;for(let f=0;f<l;f++){const u=i[f],{localName:m,prefix:N}=u;if(m===b&&N===r){if(n===d&&(t.add(u),d+=c),d<0||d>=l)break;c>0?n++:n--}}}}}else{const{root:i}=this.#t;e===i&&i.nodeType===o.ELEMENT_NODE&&c+h===1&&t.add(e)}return t}_matchAnPlusB(s,e,c){const{nth:{a:h,b:a,name:b},selector:p}=s,r=(0,y.unescapeSelector)(b),t=new Map;r?(r==="even"?(t.set("a",2),t.set("b",0)):r==="odd"&&(t.set("a",2),t.set("b",1)),/last/.test(c)&&t.set("reverse",!0)):(typeof h=="string"&&/-?\d+/.test(h)?t.set("a",h*1):t.set("a",0),typeof a=="string"&&/-?\d+/.test(a)?t.set("b",a*1):t.set("b",0),/last/.test(c)&&t.set("reverse",!0));let i=new Set;if(t.has("a")&&t.has("b")){if(/^nth-(?:last-)?child$/.test(c)){p&&t.set("selector",p);const l=Object.fromEntries(t),d=this._collectNthChild(l,e);d.size&&(i=d)}else if(/^nth-(?:last-)?of-type$/.test(c)){const l=Object.fromEntries(t),d=this._collectNthOfType(l,e);d.size&&(i=d)}}return i}_matchPseudoElementSelector(s,e={}){const{forgive:c}=e;switch(s){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(this.#s)throw new DOMException(`Unsupported pseudo-element ::${s}`,o.NOT_SUPPORTED_ERR);break}case"part":case"slotted":{if(this.#s)throw new DOMException(`Unsupported pseudo-element ::${s}()`,o.NOT_SUPPORTED_ERR);break}default:if(s.startsWith("-webkit-")){if(this.#s)throw new DOMException(`Unsupported pseudo-element ::${s}`,o.NOT_SUPPORTED_ERR)}else if(!c)throw new DOMException(`Unknown pseudo-element ::${s}`,o.SYNTAX_ERR)}}_matchDirectionPseudoClass(s,e){const c=(0,y.unescapeSelector)(s.name),h=(0,E.getDirectionality)(e);let a;return c===h&&(a=e),a??null}_matchLanguagePseudoClass(s,e){const c=(0,y.unescapeSelector)(s.name);let h;if(c)if(c==="*")if(e.hasAttribute("lang"))e.getAttribute("lang")&&(h=e);else{let a=e.parentNode;for(;a;){if(a.hasAttribute("lang")){a.getAttribute("lang")&&(h=e);break}a=a.parentNode}}else{const a=`(?:-${o.ALPHA_NUM})*`;if(new RegExp(`^(?:\\*-)?${o.ALPHA_NUM}${a}$`,"i").test(c)){let p;if(/-/.test(c)){const[r,t,...i]=c.split("-");let l;r==="*"?l=`${o.ALPHA_NUM}${a}`:l=`${r}${a}`;const d=`-${t}${a}`,n=i.length;let f="";if(n)for(let u=0;u<n;u++)f+=`-${i[u]}${a}`;p=new RegExp(`^${l}${d}${f}$`,"i")}else p=new RegExp(`^${c}${a}$`,"i");if(e.hasAttribute("lang"))p.test(e.getAttribute("lang"))&&(h=e);else{let r=e.parentNode;for(;r;){if(r.hasAttribute("lang")){const t=r.getAttribute("lang");p.test(t)&&(h=e);break}r=r.parentNode}}}}return h??null}_matchHasPseudoFunc(s,e){let c;if(Array.isArray(s)&&s.length){const[h]=s,{type:a}=h;let b;a===o.COMBINATOR?b=s.shift():b={name:" ",type:o.COMBINATOR};const p=[];for(;s.length;){const[i]=s,{type:l}=i;if(l===o.COMBINATOR)break;p.push(s.shift())}const r={combo:b,leaves:p},t=this._matchCombinator(r,e,{find:"next"});if(t.size)if(s.length){for(const i of t)if(c=this._matchHasPseudoFunc(Object.assign([],s),i),c)break}else c=!0}return!!c}_matchLogicalPseudoFunc(s,e){const{astName:c="",branches:h=[],selector:a="",twigBranches:b=[]}=s;let p;if(c==="has")if(a.includes(":has("))p=null;else{const r=h.length;let t;for(let i=0;i<r;i++){const l=h[i];if(t=this._matchHasPseudoFunc(Object.assign([],l),e),t)break}t&&(p=e)}else{const r=/^(?:is|where)$/.test(c),t=b.length;let i;for(let l=0;l<t;l++){const d=b[l],n=d.length-1,{leaves:f}=d[n];if(i=this._matchLeaves(f,e,{forgive:r}),i&&n>0){let u=new Set([e]);for(let m=n-1;m>=0;m--){const N=d[m],k=[];for(const A of u){const L=this._matchCombinator(N,A,{forgive:r,find:"prev"});L.size&&k.push(...L)}if(k.length)if(m===0){i=!0;break}else u=new Set(k);else{i=!1;break}}}if(i)break}c==="not"?i||(p=e):i&&(p=e)}return p??null}_matchPseudoClassSelector(s,e,c={}){const{children:h}=s,{localName:a,parentNode:b}=e,{forgive:p}=c,r=(0,y.unescapeSelector)(s.name);let t=new Set;if(o.REG_LOGICAL_PSEUDO.test(r)){let i;if(this.#a.has(s))i=this.#a.get(s);else{const d=(0,y.walkAST)(s),n=[],f=[];for(const[...u]of d){for(const A of u){const L=(0,y.generateCSS)(A);n.push(L)}const m=[],N=new Set;let k=u.shift();for(;k;)if(k.type===o.COMBINATOR?(m.push({combo:k,leaves:[...N]}),N.clear()):k&&N.add(k),u.length)k=u.shift();else{m.push({combo:null,leaves:[...N]}),N.clear();break}f.push(m)}i={astName:r,branches:d,twigBranches:f,selector:n.join(",")},this.#a.set(s,i)}const l=this._matchLogicalPseudoFunc(i,e);l&&t.add(l)}else if(Array.isArray(h)){const[i]=h;if(/^nth-(?:last-)?(?:child|of-type)$/.test(r)){const l=this._matchAnPlusB(i,e,r);l.size&&(t=l)}else if(r==="dir"){const l=this._matchDirectionPseudoClass(i,e);l&&t.add(l)}else if(r==="lang"){const l=this._matchLanguagePseudoClass(i,e);l&&t.add(l)}else switch(r){case"current":case"nth-col":case"nth-last-col":{if(this.#s)throw new DOMException(`Unsupported pseudo-class :${r}()`,o.NOT_SUPPORTED_ERR);break}default:if(!p)throw new DOMException(`Unknown pseudo-class :${r}()`,o.SYNTAX_ERR)}}else{const{document:i,root:l}=this.#t,{documentElement:d}=i,n=new URL(i.URL),f=/^a(?:rea)?$/,u=/^(?:(?:fieldse|inpu|selec)t|button|opt(?:group|ion)|textarea)$/,m=/^(?:(?:(?:in|out)pu|selec)t|button|form|textarea)$/,N=/^d(?:etails|ialog)$/,k=/^(?:checkbox|radio)$/,A=/^(?:date(?:time-local)?|month|time|week)$/,L=/(?:(?:rang|tim)e|date(?:time-local)?|month|number|week)$/,$=/^(?:(?:emai|te|ur)l|number|password|search|text)$/;switch(r){case"any-link":case"link":{f.test(a)&&e.hasAttribute("href")&&t.add(e);break}case"local-link":{if(f.test(a)&&e.hasAttribute("href")){const g=new URL(e.getAttribute("href"),n.href);g.origin===n.origin&&g.pathname===n.pathname&&t.add(e)}break}case"visited":break;case"target":{e.id&&n.hash&&n.hash===`#${e.id}`&&(0,E.isSameOrDescendant)(e)&&t.add(e);break}case"target-within":{if(n.hash){const g=n.hash.replace(/^#/,"");let w=i.getElementById(g);for(;w;){if(w===e){t.add(e);break}w=w.parentNode}}break}case"scope":{this.#e.nodeType===o.ELEMENT_NODE?e===this.#e&&t.add(e):e===d&&t.add(e);break}case"focus":{e===i.activeElement&&t.add(e);break}case"focus-within":{let g=i.activeElement;for(;g;){if(g===e){t.add(e);break}g=g.parentNode}break}case"open":{N.test(a)&&e.hasAttribute("open")&&t.add(e);break}case"closed":{N.test(a)&&!e.hasAttribute("open")&&t.add(e);break}case"disabled":{if(u.test(a)||(0,M.default)(a))if(e.disabled||e.hasAttribute("disabled"))t.add(e);else{let g=b;for(;g&&g.localName!=="fieldset";)g=g.parentNode;g&&b.localName!=="legend"&&g.hasAttribute("disabled")&&t.add(e)}break}case"enabled":{(u.test(a)||(0,M.default)(a))&&!(e.disabled&&e.hasAttribute("disabled"))&&t.add(e);break}case"read-only":{switch(a){case"textarea":{(e.readonly||e.hasAttribute("readonly")||e.disabled||e.hasAttribute("disabled"))&&t.add(e);break}case"input":{(!e.type||A.test(e.type)||$.test(e.type))&&(e.readonly||e.hasAttribute("readonly")||e.disabled||e.hasAttribute("disabled"))&&t.add(e);break}default:(0,E.isContentEditable)(e)||t.add(e)}break}case"read-write":{switch(a){case"textarea":{e.readonly||e.hasAttribute("readonly")||e.disabled||e.hasAttribute("disabled")||t.add(e);break}case"input":{(!e.type||A.test(e.type)||$.test(e.type))&&!(e.readonly||e.hasAttribute("readonly")||e.disabled||e.hasAttribute("disabled"))&&t.add(e);break}default:(0,E.isContentEditable)(e)&&t.add(e)}break}case"placeholder-shown":{let g;a==="textarea"?g=e:a==="input"&&(e.hasAttribute("type")?$.test(e.getAttribute("type"))&&(g=e):g=e),g&&e.value===""&&e.hasAttribute("placeholder")&&e.getAttribute("placeholder").trim().length&&t.add(e);break}case"checked":{(e.checked&&a==="input"&&e.hasAttribute("type")&&k.test(e.getAttribute("type"))||e.selected&&a==="option")&&t.add(e);break}case"indeterminate":{if(e.indeterminate&&a==="input"&&e.type==="checkbox"||a==="progress"&&!e.hasAttribute("value"))t.add(e);else if(a==="input"&&e.type==="radio"&&!e.hasAttribute("checked")){const g=e.name;let w=e.parentNode;for(;w&&w.localName!=="form";)w=w.parentNode;w||(w=d);const _=[...w.getElementsByTagName("input")];let S;for(const v of _)if(v.getAttribute("type")==="radio"&&(g?v.getAttribute("name")===g&&(S=!!v.checked):v.hasAttribute("name")||(S=!!v.checked),S))break;S||t.add(e)}break}case"default":{const g=/^(?:button|reset)$/,w=/^(?:image|submit)$/;if(a==="button"&&!(e.hasAttribute("type")&&g.test(e.getAttribute("type")))||a==="input"&&e.hasAttribute("type")&&w.test(e.getAttribute("type"))){let _=e.parentNode;for(;_&&_.localName!=="form";)_=_.parentNode;if(_){const S=i.createNodeIterator(_,o.SHOW_ELEMENT);let v=S.nextNode();for(;v;){const C=v.localName;let T;if(C==="button"?T=!(v.hasAttribute("type")&&g.test(v.getAttribute("type"))):C==="input"&&(T=v.hasAttribute("type")&&w.test(v.getAttribute("type"))),T){v===e&&t.add(e);break}v=S.nextNode()}}}else if(a==="input"&&e.hasAttribute("type")&&k.test(e.getAttribute("type"))&&(e.checked||e.hasAttribute("checked")))t.add(e);else if(a==="option"){let _=!1,S=b;for(;S&&S.localName!=="datalist";){if(S.localName==="select"){(S.multiple||S.hasAttribute("multiple"))&&(_=!0);break}S=S.parentNode}if(_)(e.selected||e.hasAttribute("selected"))&&t.add(e);else{const v=b.firstElementChild,C=new Set;let T=v;for(;T;){if(T.selected||T.hasAttribute("selected")){C.add(T);break}T=T.nextElementSibling}C.size||C.add(v),C.has(e)&&t.add(e)}}break}case"valid":{if(m.test(a))e.checkValidity()&&t.add(e);else if(/^fieldset$/.test(a)){const g=i.createNodeIterator(e,o.SHOW_ELEMENT);let w=g.nextNode();w===e&&(w=g.nextNode());let _;for(;w&&!(m.test(w.localName)&&(_=w.checkValidity(),!_));)w=g.nextNode();_&&t.add(e)}break}case"invalid":{if(m.test(a))e.checkValidity()||t.add(e);else if(/^fieldset$/.test(a)){const g=i.createNodeIterator(e,o.SHOW_ELEMENT);let w=g.nextNode();w===e&&(w=g.nextNode());let _;for(;w&&!(m.test(w.localName)&&(_=w.checkValidity(),!_));)w=g.nextNode();_||t.add(e)}break}case"in-range":{a==="input"&&!(e.readonly||e.hasAttribute("readonly"))&&!(e.disabled||e.hasAttribute("disabled"))&&e.hasAttribute("type")&&L.test(e.getAttribute("type"))&&!(e.validity.rangeUnderflow||e.validity.rangeOverflow)&&(e.hasAttribute("min")||e.hasAttribute("max")||e.getAttribute("type")==="range")&&t.add(e);break}case"out-of-range":{a==="input"&&!(e.readonly||e.hasAttribute("readonly"))&&!(e.disabled||e.hasAttribute("disabled"))&&e.hasAttribute("type")&&L.test(e.getAttribute("type"))&&(e.validity.rangeUnderflow||e.validity.rangeOverflow)&&t.add(e);break}case"required":{let g;if(/^(?:select|textarea)$/.test(a))g=e;else if(a==="input")if(e.hasAttribute("type")){const w=e.getAttribute("type");(w==="file"||k.test(w)||A.test(w)||$.test(w))&&(g=e)}else g=e;g&&(e.required||e.hasAttribute("required"))&&t.add(e);break}case"optional":{let g;if(/^(?:select|textarea)$/.test(a))g=e;else if(a==="input")if(e.hasAttribute("type")){const w=e.getAttribute("type");(w==="file"||k.test(w)||A.test(w)||$.test(w))&&(g=e)}else g=e;g&&!(e.required||e.hasAttribute("required"))&&t.add(e);break}case"root":{e===d&&t.add(e);break}case"empty":{if(e.hasChildNodes()){const g=e.childNodes.values();let w;for(const _ of g)if(w=_.nodeType!==o.ELEMENT_NODE&&_.nodeType!==o.TEXT_NODE,!w)break;w&&t.add(e)}else t.add(e);break}case"first-child":{(b&&e===b.firstElementChild||e===l&&l.nodeType===o.ELEMENT_NODE)&&t.add(e);break}case"last-child":{(b&&e===b.lastElementChild||e===l&&l.nodeType===o.ELEMENT_NODE)&&t.add(e);break}case"only-child":{(b&&e===b.firstElementChild&&e===b.lastElementChild||e===l&&l.nodeType===o.ELEMENT_NODE)&&t.add(e);break}case"first-of-type":{if(b){const[g]=this._collectNthOfType({a:0,b:1},e);g&&t.add(g)}else e===l&&l.nodeType===o.ELEMENT_NODE&&t.add(e);break}case"last-of-type":{if(b){const[g]=this._collectNthOfType({a:0,b:1,reverse:!0},e);g&&t.add(g)}else e===l&&l.nodeType===o.ELEMENT_NODE&&t.add(e);break}case"only-of-type":{if(b){const[g]=this._collectNthOfType({a:0,b:1},e);if(g===e){const[w]=this._collectNthOfType({a:0,b:1,reverse:!0},e);w===e&&t.add(e)}}else e===l&&l.nodeType===o.ELEMENT_NODE&&t.add(e);break}case"host":case"host-context":break;case"after":case"before":case"first-letter":case"first-line":{if(this.#s)throw new DOMException(`Unsupported pseudo-element ::${r}`,o.NOT_SUPPORTED_ERR);break}case"active":case"autofill":case"blank":case"buffering":case"current":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(this.#s)throw new DOMException(`Unsupported pseudo-class :${r}`,o.NOT_SUPPORTED_ERR);break}default:if(r.startsWith("-webkit-")){if(this.#s)throw new DOMException(`Unsupported pseudo-class :${r}`,o.NOT_SUPPORTED_ERR)}else if(!p)throw new DOMException(`Unknown pseudo-class :${r}`,o.SYNTAX_ERR)}}return t}_matchAttributeSelector(s,e){const{flags:c,matcher:h,name:a,value:b}=s;if(typeof c=="string"&&!/^[is]$/i.test(c)){const t=(0,y.generateCSS)(s);throw new DOMException(`Invalid selector ${t}`,o.SYNTAX_ERR)}const{attributes:p}=e;let r;if(p&&p.length){const{document:t}=this.#t;let i;t.contentType==="text/html"?typeof c=="string"&&/^s$/i.test(c)?i=!1:i=!0:typeof c=="string"&&/^i$/i.test(c)?i=!0:i=!1;let{name:l}=a;l=(0,y.unescapeSelector)(l),i&&(l=l.toLowerCase());const d=new Set;if(/\|/.test(l)){const{prefix:n,tagName:f}=(0,E.selectorToNodeProps)(l);for(let{name:u,value:m}of p)switch(i&&(u=u.toLowerCase(),m=m.toLowerCase()),n){case"":{f===u&&d.add(m);break}case"*":{/:/.test(u)?u.endsWith(`:${f}`)&&d.add(m):f===u&&d.add(m);break}default:if(/:/.test(u)){const[N,k]=u.split(":");n===N&&f===k&&(0,E.isNamespaceDeclared)(n,e)&&d.add(m)}}}else for(let{name:n,value:f}of p)if(i&&(n=n.toLowerCase(),f=f.toLowerCase()),/:/.test(n)){const[u,m]=n.split(":");if(u==="xml"&&m==="lang")continue;l===m&&d.add(f)}else l===n&&d.add(f);if(d.size){const{name:n,value:f}=b||{};let u;switch(n?i?u=n.toLowerCase():u=n:f?i?u=f.toLowerCase():u=f:f===""&&(u=f),h){case"=":{typeof u=="string"&&d.has(u)&&(r=e);break}case"~=":{if(u&&typeof u=="string"){for(const m of d)if(new Set(m.split(/\s+/)).has(u)){r=e;break}}break}case"|=":{if(u&&typeof u=="string"){let m;for(const N of d)if(N===u||N.startsWith(`${u}-`)){m=N;break}m&&(r=e)}break}case"^=":{if(u&&typeof u=="string"){let m;for(const N of d)if(N.startsWith(`${u}`)){m=N;break}m&&(r=e)}break}case"$=":{if(u&&typeof u=="string"){let m;for(const N of d)if(N.endsWith(`${u}`)){m=N;break}m&&(r=e)}break}case"*=":{if(u&&typeof u=="string"){let m;for(const N of d)if(N.includes(`${u}`)){m=N;break}m&&(r=e)}break}case null:default:r=e}}}return r??null}_matchClassSelector(s,e){const c=(0,y.unescapeSelector)(s.name);let h;return e.classList.contains(c)&&(h=e),h??null}_matchIDSelector(s,e){const c=(0,y.unescapeSelector)(s.name),{id:h}=e;let a;return c===h&&(a=e),a??null}_matchTypeSelector(s,e){const c=(0,y.unescapeSelector)(s.name),{localName:h,prefix:a}=e,{document:b}=this.#t;let{prefix:p,tagName:r}=(0,E.selectorToNodeProps)(c,e);b.contentType==="text/html"&&(p=p.toLowerCase(),r=r.toLowerCase());let t,i;/:/.test(h)?[t,i]=h.split(":"):(t=a||"",i=h);let l;return p===""&&t===""?e.namespaceURI===null&&(r==="*"||r===i)&&(l=e):p==="*"?(r==="*"||r===i)&&(l=e):p===t&&(0,E.isNamespaceDeclared)(p,e)&&(r==="*"||r===i)&&(l=e),l??null}_matchShadowHostPseudoClass(s,e){const{children:c}=s,h=(0,y.unescapeSelector)(s.name);let a;if(Array.isArray(c)){const[b]=(0,y.walkAST)(c[0]),[...p]=b,{host:r}=e;if(h==="host"){let t;for(const i of p){const{type:l}=i;if(l===o.COMBINATOR){const d=(0,y.generateCSS)(s);throw new DOMException(`Invalid selector ${d}`,o.SYNTAX_ERR)}if(t=this._matchSelector(i,r).has(r),!t)break}t&&(a=e)}else if(h==="host-context"){let t=r,i;for(;t;){for(const l of p){const{type:d}=l;if(d===o.COMBINATOR){const n=(0,y.generateCSS)(s);throw new DOMException(`Invalid selector ${n}`,o.SYNTAX_ERR)}if(i=this._matchSelector(l,t).has(t),!i)break}if(i)break;t=t.parentNode}i&&(a=e)}}else h==="host"&&(a=e);return a??null}_matchSelector(s,e,c){const{type:h}=s,a=(0,y.unescapeSelector)(s.name),{shadow:b}=this.#t;let p=new Set;if(e.nodeType===o.ELEMENT_NODE)switch(h){case o.SELECTOR_ATTR:{const r=this._matchAttributeSelector(s,e);r&&p.add(r);break}case o.SELECTOR_CLASS:{const r=this._matchClassSelector(s,e);r&&p.add(r);break}case o.SELECTOR_ID:{const r=this._matchIDSelector(s,e);r&&p.add(r);break}case o.SELECTOR_PSEUDO_CLASS:{const r=this._matchPseudoClassSelector(s,e,c);r.size&&(p=r);break}case o.SELECTOR_PSEUDO_ELEMENT:{this._matchPseudoElementSelector(a,c);break}case o.SELECTOR_TYPE:default:{const r=this._matchTypeSelector(s,e);r&&p.add(r)}}else if(b&&h===o.SELECTOR_PSEUDO_CLASS&&e.nodeType===o.DOCUMENT_FRAGMENT_NODE){if(a!=="has"&&o.REG_LOGICAL_PSEUDO.test(a)){const r=this._matchPseudoClassSelector(s,e,c);r.size&&(p=r)}else if(o.REG_SHADOW_HOST.test(a)){const r=this._matchShadowHostPseudoClass(s,e);r&&p.add(r)}}return p}_matchLeaves(s,e,c){let h;for(const a of s)if(h=this._matchSelector(a,e,c).has(e),!h)break;return!!h}_findDescendantNodes(s,e){const[c,...h]=s,{type:a}=c,b=(0,y.unescapeSelector)(c.name),p=h.length>0,r=new Set;let t=!1;switch(a){case o.SELECTOR_ID:{const{root:i}=this.#t;if(i.nodeType===o.ELEMENT_NODE)t=!0;else{const l=i.getElementById(b);if(l&&l!==e){const d=(0,E.isSameOrDescendant)(l,e);let n;d&&(n=l),n&&(p?this._matchLeaves(h,n)&&r.add(n):r.add(n))}}break}case o.SELECTOR_PSEUDO_ELEMENT:{this._matchPseudoElementSelector(b);break}default:t=!0}return{nodes:r,pending:t}}_matchCombinator(s,e,c={}){const{combo:h,leaves:a}=s,{name:b}=h,{find:p,forgive:r}=c;let t=new Set;if(p==="next")switch(b){case"+":{const i=e.nextElementSibling;i&&this._matchLeaves(a,i,{forgive:r})&&t.add(i);break}case"~":{let i=e.nextElementSibling;for(;i;)this._matchLeaves(a,i,{forgive:r})&&t.add(i),i=i.nextElementSibling;break}case">":{const i=[...e.children];for(const l of i)this._matchLeaves(a,l,{forgive:r})&&t.add(l);break}case" ":default:{const{nodes:i,pending:l}=this._findDescendantNodes(a,e);if(i.size)t=i;else if(l){const{document:d}=this.#t,n=d.createNodeIterator(e,o.SHOW_ELEMENT);let f=n.nextNode();for(f===e&&(f=n.nextNode());f;)this._matchLeaves(a,f,{forgive:r})&&t.add(f),f=n.nextNode()}}}else switch(b){case"+":{const i=e.previousElementSibling;i&&this._matchLeaves(a,i,{forgive:r})&&t.add(i);break}case"~":{const i=[];let l=e.previousElementSibling;for(;l;)this._matchLeaves(a,l,{forgive:r})&&i.push(l),l=l.previousElementSibling;i.length&&(t=new Set(i.reverse()));break}case">":{const i=e.parentNode;i&&this._matchLeaves(a,i,{forgive:r})&&t.add(i);break}case" ":default:{const i=[];let l=e.parentNode;for(;l;)this._matchLeaves(a,l,{forgive:r})&&i.push(l),l=l.parentNode;i.length&&(t=new Set(i.reverse()))}}return t}_findNodes(s,e){const{leaves:[c,...h]}=s,{type:a}=c,b=(0,y.unescapeSelector)(c.name),p=h.length>0,{document:r,root:t,shadow:i}=this.#t;let l=new Set,d=!1;switch(a){case o.SELECTOR_ID:{let n;if(e===I)this._matchLeaves([c],this.#e)&&(n=this.#e);else if(e===D){let f=this.#e;for(;f;){if(this._matchLeaves([c],f)){n=f;break}f=f.parentNode}}else t.nodeType===o.ELEMENT_NODE?d=!0:n=t.getElementById(b);n&&(p?this._matchLeaves(h,n)&&l.add(n):l.add(n));break}case o.SELECTOR_CLASS:{const n=[];if(e===I)this.#e.nodeType===o.ELEMENT_NODE&&this.#e.classList.contains(b)&&n.push(this.#e);else if(e===D){let f=this.#e;for(;f&&f.nodeType===o.ELEMENT_NODE;)f.classList.contains(b)&&n.push(f),f=f.parentNode}else if(t.nodeType===o.DOCUMENT_FRAGMENT_NODE){const f=[...t.children];for(const u of f){u.classList.contains(b)&&n.push(u);const m=[...u.getElementsByClassName(b)];n.push(...m)}}else{const f=[...t.getElementsByClassName(b)];n.push(...f)}if(n.length)if(p)for(const f of n)this._matchLeaves(h,f)&&l.add(f);else l=new Set(n);break}case o.SELECTOR_TYPE:{const n=[];if(e===I)this.#e.nodeType===o.ELEMENT_NODE&&this._matchLeaves([c],this.#e)&&n.push(this.#e);else if(e===D){let f=this.#e;for(;f&&f.nodeType===o.ELEMENT_NODE;)this._matchLeaves([c],f)&&n.push(f),f=f.parentNode}else if(r.contentType!=="text/html"||/[*|]/.test(b))d=!0;else if(t.nodeType===o.DOCUMENT_FRAGMENT_NODE){const f=b.toLowerCase(),u=[...t.children];for(const m of u){m.localName===f&&n.push(m);const N=[...m.getElementsByTagName(b)];n.push(...N)}}else{const f=[...t.getElementsByTagName(b)];n.push(...f)}if(n.length)if(p)for(const f of n)this._matchLeaves(h,f)&&l.add(f);else l=new Set(n);break}case o.SELECTOR_PSEUDO_ELEMENT:{this._matchPseudoElementSelector(b);break}default:{const n=[];if(e!==D&&o.REG_SHADOW_HOST.test(b)){if(i&&this.#e.nodeType===o.DOCUMENT_FRAGMENT_NODE){const f=this._matchShadowHostPseudoClass(c,this.#e);f&&n.push(f)}}else if(e===I)this._matchLeaves([c],this.#e)&&n.push(this.#e);else if(e===D){let f=this.#e;for(;f;)this._matchLeaves([c],f)&&n.push(f),f=f.parentNode}else d=!0;if(n.length)if(p)for(const f of n)this._matchLeaves(h,f)&&l.add(f);else l=new Set(n)}}return{nodes:l,pending:d}}_getFirstTwig(s){const e=s.length-1,c=s[0];let h,a;if(e){const b=s[e],{leaves:[{type:p}]}=b;p===o.SELECTOR_PSEUDO_ELEMENT||p===o.SELECTOR_ID?(h="prev",a=b):(h="next",a=c)}else h="prev",a=c;return{find:h,twig:a}}_collectNodes(s){const e=this.#i.values();if(s===R||s===O){const c=new Set;let h=0;for(const{branch:a}of e){const{find:b,twig:p}=this._getFirstTwig(a),{nodes:r,pending:t}=this._findNodes(p,s);r.size?this.#r[h]=r:t?c.add(new Map([["index",h],["twig",p]])):this.#i[h].skip=!0,this.#i[h].find=b,h++}if(c.size){const{document:a,root:b}=this.#t,p=a.createNodeIterator(b,o.SHOW_ELEMENT);let r=p.nextNode();for(;r;){let t=!1;if(this.#e.nodeType===o.ELEMENT_NODE?t=(0,E.isSameOrDescendant)(r,this.#e):t=!0,t)for(const i of c){const{leaves:l}=i.get("twig");if(this._matchLeaves(l,r)){const n=i.get("index");this.#r[n].add(r)}}r=p.nextNode()}}}else{let c=0;for(const{branch:h}of e){const a=h[h.length-1],{nodes:b}=this._findNodes(a,s);b.size?this.#r[c]=b:this.#i[c].skip=!0,this.#i[c].find="prev",c++}}return[this.#i,this.#r]}_sortNodes(s){const e=[...s];return e.length>1&&e.sort((c,h)=>{const a=c.compareDocumentPosition(h);let b;return a&o.DOCUMENT_POSITION_PRECEDING||a&o.DOCUMENT_POSITION_CONTAINS?b=1:b=-1,b}),e}_matchNodes(s){const[...e]=this.#i,c=e.length;let h=new Set;for(let a=0;a<c;a++){const{branch:b,find:p,skip:r}=e[a],t=b.length;if(!r&&t){const i=this.#r[a],l=t-1;if(l===0)if((s===R||s===O)&&this.#e.nodeType===o.ELEMENT_NODE){for(const d of i)if(d!==this.#e&&(0,E.isSameOrDescendant)(d,this.#e)&&(h.add(d),s===O))break}else if(s===O){const[d]=this._sortNodes(i);h.add(d)}else{const d=[...h],n=[...i];h=new Set([...d,...n])}else if(p==="next"){let{combo:d}=b[0];for(const n of i){let f=new Set([n]);for(let u=1;u<t;u++){const{combo:m,leaves:N}=b[u],k=[];for(const A of f){const L={combo:d,leaves:N},$=this._matchCombinator(L,A,{find:p});$.size&&k.push(...$)}if(k.length)if(u===l){if(s===O){const[A]=this._sortNodes(k);h.add(A)}else{const A=[...h];h=new Set([...A,...k])}break}else d=m,f=new Set(k);else break}}}else for(const d of i){let n=new Set([d]),f;for(let u=l-1;u>=0;u--){const m=b[u],N=[];for(const k of n){const A=this._matchCombinator(m,k,{find:p});A.size&&N.push(...A)}if(N.length)if(f=!0,u===0){h.add(d);break}else n=new Set(N);else{f=!1;break}}if(f&&s!==R)break}}}return h}_find(s){return this._collectNodes(s),this._matchNodes(s)}matches(){if(this.#e.nodeType!==o.ELEMENT_NODE)throw new TypeError(`Unexpected node ${this.#e.nodeName}`);let s;try{s=this._find(I).has(this.#e)}catch(e){this._onError(e)}return!!s}closest(){if(this.#e.nodeType!==o.ELEMENT_NODE)throw new TypeError(`Unexpected node ${this.#e.nodeName}`);let s;try{const e=this._find(D);let c=this.#e;for(;c;){if(e.has(c)){s=c;break}c=c.parentNode}}catch(e){this._onError(e)}return s??null}querySelector(){let s;try{const e=this._find(O);e.delete(this.#e),e.size>1?[s]=this._sortNodes(e):e.size&&([s]=[...e])}catch(e){this._onError(e)}return s??null}querySelectorAll(){const s=[];try{const e=this._find(R);e.delete(this.#e),e.size>1&&this.#c?s.push(...this._sortNodes(e)):e.size&&s.push(...e)}catch(e){this._onError(e)}return s}}0&&(module.exports={Matcher});
|
|
2
2
|
//# sourceMappingURL=matcher.js.map
|