codigo0/node_modules/@asamuzakjp/dom-selector/dist/cjs/index.cjs.map

1 line
255 KiB
Plaintext
Raw Normal View History

{"version":3,"sources":["../../src/index.js","../../src/js/parser.js","../../src/js/utility.js","../../src/js/constant.js","../../src/js/matcher.js","../../src/js/finder.js"],"sourcesContent":["/*!\n * DOM Selector - A CSS selector engine.\n * @license MIT\n * @copyright asamuzaK (Kazz)\n * @see {@link https://github.com/asamuzaK/domSelector/blob/main/LICENSE}\n */\n\n/* import */\nimport { LRUCache } from 'lru-cache';\nimport { Finder } from './js/finder.js';\nimport { filterSelector, getType, initNwsapi } from './js/utility.js';\n\n/* constants */\nimport {\n DOCUMENT_NODE,\n DOCUMENT_FRAGMENT_NODE,\n ELEMENT_NODE,\n TARGET_ALL,\n TARGET_FIRST,\n TARGET_LINEAL,\n TARGET_SELF\n} from './js/constant.js';\nconst MAX_CACHE = 1024;\n\n/**\n * @typedef {object} CheckResult\n * @property {boolean} match - The match result.\n * @property {string?} pseudoElement - The pseudo-element, if any.\n */\n\n/* DOMSelector */\nexport class DOMSelector {\n /* private fields */\n #window;\n #document;\n #finder;\n #idlUtils;\n #nwsapi;\n #cache;\n\n /**\n * Creates an instance of DOMSelector.\n * @param {Window} window - The window object.\n * @param {Document} document - The document object.\n * @param {object} [opt] - Options.\n */\n constructor(window, document, opt = {}) {\n const { idlUtils } = opt;\n this.#window = window;\n this.#document = document ?? window.document;\n this.#finder = new Finder(window);\n this.#idlUtils = idlUtils;\n this.#nwsapi = initNwsapi(window, document);\n this.#cache = new LRUCache({\n max: MAX_CACHE\n });\n }\n\n /**\n * Clears the internal cache of finder results.\n * @returns {void}\n */\n clear = () => {\n this.#finder.clearResults(true);\n };\n\n /**\n * Checks if an element matches a CSS selector.\n * @param {string} selector - The CSS selector to check against.\n * @param {Element} node - The element node to check.\n * @param {object} [opt] - Optional parameters.\n * @returns {CheckResult} An object containing the check result.\n */\n check = (selector, node, opt = {}) => {\n if (!node?.nodeType) {\n const e = new this.#window.TypeError(`Unexpected type ${getType(node)}`);\n return this.#finder.onError(e, opt);\n } else if (node.nodeType !== ELEMENT_NODE) {\n const e = new this.#window.TypeError(`Unexpected node ${node.nodeName}`);\n return this.#finder.onError(e, opt);\n }\n const document = node.ownerDocument;\n if (\n document === this.#document &&\n document.contentType === 'text/html' &&\n document.documentElement &&\n node.parentNode\n ) {\n const cacheKey = `check_${selector}`;\n let filterMatches = false;\n if (this.#cache.has(cacheKey)) {\n filterMatches = this.#cache.get(cacheKey);\n } else {\n filterMatches = filterSelector(selector, TARGET_SELF);\n this.#cache.set(cacheKey, filterMatches);\n }\n if (filterMatches) {\n try {\n const n = this.#idlUtils ? this.#idlUtils.wrapperForImpl(node) : node;\n const match = this.#nwsapi.match(selector, n);\n return {\n match,\n pseudoElement: null\n };\n } catch (e) {\n // fall through\n }\n }\n }\n let res;\n try {\n if (this.#idlUtils) {\n node = this.#idlUtils.wrapperForImpl(node);\n }\n opt.check = true;\n opt.noexept = true;\n opt.warn = false;\n this.#finder.setup(selector, node, opt);\n res = this.#finder.find(TARGET_SELF);\n } catch (e) {\n this.#finder.onError(e, opt);\n }\n return res;\n };\n\n /**\n * Returns true if the element matches the selector.\n * @param {string} selector - The CSS selector to match against.\n * @param {Element} node - The element node to test.\n * @param {object} [opt] - Optional parameters.\n * @returns {boolean} `true` if the element matches, or `false` otherwise.\n */\n matches = (selector, node, opt = {}) => {\n if (!node?.nodeType) {\n const e