@whitesev/domutils 1.5.5 → 1.5.7

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 CHANGED
@@ -460,6 +460,13 @@ define((function () { 'use strict';
460
460
  globalThis.clearInterval(timeId);
461
461
  }
462
462
  },
463
+ /**
464
+ * 判断是否是元素列表
465
+ * @param $ele
466
+ */
467
+ isNodeList($ele) {
468
+ return Array.isArray($ele) || $ele instanceof NodeList;
469
+ },
463
470
  };
464
471
 
465
472
  /* 数据 */
@@ -586,12 +593,12 @@ define((function () { 'use strict';
586
593
  : elementItem;
587
594
  let findValue = selectorList.find((selectorItem) => {
588
595
  // 判断目标元素是否匹配选择器
589
- if (eventTarget?.matches(selectorItem)) {
596
+ if (DOMUtilsContext.matches(eventTarget, selectorItem)) {
590
597
  /* 当前目标可以被selector所匹配到 */
591
598
  return true;
592
599
  }
593
600
  /* 在上层与主元素之间寻找可以被selector所匹配到的 */
594
- let $closestMatches = eventTarget?.closest(selectorItem);
601
+ let $closestMatches = DOMUtilsContext.closest(eventTarget, selectorItem);
595
602
  if ($closestMatches && totalParent?.contains($closestMatches)) {
596
603
  eventTarget = $closestMatches;
597
604
  return true;
@@ -973,7 +980,7 @@ define((function () { 'use strict';
973
980
  if (element == null) {
974
981
  return;
975
982
  }
976
- if (isNodeList(element)) {
983
+ if (DOMUtilsCommonUtils.isNodeList(element)) {
977
984
  // 设置
978
985
  element.forEach(($ele) => {
979
986
  DOMUtilsContext.click($ele, handler, details, useDispatchToTriggerEvent);
@@ -1009,7 +1016,7 @@ define((function () { 'use strict';
1009
1016
  if (element == null) {
1010
1017
  return;
1011
1018
  }
1012
- if (isNodeList(element)) {
1019
+ if (DOMUtilsCommonUtils.isNodeList(element)) {
1013
1020
  // 设置
1014
1021
  element.forEach(($ele) => {
1015
1022
  DOMUtilsContext.focus($ele, handler, details, useDispatchToTriggerEvent);
@@ -1045,7 +1052,7 @@ define((function () { 'use strict';
1045
1052
  if (element == null) {
1046
1053
  return;
1047
1054
  }
1048
- if (isNodeList(element)) {
1055
+ if (DOMUtilsCommonUtils.isNodeList(element)) {
1049
1056
  // 设置
1050
1057
  element.forEach(($ele) => {
1051
1058
  DOMUtilsContext.focus($ele, handler, details, useDispatchToTriggerEvent);
@@ -1081,7 +1088,7 @@ define((function () { 'use strict';
1081
1088
  if (element == null) {
1082
1089
  return;
1083
1090
  }
1084
- if (isNodeList(element)) {
1091
+ if (DOMUtilsCommonUtils.isNodeList(element)) {
1085
1092
  // 设置
1086
1093
  element.forEach(($ele) => {
1087
1094
  DOMUtilsContext.hover($ele, handler, option);
@@ -1114,7 +1121,7 @@ define((function () { 'use strict';
1114
1121
  if (typeof element === "string") {
1115
1122
  element = DOMUtilsContext.selectorAll(element);
1116
1123
  }
1117
- if (isNodeList(element)) {
1124
+ if (DOMUtilsCommonUtils.isNodeList(element)) {
1118
1125
  // 设置
1119
1126
  element.forEach(($ele) => {
1120
1127
  DOMUtilsContext.keyup($ele, handler, option);
@@ -1146,7 +1153,7 @@ define((function () { 'use strict';
1146
1153
  if (typeof element === "string") {
1147
1154
  element = DOMUtilsContext.selectorAll(element);
1148
1155
  }
1149
- if (isNodeList(element)) {
1156
+ if (DOMUtilsCommonUtils.isNodeList(element)) {
1150
1157
  // 设置
1151
1158
  element.forEach(($ele) => {
1152
1159
  DOMUtilsContext.keydown($ele, handler, option);
@@ -1178,7 +1185,7 @@ define((function () { 'use strict';
1178
1185
  if (typeof element === "string") {
1179
1186
  element = DOMUtilsContext.selectorAll(element);
1180
1187
  }
1181
- if (isNodeList(element)) {
1188
+ if (DOMUtilsCommonUtils.isNodeList(element)) {
1182
1189
  // 设置
1183
1190
  element.forEach(($ele) => {
1184
1191
  DOMUtilsContext.keypress($ele, handler, option);
@@ -1332,21 +1339,121 @@ define((function () { 'use strict';
1332
1339
  return Array.from(context.windowApi.document.querySelectorAll(selector));
1333
1340
  }
1334
1341
  }
1342
+ /**
1343
+ * 匹配元素,可使用以下的额外语法
1344
+ */
1345
+ matches($el, selector) {
1346
+ selector = selector.trim();
1347
+ if ($el == null) {
1348
+ return false;
1349
+ }
1350
+ if (selector.match(/[^\s]{1}:empty$/gi)) {
1351
+ // empty 语法
1352
+ selector = selector.replace(/:empty$/gi, "");
1353
+ return $el.matches(selector) && $el?.innerHTML?.trim() === "";
1354
+ }
1355
+ else if (selector.match(/[^\s]{1}:contains\("(.*)"\)$/i) ||
1356
+ selector.match(/[^\s]{1}:contains\('(.*)'\)$/i)) {
1357
+ // contains 语法
1358
+ let textMatch = selector.match(/:contains\(("|')(.*)("|')\)$/i);
1359
+ let text = textMatch[2];
1360
+ selector = selector.replace(/:contains\(("|')(.*)("|')\)$/gi, "");
1361
+ // @ts-ignore
1362
+ let content = $el?.textContent || $el?.innerText;
1363
+ if (typeof content !== "string") {
1364
+ content = "";
1365
+ }
1366
+ return $el.matches(selector) && content?.includes(text);
1367
+ }
1368
+ else if (selector.match(/[^\s]{1}:regexp\("(.*)"\)$/i) ||
1369
+ selector.match(/[^\s]{1}:regexp\('(.*)'\)$/i)) {
1370
+ // regexp 语法
1371
+ let textMatch = selector.match(/:regexp\(("|')(.*)("|')\)$/i);
1372
+ let pattern = textMatch[2];
1373
+ let flagMatch = pattern.match(/("|'),[\s]*("|')([igm]{0,3})$/i);
1374
+ let flags = "";
1375
+ if (flagMatch) {
1376
+ pattern = pattern.replace(/("|'),[\s]*("|')([igm]{0,3})$/gi, "");
1377
+ flags = flagMatch[3];
1378
+ }
1379
+ let regexp = new RegExp(pattern, flags);
1380
+ selector = selector.replace(/:regexp\(("|')(.*)("|')\)$/gi, "");
1381
+ // @ts-ignore
1382
+ let content = $el?.textContent || $el?.innerText;
1383
+ if (typeof content !== "string") {
1384
+ content = "";
1385
+ }
1386
+ return $el.matches(selector) && Boolean(content?.match(regexp));
1387
+ }
1388
+ else {
1389
+ // 普通语法
1390
+ return $el.matches(selector);
1391
+ }
1392
+ }
1393
+ closest($el, selector) {
1394
+ selector = selector.trim();
1395
+ if (selector.match(/[^\s]{1}:empty$/gi)) {
1396
+ // empty 语法
1397
+ selector = selector.replace(/:empty$/gi, "");
1398
+ let $closest = $el?.closest(selector);
1399
+ if ($closest && $closest?.innerHTML?.trim() === "") {
1400
+ return $closest;
1401
+ }
1402
+ return null;
1403
+ }
1404
+ else if (selector.match(/[^\s]{1}:contains\("(.*)"\)$/i) ||
1405
+ selector.match(/[^\s]{1}:contains\('(.*)'\)$/i)) {
1406
+ // contains 语法
1407
+ let textMatch = selector.match(/:contains\(("|')(.*)("|')\)$/i);
1408
+ let text = textMatch[2];
1409
+ selector = selector.replace(/:contains\(("|')(.*)("|')\)$/gi, "");
1410
+ let $closest = $el?.closest(selector);
1411
+ if ($closest) {
1412
+ // @ts-ignore
1413
+ let content = $el?.textContent || $el?.innerText;
1414
+ if (typeof content === "string" && content.includes(text)) {
1415
+ return $closest;
1416
+ }
1417
+ }
1418
+ return null;
1419
+ }
1420
+ else if (selector.match(/[^\s]{1}:regexp\("(.*)"\)$/i) ||
1421
+ selector.match(/[^\s]{1}:regexp\('(.*)'\)$/i)) {
1422
+ // regexp 语法
1423
+ let textMatch = selector.match(/:regexp\(("|')(.*)("|')\)$/i);
1424
+ let pattern = textMatch[2];
1425
+ let flagMatch = pattern.match(/("|'),[\s]*("|')([igm]{0,3})$/i);
1426
+ let flags = "";
1427
+ if (flagMatch) {
1428
+ pattern = pattern.replace(/("|'),[\s]*("|')([igm]{0,3})$/gi, "");
1429
+ flags = flagMatch[3];
1430
+ }
1431
+ let regexp = new RegExp(pattern, flags);
1432
+ selector = selector.replace(/:regexp\(("|')(.*)("|')\)$/gi, "");
1433
+ let $closest = $el?.closest(selector);
1434
+ if ($closest) {
1435
+ // @ts-ignore
1436
+ let content = $el?.textContent || $el?.innerText;
1437
+ if (typeof content === "string" && content.match(regexp)) {
1438
+ return $closest;
1439
+ }
1440
+ }
1441
+ return null;
1442
+ }
1443
+ else {
1444
+ // 普通语法
1445
+ let $closest = $el?.closest(selector);
1446
+ return $closest;
1447
+ }
1448
+ }
1335
1449
  }
1336
1450
 
1337
- /**
1338
- * 判断是否是元素列表
1339
- * @param $ele
1340
- */
1341
- const isNodeList = ($ele) => {
1342
- return Array.isArray($ele) || $ele instanceof NodeList;
1343
- };
1344
1451
  class DOMUtils extends DOMUtilsEvent {
1345
1452
  constructor(option) {
1346
1453
  super(option);
1347
1454
  }
1348
1455
  /** 版本号 */
1349
- version = "2025.5.26";
1456
+ version = "2025.5.30";
1350
1457
  attr(element, attrName, attrValue) {
1351
1458
  let DOMUtilsContext = this;
1352
1459
  if (typeof element === "string") {
@@ -1355,7 +1462,7 @@ define((function () { 'use strict';
1355
1462
  if (element == null) {
1356
1463
  return;
1357
1464
  }
1358
- if (isNodeList(element)) {
1465
+ if (DOMUtilsCommonUtils.isNodeList(element)) {
1359
1466
  if (attrValue == null) {
1360
1467
  // 获取属性
1361
1468
  return DOMUtilsContext.attr(element[0], attrName, attrValue);
@@ -1465,7 +1572,7 @@ define((function () { 'use strict';
1465
1572
  if (element == null) {
1466
1573
  return;
1467
1574
  }
1468
- if (isNodeList(element)) {
1575
+ if (DOMUtilsCommonUtils.isNodeList(element)) {
1469
1576
  if (typeof property === "string") {
1470
1577
  if (value == null) {
1471
1578
  // 获取属性
@@ -1532,7 +1639,7 @@ define((function () { 'use strict';
1532
1639
  if (element == null) {
1533
1640
  return;
1534
1641
  }
1535
- if (isNodeList(element)) {
1642
+ if (DOMUtilsCommonUtils.isNodeList(element)) {
1536
1643
  if (text == null) {
1537
1644
  // 获取
1538
1645
  return DOMUtilsContext.text(element[0]);
@@ -1568,7 +1675,7 @@ define((function () { 'use strict';
1568
1675
  if (element == null) {
1569
1676
  return;
1570
1677
  }
1571
- if (isNodeList(element)) {
1678
+ if (DOMUtilsCommonUtils.isNodeList(element)) {
1572
1679
  if (html == null) {
1573
1680
  // 获取
1574
1681
  return DOMUtilsContext.html(element[0]);
@@ -1638,7 +1745,7 @@ define((function () { 'use strict';
1638
1745
  if (element == null) {
1639
1746
  return;
1640
1747
  }
1641
- if (isNodeList(element)) {
1748
+ if (DOMUtilsCommonUtils.isNodeList(element)) {
1642
1749
  if (value == null) {
1643
1750
  // 获取
1644
1751
  return DOMUtilsContext.val(element[0]);
@@ -1680,7 +1787,7 @@ define((function () { 'use strict';
1680
1787
  if (element == null) {
1681
1788
  return;
1682
1789
  }
1683
- if (isNodeList(element)) {
1790
+ if (DOMUtilsCommonUtils.isNodeList(element)) {
1684
1791
  if (propValue == null) {
1685
1792
  // 获取
1686
1793
  return DOMUtilsContext.prop(element[0], propName);
@@ -1722,7 +1829,7 @@ define((function () { 'use strict';
1722
1829
  if (element == null) {
1723
1830
  return;
1724
1831
  }
1725
- if (isNodeList(element)) {
1832
+ if (DOMUtilsCommonUtils.isNodeList(element)) {
1726
1833
  // 设置
1727
1834
  element.forEach(($ele) => {
1728
1835
  DOMUtilsContext.removeAttr($ele, attrName);
@@ -1748,7 +1855,7 @@ define((function () { 'use strict';
1748
1855
  if (element == null) {
1749
1856
  return;
1750
1857
  }
1751
- if (isNodeList(element)) {
1858
+ if (DOMUtilsCommonUtils.isNodeList(element)) {
1752
1859
  // 设置
1753
1860
  element.forEach(($ele) => {
1754
1861
  DOMUtilsContext.removeClass($ele, className);
@@ -1785,7 +1892,7 @@ define((function () { 'use strict';
1785
1892
  if (element == null) {
1786
1893
  return;
1787
1894
  }
1788
- if (isNodeList(element)) {
1895
+ if (DOMUtilsCommonUtils.isNodeList(element)) {
1789
1896
  // 设置
1790
1897
  element.forEach(($ele) => {
1791
1898
  DOMUtilsContext.removeProp($ele, propName);
@@ -1811,7 +1918,7 @@ define((function () { 'use strict';
1811
1918
  if (element == null) {
1812
1919
  return;
1813
1920
  }
1814
- if (isNodeList(element)) {
1921
+ if (DOMUtilsCommonUtils.isNodeList(element)) {
1815
1922
  // 设置
1816
1923
  element.forEach(($ele) => {
1817
1924
  DOMUtilsContext.replaceWith($ele, newElement);
@@ -1840,7 +1947,7 @@ define((function () { 'use strict';
1840
1947
  if (element == null) {
1841
1948
  return;
1842
1949
  }
1843
- if (isNodeList(element)) {
1950
+ if (DOMUtilsCommonUtils.isNodeList(element)) {
1844
1951
  // 设置
1845
1952
  element.forEach(($ele) => {
1846
1953
  DOMUtilsContext.addClass($ele, className);
@@ -1870,7 +1977,7 @@ define((function () { 'use strict';
1870
1977
  if (element == null) {
1871
1978
  return false;
1872
1979
  }
1873
- if (isNodeList(element)) {
1980
+ if (DOMUtilsCommonUtils.isNodeList(element)) {
1874
1981
  let flag = true;
1875
1982
  for (let index = 0; index < element.length; index++) {
1876
1983
  const $ele = element[index];
@@ -1909,7 +2016,7 @@ define((function () { 'use strict';
1909
2016
  if (element == null) {
1910
2017
  return;
1911
2018
  }
1912
- if (isNodeList(element)) {
2019
+ if (DOMUtilsCommonUtils.isNodeList(element)) {
1913
2020
  // 设置
1914
2021
  element.forEach(($ele) => {
1915
2022
  DOMUtilsContext.append($ele, content);
@@ -1956,7 +2063,7 @@ define((function () { 'use strict';
1956
2063
  if (element == null) {
1957
2064
  return;
1958
2065
  }
1959
- if (isNodeList(element)) {
2066
+ if (DOMUtilsCommonUtils.isNodeList(element)) {
1960
2067
  // 设置
1961
2068
  element.forEach(($ele) => {
1962
2069
  DOMUtilsContext.prepend($ele, content);
@@ -1993,7 +2100,7 @@ define((function () { 'use strict';
1993
2100
  if (element == null) {
1994
2101
  return;
1995
2102
  }
1996
- if (isNodeList(element)) {
2103
+ if (DOMUtilsCommonUtils.isNodeList(element)) {
1997
2104
  // 设置
1998
2105
  element.forEach(($ele) => {
1999
2106
  DOMUtilsContext.after($ele, content);
@@ -2032,7 +2139,7 @@ define((function () { 'use strict';
2032
2139
  if (element == null) {
2033
2140
  return;
2034
2141
  }
2035
- if (isNodeList(element)) {
2142
+ if (DOMUtilsCommonUtils.isNodeList(element)) {
2036
2143
  // 设置
2037
2144
  element.forEach(($ele) => {
2038
2145
  DOMUtilsContext.before($ele, content);
@@ -2069,7 +2176,7 @@ define((function () { 'use strict';
2069
2176
  if (element == null) {
2070
2177
  return;
2071
2178
  }
2072
- if (isNodeList(element)) {
2179
+ if (DOMUtilsCommonUtils.isNodeList(element)) {
2073
2180
  element.forEach(($ele) => {
2074
2181
  DOMUtilsContext.remove($ele);
2075
2182
  });
@@ -2093,7 +2200,7 @@ define((function () { 'use strict';
2093
2200
  if (element == null) {
2094
2201
  return;
2095
2202
  }
2096
- if (isNodeList(element)) {
2203
+ if (DOMUtilsCommonUtils.isNodeList(element)) {
2097
2204
  // 设置
2098
2205
  element.forEach(($ele) => {
2099
2206
  DOMUtilsContext.empty($ele);
@@ -2300,7 +2407,7 @@ define((function () { 'use strict';
2300
2407
  if (element == null) {
2301
2408
  return;
2302
2409
  }
2303
- if (isNodeList(element)) {
2410
+ if (DOMUtilsCommonUtils.isNodeList(element)) {
2304
2411
  // 设置
2305
2412
  element.forEach(($ele) => {
2306
2413
  DOMUtilsContext.animate($ele, styles, duration, callback);
@@ -2362,7 +2469,7 @@ define((function () { 'use strict';
2362
2469
  if (element == null) {
2363
2470
  return;
2364
2471
  }
2365
- if (isNodeList(element)) {
2472
+ if (DOMUtilsCommonUtils.isNodeList(element)) {
2366
2473
  // 设置
2367
2474
  element.forEach(($ele) => {
2368
2475
  DOMUtilsContext.wrap($ele, wrapperHTML);
@@ -2442,7 +2549,7 @@ define((function () { 'use strict';
2442
2549
  if (element == null) {
2443
2550
  return;
2444
2551
  }
2445
- if (isNodeList(element)) {
2552
+ if (DOMUtilsCommonUtils.isNodeList(element)) {
2446
2553
  let resultArray = [];
2447
2554
  element.forEach(($ele) => {
2448
2555
  resultArray.push(DOMUtilsContext.parent($ele));
@@ -2621,7 +2728,7 @@ define((function () { 'use strict';
2621
2728
  if (typeof element === "string") {
2622
2729
  element = DOMUtilsContext.selectorAll(element);
2623
2730
  }
2624
- if (isNodeList(element)) {
2731
+ if (DOMUtilsCommonUtils.isNodeList(element)) {
2625
2732
  // 设置
2626
2733
  element.forEach(($ele) => {
2627
2734
  DOMUtilsContext.fadeIn($ele, duration, callback);
@@ -2672,7 +2779,7 @@ define((function () { 'use strict';
2672
2779
  if (typeof element === "string") {
2673
2780
  element = DOMUtilsContext.selectorAll(element);
2674
2781
  }
2675
- if (isNodeList(element)) {
2782
+ if (DOMUtilsCommonUtils.isNodeList(element)) {
2676
2783
  // 设置
2677
2784
  element.forEach(($ele) => {
2678
2785
  DOMUtilsContext.fadeOut($ele, duration, callback);
@@ -2718,7 +2825,7 @@ define((function () { 'use strict';
2718
2825
  if (element == null) {
2719
2826
  return;
2720
2827
  }
2721
- if (isNodeList(element)) {
2828
+ if (DOMUtilsCommonUtils.isNodeList(element)) {
2722
2829
  // 设置
2723
2830
  element.forEach(($ele) => {
2724
2831
  DOMUtilsContext.toggle($ele);