You cannot select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
1 line
251 KiB
Plaintext
1 line
251 KiB
Plaintext
|
3 months ago
|
{"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 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/* DOMSelector */\nexport class DOMSelector {\n /* private fields */\n #window;\n #document;\n #finder;\n #idlUtils;\n #nwsapi;\n #cache;\n\n /**\n * constructor\n * @param {object} window - window\n * @param {object} document - document\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 * @typedef CheckResult\n * @type {object}\n * @property {boolean} match - match result excluding pseudo-element selector\n * @property {string?} pseudoElement - pseudo-element selector\n */\n\n /**\n * check\n * @param {string} selector - CSS selector\n * @param {object} node - Element node\n * @param {object} [opt] - options\n * @returns {CheckResult} - 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 * matches\n * @param {string} selector - CSS selector\n * @param {object} node - Element node\n * @param {object} [opt] - options\n * @returns {boolean} - `true` if matched `false` otherwise\n */\n matches(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 ===
|