@whitesev/domutils 1.3.5 → 1.3.6
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/index.amd.js +41 -0
- package/dist/index.amd.js.map +1 -1
- package/dist/index.cjs.js +41 -0
- package/dist/index.cjs.js.map +1 -1
- package/dist/index.esm.js +41 -0
- package/dist/index.esm.js.map +1 -1
- package/dist/index.iife.js +41 -0
- package/dist/index.iife.js.map +1 -1
- package/dist/index.system.js +41 -0
- package/dist/index.system.js.map +1 -1
- package/dist/index.umd.js +41 -0
- package/dist/index.umd.js.map +1 -1
- package/dist/types/src/DOMUtilsEvent.d.ts +38 -0
- package/package.json +1 -1
- package/src/DOMUtils.ts +1 -1
- package/src/DOMUtilsEvent.ts +92 -0
|
@@ -575,4 +575,42 @@ export declare class DOMUtilsEvent {
|
|
|
575
575
|
listenKeyboard(element: DOMUtilsTargetElementType | Window | Node | typeof globalThis, eventName: ("keyup" | "keypress" | "keydown") | undefined, callback: (keyName: string, keyValue: number, otherCodeList: string[], event: KeyboardEvent) => void, options?: AddEventListenerOptions | boolean): {
|
|
576
576
|
removeListen(): void;
|
|
577
577
|
};
|
|
578
|
+
/**
|
|
579
|
+
* 选择器,可使用以下的额外语法
|
|
580
|
+
*
|
|
581
|
+
* + :contains([text]) 作用: 找到包含指定文本内容的指定元素
|
|
582
|
+
* + :empty 作用:找到既没有文本内容也没有子元素的指定元素
|
|
583
|
+
* + :regexp([text]) 作用: 找到符合正则表达式的内容的指定元素
|
|
584
|
+
* @param selector
|
|
585
|
+
* @example
|
|
586
|
+
* DOMUtils.selector("div:contains('测试')")
|
|
587
|
+
* > div.xxx
|
|
588
|
+
* @example
|
|
589
|
+
* DOMUtils.selector("div:empty")
|
|
590
|
+
* > div.xxx
|
|
591
|
+
* @example
|
|
592
|
+
* DOMUtils.selector("div:regexp('^xxxx$')")
|
|
593
|
+
* > div.xxx
|
|
594
|
+
*/
|
|
595
|
+
selector<K extends keyof HTMLElementTagNameMap>(selector: K): HTMLElementTagNameMap[K] | undefined;
|
|
596
|
+
selector<E extends Element = Element>(selector: string): E | undefined;
|
|
597
|
+
/**
|
|
598
|
+
* 选择器,可使用以下的额外语法
|
|
599
|
+
*
|
|
600
|
+
* + :contains([text]) 作用: 找到包含指定文本内容的指定元素
|
|
601
|
+
* + :empty 作用:找到既没有文本内容也没有子元素的指定元素
|
|
602
|
+
* + :regexp([text]) 作用: 找到符合正则表达式的内容的指定元素
|
|
603
|
+
* @param selector
|
|
604
|
+
* @example
|
|
605
|
+
* DOMUtils.selectorAll("div:contains('测试')")
|
|
606
|
+
* > [div.xxx]
|
|
607
|
+
* @example
|
|
608
|
+
* DOMUtils.selectorAll("div:empty")
|
|
609
|
+
* > [div.xxx]
|
|
610
|
+
* @example
|
|
611
|
+
* DOMUtils.selectorAll("div:regexp('^xxxx$')")
|
|
612
|
+
* > [div.xxx]
|
|
613
|
+
*/
|
|
614
|
+
selectorAll<K extends keyof HTMLElementTagNameMap>(selector: K): HTMLElementTagNameMap[K][];
|
|
615
|
+
selectorAll<E extends Element = Element>(selector: string): E[];
|
|
578
616
|
}
|
package/package.json
CHANGED
package/src/DOMUtils.ts
CHANGED
package/src/DOMUtilsEvent.ts
CHANGED
|
@@ -1426,4 +1426,96 @@ export class DOMUtilsEvent {
|
|
|
1426
1426
|
},
|
|
1427
1427
|
};
|
|
1428
1428
|
}
|
|
1429
|
+
/**
|
|
1430
|
+
* 选择器,可使用以下的额外语法
|
|
1431
|
+
*
|
|
1432
|
+
* + :contains([text]) 作用: 找到包含指定文本内容的指定元素
|
|
1433
|
+
* + :empty 作用:找到既没有文本内容也没有子元素的指定元素
|
|
1434
|
+
* + :regexp([text]) 作用: 找到符合正则表达式的内容的指定元素
|
|
1435
|
+
* @param selector
|
|
1436
|
+
* @example
|
|
1437
|
+
* DOMUtils.selector("div:contains('测试')")
|
|
1438
|
+
* > div.xxx
|
|
1439
|
+
* @example
|
|
1440
|
+
* DOMUtils.selector("div:empty")
|
|
1441
|
+
* > div.xxx
|
|
1442
|
+
* @example
|
|
1443
|
+
* DOMUtils.selector("div:regexp('^xxxx$')")
|
|
1444
|
+
* > div.xxx
|
|
1445
|
+
*/
|
|
1446
|
+
selector<K extends keyof HTMLElementTagNameMap>(
|
|
1447
|
+
selector: K
|
|
1448
|
+
): HTMLElementTagNameMap[K] | undefined;
|
|
1449
|
+
selector<E extends Element = Element>(selector: string): E | undefined;
|
|
1450
|
+
selector<E extends Element = Element>(selector: string) {
|
|
1451
|
+
return this.selectorAll<E>(selector)[0];
|
|
1452
|
+
}
|
|
1453
|
+
/**
|
|
1454
|
+
* 选择器,可使用以下的额外语法
|
|
1455
|
+
*
|
|
1456
|
+
* + :contains([text]) 作用: 找到包含指定文本内容的指定元素
|
|
1457
|
+
* + :empty 作用:找到既没有文本内容也没有子元素的指定元素
|
|
1458
|
+
* + :regexp([text]) 作用: 找到符合正则表达式的内容的指定元素
|
|
1459
|
+
* @param selector
|
|
1460
|
+
* @example
|
|
1461
|
+
* DOMUtils.selectorAll("div:contains('测试')")
|
|
1462
|
+
* > [div.xxx]
|
|
1463
|
+
* @example
|
|
1464
|
+
* DOMUtils.selectorAll("div:empty")
|
|
1465
|
+
* > [div.xxx]
|
|
1466
|
+
* @example
|
|
1467
|
+
* DOMUtils.selectorAll("div:regexp('^xxxx$')")
|
|
1468
|
+
* > [div.xxx]
|
|
1469
|
+
*/
|
|
1470
|
+
selectorAll<K extends keyof HTMLElementTagNameMap>(
|
|
1471
|
+
selector: K
|
|
1472
|
+
): HTMLElementTagNameMap[K][];
|
|
1473
|
+
selectorAll<E extends Element = Element>(selector: string): E[];
|
|
1474
|
+
selectorAll<E extends Element = Element>(selector: string) {
|
|
1475
|
+
const context = this;
|
|
1476
|
+
selector = selector.trim();
|
|
1477
|
+
if (selector.match(/[^\s]{1}:empty$/gi)) {
|
|
1478
|
+
// empty 语法
|
|
1479
|
+
selector = selector.replace(/:empty$/gi, "");
|
|
1480
|
+
return Array.from(
|
|
1481
|
+
context.windowApi.document.querySelectorAll<E>(selector)
|
|
1482
|
+
).filter(($ele) => {
|
|
1483
|
+
return $ele?.innerHTML?.trim() === "";
|
|
1484
|
+
});
|
|
1485
|
+
} else if (
|
|
1486
|
+
selector.match(/[^\s]{1}:contains\("(.*)"\)$/gi) ||
|
|
1487
|
+
selector.match(/[^\s]{1}:contains\('(.*)'\)$/gi)
|
|
1488
|
+
) {
|
|
1489
|
+
// contains 语法
|
|
1490
|
+
let textMatch = selector.match(/:contains\(("|')(.*)("|')\)$/i);
|
|
1491
|
+
let text = textMatch![2];
|
|
1492
|
+
selector = selector.replace(/:contains\(("|')(.*)("|')\)$/gi, "");
|
|
1493
|
+
return Array.from(
|
|
1494
|
+
context.windowApi.document.querySelectorAll<E>(selector)
|
|
1495
|
+
).filter(($ele) => {
|
|
1496
|
+
// @ts-ignore
|
|
1497
|
+
return ($ele?.textContent || $ele?.innerText)?.includes(text);
|
|
1498
|
+
});
|
|
1499
|
+
} else if (
|
|
1500
|
+
selector.match(/[^\s]{1}:regexp\("(.*)"\)$/gi) ||
|
|
1501
|
+
selector.match(/[^\s]{1}:regexp\('(.*)'\)$/gi)
|
|
1502
|
+
) {
|
|
1503
|
+
// regexp 语法
|
|
1504
|
+
let textMatch = selector.match(/:regexp\(("|')(.*)("|')\)$/i);
|
|
1505
|
+
let text = textMatch![2];
|
|
1506
|
+
let regexp = new RegExp(text);
|
|
1507
|
+
selector = selector.replace(/:regexp\(("|')(.*)("|')\)$/gi, "");
|
|
1508
|
+
return Array.from(
|
|
1509
|
+
context.windowApi.document.querySelectorAll<E>(selector)
|
|
1510
|
+
).filter(($ele) => {
|
|
1511
|
+
// @ts-ignore
|
|
1512
|
+
return Boolean(($ele?.textContent || $ele?.innerText)?.match(regexp));
|
|
1513
|
+
});
|
|
1514
|
+
} else {
|
|
1515
|
+
// 普通语法
|
|
1516
|
+
return Array.from(
|
|
1517
|
+
context.windowApi.document.querySelectorAll<E>(selector)
|
|
1518
|
+
);
|
|
1519
|
+
}
|
|
1520
|
+
}
|
|
1429
1521
|
}
|