@whitesev/domutils 1.0.0 → 1.0.3

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.esm.js CHANGED
@@ -1,3 +1,36 @@
1
+ const DOMUtilsCoreDefaultEnv = {
2
+ document: document,
3
+ window: window,
4
+ globalThis: globalThis,
5
+ self: self,
6
+ };
7
+ const DOMUtilsCoreEnv = {
8
+ document: document,
9
+ window: window,
10
+ globalThis: globalThis,
11
+ self: self,
12
+ };
13
+ const DOMUtilsCore = {
14
+ init(option) {
15
+ if (!option) {
16
+ option = Object.assign({}, DOMUtilsCoreDefaultEnv);
17
+ }
18
+ Object.assign(DOMUtilsCoreEnv, option);
19
+ },
20
+ get document() {
21
+ return DOMUtilsCoreEnv.document;
22
+ },
23
+ get window() {
24
+ return DOMUtilsCoreEnv.window;
25
+ },
26
+ get globalThis() {
27
+ return DOMUtilsCoreEnv.globalThis;
28
+ },
29
+ get self() {
30
+ return DOMUtilsCoreEnv.self;
31
+ },
32
+ };
33
+
1
34
  /** 通用工具类 */
2
35
  const CommonDOMUtils = {
3
36
  /**
@@ -14,7 +47,7 @@ const CommonDOMUtils = {
14
47
  showElement(element) {
15
48
  let dupNode = element.cloneNode(true);
16
49
  dupNode.setAttribute("style", "visibility: hidden !important;display:block !important;");
17
- document.documentElement.appendChild(dupNode);
50
+ DOMUtilsCore.document.documentElement.appendChild(dupNode);
18
51
  return {
19
52
  /**
20
53
  * 恢复修改的style
@@ -107,11 +140,14 @@ const OriginPrototype = {
107
140
  };
108
141
 
109
142
  class DOMUtils {
143
+ constructor(option) {
144
+ DOMUtilsCore.init(option);
145
+ }
110
146
  /** 版本号 */
111
147
  version = "2024.5.24";
112
148
  attr(element, attrName, attrValue) {
113
149
  if (typeof element === "string") {
114
- element = document.querySelector(element);
150
+ element = DOMUtilsCore.document.querySelector(element);
115
151
  }
116
152
  if (element == null) {
117
153
  return;
@@ -148,7 +184,7 @@ class DOMUtils {
148
184
  property,
149
185
  /** 自定义属性 */
150
186
  attributes) {
151
- let tempElement = document.createElement(tagName);
187
+ let tempElement = DOMUtilsCore.document.createElement(tagName);
152
188
  if (typeof property === "string") {
153
189
  tempElement.innerHTML = property;
154
190
  return tempElement;
@@ -202,7 +238,7 @@ class DOMUtils {
202
238
  return propertyValue;
203
239
  }
204
240
  if (typeof element === "string") {
205
- element = document.querySelector(element);
241
+ element = DOMUtilsCore.document.querySelector(element);
206
242
  }
207
243
  if (element == null) {
208
244
  return;
@@ -236,7 +272,7 @@ class DOMUtils {
236
272
  }
237
273
  text(element, text) {
238
274
  if (typeof element === "string") {
239
- element = document.querySelector(element);
275
+ element = DOMUtilsCore.document.querySelector(element);
240
276
  }
241
277
  if (element == null) {
242
278
  return;
@@ -258,7 +294,7 @@ class DOMUtils {
258
294
  }
259
295
  html(element, html) {
260
296
  if (typeof element === "string") {
261
- element = document.querySelector(element);
297
+ element = DOMUtilsCore.document.querySelector(element);
262
298
  }
263
299
  if (element == null) {
264
300
  return;
@@ -292,7 +328,7 @@ class DOMUtils {
292
328
  click(element, handler, details, useDispatchToTriggerEvent) {
293
329
  let DOMUtilsContext = this;
294
330
  if (typeof element === "string") {
295
- element = document.querySelector(element);
331
+ element = DOMUtilsCore.document.querySelector(element);
296
332
  }
297
333
  if (element == null) {
298
334
  return;
@@ -321,7 +357,7 @@ class DOMUtils {
321
357
  blur(element, handler, details, useDispatchToTriggerEvent) {
322
358
  let DOMUtilsContext = this;
323
359
  if (typeof element === "string") {
324
- element = document.querySelector(element);
360
+ element = DOMUtilsCore.document.querySelector(element);
325
361
  }
326
362
  if (element == null) {
327
363
  return;
@@ -350,7 +386,7 @@ class DOMUtils {
350
386
  focus(element, handler, details, useDispatchToTriggerEvent) {
351
387
  let DOMUtilsContext = this;
352
388
  if (typeof element === "string") {
353
- element = document.querySelector(element);
389
+ element = DOMUtilsCore.document.querySelector(element);
354
390
  }
355
391
  if (element == null) {
356
392
  return;
@@ -399,7 +435,7 @@ class DOMUtils {
399
435
  }
400
436
  val(element, value) {
401
437
  if (typeof element === "string") {
402
- element = document.querySelector(element);
438
+ element = DOMUtilsCore.document.querySelector(element);
403
439
  }
404
440
  if (element == null) {
405
441
  return;
@@ -428,7 +464,7 @@ class DOMUtils {
428
464
  return;
429
465
  }
430
466
  if (typeof element === "string") {
431
- element = document.querySelector(element);
467
+ element = DOMUtilsCore.document.querySelector(element);
432
468
  }
433
469
  if (propValue == null) {
434
470
  return element[propName];
@@ -448,7 +484,7 @@ class DOMUtils {
448
484
  * */
449
485
  removeAttr(element, attrName) {
450
486
  if (typeof element === "string") {
451
- element = document.querySelector(element);
487
+ element = DOMUtilsCore.document.querySelector(element);
452
488
  }
453
489
  if (element == null) {
454
490
  return;
@@ -466,7 +502,7 @@ class DOMUtils {
466
502
  */
467
503
  removeClass(element, className) {
468
504
  if (typeof element === "string") {
469
- element = document.querySelector(element);
505
+ element = DOMUtilsCore.document.querySelector(element);
470
506
  }
471
507
  if (element == null) {
472
508
  return;
@@ -487,7 +523,7 @@ class DOMUtils {
487
523
  * */
488
524
  removeProp(element, propName) {
489
525
  if (typeof element === "string") {
490
- element = document.querySelector(element);
526
+ element = DOMUtilsCore.document.querySelector(element);
491
527
  }
492
528
  if (element == null) {
493
529
  return;
@@ -506,7 +542,7 @@ class DOMUtils {
506
542
  replaceWith(element, newElement) {
507
543
  let DOMUtilsContext = this;
508
544
  if (typeof element === "string") {
509
- element = document.querySelector(element);
545
+ element = DOMUtilsCore.document.querySelector(element);
510
546
  }
511
547
  if (element == null) {
512
548
  return;
@@ -534,7 +570,7 @@ class DOMUtils {
534
570
  * */
535
571
  addClass(element, className) {
536
572
  if (typeof element === "string") {
537
- element = document.querySelector(element);
573
+ element = DOMUtilsCore.document.querySelector(element);
538
574
  }
539
575
  if (element == null) {
540
576
  return;
@@ -552,7 +588,7 @@ class DOMUtils {
552
588
  * */
553
589
  append(element, content) {
554
590
  if (typeof element === "string") {
555
- element = document.querySelector(element);
591
+ element = DOMUtilsCore.document.querySelector(element);
556
592
  }
557
593
  if (element == null) {
558
594
  return;
@@ -575,7 +611,7 @@ class DOMUtils {
575
611
  * */
576
612
  prepend(element, content) {
577
613
  if (typeof element === "string") {
578
- element = document.querySelector(element);
614
+ element = DOMUtilsCore.document.querySelector(element);
579
615
  }
580
616
  if (element == null) {
581
617
  return;
@@ -598,7 +634,7 @@ class DOMUtils {
598
634
  * */
599
635
  after(element, content) {
600
636
  if (typeof element === "string") {
601
- element = document.querySelector(element);
637
+ element = DOMUtilsCore.document.querySelector(element);
602
638
  }
603
639
  if (element == null) {
604
640
  return;
@@ -621,7 +657,7 @@ class DOMUtils {
621
657
  * */
622
658
  before(element, content) {
623
659
  if (typeof element === "string") {
624
- element = document.querySelector(element);
660
+ element = DOMUtilsCore.document.querySelector(element);
625
661
  }
626
662
  if (element == null) {
627
663
  return;
@@ -645,7 +681,7 @@ class DOMUtils {
645
681
  remove(target) {
646
682
  let DOMUtilsContext = this;
647
683
  if (typeof target === "string") {
648
- target = document.querySelectorAll(target);
684
+ target = DOMUtilsCore.document.querySelectorAll(target);
649
685
  }
650
686
  if (target == null) {
651
687
  return;
@@ -670,7 +706,7 @@ class DOMUtils {
670
706
  * */
671
707
  empty(element) {
672
708
  if (typeof element === "string") {
673
- element = document.querySelector(element);
709
+ element = DOMUtilsCore.document.querySelector(element);
674
710
  }
675
711
  if (element == null) {
676
712
  return;
@@ -707,7 +743,7 @@ class DOMUtils {
707
743
  let DOMUtilsContext = this;
708
744
  let args = arguments;
709
745
  if (typeof element === "string") {
710
- element = document.querySelectorAll(element);
746
+ element = DOMUtilsCore.document.querySelectorAll(element);
711
747
  }
712
748
  if (element == null) {
713
749
  return;
@@ -758,7 +794,7 @@ class DOMUtils {
758
794
  if (_selector_) {
759
795
  /* 存在自定义子元素选择器 */
760
796
  let totalParent = CommonDOMUtils.isWin(elementItem)
761
- ? document.documentElement
797
+ ? DOMUtilsCore.document.documentElement
762
798
  : elementItem;
763
799
  if (target.matches(_selector_)) {
764
800
  /* 当前目标可以被selector所匹配到 */
@@ -824,7 +860,7 @@ class DOMUtils {
824
860
  }
825
861
  let args = arguments;
826
862
  if (typeof element === "string") {
827
- element = document.querySelectorAll(element);
863
+ element = DOMUtilsCore.document.querySelectorAll(element);
828
864
  }
829
865
  if (element == null) {
830
866
  return;
@@ -908,7 +944,7 @@ class DOMUtils {
908
944
  */
909
945
  offAll(element, eventType) {
910
946
  if (typeof element === "string") {
911
- element = document.querySelectorAll(element);
947
+ element = DOMUtilsCore.document.querySelectorAll(element);
912
948
  }
913
949
  if (element == null) {
914
950
  return;
@@ -968,7 +1004,7 @@ class DOMUtils {
968
1004
  */
969
1005
  trigger(element, eventType, details, useDispatchToTriggerEvent = true) {
970
1006
  if (typeof element === "string") {
971
- element = document.querySelector(element);
1007
+ element = DOMUtilsCore.document.querySelector(element);
972
1008
  }
973
1009
  if (element == null) {
974
1010
  return;
@@ -1026,7 +1062,7 @@ class DOMUtils {
1026
1062
  */
1027
1063
  offset(element) {
1028
1064
  if (typeof element === "string") {
1029
- element = document.querySelector(element);
1065
+ element = DOMUtilsCore.document.querySelector(element);
1030
1066
  }
1031
1067
  if (element == null) {
1032
1068
  return;
@@ -1034,21 +1070,21 @@ class DOMUtils {
1034
1070
  let rect = element.getBoundingClientRect();
1035
1071
  return {
1036
1072
  /** y轴偏移 */
1037
- top: rect.top + globalThis.scrollY,
1073
+ top: rect.top + DOMUtilsCore.globalThis.scrollY,
1038
1074
  /** x轴偏移 */
1039
- left: rect.left + globalThis.scrollX,
1075
+ left: rect.left + DOMUtilsCore.globalThis.scrollX,
1040
1076
  };
1041
1077
  }
1042
1078
  width(element, isShow = false) {
1043
1079
  let DOMUtilsContext = this;
1044
1080
  if (typeof element === "string") {
1045
- element = document.querySelector(element);
1081
+ element = DOMUtilsCore.document.querySelector(element);
1046
1082
  }
1047
1083
  if (element == null) {
1048
1084
  return;
1049
1085
  }
1050
1086
  if (CommonDOMUtils.isWin(element)) {
1051
- return window.document.documentElement.clientWidth;
1087
+ return DOMUtilsCore.window.document.documentElement.clientWidth;
1052
1088
  }
1053
1089
  if (element.nodeType === 9) {
1054
1090
  /* Document文档节点 */
@@ -1091,10 +1127,10 @@ class DOMUtils {
1091
1127
  height(element, isShow = false) {
1092
1128
  let DOMUtilsContext = this;
1093
1129
  if (CommonDOMUtils.isWin(element)) {
1094
- return window.document.documentElement.clientHeight;
1130
+ return DOMUtilsCore.window.document.documentElement.clientHeight;
1095
1131
  }
1096
1132
  if (typeof element === "string") {
1097
- element = document.querySelector(element);
1133
+ element = DOMUtilsCore.document.querySelector(element);
1098
1134
  }
1099
1135
  if (element == null) {
1100
1136
  // @ts-ignore
@@ -1141,10 +1177,10 @@ class DOMUtils {
1141
1177
  outerWidth(element, isShow = false) {
1142
1178
  let DOMUtilsContext = this;
1143
1179
  if (CommonDOMUtils.isWin(element)) {
1144
- return window.innerWidth;
1180
+ return DOMUtilsCore.window.innerWidth;
1145
1181
  }
1146
1182
  if (typeof element === "string") {
1147
- element = document.querySelector(element);
1183
+ element = DOMUtilsCore.document.querySelector(element);
1148
1184
  }
1149
1185
  if (element == null) {
1150
1186
  // @ts-ignore
@@ -1167,10 +1203,10 @@ class DOMUtils {
1167
1203
  outerHeight(element, isShow = false) {
1168
1204
  let DOMUtilsContext = this;
1169
1205
  if (CommonDOMUtils.isWin(element)) {
1170
- return window.innerHeight;
1206
+ return DOMUtilsCore.window.innerHeight;
1171
1207
  }
1172
1208
  if (typeof element === "string") {
1173
- element = document.querySelector(element);
1209
+ element = DOMUtilsCore.document.querySelector(element);
1174
1210
  }
1175
1211
  if (element == null) {
1176
1212
  // @ts-ignore
@@ -1231,7 +1267,7 @@ class DOMUtils {
1231
1267
  */
1232
1268
  animate(element, styles, duration = 1000, callback = null) {
1233
1269
  if (typeof element === "string") {
1234
- element = document.querySelector(element);
1270
+ element = DOMUtilsCore.document.querySelector(element);
1235
1271
  }
1236
1272
  if (element == null) {
1237
1273
  return;
@@ -1283,14 +1319,14 @@ class DOMUtils {
1283
1319
  */
1284
1320
  wrap(element, wrapperHTML) {
1285
1321
  if (typeof element === "string") {
1286
- element = document.querySelector(element);
1322
+ element = DOMUtilsCore.document.querySelector(element);
1287
1323
  }
1288
1324
  if (element == null) {
1289
1325
  return;
1290
1326
  }
1291
1327
  element = element;
1292
1328
  // 创建一个新的div元素,并将wrapperHTML作为其innerHTML
1293
- let wrapper = document.createElement("div");
1329
+ let wrapper = DOMUtilsCore.document.createElement("div");
1294
1330
  wrapper.innerHTML = wrapperHTML;
1295
1331
  let wrapperFirstChild = wrapper.firstChild;
1296
1332
  // 将要包裹的元素插入目标元素前面
@@ -1300,7 +1336,7 @@ class DOMUtils {
1300
1336
  }
1301
1337
  prev(element) {
1302
1338
  if (typeof element === "string") {
1303
- element = document.querySelector(element);
1339
+ element = DOMUtilsCore.document.querySelector(element);
1304
1340
  }
1305
1341
  if (element == null) {
1306
1342
  return;
@@ -1309,7 +1345,7 @@ class DOMUtils {
1309
1345
  }
1310
1346
  next(element) {
1311
1347
  if (typeof element === "string") {
1312
- element = document.querySelector(element);
1348
+ element = DOMUtilsCore.document.querySelector(element);
1313
1349
  }
1314
1350
  if (element == null) {
1315
1351
  return;
@@ -1322,15 +1358,15 @@ class DOMUtils {
1322
1358
  * let DOMUtils = window.DOMUtils.noConflict()
1323
1359
  */
1324
1360
  noConflict() {
1325
- if (window.DOMUtils) {
1361
+ if (DOMUtilsCore.window.DOMUtils) {
1326
1362
  CommonDOMUtils.delete(window, "DOMUtils");
1327
1363
  }
1328
- window.DOMUtils = this;
1364
+ DOMUtilsCore.window.DOMUtils = this;
1329
1365
  return this;
1330
1366
  }
1331
1367
  siblings(element) {
1332
1368
  if (typeof element === "string") {
1333
- element = document.querySelector(element);
1369
+ element = DOMUtilsCore.document.querySelector(element);
1334
1370
  }
1335
1371
  if (element == null) {
1336
1372
  return;
@@ -1351,7 +1387,7 @@ class DOMUtils {
1351
1387
  parent(element) {
1352
1388
  let DOMUtilsContext = this;
1353
1389
  if (typeof element === "string") {
1354
- element = document.querySelector(element);
1390
+ element = DOMUtilsCore.document.querySelector(element);
1355
1391
  }
1356
1392
  if (element == null) {
1357
1393
  return;
@@ -1379,7 +1415,7 @@ class DOMUtils {
1379
1415
  }
1380
1416
  }
1381
1417
  function parseHTMLByCreateDom() {
1382
- let tempDIV = document.createElement("div");
1418
+ let tempDIV = DOMUtilsCore.document.createElement("div");
1383
1419
  tempDIV.innerHTML = html;
1384
1420
  if (isComplete) {
1385
1421
  return tempDIV;
@@ -1412,7 +1448,7 @@ class DOMUtils {
1412
1448
  hover(element, handler, option) {
1413
1449
  let DOMUtilsContext = this;
1414
1450
  if (typeof element === "string") {
1415
- element = document.querySelector(element);
1451
+ element = DOMUtilsCore.document.querySelector(element);
1416
1452
  }
1417
1453
  if (element == null) {
1418
1454
  return;
@@ -1435,7 +1471,7 @@ class DOMUtils {
1435
1471
  return;
1436
1472
  }
1437
1473
  if (typeof target === "string") {
1438
- target = document.querySelectorAll(target);
1474
+ target = DOMUtilsCore.document.querySelectorAll(target);
1439
1475
  }
1440
1476
  if (target instanceof NodeList || target instanceof Array) {
1441
1477
  target = target;
@@ -1444,6 +1480,7 @@ class DOMUtils {
1444
1480
  }
1445
1481
  }
1446
1482
  else {
1483
+ target = target;
1447
1484
  target.style.display = "";
1448
1485
  if (!CommonDOMUtils.isShow(target)) {
1449
1486
  /* 仍然是不显示,尝试使用强覆盖 */
@@ -1466,7 +1503,7 @@ class DOMUtils {
1466
1503
  return;
1467
1504
  }
1468
1505
  if (typeof target === "string") {
1469
- target = document.querySelectorAll(target);
1506
+ target = DOMUtilsCore.document.querySelectorAll(target);
1470
1507
  }
1471
1508
  if (target instanceof NodeList || target instanceof Array) {
1472
1509
  target = target;
@@ -1475,6 +1512,7 @@ class DOMUtils {
1475
1512
  }
1476
1513
  }
1477
1514
  else {
1515
+ target = target;
1478
1516
  target.style.display = "none";
1479
1517
  if (CommonDOMUtils.isShow(target)) {
1480
1518
  /* 仍然是显示,尝试使用强覆盖 */
@@ -1503,7 +1541,7 @@ class DOMUtils {
1503
1541
  return;
1504
1542
  }
1505
1543
  if (typeof target === "string") {
1506
- target = document.querySelector(target);
1544
+ target = DOMUtilsCore.document.querySelector(target);
1507
1545
  }
1508
1546
  DOMUtilsContext.on(target, "keyup", null, handler, option);
1509
1547
  }
@@ -1528,7 +1566,7 @@ class DOMUtils {
1528
1566
  return;
1529
1567
  }
1530
1568
  if (typeof target === "string") {
1531
- target = document.querySelector(target);
1569
+ target = DOMUtilsCore.document.querySelector(target);
1532
1570
  }
1533
1571
  DOMUtilsContext.on(target, "keydown", null, handler, option);
1534
1572
  }
@@ -1553,7 +1591,7 @@ class DOMUtils {
1553
1591
  return;
1554
1592
  }
1555
1593
  if (typeof target === "string") {
1556
- target = document.querySelector(target);
1594
+ target = DOMUtilsCore.document.querySelector(target);
1557
1595
  }
1558
1596
  DOMUtilsContext.on(target, "keypress", null, handler, option);
1559
1597
  }
@@ -1576,7 +1614,7 @@ class DOMUtils {
1576
1614
  return;
1577
1615
  }
1578
1616
  if (typeof element === "string") {
1579
- element = document.querySelector(element);
1617
+ element = DOMUtilsCore.document.querySelector(element);
1580
1618
  }
1581
1619
  element = element;
1582
1620
  element.style.opacity = "0";
@@ -1590,16 +1628,16 @@ class DOMUtils {
1590
1628
  element = element;
1591
1629
  element.style.opacity = Math.min(progress / duration, 1).toString();
1592
1630
  if (progress < duration) {
1593
- window.requestAnimationFrame(step);
1631
+ DOMUtilsCore.window.requestAnimationFrame(step);
1594
1632
  }
1595
1633
  else {
1596
1634
  if (callback && typeof callback === "function") {
1597
1635
  callback();
1598
1636
  }
1599
- window.cancelAnimationFrame(timer);
1637
+ DOMUtilsCore.window.cancelAnimationFrame(timer);
1600
1638
  }
1601
1639
  }
1602
- timer = window.requestAnimationFrame(step);
1640
+ timer = DOMUtilsCore.window.requestAnimationFrame(step);
1603
1641
  }
1604
1642
  /**
1605
1643
  * 淡出元素
@@ -1620,7 +1658,7 @@ class DOMUtils {
1620
1658
  return;
1621
1659
  }
1622
1660
  if (typeof element === "string") {
1623
- element = document.querySelector(element);
1661
+ element = DOMUtilsCore.document.querySelector(element);
1624
1662
  }
1625
1663
  element = element;
1626
1664
  element.style.opacity = "1";
@@ -1633,17 +1671,17 @@ class DOMUtils {
1633
1671
  element = element;
1634
1672
  element.style.opacity = Math.max(1 - progress / duration, 0).toString();
1635
1673
  if (progress < duration) {
1636
- window.requestAnimationFrame(step);
1674
+ DOMUtilsCore.window.requestAnimationFrame(step);
1637
1675
  }
1638
1676
  else {
1639
1677
  element.style.display = "none";
1640
1678
  if (typeof callback === "function") {
1641
1679
  callback();
1642
1680
  }
1643
- window.cancelAnimationFrame(timer);
1681
+ DOMUtilsCore.window.cancelAnimationFrame(timer);
1644
1682
  }
1645
1683
  }
1646
- timer = window.requestAnimationFrame(step);
1684
+ timer = DOMUtilsCore.window.requestAnimationFrame(step);
1647
1685
  }
1648
1686
  /**
1649
1687
  * 切换元素的显示和隐藏状态
@@ -1656,7 +1694,7 @@ class DOMUtils {
1656
1694
  toggle(element) {
1657
1695
  let DOMUtilsContext = this;
1658
1696
  if (typeof element === "string") {
1659
- element = document.querySelector(element);
1697
+ element = DOMUtilsCore.document.querySelector(element);
1660
1698
  }
1661
1699
  if (element == null) {
1662
1700
  return;
@@ -1668,6 +1706,14 @@ class DOMUtils {
1668
1706
  DOMUtilsContext.hide(element);
1669
1707
  }
1670
1708
  }
1709
+ /**
1710
+ * 创建一个新的DOMUtils实例
1711
+ * @param option
1712
+ * @returns
1713
+ */
1714
+ createDOMUtils(option) {
1715
+ return new DOMUtils(option);
1716
+ }
1671
1717
  }
1672
1718
  let domUtils = new DOMUtils();
1673
1719