@asamuzakjp/dom-selector 6.6.2 → 6.7.2
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/index.cjs +70 -54
- package/dist/cjs/index.cjs.map +1 -1
- package/dist/cjs/index.d.cts +12 -10
- package/package.json +6 -6
- package/src/index.js +51 -44
- package/src/js/finder.js +16 -7
- package/src/js/matcher.js +13 -6
- package/types/index.d.ts +11 -9
- package/types/js/finder.d.ts +1 -0
package/dist/cjs/index.cjs
CHANGED
|
@@ -1326,7 +1326,9 @@ var matchAttributeSelector = (ast, node, opt = {}) => {
|
|
|
1326
1326
|
}
|
|
1327
1327
|
case "*": {
|
|
1328
1328
|
if (itemName.indexOf(":") > -1) {
|
|
1329
|
-
|
|
1329
|
+
const [, ...restItemName] = itemName.split(":");
|
|
1330
|
+
const itemLocalName = restItemName.join(":").replace(/^:/, "");
|
|
1331
|
+
if (itemLocalName === astLocalName) {
|
|
1330
1332
|
attrValues.add(itemValue);
|
|
1331
1333
|
}
|
|
1332
1334
|
} else if (astLocalName === itemName) {
|
|
@@ -1347,7 +1349,8 @@ var matchAttributeSelector = (ast, node, opt = {}) => {
|
|
|
1347
1349
|
);
|
|
1348
1350
|
}
|
|
1349
1351
|
if (itemName.indexOf(":") > -1) {
|
|
1350
|
-
const [itemPrefix,
|
|
1352
|
+
const [itemPrefix, ...restItemName] = itemName.split(":");
|
|
1353
|
+
const itemLocalName = restItemName.join(":").replace(/^:/, "");
|
|
1351
1354
|
if (itemPrefix === "xml" && itemLocalName === "lang") {
|
|
1352
1355
|
continue;
|
|
1353
1356
|
} else if (astPrefix === itemPrefix && astLocalName === itemLocalName) {
|
|
@@ -1367,8 +1370,11 @@ var matchAttributeSelector = (ast, node, opt = {}) => {
|
|
|
1367
1370
|
itemValue = itemValue.toLowerCase();
|
|
1368
1371
|
}
|
|
1369
1372
|
if (itemName.indexOf(":") > -1) {
|
|
1370
|
-
const [itemPrefix,
|
|
1371
|
-
|
|
1373
|
+
const [itemPrefix, ...restItemName] = itemName.split(":");
|
|
1374
|
+
const itemLocalName = restItemName.join(":").replace(/^:/, "");
|
|
1375
|
+
if (!itemPrefix && astAttrName === `:${itemLocalName}`) {
|
|
1376
|
+
attrValues.add(itemValue);
|
|
1377
|
+
} else if (itemPrefix === "xml" && itemLocalName === "lang") {
|
|
1372
1378
|
continue;
|
|
1373
1379
|
} else if (astAttrName === itemLocalName) {
|
|
1374
1380
|
attrValues.add(itemValue);
|
|
@@ -1599,8 +1605,6 @@ var Finder = class {
|
|
|
1599
1605
|
this.#window = window;
|
|
1600
1606
|
this.#astCache = /* @__PURE__ */ new WeakMap();
|
|
1601
1607
|
this.#documentCache = /* @__PURE__ */ new WeakMap();
|
|
1602
|
-
this.#invalidateResults = /* @__PURE__ */ new WeakMap();
|
|
1603
|
-
this.#results = /* @__PURE__ */ new WeakMap();
|
|
1604
1608
|
this.#event = null;
|
|
1605
1609
|
this.#focus = null;
|
|
1606
1610
|
this.#lastFocusVisible = null;
|
|
@@ -1619,6 +1623,7 @@ var Finder = class {
|
|
|
1619
1623
|
}
|
|
1620
1624
|
]);
|
|
1621
1625
|
this._registerEventListeners();
|
|
1626
|
+
this.clearResults(true);
|
|
1622
1627
|
}
|
|
1623
1628
|
/**
|
|
1624
1629
|
* Handles errors.
|
|
@@ -1668,14 +1673,25 @@ var Finder = class {
|
|
|
1668
1673
|
this.#node = node;
|
|
1669
1674
|
this.#selector = selector;
|
|
1670
1675
|
[this.#ast, this.#nodes] = this._correspond(selector);
|
|
1671
|
-
this.#invalidateResults = /* @__PURE__ */ new WeakMap();
|
|
1672
1676
|
this.#pseudoElement = [];
|
|
1673
1677
|
this.#walkers = /* @__PURE__ */ new WeakMap();
|
|
1674
1678
|
this.#nodeWalker = null;
|
|
1675
1679
|
this.#rootWalker = null;
|
|
1676
1680
|
this.#verifyShadowHost = null;
|
|
1681
|
+
this.clearResults();
|
|
1677
1682
|
return this;
|
|
1678
1683
|
};
|
|
1684
|
+
/**
|
|
1685
|
+
* Clear cached results.
|
|
1686
|
+
* @param {boolean} all - clear all results
|
|
1687
|
+
* @returns {void}
|
|
1688
|
+
*/
|
|
1689
|
+
clearResults = (all = false) => {
|
|
1690
|
+
this.#invalidateResults = /* @__PURE__ */ new WeakMap();
|
|
1691
|
+
if (all) {
|
|
1692
|
+
this.#results = /* @__PURE__ */ new WeakMap();
|
|
1693
|
+
}
|
|
1694
|
+
};
|
|
1679
1695
|
/**
|
|
1680
1696
|
* Handles focus events.
|
|
1681
1697
|
* @private
|
|
@@ -2385,6 +2401,7 @@ var Finder = class {
|
|
|
2385
2401
|
break;
|
|
2386
2402
|
}
|
|
2387
2403
|
case "current":
|
|
2404
|
+
case "heading":
|
|
2388
2405
|
case "nth-col":
|
|
2389
2406
|
case "nth-last-col": {
|
|
2390
2407
|
if (warn) {
|
|
@@ -2966,6 +2983,7 @@ var Finder = class {
|
|
|
2966
2983
|
case "fullscreen":
|
|
2967
2984
|
case "future":
|
|
2968
2985
|
case "has-slotted":
|
|
2986
|
+
case "heading":
|
|
2969
2987
|
case "modal":
|
|
2970
2988
|
case "muted":
|
|
2971
2989
|
case "past":
|
|
@@ -3782,9 +3800,6 @@ var Finder = class {
|
|
|
3782
3800
|
break;
|
|
3783
3801
|
}
|
|
3784
3802
|
case CLASS_SELECTOR: {
|
|
3785
|
-
if (!complex && !filterLeaves.length) {
|
|
3786
|
-
this.#invalidate = true;
|
|
3787
|
-
}
|
|
3788
3803
|
result = this._findEntryNodesForClass(leaves, targetType, {
|
|
3789
3804
|
complex,
|
|
3790
3805
|
precede
|
|
@@ -4317,10 +4332,10 @@ var DOMSelector = class {
|
|
|
4317
4332
|
#nwsapi;
|
|
4318
4333
|
#cache;
|
|
4319
4334
|
/**
|
|
4320
|
-
*
|
|
4321
|
-
* @param {
|
|
4322
|
-
* @param {
|
|
4323
|
-
* @param {object} [opt] -
|
|
4335
|
+
* Creates an instance of DOMSelector.
|
|
4336
|
+
* @param {Window} window - The window object.
|
|
4337
|
+
* @param {Document} document - The document object.
|
|
4338
|
+
* @param {object} [opt] - Options.
|
|
4324
4339
|
*/
|
|
4325
4340
|
constructor(window, document, opt = {}) {
|
|
4326
4341
|
const { idlUtils } = opt;
|
|
@@ -4334,19 +4349,20 @@ var DOMSelector = class {
|
|
|
4334
4349
|
});
|
|
4335
4350
|
}
|
|
4336
4351
|
/**
|
|
4337
|
-
*
|
|
4338
|
-
* @
|
|
4339
|
-
* @property {boolean} match - match result excluding pseudo-element selector
|
|
4340
|
-
* @property {string?} pseudoElement - pseudo-element selector
|
|
4352
|
+
* Clears the internal cache of finder results.
|
|
4353
|
+
* @returns {void}
|
|
4341
4354
|
*/
|
|
4355
|
+
clear = () => {
|
|
4356
|
+
this.#finder.clearResults(true);
|
|
4357
|
+
};
|
|
4342
4358
|
/**
|
|
4343
|
-
*
|
|
4344
|
-
* @param {string} selector - CSS selector
|
|
4345
|
-
* @param {
|
|
4346
|
-
* @param {object} [opt] -
|
|
4347
|
-
* @returns {CheckResult}
|
|
4359
|
+
* Checks if an element matches a CSS selector.
|
|
4360
|
+
* @param {string} selector - The CSS selector to check against.
|
|
4361
|
+
* @param {Element} node - The element node to check.
|
|
4362
|
+
* @param {object} [opt] - Optional parameters.
|
|
4363
|
+
* @returns {CheckResult} An object containing the check result.
|
|
4348
4364
|
*/
|
|
4349
|
-
check(selector, node, opt = {}) {
|
|
4365
|
+
check = (selector, node, opt = {}) => {
|
|
4350
4366
|
if (!node?.nodeType) {
|
|
4351
4367
|
const e = new this.#window.TypeError(`Unexpected type ${getType(node)}`);
|
|
4352
4368
|
return this.#finder.onError(e, opt);
|
|
@@ -4390,15 +4406,15 @@ var DOMSelector = class {
|
|
|
4390
4406
|
this.#finder.onError(e, opt);
|
|
4391
4407
|
}
|
|
4392
4408
|
return res;
|
|
4393
|
-
}
|
|
4409
|
+
};
|
|
4394
4410
|
/**
|
|
4395
|
-
* matches
|
|
4396
|
-
* @param {string} selector - CSS selector
|
|
4397
|
-
* @param {
|
|
4398
|
-
* @param {object} [opt] -
|
|
4399
|
-
* @returns {boolean}
|
|
4411
|
+
* Returns true if the element matches the selector.
|
|
4412
|
+
* @param {string} selector - The CSS selector to match against.
|
|
4413
|
+
* @param {Element} node - The element node to test.
|
|
4414
|
+
* @param {object} [opt] - Optional parameters.
|
|
4415
|
+
* @returns {boolean} `true` if the element matches, or `false` otherwise.
|
|
4400
4416
|
*/
|
|
4401
|
-
matches(selector, node, opt = {}) {
|
|
4417
|
+
matches = (selector, node, opt = {}) => {
|
|
4402
4418
|
if (!node?.nodeType) {
|
|
4403
4419
|
const e = new this.#window.TypeError(`Unexpected type ${getType(node)}`);
|
|
4404
4420
|
return this.#finder.onError(e, opt);
|
|
@@ -4437,15 +4453,15 @@ var DOMSelector = class {
|
|
|
4437
4453
|
this.#finder.onError(e, opt);
|
|
4438
4454
|
}
|
|
4439
4455
|
return !!res;
|
|
4440
|
-
}
|
|
4456
|
+
};
|
|
4441
4457
|
/**
|
|
4442
|
-
*
|
|
4443
|
-
* @param {string} selector - CSS selector
|
|
4444
|
-
* @param {
|
|
4445
|
-
* @param {object} [opt] -
|
|
4446
|
-
* @returns {?
|
|
4458
|
+
* Traverses up the DOM tree to find the first node that matches the selector.
|
|
4459
|
+
* @param {string} selector - The CSS selector to match against.
|
|
4460
|
+
* @param {Element} node - The element from which to start traversing.
|
|
4461
|
+
* @param {object} [opt] - Optional parameters.
|
|
4462
|
+
* @returns {?Element} The first matching ancestor element, or `null`.
|
|
4447
4463
|
*/
|
|
4448
|
-
closest(selector, node, opt = {}) {
|
|
4464
|
+
closest = (selector, node, opt = {}) => {
|
|
4449
4465
|
if (!node?.nodeType) {
|
|
4450
4466
|
const e = new this.#window.TypeError(`Unexpected type ${getType(node)}`);
|
|
4451
4467
|
return this.#finder.onError(e, opt);
|
|
@@ -4493,15 +4509,15 @@ var DOMSelector = class {
|
|
|
4493
4509
|
this.#finder.onError(e, opt);
|
|
4494
4510
|
}
|
|
4495
4511
|
return res ?? null;
|
|
4496
|
-
}
|
|
4512
|
+
};
|
|
4497
4513
|
/**
|
|
4498
|
-
*
|
|
4499
|
-
* @param {string} selector - CSS selector
|
|
4500
|
-
* @param {
|
|
4501
|
-
* @param {object} [opt] -
|
|
4502
|
-
* @returns {?
|
|
4514
|
+
* Returns the first element within the subtree that matches the selector.
|
|
4515
|
+
* @param {string} selector - The CSS selector to match.
|
|
4516
|
+
* @param {Document|DocumentFragment|Element} node - The node to find within.
|
|
4517
|
+
* @param {object} [opt] - Optional parameters.
|
|
4518
|
+
* @returns {?Element} The first matching element, or `null`.
|
|
4503
4519
|
*/
|
|
4504
|
-
querySelector(selector, node, opt = {}) {
|
|
4520
|
+
querySelector = (selector, node, opt = {}) => {
|
|
4505
4521
|
if (!node?.nodeType) {
|
|
4506
4522
|
const e = new this.#window.TypeError(`Unexpected type ${getType(node)}`);
|
|
4507
4523
|
return this.#finder.onError(e, opt);
|
|
@@ -4520,16 +4536,16 @@ var DOMSelector = class {
|
|
|
4520
4536
|
this.#finder.onError(e, opt);
|
|
4521
4537
|
}
|
|
4522
4538
|
return res ?? null;
|
|
4523
|
-
}
|
|
4539
|
+
};
|
|
4524
4540
|
/**
|
|
4525
|
-
*
|
|
4526
|
-
*
|
|
4527
|
-
* @param {string} selector - CSS selector
|
|
4528
|
-
* @param {
|
|
4529
|
-
* @param {object} [opt] -
|
|
4530
|
-
* @returns {Array
|
|
4541
|
+
* Returns an array of elements within the subtree that match the selector.
|
|
4542
|
+
* Note: This method returns an Array, not a NodeList.
|
|
4543
|
+
* @param {string} selector - The CSS selector to match.
|
|
4544
|
+
* @param {Document|DocumentFragment|Element} node - The node to find within.
|
|
4545
|
+
* @param {object} [opt] - Optional parameters.
|
|
4546
|
+
* @returns {Array<Element>} An array of elements, or an empty array.
|
|
4531
4547
|
*/
|
|
4532
|
-
querySelectorAll(selector, node, opt = {}) {
|
|
4548
|
+
querySelectorAll = (selector, node, opt = {}) => {
|
|
4533
4549
|
if (!node?.nodeType) {
|
|
4534
4550
|
const e = new this.#window.TypeError(`Unexpected type ${getType(node)}`);
|
|
4535
4551
|
return this.#finder.onError(e, opt);
|
|
@@ -4567,7 +4583,7 @@ var DOMSelector = class {
|
|
|
4567
4583
|
this.#finder.onError(e, opt);
|
|
4568
4584
|
}
|
|
4569
4585
|
return res ?? [];
|
|
4570
|
-
}
|
|
4586
|
+
};
|
|
4571
4587
|
};
|
|
4572
4588
|
// Annotate the CommonJS export names for ESM import in node:
|
|
4573
4589
|
0 && (module.exports = {
|