@whitesev/domutils 1.6.8 → 1.7.1

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.
Files changed (57) hide show
  1. package/dist/index.amd.js +1962 -1062
  2. package/dist/index.amd.js.map +1 -1
  3. package/dist/index.amd.min.js +2 -0
  4. package/dist/index.amd.min.js.map +1 -0
  5. package/dist/index.cjs.js +1962 -1062
  6. package/dist/index.cjs.js.map +1 -1
  7. package/dist/index.cjs.min.js +2 -0
  8. package/dist/index.cjs.min.js.map +1 -0
  9. package/dist/index.esm.js +1962 -1062
  10. package/dist/index.esm.js.map +1 -1
  11. package/dist/index.esm.min.js +2 -0
  12. package/dist/index.esm.min.js.map +1 -0
  13. package/dist/index.iife.js +1962 -1062
  14. package/dist/index.iife.js.map +1 -1
  15. package/dist/index.iife.min.js +2 -0
  16. package/dist/index.iife.min.js.map +1 -0
  17. package/dist/index.system.js +1962 -1062
  18. package/dist/index.system.js.map +1 -1
  19. package/dist/index.system.min.js +2 -0
  20. package/dist/index.system.min.js.map +1 -0
  21. package/dist/index.umd.js +1962 -1062
  22. package/dist/index.umd.js.map +1 -1
  23. package/dist/index.umd.min.js +2 -0
  24. package/dist/index.umd.min.js.map +1 -0
  25. package/dist/types/index.d.ts +1 -1
  26. package/dist/types/src/{DOMUtilsCommonUtils.d.ts → CommonUtils.d.ts} +20 -8
  27. package/dist/types/src/ElementAnimate.d.ts +89 -0
  28. package/dist/types/src/{DOMUtilsEvent.d.ts → ElementEvent.d.ts} +27 -92
  29. package/dist/types/src/ElementHandler.d.ts +17 -0
  30. package/dist/types/src/ElementSelector.d.ts +96 -0
  31. package/dist/types/src/ElementWait.d.ts +278 -0
  32. package/dist/types/src/GlobalData.d.ts +4 -0
  33. package/dist/types/src/{DOMUtilsOriginPrototype.d.ts → OriginPrototype.d.ts} +1 -2
  34. package/dist/types/src/Utils.d.ts +68 -0
  35. package/dist/types/src/{DOMUtils.d.ts → index.d.ts} +157 -177
  36. package/dist/types/src/types/DOMUtilsEvent.d.ts +23 -0
  37. package/dist/types/src/types/env.d.ts +9 -0
  38. package/dist/types/src/types/global.d.ts +0 -2
  39. package/dist/types/src/types/gm.d.ts +0 -4
  40. package/index.ts +1 -1
  41. package/package.json +13 -12
  42. package/src/{DOMUtilsCommonUtils.ts → CommonUtils.ts} +25 -11
  43. package/src/ElementAnimate.ts +290 -0
  44. package/src/{DOMUtilsEvent.ts → ElementEvent.ts} +220 -396
  45. package/src/ElementHandler.ts +43 -0
  46. package/src/ElementSelector.ts +260 -0
  47. package/src/ElementWait.ts +699 -0
  48. package/src/GlobalData.ts +5 -0
  49. package/src/{DOMUtilsOriginPrototype.ts → OriginPrototype.ts} +1 -3
  50. package/src/Utils.ts +386 -0
  51. package/src/{DOMUtils.ts → index.ts} +678 -757
  52. package/src/types/DOMUtilsEvent.d.ts +23 -0
  53. package/src/types/env.d.ts +9 -0
  54. package/src/types/global.d.ts +0 -2
  55. package/src/types/gm.d.ts +0 -4
  56. package/dist/types/src/DOMUtilsData.d.ts +0 -5
  57. package/src/DOMUtilsData.ts +0 -7
package/dist/index.umd.js CHANGED
@@ -308,7 +308,7 @@
308
308
  const setTimeout$1 = (...args) => loadOrReturnBroker().setTimeout(...args);
309
309
 
310
310
  /** 通用工具类 */
311
- const DOMUtilsCommonUtils = {
311
+ const CommonUtils = {
312
312
  windowApi: new WindowApi({
313
313
  document: document,
314
314
  window: window,
@@ -320,15 +320,16 @@
320
320
  }),
321
321
  /**
322
322
  * 判断元素是否已显示或已连接
323
- * @param element
323
+ * @param $el
324
324
  */
325
- isShow(element) {
326
- return Boolean(element.getClientRects().length);
325
+ isShow($el) {
326
+ return Boolean($el.getClientRects().length);
327
327
  },
328
328
  /**
329
- * 获取安全的html
329
+ * 创建安全的html
330
+ * @param text 字符串
330
331
  */
331
- getSafeHTML(text) {
332
+ createSafeHTML(text) {
332
333
  if (window.trustedTypes) {
333
334
  const policy = window.trustedTypes.createPolicy("safe-innerHTML", {
334
335
  createHTML: (html) => html,
@@ -346,14 +347,14 @@
346
347
  */
347
348
  setSafeHTML($el, text) {
348
349
  // 创建 TrustedHTML 策略(需 CSP 允许)
349
- $el.innerHTML = this.getSafeHTML(text);
350
+ $el.innerHTML = this.createSafeHTML(text);
350
351
  },
351
352
  /**
352
- * 用于显示元素并获取它的高度宽度等其它属性
353
- * @param element
353
+ * 用于强制显示元素并获取它的高度宽度等其它属性
354
+ * @param $el
354
355
  */
355
- showElement(element) {
356
- const dupNode = element.cloneNode(true);
356
+ forceShow($el) {
357
+ const dupNode = $el.cloneNode(true);
357
358
  dupNode.setAttribute("style", "visibility: hidden !important;display:block !important;");
358
359
  this.windowApi.document.documentElement.appendChild(dupNode);
359
360
  return {
@@ -429,6 +430,19 @@
429
430
  }
430
431
  return true;
431
432
  },
433
+ /**
434
+ * 判断对象是否是元素
435
+ * @param $el
436
+ * @returns
437
+ * + true 是元素
438
+ * + false 不是元素
439
+ * @example
440
+ * DOMUtilsCommonUtils.isDOM(document.querySelector("a"))
441
+ * > true
442
+ */
443
+ isDOM($el) {
444
+ return $el instanceof Node;
445
+ },
432
446
  /**
433
447
  * 删除对象上的属性
434
448
  * @param target
@@ -454,70 +468,1157 @@
454
468
  }
455
469
  },
456
470
  /**
457
- * 配合 .setTimeout 使用
471
+ * 配合 .setTimeout 使用
472
+ */
473
+ clearTimeout(timeId) {
474
+ try {
475
+ if (timeId != null) {
476
+ clearTimeout$1(timeId);
477
+ }
478
+ }
479
+ catch {
480
+ // TODO
481
+ }
482
+ finally {
483
+ this.windowApi.clearTimeout(timeId);
484
+ }
485
+ },
486
+ /**
487
+ * 自动使用 Worker 执行 setInterval
488
+ */
489
+ setInterval(callback, timeout = 0) {
490
+ try {
491
+ return setInterval$1(callback, timeout);
492
+ }
493
+ catch {
494
+ return this.windowApi.setInterval(callback, timeout);
495
+ }
496
+ },
497
+ /**
498
+ * 配合 .setInterval 使用
499
+ */
500
+ clearInterval(timeId) {
501
+ try {
502
+ if (timeId != null) {
503
+ clearInterval$1(timeId);
504
+ }
505
+ }
506
+ catch {
507
+ // TODO
508
+ }
509
+ finally {
510
+ this.windowApi.clearInterval(timeId);
511
+ }
512
+ },
513
+ /**
514
+ * 判断是否是元素列表
515
+ * @param $ele
516
+ */
517
+ isNodeList($ele) {
518
+ return Array.isArray($ele) || $ele instanceof NodeList;
519
+ },
520
+ /** 获取 animationend 在各个浏览器的兼容名 */
521
+ getAnimationEndNameList() {
522
+ return ["webkitAnimationEnd", "mozAnimationEnd", "MSAnimationEnd", "oanimationend", "animationend"];
523
+ },
524
+ /** 获取 transitionend 在各个浏览器的兼容名 */
525
+ getTransitionEndNameList() {
526
+ return ["webkitTransitionEnd", "mozTransitionEnd", "MSTransitionEnd", "otransitionend", "transitionend"];
527
+ },
528
+ };
529
+
530
+ const version = "1.7.1";
531
+
532
+ /* 数据 */
533
+ const GlobalData = {
534
+ /** .on添加在元素存储的事件 */
535
+ domEventSymbol: Symbol("events_" + (((1 + Math.random()) * 0x10000) | 0).toString(16).substring(1)),
536
+ };
537
+
538
+ class ElementSelector {
539
+ windowApi;
540
+ constructor(windowApiOption) {
541
+ this.windowApi = new WindowApi(windowApiOption);
542
+ }
543
+ selector(selector, parent) {
544
+ return this.selectorAll(selector, parent)[0];
545
+ }
546
+ selectorAll(selector, parent) {
547
+ const context = this;
548
+ parent = parent || context.windowApi.document;
549
+ selector = selector.trim();
550
+ if (selector.match(/[^\s]{1}:empty$/gi)) {
551
+ // empty 语法
552
+ selector = selector.replace(/:empty$/gi, "");
553
+ return Array.from(parent.querySelectorAll(selector)).filter(($ele) => {
554
+ return $ele?.innerHTML?.trim() === "";
555
+ });
556
+ }
557
+ else if (selector.match(/[^\s]{1}:contains\("(.*)"\)$/i) || selector.match(/[^\s]{1}:contains\('(.*)'\)$/i)) {
558
+ // contains 语法
559
+ const textMatch = selector.match(/:contains\(("|')(.*)("|')\)$/i);
560
+ const text = textMatch[2];
561
+ selector = selector.replace(/:contains\(("|')(.*)("|')\)$/gi, "");
562
+ return Array.from(parent.querySelectorAll(selector)).filter(($ele) => {
563
+ // @ts-ignore
564
+ return ($ele?.textContent || $ele?.innerText)?.includes(text);
565
+ });
566
+ }
567
+ else if (selector.match(/[^\s]{1}:regexp\("(.*)"\)$/i) || selector.match(/[^\s]{1}:regexp\('(.*)'\)$/i)) {
568
+ // regexp 语法
569
+ const textMatch = selector.match(/:regexp\(("|')(.*)("|')\)$/i);
570
+ let pattern = textMatch[2];
571
+ const flagMatch = pattern.match(/("|'),[\s]*("|')([igm]{0,3})$/i);
572
+ let flags = "";
573
+ if (flagMatch) {
574
+ pattern = pattern.replace(/("|'),[\s]*("|')([igm]{0,3})$/gi, "");
575
+ flags = flagMatch[3];
576
+ }
577
+ const regexp = new RegExp(pattern, flags);
578
+ selector = selector.replace(/:regexp\(("|')(.*)("|')\)$/gi, "");
579
+ return Array.from(parent.querySelectorAll(selector)).filter(($ele) => {
580
+ // @ts-ignore
581
+ return Boolean(($ele?.textContent || $ele?.innerText)?.match(regexp));
582
+ });
583
+ }
584
+ else {
585
+ // 普通语法
586
+ return Array.from(parent.querySelectorAll(selector));
587
+ }
588
+ }
589
+ /**
590
+ * 匹配元素,可使用以下的额外语法
591
+ *
592
+ * + :contains([text]) 作用: 找到包含指定文本内容的指定元素
593
+ * + :empty 作用:找到既没有文本内容也没有子元素的指定元素
594
+ * + :regexp([text]) 作用: 找到符合正则表达式的内容的指定元素
595
+ * @param $el 元素
596
+ * @param selector 选择器
597
+ * @example
598
+ * DOMUtils.matches("div:contains('测试')")
599
+ * > true
600
+ * @example
601
+ * DOMUtils.matches("div:empty")
602
+ * > true
603
+ * @example
604
+ * DOMUtils.matches("div:regexp('^xxxx$')")
605
+ * > true
606
+ * @example
607
+ * DOMUtils.matches("div:regexp(/^xxx/ig)")
608
+ * > false
609
+ */
610
+ matches($el, selector) {
611
+ selector = selector.trim();
612
+ if ($el == null) {
613
+ return false;
614
+ }
615
+ if (selector.match(/[^\s]{1}:empty$/gi)) {
616
+ // empty 语法
617
+ selector = selector.replace(/:empty$/gi, "");
618
+ return $el.matches(selector) && $el?.innerHTML?.trim() === "";
619
+ }
620
+ else if (selector.match(/[^\s]{1}:contains\("(.*)"\)$/i) || selector.match(/[^\s]{1}:contains\('(.*)'\)$/i)) {
621
+ // contains 语法
622
+ const textMatch = selector.match(/:contains\(("|')(.*)("|')\)$/i);
623
+ const text = textMatch[2];
624
+ selector = selector.replace(/:contains\(("|')(.*)("|')\)$/gi, "");
625
+ // @ts-ignore
626
+ let content = $el?.textContent || $el?.innerText;
627
+ if (typeof content !== "string") {
628
+ content = "";
629
+ }
630
+ return $el.matches(selector) && content?.includes(text);
631
+ }
632
+ else if (selector.match(/[^\s]{1}:regexp\("(.*)"\)$/i) || selector.match(/[^\s]{1}:regexp\('(.*)'\)$/i)) {
633
+ // regexp 语法
634
+ const textMatch = selector.match(/:regexp\(("|')(.*)("|')\)$/i);
635
+ let pattern = textMatch[2];
636
+ const flagMatch = pattern.match(/("|'),[\s]*("|')([igm]{0,3})$/i);
637
+ let flags = "";
638
+ if (flagMatch) {
639
+ pattern = pattern.replace(/("|'),[\s]*("|')([igm]{0,3})$/gi, "");
640
+ flags = flagMatch[3];
641
+ }
642
+ const regexp = new RegExp(pattern, flags);
643
+ selector = selector.replace(/:regexp\(("|')(.*)("|')\)$/gi, "");
644
+ // @ts-ignore
645
+ let content = $el?.textContent || $el?.innerText;
646
+ if (typeof content !== "string") {
647
+ content = "";
648
+ }
649
+ return $el.matches(selector) && Boolean(content?.match(regexp));
650
+ }
651
+ else {
652
+ // 普通语法
653
+ return $el.matches(selector);
654
+ }
655
+ }
656
+ closest($el, selector) {
657
+ selector = selector.trim();
658
+ if (selector.match(/[^\s]{1}:empty$/gi)) {
659
+ // empty 语法
660
+ selector = selector.replace(/:empty$/gi, "");
661
+ const $closest = $el?.closest(selector);
662
+ if ($closest && $closest?.innerHTML?.trim() === "") {
663
+ return $closest;
664
+ }
665
+ return null;
666
+ }
667
+ else if (selector.match(/[^\s]{1}:contains\("(.*)"\)$/i) || selector.match(/[^\s]{1}:contains\('(.*)'\)$/i)) {
668
+ // contains 语法
669
+ const textMatch = selector.match(/:contains\(("|')(.*)("|')\)$/i);
670
+ const text = textMatch[2];
671
+ selector = selector.replace(/:contains\(("|')(.*)("|')\)$/gi, "");
672
+ const $closest = $el?.closest(selector);
673
+ if ($closest) {
674
+ // @ts-ignore
675
+ const content = $el?.textContent || $el?.innerText;
676
+ if (typeof content === "string" && content.includes(text)) {
677
+ return $closest;
678
+ }
679
+ }
680
+ return null;
681
+ }
682
+ else if (selector.match(/[^\s]{1}:regexp\("(.*)"\)$/i) || selector.match(/[^\s]{1}:regexp\('(.*)'\)$/i)) {
683
+ // regexp 语法
684
+ const textMatch = selector.match(/:regexp\(("|')(.*)("|')\)$/i);
685
+ let pattern = textMatch[2];
686
+ const flagMatch = pattern.match(/("|'),[\s]*("|')([igm]{0,3})$/i);
687
+ let flags = "";
688
+ if (flagMatch) {
689
+ pattern = pattern.replace(/("|'),[\s]*("|')([igm]{0,3})$/gi, "");
690
+ flags = flagMatch[3];
691
+ }
692
+ const regexp = new RegExp(pattern, flags);
693
+ selector = selector.replace(/:regexp\(("|')(.*)("|')\)$/gi, "");
694
+ const $closest = $el?.closest(selector);
695
+ if ($closest) {
696
+ // @ts-ignore
697
+ const content = $el?.textContent || $el?.innerText;
698
+ if (typeof content === "string" && content.match(regexp)) {
699
+ return $closest;
700
+ }
701
+ }
702
+ return null;
703
+ }
704
+ else {
705
+ // 普通语法
706
+ const $closest = $el?.closest(selector);
707
+ return $closest;
708
+ }
709
+ }
710
+ }
711
+ const elementSelector = new ElementSelector();
712
+
713
+ /**
714
+ * 判断对象是否是元素
715
+ * @param $el
716
+ * @returns
717
+ * + true 是元素
718
+ * + false 不是元素
719
+ * @example
720
+ * DOMUtilsCommonUtils.isDOM(document.querySelector("a"))
721
+ * > true
722
+ */
723
+ const isDOM = function ($el) {
724
+ return $el instanceof Node;
725
+ };
726
+ class Utils {
727
+ windowApi;
728
+ constructor(option) {
729
+ this.windowApi = new WindowApi(option);
730
+ }
731
+ /**
732
+ * 判断对象是否是jQuery对象
733
+ * @param target
734
+ * @returns
735
+ * + true 是jQuery对象
736
+ * + false 不是jQuery对象
737
+ * @example
738
+ * Utils.isJQuery($("a"))
739
+ * > true
740
+ */
741
+ isJQuery(target) {
742
+ let result = false;
743
+ if (typeof jQuery === "object" && target instanceof jQuery) {
744
+ result = true;
745
+ }
746
+ if (target == null) {
747
+ return false;
748
+ }
749
+ if (typeof target === "object") {
750
+ /* 也有种可能,这个jQuery对象是1.8.3版本的,页面中的jQuery是3.4.1版本的 */
751
+ const jQueryProps = [
752
+ "add",
753
+ "addBack",
754
+ "addClass",
755
+ "after",
756
+ "ajaxComplete",
757
+ "ajaxError",
758
+ "ajaxSend",
759
+ "ajaxStart",
760
+ "ajaxStop",
761
+ "ajaxSuccess",
762
+ "animate",
763
+ "append",
764
+ "appendTo",
765
+ "attr",
766
+ "before",
767
+ "bind",
768
+ "blur",
769
+ "change",
770
+ "children",
771
+ "clearQueue",
772
+ "click",
773
+ "clone",
774
+ "closest",
775
+ "constructor",
776
+ "contents",
777
+ "contextmenu",
778
+ "css",
779
+ "data",
780
+ "dblclick",
781
+ "delay",
782
+ "delegate",
783
+ "dequeue",
784
+ "each",
785
+ "empty",
786
+ "end",
787
+ "eq",
788
+ "extend",
789
+ "fadeIn",
790
+ "fadeOut",
791
+ "fadeTo",
792
+ "fadeToggle",
793
+ "filter",
794
+ "find",
795
+ "first",
796
+ "focus",
797
+ "focusin",
798
+ "focusout",
799
+ "get",
800
+ "has",
801
+ "hasClass",
802
+ "height",
803
+ "hide",
804
+ "hover",
805
+ "html",
806
+ "index",
807
+ "init",
808
+ "innerHeight",
809
+ "innerWidth",
810
+ "insertAfter",
811
+ "insertBefore",
812
+ "is",
813
+ "jquery",
814
+ "keydown",
815
+ "keypress",
816
+ "keyup",
817
+ "last",
818
+ "load",
819
+ "map",
820
+ "mousedown",
821
+ "mouseenter",
822
+ "mouseleave",
823
+ "mousemove",
824
+ "mouseout",
825
+ "mouseover",
826
+ "mouseup",
827
+ "next",
828
+ "nextAll",
829
+ "not",
830
+ "off",
831
+ "offset",
832
+ "offsetParent",
833
+ "on",
834
+ "one",
835
+ "outerHeight",
836
+ "outerWidth",
837
+ "parent",
838
+ "parents",
839
+ "position",
840
+ "prepend",
841
+ "prependTo",
842
+ "prev",
843
+ "prevAll",
844
+ "prevUntil",
845
+ "promise",
846
+ "prop",
847
+ "pushStack",
848
+ "queue",
849
+ "ready",
850
+ "remove",
851
+ "removeAttr",
852
+ "removeClass",
853
+ "removeData",
854
+ "removeProp",
855
+ "replaceAll",
856
+ "replaceWith",
857
+ "resize",
858
+ "scroll",
859
+ "scrollLeft",
860
+ "scrollTop",
861
+ "select",
862
+ "show",
863
+ "siblings",
864
+ "slice",
865
+ "slideDown",
866
+ "slideToggle",
867
+ "slideUp",
868
+ "sort",
869
+ "splice",
870
+ "text",
871
+ "toArray",
872
+ "toggle",
873
+ "toggleClass",
874
+ "trigger",
875
+ "triggerHandler",
876
+ "unbind",
877
+ "width",
878
+ "wrap",
879
+ ];
880
+ for (const jQueryPropsName of jQueryProps) {
881
+ if (!(jQueryPropsName in target)) {
882
+ result = false;
883
+ break;
884
+ }
885
+ else {
886
+ result = true;
887
+ }
888
+ }
889
+ }
890
+ return result;
891
+ }
892
+ assign(target = {}, source = {}, isAdd = false) {
893
+ const UtilsContext = this;
894
+ if (Array.isArray(source)) {
895
+ const canTraverse = source.filter((item) => {
896
+ return typeof item === "object";
897
+ });
898
+ if (!canTraverse.length) {
899
+ return source;
900
+ }
901
+ }
902
+ if (source == null) {
903
+ return target;
904
+ }
905
+ if (target == null) {
906
+ target = {};
907
+ }
908
+ if (isAdd) {
909
+ for (const sourceKeyName in source) {
910
+ const targetKeyName = sourceKeyName;
911
+ const targetValue = Reflect.get(target, targetKeyName);
912
+ const sourceValue = Reflect.get(source, sourceKeyName);
913
+ if (typeof sourceValue === "object" && sourceValue != null && sourceKeyName in target && !isDOM(sourceValue)) {
914
+ /* 源端的值是object类型,且不是元素节点 */
915
+ Reflect.set(target, sourceKeyName, UtilsContext.assign(targetValue, sourceValue, isAdd));
916
+ continue;
917
+ }
918
+ Reflect.set(target, sourceKeyName, sourceValue);
919
+ }
920
+ }
921
+ else {
922
+ for (const targetKeyName in target) {
923
+ if (targetKeyName in source) {
924
+ const targetValue = Reflect.get(target, targetKeyName);
925
+ const sourceValue = Reflect.get(source, targetKeyName);
926
+ if (typeof sourceValue === "object" &&
927
+ sourceValue != null &&
928
+ !isDOM(sourceValue) &&
929
+ Object.keys(sourceValue).length) {
930
+ /* 源端的值是object类型,且不是元素节点 */
931
+ Reflect.set(target, targetKeyName, UtilsContext.assign(targetValue, sourceValue, isAdd));
932
+ continue;
933
+ }
934
+ /* 直接赋值 */
935
+ Reflect.set(target, targetKeyName, sourceValue);
936
+ }
937
+ }
938
+ }
939
+ return target;
940
+ }
941
+ mutationObserver(target, observer_config) {
942
+ const that = this;
943
+ const default_obverser_config = {
944
+ /* 监听到元素有反馈,需执行的函数 */
945
+ callback: () => { },
946
+ config: {
947
+ /**
948
+ * + true 监听以 target 为根节点的整个子树。包括子树中所有节点的属性,而不仅仅是针对 target
949
+ * + false (默认) 不生效
950
+ */
951
+ subtree: void 0,
952
+ /**
953
+ * + true 监听 target 节点中发生的节点的新增与删除(同时,如果 subtree 为 true,会针对整个子树生效)
954
+ * + false (默认) 不生效
955
+ */
956
+ childList: void 0,
957
+ /**
958
+ * + true 观察所有监听的节点属性值的变化。默认值为 true,当声明了 attributeFilter 或 attributeOldValue
959
+ * + false (默认) 不生效
960
+ */
961
+ attributes: void 0,
962
+ /**
963
+ * 一个用于声明哪些属性名会被监听的数组。如果不声明该属性,所有属性的变化都将触发通知
964
+ */
965
+ attributeFilter: void 0,
966
+ /**
967
+ * + true 记录上一次被监听的节点的属性变化;可查阅 MutationObserver 中的 Monitoring attribute values 了解关于观察属性变化和属性值记录的详情
968
+ * + false (默认) 不生效
969
+ */
970
+ attributeOldValue: void 0,
971
+ /**
972
+ * + true 监听声明的 target 节点上所有字符的变化。默认值为 true,如果声明了 characterDataOldValue
973
+ * + false (默认) 不生效
974
+ */
975
+ characterData: void 0,
976
+ /**
977
+ * + true 记录前一个被监听的节点中发生的文本变化
978
+ * + false (默认) 不生效
979
+ */
980
+ characterDataOldValue: void 0,
981
+ },
982
+ immediate: false,
983
+ };
984
+ observer_config = that.assign(default_obverser_config, observer_config);
985
+ const windowMutationObserver = this.windowApi.window.MutationObserver ||
986
+ this.windowApi.window.webkitMutationObserver ||
987
+ this.windowApi.window.MozMutationObserver;
988
+ // 观察者对象
989
+ const mutationObserver = new windowMutationObserver(function (mutations, observer) {
990
+ if (typeof observer_config.callback === "function") {
991
+ observer_config.callback(mutations, observer);
992
+ }
993
+ });
994
+ if (Array.isArray(target) || target instanceof NodeList) {
995
+ // 传入的是数组或者元素数组
996
+ target.forEach((item) => {
997
+ mutationObserver.observe(item, observer_config.config);
998
+ });
999
+ }
1000
+ else if (that.isJQuery(target)) {
1001
+ /* 传入的参数是jQuery对象 */
1002
+ target.each((_, item) => {
1003
+ mutationObserver.observe(item, observer_config.config);
1004
+ });
1005
+ }
1006
+ else {
1007
+ mutationObserver.observe(target, observer_config.config);
1008
+ }
1009
+ if (observer_config.immediate) {
1010
+ /* 主动触发一次 */
1011
+ if (typeof observer_config.callback === "function") {
1012
+ observer_config.callback([], mutationObserver);
1013
+ }
1014
+ }
1015
+ return mutationObserver;
1016
+ }
1017
+ }
1018
+ const utils = new Utils();
1019
+
1020
+ class ElementWait extends ElementSelector {
1021
+ windowApi;
1022
+ constructor(windowApiOption) {
1023
+ super(windowApiOption);
1024
+ this.windowApi = new WindowApi(windowApiOption);
1025
+ }
1026
+ wait(checkFn, timeout, parent) {
1027
+ const UtilsContext = this;
1028
+ const __timeout__ = typeof timeout === "number" ? timeout : 0;
1029
+ return new Promise((resolve) => {
1030
+ const observer = utils.mutationObserver(parent || UtilsContext.windowApi.document, {
1031
+ config: {
1032
+ subtree: true,
1033
+ childList: true,
1034
+ attributes: true,
1035
+ },
1036
+ immediate: true,
1037
+ callback(_, __observer__) {
1038
+ const result = checkFn();
1039
+ if (result.success) {
1040
+ // 取消观察器
1041
+ if (typeof __observer__?.disconnect === "function") {
1042
+ __observer__.disconnect();
1043
+ }
1044
+ resolve(result.data);
1045
+ }
1046
+ },
1047
+ });
1048
+ if (__timeout__ > 0) {
1049
+ CommonUtils.setTimeout(() => {
1050
+ // 取消观察器
1051
+ if (typeof observer?.disconnect === "function") {
1052
+ observer.disconnect();
1053
+ }
1054
+ resolve(null);
1055
+ }, __timeout__);
1056
+ }
1057
+ });
1058
+ }
1059
+ waitNode(...args) {
1060
+ // 过滤掉undefined
1061
+ args = args.filter((arg) => arg !== void 0);
1062
+ const UtilsContext = this;
1063
+ // 选择器
1064
+ const selector = args[0];
1065
+ // 父元素(监听的元素)
1066
+ let parent = UtilsContext.windowApi.document;
1067
+ // 超时时间
1068
+ let timeout = 0;
1069
+ if (typeof args[0] !== "string" && !Array.isArray(args[0]) && typeof args[0] !== "function") {
1070
+ throw new TypeError("Utils.waitNode 第一个参数必须是string|string[]|Function");
1071
+ }
1072
+ if (args.length === 1) ;
1073
+ else if (args.length === 2) {
1074
+ const secondParam = args[1];
1075
+ if (typeof secondParam === "number") {
1076
+ // "div",10000
1077
+ timeout = secondParam;
1078
+ }
1079
+ else if (typeof secondParam === "object" && secondParam instanceof Node) {
1080
+ // "div",document
1081
+ parent = secondParam;
1082
+ }
1083
+ else {
1084
+ throw new TypeError("Utils.waitNode 第二个参数必须是number|Node");
1085
+ }
1086
+ }
1087
+ else if (args.length === 3) {
1088
+ // "div",document,10000
1089
+ // 第二个参数,parent
1090
+ const secondParam = args[1];
1091
+ // 第三个参数,timeout
1092
+ const thirdParam = args[2];
1093
+ if (typeof secondParam === "object" && secondParam instanceof Node) {
1094
+ parent = secondParam;
1095
+ if (typeof thirdParam === "number") {
1096
+ timeout = thirdParam;
1097
+ }
1098
+ else {
1099
+ throw new TypeError("Utils.waitNode 第三个参数必须是number");
1100
+ }
1101
+ }
1102
+ else {
1103
+ throw new TypeError("Utils.waitNode 第二个参数必须是Node");
1104
+ }
1105
+ }
1106
+ else {
1107
+ throw new TypeError("Utils.waitNode 参数个数错误");
1108
+ }
1109
+ function getNode() {
1110
+ if (Array.isArray(selector)) {
1111
+ const result = [];
1112
+ for (let index = 0; index < selector.length; index++) {
1113
+ const node = elementSelector.selector(selector[index]);
1114
+ if (node) {
1115
+ result.push(node);
1116
+ }
1117
+ }
1118
+ if (result.length === selector.length) {
1119
+ return result;
1120
+ }
1121
+ }
1122
+ else if (typeof selector === "function") {
1123
+ return selector();
1124
+ }
1125
+ else {
1126
+ return elementSelector.selector(selector, parent);
1127
+ }
1128
+ }
1129
+ return UtilsContext.wait(() => {
1130
+ const node = getNode();
1131
+ if (node) {
1132
+ return {
1133
+ success: true,
1134
+ data: node,
1135
+ };
1136
+ }
1137
+ else {
1138
+ return {
1139
+ success: false,
1140
+ data: node,
1141
+ };
1142
+ }
1143
+ }, timeout, parent);
1144
+ }
1145
+ waitAnyNode(...args) {
1146
+ // 过滤掉undefined
1147
+ args = args.filter((arg) => arg !== void 0);
1148
+ const UtilsContext = this;
1149
+ // 选择器
1150
+ const selectorList = args[0];
1151
+ // 父元素(监听的元素)
1152
+ let parent = UtilsContext.windowApi.document;
1153
+ // 超时时间
1154
+ let timeout = 0;
1155
+ if (typeof args[0] !== "object" && !Array.isArray(args[0])) {
1156
+ throw new TypeError("Utils.waitAnyNode 第一个参数必须是string[]");
1157
+ }
1158
+ if (args.length === 1) ;
1159
+ else if (args.length === 2) {
1160
+ const secondParam = args[1];
1161
+ if (typeof secondParam === "number") {
1162
+ // "div",10000
1163
+ timeout = secondParam;
1164
+ }
1165
+ else if (typeof secondParam === "object" && secondParam instanceof Node) {
1166
+ // "div",document
1167
+ parent = secondParam;
1168
+ }
1169
+ else {
1170
+ throw new TypeError("Utils.waitAnyNode 第二个参数必须是number|Node");
1171
+ }
1172
+ }
1173
+ else if (args.length === 3) {
1174
+ // "div",document,10000
1175
+ // 第二个参数,parent
1176
+ const secondParam = args[1];
1177
+ // 第三个参数,timeout
1178
+ const thirdParam = args[2];
1179
+ if (typeof secondParam === "object" && secondParam instanceof Node) {
1180
+ parent = secondParam;
1181
+ if (typeof thirdParam === "number") {
1182
+ timeout = thirdParam;
1183
+ }
1184
+ else {
1185
+ throw new TypeError("Utils.waitAnyNode 第三个参数必须是number");
1186
+ }
1187
+ }
1188
+ else {
1189
+ throw new TypeError("Utils.waitAnyNode 第二个参数必须是Node");
1190
+ }
1191
+ }
1192
+ else {
1193
+ throw new TypeError("Utils.waitAnyNode 参数个数错误");
1194
+ }
1195
+ const promiseList = selectorList.map((selector) => {
1196
+ return UtilsContext.waitNode(selector, parent, timeout);
1197
+ });
1198
+ return Promise.any(promiseList);
1199
+ }
1200
+ waitNodeList(...args) {
1201
+ // 过滤掉undefined
1202
+ args = args.filter((arg) => arg !== void 0);
1203
+ const UtilsContext = this;
1204
+ // 选择器数组
1205
+ const selector = args[0];
1206
+ // 父元素(监听的元素)
1207
+ let parent = UtilsContext.windowApi.document;
1208
+ // 超时时间
1209
+ let timeout = 0;
1210
+ if (typeof args[0] !== "string" && !Array.isArray(args[0])) {
1211
+ throw new TypeError("Utils.waitNodeList 第一个参数必须是string|string[]");
1212
+ }
1213
+ if (args.length === 1) ;
1214
+ else if (args.length === 2) {
1215
+ const secondParam = args[1];
1216
+ if (typeof secondParam === "number") {
1217
+ // "div",10000
1218
+ timeout = secondParam;
1219
+ }
1220
+ else if (typeof secondParam === "object" && secondParam instanceof Node) {
1221
+ // "div",document
1222
+ parent = secondParam;
1223
+ }
1224
+ else {
1225
+ throw new TypeError("Utils.waitNodeList 第二个参数必须是number|Node");
1226
+ }
1227
+ }
1228
+ else if (args.length === 3) {
1229
+ // "div",document,10000
1230
+ // 第二个参数,parent
1231
+ const secondParam = args[1];
1232
+ // 第三个参数,timeout
1233
+ const thirdParam = args[2];
1234
+ if (typeof secondParam === "object" && secondParam instanceof Node) {
1235
+ parent = secondParam;
1236
+ if (typeof thirdParam === "number") {
1237
+ timeout = thirdParam;
1238
+ }
1239
+ else {
1240
+ throw new TypeError("Utils.waitNodeList 第三个参数必须是number");
1241
+ }
1242
+ }
1243
+ else {
1244
+ throw new TypeError("Utils.waitNodeList 第二个参数必须是Node");
1245
+ }
1246
+ }
1247
+ else {
1248
+ throw new TypeError("Utils.waitNodeList 参数个数错误");
1249
+ }
1250
+ function getNodeList() {
1251
+ if (Array.isArray(selector)) {
1252
+ const result = [];
1253
+ for (let index = 0; index < selector.length; index++) {
1254
+ const nodeList = elementSelector.selectorAll(selector[index], parent);
1255
+ if (nodeList.length) {
1256
+ result.push(nodeList);
1257
+ }
1258
+ }
1259
+ if (result.length === selector.length) {
1260
+ return result;
1261
+ }
1262
+ }
1263
+ else {
1264
+ const nodeList = elementSelector.selectorAll(selector, parent);
1265
+ if (nodeList.length) {
1266
+ return nodeList;
1267
+ }
1268
+ }
1269
+ }
1270
+ return UtilsContext.wait(() => {
1271
+ const node = getNodeList();
1272
+ if (node) {
1273
+ return {
1274
+ success: true,
1275
+ data: node,
1276
+ };
1277
+ }
1278
+ else {
1279
+ return {
1280
+ success: false,
1281
+ data: node,
1282
+ };
1283
+ }
1284
+ }, timeout, parent);
1285
+ }
1286
+ waitAnyNodeList(...args) {
1287
+ // 过滤掉undefined
1288
+ args = args.filter((arg) => arg !== void 0);
1289
+ const UtilsContext = this;
1290
+ // 选择器数组
1291
+ const selectorList = args[0];
1292
+ // 父元素(监听的元素)
1293
+ let parent = UtilsContext.windowApi.document;
1294
+ // 超时时间
1295
+ let timeout = 0;
1296
+ if (!Array.isArray(args[0])) {
1297
+ throw new TypeError("Utils.waitAnyNodeList 第一个参数必须是string[]");
1298
+ }
1299
+ if (args.length === 1) ;
1300
+ else if (args.length === 2) {
1301
+ const secondParam = args[1];
1302
+ if (typeof secondParam === "number") {
1303
+ // "div",10000
1304
+ timeout = secondParam;
1305
+ }
1306
+ else if (typeof secondParam === "object" && secondParam instanceof Node) {
1307
+ // "div",document
1308
+ parent = secondParam;
1309
+ }
1310
+ else {
1311
+ throw new TypeError("Utils.waitAnyNodeList 第二个参数必须是number|Node");
1312
+ }
1313
+ }
1314
+ else if (args.length === 3) {
1315
+ // "div",document,10000
1316
+ // 第二个参数,parent
1317
+ const secondParam = args[1];
1318
+ // 第三个参数,timeout
1319
+ const thirdParam = args[2];
1320
+ if (typeof secondParam === "object" && secondParam instanceof Node) {
1321
+ parent = secondParam;
1322
+ if (typeof thirdParam === "number") {
1323
+ timeout = thirdParam;
1324
+ }
1325
+ else {
1326
+ throw new TypeError("Utils.waitAnyNodeList 第三个参数必须是number");
1327
+ }
1328
+ }
1329
+ else {
1330
+ throw new TypeError("Utils.waitAnyNodeList 第二个参数必须是Node");
1331
+ }
1332
+ }
1333
+ else {
1334
+ throw new TypeError("Utils.waitAnyNodeList 参数个数错误");
1335
+ }
1336
+ const promiseList = selectorList.map((selector) => {
1337
+ return UtilsContext.waitNodeList(selector, parent, timeout);
1338
+ });
1339
+ return Promise.any(promiseList);
1340
+ }
1341
+ }
1342
+ new ElementWait();
1343
+
1344
+ class ElementAnimate extends ElementWait {
1345
+ windowApi;
1346
+ constructor(windowApiOption) {
1347
+ super(windowApiOption);
1348
+ this.windowApi = new WindowApi(windowApiOption);
1349
+ }
1350
+ /**
1351
+ * 在一定时间内改变元素的样式属性,实现动画效果
1352
+ * @param element 需要进行动画的元素
1353
+ * @param styles 动画结束时元素的样式属性
1354
+ * @param duration 动画持续时间,单位为毫秒
1355
+ * @param callback 动画结束后执行的函数
1356
+ * @example
1357
+ * // 监听元素a.xx的从显示变为隐藏
1358
+ * DOMUtils.animate(document.querySelector("a.xx"),{ top:100},1000,function(){
1359
+ * console.log("已往上位移100px")
1360
+ * })
1361
+ */
1362
+ animate(element, styles, duration = 1000, callback = null) {
1363
+ const context = this;
1364
+ if (typeof element === "string") {
1365
+ element = elementSelector.selectorAll(element);
1366
+ }
1367
+ if (element == null) {
1368
+ return;
1369
+ }
1370
+ if (CommonUtils.isNodeList(element)) {
1371
+ // 设置
1372
+ element.forEach(($ele) => {
1373
+ context.animate($ele, styles, duration, callback);
1374
+ });
1375
+ return;
1376
+ }
1377
+ if (typeof duration !== "number" || duration <= 0) {
1378
+ throw new TypeError("duration must be a positive number");
1379
+ }
1380
+ if (typeof callback !== "function" && callback !== void 0) {
1381
+ throw new TypeError("callback must be a function or null");
1382
+ }
1383
+ if (typeof styles !== "object" || styles === void 0) {
1384
+ throw new TypeError("styles must be an object");
1385
+ }
1386
+ if (Object.keys(styles).length === 0) {
1387
+ throw new Error("styles must contain at least one property");
1388
+ }
1389
+ const start = performance.now();
1390
+ const from = {};
1391
+ const to = {};
1392
+ for (const prop in styles) {
1393
+ from[prop] = element.style[prop] || context.windowApi.globalThis.getComputedStyle(element)[prop];
1394
+ to[prop] = styles[prop];
1395
+ }
1396
+ const timer = CommonUtils.setInterval(function () {
1397
+ const timePassed = performance.now() - start;
1398
+ let progress = timePassed / duration;
1399
+ if (progress > 1) {
1400
+ progress = 1;
1401
+ }
1402
+ for (const prop in styles) {
1403
+ element.style[prop] = from[prop] + (to[prop] - from[prop]) * progress + "px";
1404
+ }
1405
+ if (progress === 1) {
1406
+ CommonUtils.clearInterval(timer);
1407
+ if (callback) {
1408
+ callback();
1409
+ }
1410
+ }
1411
+ }, 10);
1412
+ }
1413
+ /**
1414
+ * 显示元素
1415
+ * @param target 当前元素
1416
+ * @param checkVisiblie 是否检测元素是否显示
1417
+ * + true (默认)如果检测到还未显示,则强制使用display: unset !important;
1418
+ * + false 不检测,直接设置display属性为空
1419
+ * @example
1420
+ * // 显示a.xx元素
1421
+ * DOMUtils.show(document.querySelector("a.xx"))
1422
+ * DOMUtils.show(document.querySelectorAll("a.xx"))
1423
+ * DOMUtils.show("a.xx")
1424
+ */
1425
+ show(target, checkVisiblie = true) {
1426
+ const context = this;
1427
+ if (target == null) {
1428
+ return;
1429
+ }
1430
+ if (typeof target === "string") {
1431
+ target = elementSelector.selectorAll(target);
1432
+ }
1433
+ if (target instanceof NodeList || target instanceof Array) {
1434
+ target = target;
1435
+ for (const element of target) {
1436
+ context.show(element, checkVisiblie);
1437
+ }
1438
+ }
1439
+ else {
1440
+ target = target;
1441
+ target.style.display = "";
1442
+ if (checkVisiblie) {
1443
+ if (!CommonUtils.isShow(target)) {
1444
+ /* 仍然是不显示,尝试使用强覆盖 */
1445
+ target.style.setProperty("display", "unset", "important");
1446
+ }
1447
+ }
1448
+ }
1449
+ }
1450
+ /**
1451
+ * 隐藏元素
1452
+ * @param target 当前元素
1453
+ * @param checkVisiblie 是否检测元素是否显示
1454
+ * + true (默认)如果检测到显示,则强制使用display: none !important;
1455
+ * + false 不检测,直接设置display属性为none
1456
+ * @example
1457
+ * // 隐藏a.xx元素
1458
+ * DOMUtils.hide(document.querySelector("a.xx"))
1459
+ * DOMUtils.hide(document.querySelectorAll("a.xx"))
1460
+ * DOMUtils.hide("a.xx")
1461
+ */
1462
+ hide(target, checkVisiblie = true) {
1463
+ const context = this;
1464
+ if (target == null) {
1465
+ return;
1466
+ }
1467
+ if (typeof target === "string") {
1468
+ target = elementSelector.selectorAll(target);
1469
+ }
1470
+ if (target instanceof NodeList || target instanceof Array) {
1471
+ target = target;
1472
+ for (const element of target) {
1473
+ context.hide(element, checkVisiblie);
1474
+ }
1475
+ }
1476
+ else {
1477
+ target = target;
1478
+ target.style.display = "none";
1479
+ if (checkVisiblie) {
1480
+ if (CommonUtils.isShow(target)) {
1481
+ /* 仍然是显示,尝试使用强覆盖 */
1482
+ target.style.setProperty("display", "none", "important");
1483
+ }
1484
+ }
1485
+ }
1486
+ }
1487
+ /**
1488
+ * 淡入元素
1489
+ * @param element 当前元素
1490
+ * @param duration 动画持续时间(毫秒),默认400毫秒
1491
+ * @param callback 动画结束的回调
1492
+ * @example
1493
+ * // 元素a.xx淡入
1494
+ * DOMUtils.fadeIn(document.querySelector("a.xx"),2500,()=>{
1495
+ * console.log("淡入完毕");
1496
+ * })
1497
+ * DOMUtils.fadeIn("a.xx",undefined,()=>{
1498
+ * console.log("淡入完毕");
1499
+ * })
1500
+ */
1501
+ fadeIn(element, duration = 400, callback) {
1502
+ if (element == null) {
1503
+ return;
1504
+ }
1505
+ const context = this;
1506
+ if (typeof element === "string") {
1507
+ element = elementSelector.selectorAll(element);
1508
+ }
1509
+ if (CommonUtils.isNodeList(element)) {
1510
+ // 设置
1511
+ element.forEach(($ele) => {
1512
+ context.fadeIn($ele, duration, callback);
1513
+ });
1514
+ return;
1515
+ }
1516
+ element.style.opacity = "0";
1517
+ element.style.display = "";
1518
+ let start = null;
1519
+ let timer = null;
1520
+ function step(timestamp) {
1521
+ if (!start)
1522
+ start = timestamp;
1523
+ const progress = timestamp - start;
1524
+ element = element;
1525
+ element.style.opacity = Math.min(progress / duration, 1).toString();
1526
+ if (progress < duration) {
1527
+ context.windowApi.window.requestAnimationFrame(step);
1528
+ }
1529
+ else {
1530
+ if (callback && typeof callback === "function") {
1531
+ callback();
1532
+ }
1533
+ context.windowApi.window.cancelAnimationFrame(timer);
1534
+ }
1535
+ }
1536
+ timer = context.windowApi.window.requestAnimationFrame(step);
1537
+ }
1538
+ /**
1539
+ * 淡出元素
1540
+ * @param element 当前元素
1541
+ * @param duration 动画持续时间(毫秒),默认400毫秒
1542
+ * @param callback 动画结束的回调
1543
+ * @example
1544
+ * // 元素a.xx淡出
1545
+ * DOMUtils.fadeOut(document.querySelector("a.xx"),2500,()=>{
1546
+ * console.log("淡出完毕");
1547
+ * })
1548
+ * DOMUtils.fadeOut("a.xx",undefined,()=>{
1549
+ * console.log("淡出完毕");
1550
+ * })
458
1551
  */
459
- clearTimeout(timeId) {
460
- try {
461
- if (timeId != null) {
462
- clearTimeout$1(timeId);
463
- }
1552
+ fadeOut(element, duration = 400, callback) {
1553
+ const context = this;
1554
+ if (element == null) {
1555
+ return;
464
1556
  }
465
- catch {
466
- // TODO
1557
+ if (typeof element === "string") {
1558
+ element = elementSelector.selectorAll(element);
467
1559
  }
468
- finally {
469
- this.windowApi.clearTimeout(timeId);
1560
+ if (CommonUtils.isNodeList(element)) {
1561
+ // 设置
1562
+ element.forEach(($ele) => {
1563
+ context.fadeOut($ele, duration, callback);
1564
+ });
1565
+ return;
470
1566
  }
471
- },
1567
+ element.style.opacity = "1";
1568
+ let start = null;
1569
+ let timer = null;
1570
+ function step(timestamp) {
1571
+ if (!start)
1572
+ start = timestamp;
1573
+ const progress = timestamp - start;
1574
+ element = element;
1575
+ element.style.opacity = Math.max(1 - progress / duration, 0).toString();
1576
+ if (progress < duration) {
1577
+ context.windowApi.window.requestAnimationFrame(step);
1578
+ }
1579
+ else {
1580
+ element.style.display = "none";
1581
+ if (typeof callback === "function") {
1582
+ callback();
1583
+ }
1584
+ context.windowApi.window.cancelAnimationFrame(timer);
1585
+ }
1586
+ }
1587
+ timer = context.windowApi.window.requestAnimationFrame(step);
1588
+ }
472
1589
  /**
473
- * 自动使用 Worker 执行 setInterval
1590
+ * 切换元素的显示和隐藏状态
1591
+ * @param element 当前元素
1592
+ * @param checkVisiblie 是否检测元素是否显示
1593
+ * @example
1594
+ * // 如果元素a.xx当前是隐藏,则显示,如果是显示,则隐藏
1595
+ * DOMUtils.toggle(document.querySelector("a.xx"))
1596
+ * DOMUtils.toggle("a.xx")
474
1597
  */
475
- setInterval(callback, timeout = 0) {
476
- try {
477
- return setInterval$1(callback, timeout);
1598
+ toggle(element, checkVisiblie) {
1599
+ const context = this;
1600
+ if (typeof element === "string") {
1601
+ element = elementSelector.selectorAll(element);
478
1602
  }
479
- catch {
480
- return this.windowApi.setInterval(callback, timeout);
1603
+ if (element == null) {
1604
+ return;
481
1605
  }
482
- },
483
- /**
484
- * 配合 .setInterval 使用
485
- */
486
- clearInterval(timeId) {
487
- try {
488
- if (timeId != null) {
489
- clearInterval$1(timeId);
490
- }
1606
+ if (CommonUtils.isNodeList(element)) {
1607
+ // 设置
1608
+ element.forEach(($ele) => {
1609
+ context.toggle($ele);
1610
+ });
1611
+ return;
491
1612
  }
492
- catch {
493
- // TODO
1613
+ if (context.windowApi.globalThis.getComputedStyle(element).getPropertyValue("display") === "none") {
1614
+ context.show(element, checkVisiblie);
494
1615
  }
495
- finally {
496
- this.windowApi.clearInterval(timeId);
1616
+ else {
1617
+ context.hide(element, checkVisiblie);
497
1618
  }
498
- },
499
- /**
500
- * 判断是否是元素列表
501
- * @param $ele
502
- */
503
- isNodeList($ele) {
504
- return Array.isArray($ele) || $ele instanceof NodeList;
505
- },
506
- /** 获取 animationend 在各个浏览器的兼容名 */
507
- getAnimationEndNameList() {
508
- return ["webkitAnimationEnd", "mozAnimationEnd", "MSAnimationEnd", "oanimationend", "animationend"];
509
- },
510
- /** 获取 transitionend 在各个浏览器的兼容名 */
511
- getTransitionEndNameList() {
512
- return ["webkitTransitionEnd", "mozTransitionEnd", "MSTransitionEnd", "otransitionend", "transitionend"];
513
- },
514
- };
515
-
516
- /* 数据 */
517
- const DOMUtilsData = {
518
- /** .on添加在元素存储的事件 */
519
- SymbolEvents: Symbol("events_" + (((1 + Math.random()) * 0x10000) | 0).toString(16).substring(1)),
520
- };
1619
+ }
1620
+ }
1621
+ new ElementAnimate();
521
1622
 
522
1623
  const OriginPrototype = {
523
1624
  Object: {
@@ -525,11 +1626,20 @@
525
1626
  },
526
1627
  };
527
1628
 
528
- class DOMUtilsEvent {
1629
+ class ElementEvent extends ElementAnimate {
529
1630
  windowApi;
530
1631
  constructor(windowApiOption) {
1632
+ super(windowApiOption);
531
1633
  this.windowApi = new WindowApi(windowApiOption);
532
1634
  }
1635
+ /** 获取 animationend 在各个浏览器的兼容名 */
1636
+ getAnimationEndNameList() {
1637
+ return CommonUtils.getAnimationEndNameList();
1638
+ }
1639
+ /** 获取 transitionend 在各个浏览器的兼容名 */
1640
+ getTransitionEndNameList() {
1641
+ return CommonUtils.getTransitionEndNameList();
1642
+ }
533
1643
  on(element, eventType, selector, callback, option) {
534
1644
  /**
535
1645
  * 获取option配置
@@ -560,35 +1670,37 @@
560
1670
  }
561
1671
  return option;
562
1672
  }
563
- const DOMUtilsContext = this;
1673
+ const that = this;
564
1674
  // eslint-disable-next-line prefer-rest-params
565
1675
  const args = arguments;
566
1676
  if (typeof element === "string") {
567
- element = DOMUtilsContext.selectorAll(element);
1677
+ element = that.selectorAll(element);
568
1678
  }
569
1679
  if (element == null) {
570
- return;
1680
+ return {
1681
+ off() { },
1682
+ trigger() { },
1683
+ };
571
1684
  }
572
- let elementList = [];
1685
+ let $elList = [];
573
1686
  if (element instanceof NodeList || Array.isArray(element)) {
574
- element = element;
575
- elementList = [...element];
1687
+ $elList = $elList.concat(Array.from(element));
576
1688
  }
577
1689
  else {
578
- elementList.push(element);
1690
+ $elList.push(element);
579
1691
  }
580
1692
  // 事件名
581
1693
  let eventTypeList = [];
582
1694
  if (Array.isArray(eventType)) {
583
- eventTypeList = eventTypeList.concat(eventType.filter((eventTypeItem) => typeof eventTypeItem === "string" && eventTypeItem.toString() !== ""));
1695
+ eventTypeList = eventTypeList.concat(eventType.filter((it) => typeof it === "string" && it.toString() !== ""));
584
1696
  }
585
1697
  else if (typeof eventType === "string") {
586
- eventTypeList = eventTypeList.concat(eventType.split(" ").filter((eventTypeItem) => eventTypeItem !== ""));
1698
+ eventTypeList = eventTypeList.concat(eventType.split(" ").filter((it) => it !== ""));
587
1699
  }
588
1700
  // 子元素选择器
589
1701
  let selectorList = [];
590
1702
  if (Array.isArray(selector)) {
591
- selectorList = selectorList.concat(selector.filter((selectorItem) => typeof selectorItem === "string" && selectorItem.toString() !== ""));
1703
+ selectorList = selectorList.concat(selector.filter((it) => typeof it === "string" && it.toString() !== ""));
592
1704
  }
593
1705
  else if (typeof selector === "string") {
594
1706
  selectorList.push(selector);
@@ -617,10 +1729,10 @@
617
1729
  */
618
1730
  function checkOptionOnceToRemoveEventListener() {
619
1731
  if (listenerOption.once) {
620
- DOMUtilsContext.off(element, eventType, selector, callback, option);
1732
+ that.off(element, eventType, selector, callback, option);
621
1733
  }
622
1734
  }
623
- elementList.forEach((elementItem) => {
1735
+ $elList.forEach((elementItem) => {
624
1736
  /**
625
1737
  * 事件回调
626
1738
  * @param event
@@ -633,20 +1745,20 @@
633
1745
  ? event.composedPath()[0]
634
1746
  : event.target;
635
1747
  let totalParent = elementItem;
636
- if (DOMUtilsCommonUtils.isWin(totalParent)) {
637
- if (totalParent === DOMUtilsContext.windowApi.document) {
638
- totalParent = DOMUtilsContext.windowApi.document.documentElement;
1748
+ if (CommonUtils.isWin(totalParent)) {
1749
+ if (totalParent === that.windowApi.document) {
1750
+ totalParent = that.windowApi.document.documentElement;
639
1751
  }
640
1752
  }
641
1753
  const findValue = selectorList.find((selectorItem) => {
642
1754
  // 判断目标元素是否匹配选择器
643
- if (DOMUtilsContext.matches(eventTarget, selectorItem)) {
1755
+ if (that.matches(eventTarget, selectorItem)) {
644
1756
  /* 当前目标可以被selector所匹配到 */
645
1757
  return true;
646
1758
  }
647
1759
  /* 在上层与主元素之间寻找可以被selector所匹配到的 */
648
- const $closestMatches = DOMUtilsContext.closest(eventTarget, selectorItem);
649
- if ($closestMatches && totalParent?.contains($closestMatches)) {
1760
+ const $closestMatches = that.closest(eventTarget, selectorItem);
1761
+ if ($closestMatches && totalParent?.contains?.($closestMatches)) {
650
1762
  eventTarget = $closestMatches;
651
1763
  return true;
652
1764
  }
@@ -678,7 +1790,7 @@
678
1790
  eventTypeList.forEach((eventName) => {
679
1791
  elementItem.addEventListener(eventName, domUtilsEventCallBack, listenerOption);
680
1792
  /* 获取对象上的事件 */
681
- const elementEvents = Reflect.get(elementItem, DOMUtilsData.SymbolEvents) || {};
1793
+ const elementEvents = Reflect.get(elementItem, GlobalData.domEventSymbol) || {};
682
1794
  /* 初始化对象上的xx事件 */
683
1795
  elementEvents[eventName] = elementEvents[eventName] || [];
684
1796
  elementEvents[eventName].push({
@@ -688,9 +1800,26 @@
688
1800
  originCallBack: listenerCallBack,
689
1801
  });
690
1802
  /* 覆盖事件 */
691
- Reflect.set(elementItem, DOMUtilsData.SymbolEvents, elementEvents);
1803
+ Reflect.set(elementItem, GlobalData.domEventSymbol, elementEvents);
692
1804
  });
693
1805
  });
1806
+ return {
1807
+ /**
1808
+ * 取消绑定的监听事件
1809
+ * @param filter (可选)过滤函数,对元素属性上的事件进行过滤出想要删除的事件
1810
+ */
1811
+ off(filter) {
1812
+ that.off($elList, eventTypeList, selectorList, listenerCallBack, listenerOption, filter);
1813
+ },
1814
+ /**
1815
+ * 主动触发事件
1816
+ * @param details 赋予触发的Event的额外属性,如果是Event类型,那么将自动代替默认new的Event对象
1817
+ * @param useDispatchToTriggerEvent 是否使用dispatchEvent来触发事件,默认true,如果为false,则直接调用callback,但是这种会让使用了selectorTarget的没有值
1818
+ */
1819
+ trigger(details, useDispatchToTriggerEvent) {
1820
+ that.trigger($elList, eventTypeList, details, useDispatchToTriggerEvent);
1821
+ },
1822
+ };
694
1823
  }
695
1824
  off(element, eventType, selector, callback, option, filter) {
696
1825
  /**
@@ -709,34 +1838,34 @@
709
1838
  }
710
1839
  return option;
711
1840
  }
712
- const DOMUtilsContext = this;
1841
+ const that = this;
713
1842
  // eslint-disable-next-line prefer-rest-params
714
1843
  const args = arguments;
715
1844
  if (typeof element === "string") {
716
- element = DOMUtilsContext.selectorAll(element);
1845
+ element = that.selectorAll(element);
717
1846
  }
718
1847
  if (element == null) {
719
1848
  return;
720
1849
  }
721
- let elementList = [];
1850
+ let $elList = [];
722
1851
  if (element instanceof NodeList || Array.isArray(element)) {
723
1852
  element = element;
724
- elementList = [...element];
1853
+ $elList = $elList.concat(Array.from(element));
725
1854
  }
726
1855
  else {
727
- elementList.push(element);
1856
+ $elList.push(element);
728
1857
  }
729
1858
  let eventTypeList = [];
730
1859
  if (Array.isArray(eventType)) {
731
- eventTypeList = eventTypeList.concat(eventType.filter((eventTypeItem) => typeof eventTypeItem === "string" && eventTypeItem.toString() !== ""));
1860
+ eventTypeList = eventTypeList.concat(eventType.filter((it) => typeof it === "string" && it.toString() !== ""));
732
1861
  }
733
1862
  else if (typeof eventType === "string") {
734
- eventTypeList = eventTypeList.concat(eventType.split(" ").filter((eventTypeItem) => eventTypeItem !== ""));
1863
+ eventTypeList = eventTypeList.concat(eventType.split(" ").filter((it) => it !== ""));
735
1864
  }
736
1865
  // 子元素选择器
737
1866
  let selectorList = [];
738
1867
  if (Array.isArray(selector)) {
739
- selectorList = selectorList.concat(selector.filter((selectorItem) => typeof selectorItem === "string" && selectorItem.toString() !== ""));
1868
+ selectorList = selectorList.concat(selector.filter((it) => typeof it === "string" && it.toString() !== ""));
740
1869
  }
741
1870
  else if (typeof selector === "string") {
742
1871
  selectorList.push(selector);
@@ -775,9 +1904,9 @@
775
1904
  // 目标函数、事件名、回调函数、事件配置、过滤函数
776
1905
  filter = option;
777
1906
  }
778
- elementList.forEach((elementItem) => {
1907
+ $elList.forEach(($elItem) => {
779
1908
  /* 获取对象上的事件 */
780
- const elementEvents = Reflect.get(elementItem, DOMUtilsData.SymbolEvents) || {};
1909
+ const elementEvents = Reflect.get($elItem, GlobalData.domEventSymbol) || {};
781
1910
  eventTypeList.forEach((eventName) => {
782
1911
  const handlers = elementEvents[eventName] || [];
783
1912
  const filterHandler = typeof filter === "function" ? handlers.filter(filter) : handlers;
@@ -801,7 +1930,7 @@
801
1930
  flag = false;
802
1931
  }
803
1932
  if (flag || isRemoveAll) {
804
- elementItem.removeEventListener(eventName, handler.callback, handler.option);
1933
+ $elItem.removeEventListener(eventName, handler.callback, handler.option);
805
1934
  const findIndex = handlers.findIndex((item) => item === handler);
806
1935
  if (findIndex !== -1) {
807
1936
  handlers.splice(findIndex, 1);
@@ -810,10 +1939,10 @@
810
1939
  }
811
1940
  if (handlers.length === 0) {
812
1941
  /* 如果没有任意的handler,那么删除该属性 */
813
- DOMUtilsCommonUtils.delete(elementEvents, eventType);
1942
+ CommonUtils.delete(elementEvents, eventType);
814
1943
  }
815
1944
  });
816
- Reflect.set(elementItem, DOMUtilsData.SymbolEvents, elementEvents);
1945
+ Reflect.set($elItem, GlobalData.domEventSymbol, elementEvents);
817
1946
  });
818
1947
  }
819
1948
  /**
@@ -822,19 +1951,19 @@
822
1951
  * @param eventType (可选)需要取消监听的事件
823
1952
  */
824
1953
  offAll(element, eventType) {
825
- const DOMUtilsContext = this;
1954
+ const that = this;
826
1955
  if (typeof element === "string") {
827
- element = DOMUtilsContext.selectorAll(element);
1956
+ element = that.selectorAll(element);
828
1957
  }
829
1958
  if (element == null) {
830
1959
  return;
831
1960
  }
832
- let elementList = [];
1961
+ let $elList = [];
833
1962
  if (element instanceof NodeList || Array.isArray(element)) {
834
- elementList = [...element];
1963
+ $elList = $elList.concat(Array.from(element));
835
1964
  }
836
1965
  else {
837
- elementList.push(element);
1966
+ $elList.push(element);
838
1967
  }
839
1968
  let eventTypeList = [];
840
1969
  if (Array.isArray(eventType)) {
@@ -843,12 +1972,13 @@
843
1972
  else if (typeof eventType === "string") {
844
1973
  eventTypeList = eventTypeList.concat(eventType.split(" "));
845
1974
  }
846
- elementList.forEach((elementItem) => {
847
- Object.getOwnPropertySymbols(elementItem).forEach((symbolEvents) => {
848
- if (!symbolEvents.toString().startsWith("Symbol(events_")) {
1975
+ $elList.forEach(($elItem) => {
1976
+ const symbolList = [...new Set([...Object.getOwnPropertySymbols($elItem), GlobalData.domEventSymbol])];
1977
+ symbolList.forEach((symbolItem) => {
1978
+ if (!symbolItem.toString().startsWith("Symbol(events_")) {
849
1979
  return;
850
1980
  }
851
- const elementEvents = elementItem[symbolEvents] || {};
1981
+ const elementEvents = Reflect.get($elItem, symbolItem) || {};
852
1982
  const iterEventNameList = eventTypeList.length ? eventTypeList : Object.keys(elementEvents);
853
1983
  iterEventNameList.forEach((eventName) => {
854
1984
  const handlers = elementEvents[eventName];
@@ -856,12 +1986,12 @@
856
1986
  return;
857
1987
  }
858
1988
  for (const handler of handlers) {
859
- elementItem.removeEventListener(eventName, handler.callback, {
1989
+ $elItem.removeEventListener(eventName, handler.callback, {
860
1990
  capture: handler["option"]["capture"],
861
1991
  });
862
1992
  }
863
- const events = Reflect.get(elementItem, symbolEvents);
864
- DOMUtilsCommonUtils.delete(events, eventName);
1993
+ const events = Reflect.get($elItem, symbolItem);
1994
+ CommonUtils.delete(events, eventName);
865
1995
  });
866
1996
  });
867
1997
  });
@@ -878,15 +2008,15 @@
878
2008
  if (typeof callback !== "function") {
879
2009
  return;
880
2010
  }
881
- const DOMUtilsContext = this;
2011
+ const that = this;
882
2012
  /**
883
2013
  * 检测文档是否加载完毕
884
2014
  */
885
2015
  function checkDOMReadyState() {
886
2016
  try {
887
- if (DOMUtilsContext.windowApi.document.readyState === "complete" ||
888
- (DOMUtilsContext.windowApi.document.readyState !== "loading" &&
889
- !DOMUtilsContext.windowApi.document.documentElement.doScroll)) {
2017
+ if (that.windowApi.document.readyState === "complete" ||
2018
+ (that.windowApi.document.readyState !== "loading" &&
2019
+ !that.windowApi.document.documentElement.doScroll)) {
890
2020
  return true;
891
2021
  }
892
2022
  else {
@@ -906,12 +2036,12 @@
906
2036
  }
907
2037
  const targetList = [
908
2038
  {
909
- target: DOMUtilsContext.windowApi.document,
2039
+ target: that.windowApi.document,
910
2040
  eventType: "DOMContentLoaded",
911
2041
  callback: completed,
912
2042
  },
913
2043
  {
914
- target: DOMUtilsContext.windowApi.window,
2044
+ target: that.windowApi.window,
915
2045
  eventType: "load",
916
2046
  callback: completed,
917
2047
  },
@@ -936,7 +2066,7 @@
936
2066
  }
937
2067
  if (checkDOMReadyState()) {
938
2068
  /* 检查document状态 */
939
- DOMUtilsCommonUtils.setTimeout(callback);
2069
+ CommonUtils.setTimeout(callback, 0);
940
2070
  }
941
2071
  else {
942
2072
  /* 添加监听 */
@@ -948,7 +2078,7 @@
948
2078
  * @param element 需要触发的元素|元素数组|window
949
2079
  * @param eventType 需要触发的事件
950
2080
  * @param details 赋予触发的Event的额外属性,如果是Event类型,那么将自动代替默认new的Event对象
951
- * @param useDispatchToTriggerEvent 是否使用dispatchEvent来触发事件,默认true
2081
+ * @param useDispatchToTriggerEvent 是否使用dispatchEvent来触发事件,默认true,如果为false,则直接调用callback,但是这种会让使用了selectorTarget的没有值
952
2082
  * @example
953
2083
  * // 触发元素a.xx的click事件
954
2084
  * DOMUtils.trigger(document.querySelector("a.xx"),"click")
@@ -958,51 +2088,55 @@
958
2088
  * DOMUtils.trigger("a.xx",["click","tap","hover"])
959
2089
  */
960
2090
  trigger(element, eventType, details, useDispatchToTriggerEvent = true) {
961
- const DOMUtilsContext = this;
2091
+ const that = this;
962
2092
  if (typeof element === "string") {
963
- element = DOMUtilsContext.selectorAll(element);
2093
+ element = that.selectorAll(element);
964
2094
  }
965
2095
  if (element == null) {
966
2096
  return;
967
2097
  }
968
- let elementList = [];
2098
+ let $elList = [];
969
2099
  if (element instanceof NodeList || Array.isArray(element)) {
970
- element = element;
971
- elementList = [...element];
2100
+ $elList = $elList.concat(Array.from(element));
972
2101
  }
973
2102
  else {
974
- elementList = [element];
2103
+ $elList.push(element);
975
2104
  }
976
2105
  let eventTypeList = [];
977
2106
  if (Array.isArray(eventType)) {
978
- eventTypeList = eventType;
2107
+ eventTypeList = eventType.filter((it) => typeof it === "string" && it.trim() !== "");
979
2108
  }
980
2109
  else if (typeof eventType === "string") {
981
2110
  eventTypeList = eventType.split(" ");
982
2111
  }
983
- elementList.forEach((elementItem) => {
2112
+ $elList.forEach(($elItem) => {
984
2113
  /* 获取对象上的事件 */
985
- const events = elementItem[DOMUtilsData.SymbolEvents] || {};
986
- eventTypeList.forEach((_eventType_) => {
2114
+ const elementEvents = Reflect.get($elItem, GlobalData.domEventSymbol) || {};
2115
+ eventTypeList.forEach((eventTypeItem) => {
987
2116
  let event = null;
988
2117
  if (details && details instanceof Event) {
989
2118
  event = details;
990
2119
  }
991
2120
  else {
992
- event = new Event(_eventType_);
2121
+ // 构造事件
2122
+ event = new Event(eventTypeItem);
993
2123
  if (details) {
994
- Object.keys(details).forEach((keyName) => {
995
- event[keyName] = details[keyName];
2124
+ const detailKeys = Object.keys(details);
2125
+ detailKeys.forEach((keyName) => {
2126
+ const value = Reflect.get(details, keyName);
2127
+ // 在event上添加属性
2128
+ Reflect.set(event, keyName, value);
996
2129
  });
997
2130
  }
998
2131
  }
999
- if (useDispatchToTriggerEvent == false && _eventType_ in events) {
1000
- events[_eventType_].forEach((eventsItem) => {
2132
+ if (useDispatchToTriggerEvent == false && eventTypeItem in elementEvents) {
2133
+ // 直接调用监听的事件
2134
+ elementEvents[eventTypeItem].forEach((eventsItem) => {
1001
2135
  eventsItem.callback(event);
1002
2136
  });
1003
2137
  }
1004
2138
  else {
1005
- elementItem.dispatchEvent(event);
2139
+ $elItem.dispatchEvent(event);
1006
2140
  }
1007
2141
  });
1008
2142
  });
@@ -1022,25 +2156,25 @@
1022
2156
  * })
1023
2157
  * */
1024
2158
  click(element, handler, details, useDispatchToTriggerEvent) {
1025
- const DOMUtilsContext = this;
2159
+ const that = this;
1026
2160
  if (typeof element === "string") {
1027
- element = DOMUtilsContext.selectorAll(element);
2161
+ element = that.selectorAll(element);
1028
2162
  }
1029
2163
  if (element == null) {
1030
2164
  return;
1031
2165
  }
1032
- if (DOMUtilsCommonUtils.isNodeList(element)) {
2166
+ if (CommonUtils.isNodeList(element)) {
1033
2167
  // 设置
1034
2168
  element.forEach(($ele) => {
1035
- DOMUtilsContext.click($ele, handler, details, useDispatchToTriggerEvent);
2169
+ that.click($ele, handler, details, useDispatchToTriggerEvent);
1036
2170
  });
1037
2171
  return;
1038
2172
  }
1039
2173
  if (handler == null) {
1040
- DOMUtilsContext.trigger(element, "click", details, useDispatchToTriggerEvent);
2174
+ that.trigger(element, "click", details, useDispatchToTriggerEvent);
1041
2175
  }
1042
2176
  else {
1043
- DOMUtilsContext.on(element, "click", null, handler);
2177
+ that.on(element, "click", null, handler);
1044
2178
  }
1045
2179
  }
1046
2180
  /**
@@ -1058,25 +2192,25 @@
1058
2192
  * })
1059
2193
  * */
1060
2194
  blur(element, handler, details, useDispatchToTriggerEvent) {
1061
- const DOMUtilsContext = this;
2195
+ const that = this;
1062
2196
  if (typeof element === "string") {
1063
- element = DOMUtilsContext.selectorAll(element);
2197
+ element = that.selectorAll(element);
1064
2198
  }
1065
2199
  if (element == null) {
1066
2200
  return;
1067
2201
  }
1068
- if (DOMUtilsCommonUtils.isNodeList(element)) {
2202
+ if (CommonUtils.isNodeList(element)) {
1069
2203
  // 设置
1070
2204
  element.forEach(($ele) => {
1071
- DOMUtilsContext.focus($ele, handler, details, useDispatchToTriggerEvent);
2205
+ that.focus($ele, handler, details, useDispatchToTriggerEvent);
1072
2206
  });
1073
2207
  return;
1074
2208
  }
1075
2209
  if (handler === null) {
1076
- DOMUtilsContext.trigger(element, "blur", details, useDispatchToTriggerEvent);
2210
+ that.trigger(element, "blur", details, useDispatchToTriggerEvent);
1077
2211
  }
1078
2212
  else {
1079
- DOMUtilsContext.on(element, "blur", null, handler);
2213
+ that.on(element, "blur", null, handler);
1080
2214
  }
1081
2215
  }
1082
2216
  /**
@@ -1094,25 +2228,25 @@
1094
2228
  * })
1095
2229
  * */
1096
2230
  focus(element, handler, details, useDispatchToTriggerEvent) {
1097
- const DOMUtilsContext = this;
2231
+ const that = this;
1098
2232
  if (typeof element === "string") {
1099
- element = DOMUtilsContext.selectorAll(element);
2233
+ element = that.selectorAll(element);
1100
2234
  }
1101
2235
  if (element == null) {
1102
2236
  return;
1103
2237
  }
1104
- if (DOMUtilsCommonUtils.isNodeList(element)) {
2238
+ if (CommonUtils.isNodeList(element)) {
1105
2239
  // 设置
1106
2240
  element.forEach(($ele) => {
1107
- DOMUtilsContext.focus($ele, handler, details, useDispatchToTriggerEvent);
2241
+ that.focus($ele, handler, details, useDispatchToTriggerEvent);
1108
2242
  });
1109
2243
  return;
1110
2244
  }
1111
2245
  if (handler == null) {
1112
- DOMUtilsContext.trigger(element, "focus", details, useDispatchToTriggerEvent);
2246
+ that.trigger(element, "focus", details, useDispatchToTriggerEvent);
1113
2247
  }
1114
2248
  else {
1115
- DOMUtilsContext.on(element, "focus", null, handler);
2249
+ that.on(element, "focus", null, handler);
1116
2250
  }
1117
2251
  }
1118
2252
  /**
@@ -1130,22 +2264,22 @@
1130
2264
  * })
1131
2265
  */
1132
2266
  hover(element, handler, option) {
1133
- const DOMUtilsContext = this;
2267
+ const that = this;
1134
2268
  if (typeof element === "string") {
1135
- element = DOMUtilsContext.selectorAll(element);
2269
+ element = that.selectorAll(element);
1136
2270
  }
1137
2271
  if (element == null) {
1138
2272
  return;
1139
2273
  }
1140
- if (DOMUtilsCommonUtils.isNodeList(element)) {
2274
+ if (CommonUtils.isNodeList(element)) {
1141
2275
  // 设置
1142
2276
  element.forEach(($ele) => {
1143
- DOMUtilsContext.hover($ele, handler, option);
2277
+ that.hover($ele, handler, option);
1144
2278
  });
1145
2279
  return;
1146
2280
  }
1147
- DOMUtilsContext.on(element, "mouseenter", null, handler, option);
1148
- DOMUtilsContext.on(element, "mouseleave", null, handler, option);
2281
+ that.on(element, "mouseenter", null, handler, option);
2282
+ that.on(element, "mouseleave", null, handler, option);
1149
2283
  }
1150
2284
  /**
1151
2285
  * 当动画结束时触发事件
@@ -1154,30 +2288,23 @@
1154
2288
  * @param option 配置项,这里默认配置once为true
1155
2289
  */
1156
2290
  animationend(element, handler, option) {
1157
- const DOMUtilsContext = this;
2291
+ const that = this;
1158
2292
  if (typeof element === "string") {
1159
- element = DOMUtilsContext.selector(element);
2293
+ element = that.selector(element);
1160
2294
  }
1161
2295
  if (element == null) {
1162
2296
  return;
1163
2297
  }
1164
- // if (DOMUtilsCommonUtils.isNodeList(element)) {
1165
- // // 设置
1166
- // element.forEach(($ele) => {
1167
- // DOMUtilsContext.animationend($ele as HTMLElement, handler, option);
1168
- // });
1169
- // return;
1170
- // }
1171
2298
  const defaultOption = {
1172
2299
  once: true,
1173
2300
  };
1174
2301
  Object.assign(defaultOption, option || {});
1175
- const eventNameList = DOMUtilsCommonUtils.getAnimationEndNameList();
1176
- DOMUtilsContext.on(element, eventNameList, null, handler, defaultOption);
2302
+ const eventNameList = CommonUtils.getAnimationEndNameList();
2303
+ that.on(element, eventNameList, null, handler, defaultOption);
1177
2304
  if (!defaultOption.once) {
1178
2305
  return {
1179
2306
  off() {
1180
- DOMUtilsContext.off(element, eventNameList, null, handler, defaultOption);
2307
+ that.off(element, eventNameList, null, handler, defaultOption);
1181
2308
  },
1182
2309
  };
1183
2310
  }
@@ -1189,30 +2316,23 @@
1189
2316
  * @param option 配置项,这里默认配置once为true
1190
2317
  */
1191
2318
  transitionend(element, handler, option) {
1192
- const DOMUtilsContext = this;
2319
+ const that = this;
1193
2320
  if (typeof element === "string") {
1194
- element = DOMUtilsContext.selector(element);
2321
+ element = that.selector(element);
1195
2322
  }
1196
2323
  if (element == null) {
1197
2324
  return;
1198
2325
  }
1199
- // if (DOMUtilsCommonUtils.isNodeList(element)) {
1200
- // // 设置
1201
- // element.forEach(($ele) => {
1202
- // DOMUtilsContext.transitionend($ele as HTMLElement, handler, option);
1203
- // });
1204
- // return;
1205
- // }
1206
2326
  const defaultOption = {
1207
2327
  once: true,
1208
2328
  };
1209
2329
  Object.assign(defaultOption, option || {});
1210
- const eventNameList = DOMUtilsCommonUtils.getTransitionEndNameList();
1211
- DOMUtilsContext.on(element, eventNameList, null, handler, defaultOption);
2330
+ const eventNameList = CommonUtils.getTransitionEndNameList();
2331
+ that.on(element, eventNameList, null, handler, defaultOption);
1212
2332
  if (!defaultOption.once) {
1213
2333
  return {
1214
2334
  off() {
1215
- DOMUtilsContext.off(element, eventNameList, null, handler, defaultOption);
2335
+ that.off(element, eventNameList, null, handler, defaultOption);
1216
2336
  },
1217
2337
  };
1218
2338
  }
@@ -1233,21 +2353,21 @@
1233
2353
  * })
1234
2354
  */
1235
2355
  keyup(element, handler, option) {
1236
- const DOMUtilsContext = this;
2356
+ const that = this;
1237
2357
  if (element == null) {
1238
2358
  return;
1239
2359
  }
1240
2360
  if (typeof element === "string") {
1241
- element = DOMUtilsContext.selectorAll(element);
2361
+ element = that.selectorAll(element);
1242
2362
  }
1243
- if (DOMUtilsCommonUtils.isNodeList(element)) {
2363
+ if (CommonUtils.isNodeList(element)) {
1244
2364
  // 设置
1245
2365
  element.forEach(($ele) => {
1246
- DOMUtilsContext.keyup($ele, handler, option);
2366
+ that.keyup($ele, handler, option);
1247
2367
  });
1248
2368
  return;
1249
2369
  }
1250
- DOMUtilsContext.on(element, "keyup", null, handler, option);
2370
+ that.on(element, "keyup", null, handler, option);
1251
2371
  }
1252
2372
  /**
1253
2373
  * 当按键按下时触发事件
@@ -1265,21 +2385,21 @@
1265
2385
  * })
1266
2386
  */
1267
2387
  keydown(element, handler, option) {
1268
- const DOMUtilsContext = this;
2388
+ const that = this;
1269
2389
  if (element == null) {
1270
2390
  return;
1271
2391
  }
1272
2392
  if (typeof element === "string") {
1273
- element = DOMUtilsContext.selectorAll(element);
2393
+ element = that.selectorAll(element);
1274
2394
  }
1275
- if (DOMUtilsCommonUtils.isNodeList(element)) {
2395
+ if (CommonUtils.isNodeList(element)) {
1276
2396
  // 设置
1277
2397
  element.forEach(($ele) => {
1278
- DOMUtilsContext.keydown($ele, handler, option);
2398
+ that.keydown($ele, handler, option);
1279
2399
  });
1280
2400
  return;
1281
2401
  }
1282
- DOMUtilsContext.on(element, "keydown", null, handler, option);
2402
+ that.on(element, "keydown", null, handler, option);
1283
2403
  }
1284
2404
  /**
1285
2405
  * 当按键按下时触发事件
@@ -1297,21 +2417,21 @@
1297
2417
  * })
1298
2418
  */
1299
2419
  keypress(element, handler, option) {
1300
- const DOMUtilsContext = this;
2420
+ const that = this;
1301
2421
  if (element == null) {
1302
2422
  return;
1303
2423
  }
1304
2424
  if (typeof element === "string") {
1305
- element = DOMUtilsContext.selectorAll(element);
2425
+ element = that.selectorAll(element);
1306
2426
  }
1307
- if (DOMUtilsCommonUtils.isNodeList(element)) {
2427
+ if (CommonUtils.isNodeList(element)) {
1308
2428
  // 设置
1309
2429
  element.forEach(($ele) => {
1310
- DOMUtilsContext.keypress($ele, handler, option);
2430
+ that.keypress($ele, handler, option);
1311
2431
  });
1312
2432
  return;
1313
2433
  }
1314
- DOMUtilsContext.on(element, "keypress", null, handler, option);
2434
+ that.on(element, "keypress", null, handler, option);
1315
2435
  }
1316
2436
  /**
1317
2437
  * 监听某个元素键盘按键事件或window全局按键事件
@@ -1377,9 +2497,9 @@
1377
2497
  收藏 171
1378
2498
  **/
1379
2499
  listenKeyboard(element, eventName = "keypress", callback, options) {
1380
- const DOMUtilsContext = this;
2500
+ const that = this;
1381
2501
  if (typeof element === "string") {
1382
- element = DOMUtilsContext.selectorAll(element);
2502
+ element = that.selectorAll(element);
1383
2503
  }
1384
2504
  const keyboardEventCallBack = function (event) {
1385
2505
  /** 键名 */
@@ -1387,233 +2507,146 @@
1387
2507
  /** 键值 */
1388
2508
  const keyValue = event.charCode || event.keyCode || event.which;
1389
2509
  /** 组合键列表 */
1390
- const otherCodeList = [];
1391
- if (event.ctrlKey) {
1392
- otherCodeList.push("ctrl");
1393
- }
1394
- if (event.altKey) {
1395
- otherCodeList.push("alt");
1396
- }
1397
- if (event.metaKey) {
1398
- otherCodeList.push("meta");
1399
- }
1400
- if (event.shiftKey) {
1401
- otherCodeList.push("shift");
1402
- }
1403
- if (typeof callback === "function") {
1404
- callback(keyName, keyValue, otherCodeList, event);
1405
- }
1406
- };
1407
- DOMUtilsContext.on(element, eventName, keyboardEventCallBack, options);
1408
- return {
1409
- removeListen: () => {
1410
- DOMUtilsContext.off(element, eventName, keyboardEventCallBack, options);
1411
- },
1412
- };
1413
- }
1414
- selector(selector, parent) {
1415
- return this.selectorAll(selector, parent)[0];
1416
- }
1417
- selectorAll(selector, parent) {
1418
- const context = this;
1419
- parent = parent || context.windowApi.document;
1420
- selector = selector.trim();
1421
- if (selector.match(/[^\s]{1}:empty$/gi)) {
1422
- // empty 语法
1423
- selector = selector.replace(/:empty$/gi, "");
1424
- return Array.from(parent.querySelectorAll(selector)).filter(($ele) => {
1425
- return $ele?.innerHTML?.trim() === "";
1426
- });
1427
- }
1428
- else if (selector.match(/[^\s]{1}:contains\("(.*)"\)$/i) || selector.match(/[^\s]{1}:contains\('(.*)'\)$/i)) {
1429
- // contains 语法
1430
- const textMatch = selector.match(/:contains\(("|')(.*)("|')\)$/i);
1431
- const text = textMatch[2];
1432
- selector = selector.replace(/:contains\(("|')(.*)("|')\)$/gi, "");
1433
- return Array.from(parent.querySelectorAll(selector)).filter(($ele) => {
1434
- // @ts-ignore
1435
- return ($ele?.textContent || $ele?.innerText)?.includes(text);
1436
- });
1437
- }
1438
- else if (selector.match(/[^\s]{1}:regexp\("(.*)"\)$/i) || selector.match(/[^\s]{1}:regexp\('(.*)'\)$/i)) {
1439
- // regexp 语法
1440
- const textMatch = selector.match(/:regexp\(("|')(.*)("|')\)$/i);
1441
- let pattern = textMatch[2];
1442
- const flagMatch = pattern.match(/("|'),[\s]*("|')([igm]{0,3})$/i);
1443
- let flags = "";
1444
- if (flagMatch) {
1445
- pattern = pattern.replace(/("|'),[\s]*("|')([igm]{0,3})$/gi, "");
1446
- flags = flagMatch[3];
1447
- }
1448
- const regexp = new RegExp(pattern, flags);
1449
- selector = selector.replace(/:regexp\(("|')(.*)("|')\)$/gi, "");
1450
- return Array.from(parent.querySelectorAll(selector)).filter(($ele) => {
1451
- // @ts-ignore
1452
- return Boolean(($ele?.textContent || $ele?.innerText)?.match(regexp));
1453
- });
1454
- }
1455
- else {
1456
- // 普通语法
1457
- return Array.from(parent.querySelectorAll(selector));
1458
- }
1459
- }
1460
- /**
1461
- * 匹配元素,可使用以下的额外语法
1462
- *
1463
- * + :contains([text]) 作用: 找到包含指定文本内容的指定元素
1464
- * + :empty 作用:找到既没有文本内容也没有子元素的指定元素
1465
- * + :regexp([text]) 作用: 找到符合正则表达式的内容的指定元素
1466
- * @param $el 元素
1467
- * @param selector 选择器
1468
- * @example
1469
- * DOMUtils.matches("div:contains('测试')")
1470
- * > true
1471
- * @example
1472
- * DOMUtils.matches("div:empty")
1473
- * > true
1474
- * @example
1475
- * DOMUtils.matches("div:regexp('^xxxx$')")
1476
- * > true
1477
- * @example
1478
- * DOMUtils.matches("div:regexp(/^xxx/ig)")
1479
- * > false
1480
- */
1481
- matches($el, selector) {
1482
- selector = selector.trim();
1483
- if ($el == null) {
1484
- return false;
1485
- }
1486
- if (selector.match(/[^\s]{1}:empty$/gi)) {
1487
- // empty 语法
1488
- selector = selector.replace(/:empty$/gi, "");
1489
- return $el.matches(selector) && $el?.innerHTML?.trim() === "";
1490
- }
1491
- else if (selector.match(/[^\s]{1}:contains\("(.*)"\)$/i) || selector.match(/[^\s]{1}:contains\('(.*)'\)$/i)) {
1492
- // contains 语法
1493
- const textMatch = selector.match(/:contains\(("|')(.*)("|')\)$/i);
1494
- const text = textMatch[2];
1495
- selector = selector.replace(/:contains\(("|')(.*)("|')\)$/gi, "");
1496
- // @ts-ignore
1497
- let content = $el?.textContent || $el?.innerText;
1498
- if (typeof content !== "string") {
1499
- content = "";
1500
- }
1501
- return $el.matches(selector) && content?.includes(text);
1502
- }
1503
- else if (selector.match(/[^\s]{1}:regexp\("(.*)"\)$/i) || selector.match(/[^\s]{1}:regexp\('(.*)'\)$/i)) {
1504
- // regexp 语法
1505
- const textMatch = selector.match(/:regexp\(("|')(.*)("|')\)$/i);
1506
- let pattern = textMatch[2];
1507
- const flagMatch = pattern.match(/("|'),[\s]*("|')([igm]{0,3})$/i);
1508
- let flags = "";
1509
- if (flagMatch) {
1510
- pattern = pattern.replace(/("|'),[\s]*("|')([igm]{0,3})$/gi, "");
1511
- flags = flagMatch[3];
1512
- }
1513
- const regexp = new RegExp(pattern, flags);
1514
- selector = selector.replace(/:regexp\(("|')(.*)("|')\)$/gi, "");
1515
- // @ts-ignore
1516
- let content = $el?.textContent || $el?.innerText;
1517
- if (typeof content !== "string") {
1518
- content = "";
2510
+ const otherCodeList = [];
2511
+ if (event.ctrlKey) {
2512
+ otherCodeList.push("ctrl");
1519
2513
  }
1520
- return $el.matches(selector) && Boolean(content?.match(regexp));
1521
- }
1522
- else {
1523
- // 普通语法
1524
- return $el.matches(selector);
1525
- }
1526
- }
1527
- closest($el, selector) {
1528
- selector = selector.trim();
1529
- if (selector.match(/[^\s]{1}:empty$/gi)) {
1530
- // empty 语法
1531
- selector = selector.replace(/:empty$/gi, "");
1532
- const $closest = $el?.closest(selector);
1533
- if ($closest && $closest?.innerHTML?.trim() === "") {
1534
- return $closest;
2514
+ if (event.altKey) {
2515
+ otherCodeList.push("alt");
1535
2516
  }
1536
- return null;
1537
- }
1538
- else if (selector.match(/[^\s]{1}:contains\("(.*)"\)$/i) || selector.match(/[^\s]{1}:contains\('(.*)'\)$/i)) {
1539
- // contains 语法
1540
- const textMatch = selector.match(/:contains\(("|')(.*)("|')\)$/i);
1541
- const text = textMatch[2];
1542
- selector = selector.replace(/:contains\(("|')(.*)("|')\)$/gi, "");
1543
- const $closest = $el?.closest(selector);
1544
- if ($closest) {
1545
- // @ts-ignore
1546
- const content = $el?.textContent || $el?.innerText;
1547
- if (typeof content === "string" && content.includes(text)) {
1548
- return $closest;
1549
- }
2517
+ if (event.metaKey) {
2518
+ otherCodeList.push("meta");
1550
2519
  }
1551
- return null;
1552
- }
1553
- else if (selector.match(/[^\s]{1}:regexp\("(.*)"\)$/i) || selector.match(/[^\s]{1}:regexp\('(.*)'\)$/i)) {
1554
- // regexp 语法
1555
- const textMatch = selector.match(/:regexp\(("|')(.*)("|')\)$/i);
1556
- let pattern = textMatch[2];
1557
- const flagMatch = pattern.match(/("|'),[\s]*("|')([igm]{0,3})$/i);
1558
- let flags = "";
1559
- if (flagMatch) {
1560
- pattern = pattern.replace(/("|'),[\s]*("|')([igm]{0,3})$/gi, "");
1561
- flags = flagMatch[3];
2520
+ if (event.shiftKey) {
2521
+ otherCodeList.push("shift");
1562
2522
  }
1563
- const regexp = new RegExp(pattern, flags);
1564
- selector = selector.replace(/:regexp\(("|')(.*)("|')\)$/gi, "");
1565
- const $closest = $el?.closest(selector);
1566
- if ($closest) {
1567
- // @ts-ignore
1568
- const content = $el?.textContent || $el?.innerText;
1569
- if (typeof content === "string" && content.match(regexp)) {
1570
- return $closest;
1571
- }
2523
+ if (typeof callback === "function") {
2524
+ callback(keyName, keyValue, otherCodeList, event);
1572
2525
  }
1573
- return null;
2526
+ };
2527
+ that.on(element, eventName, keyboardEventCallBack, options);
2528
+ return {
2529
+ removeListen: () => {
2530
+ that.off(element, eventName, keyboardEventCallBack, options);
2531
+ },
2532
+ };
2533
+ }
2534
+ preventEvent(...args) {
2535
+ /**
2536
+ * 阻止事件的默认行为发生,并阻止事件传播
2537
+ */
2538
+ const stopEvent = (event) => {
2539
+ /* 阻止事件的默认行为发生。例如,当点击一个链接时,浏览器会默认打开链接的URL */
2540
+ event?.preventDefault();
2541
+ /* 停止事件的传播,阻止它继续向更上层的元素冒泡,事件将不会再传播给其他的元素 */
2542
+ event?.stopPropagation();
2543
+ /* 阻止事件传播,并且还能阻止元素上的其他事件处理程序被触发 */
2544
+ event?.stopImmediatePropagation();
2545
+ return false;
2546
+ };
2547
+ if (args.length === 1) {
2548
+ /* 直接阻止事件 */
2549
+ return stopEvent(args[0]);
1574
2550
  }
1575
2551
  else {
1576
- // 普通语法
1577
- const $closest = $el?.closest(selector);
1578
- return $closest;
2552
+ const $el = args[0];
2553
+ let eventNameList = args[1];
2554
+ const capture = args[2];
2555
+ /* 添加对应的事件来阻止触发 */
2556
+ if (typeof eventNameList === "string") {
2557
+ eventNameList = [eventNameList];
2558
+ }
2559
+ this.on($el, eventNameList, stopEvent, { capture: Boolean(capture) });
1579
2560
  }
1580
2561
  }
1581
2562
  }
2563
+ new ElementEvent();
1582
2564
 
1583
- const version = "1.6.8";
2565
+ class ElementHandler extends ElementEvent {
2566
+ windowApi;
2567
+ constructor(windowApiOption) {
2568
+ super(windowApiOption);
2569
+ this.windowApi = new WindowApi(windowApiOption);
2570
+ }
2571
+ /**
2572
+ * 获取元素的选择器字符串
2573
+ * @param $el
2574
+ * @example
2575
+ * DOMUtils.getElementSelector(document.querySelector("a"))
2576
+ * > '.....'
2577
+ */
2578
+ getElementSelector($el) {
2579
+ const that = this;
2580
+ if (!$el)
2581
+ return void 0;
2582
+ if (!$el.parentElement)
2583
+ return void 0;
2584
+ /* 如果元素有id属性,则直接返回id选择器 */
2585
+ if ($el.id)
2586
+ return `#${$el.id}`;
2587
+ /* 递归地获取父元素的选择器 */
2588
+ let selector = that.getElementSelector($el.parentElement);
2589
+ if (!selector) {
2590
+ return $el.tagName.toLowerCase();
2591
+ }
2592
+ /* 如果有多个相同类型的兄弟元素,则需要添加索引 */
2593
+ if ($el.parentElement.querySelectorAll($el.tagName).length > 1) {
2594
+ const index = Array.prototype.indexOf.call($el.parentElement.children, $el) + 1;
2595
+ selector += ` > ${$el.tagName.toLowerCase()}:nth-child(${index})`;
2596
+ }
2597
+ else {
2598
+ selector += ` > ${$el.tagName.toLowerCase()}`;
2599
+ }
2600
+ return selector;
2601
+ }
2602
+ }
2603
+ new ElementHandler();
1584
2604
 
1585
- class DOMUtils extends DOMUtilsEvent {
2605
+ class DOMUtils extends ElementHandler {
1586
2606
  constructor(option) {
1587
2607
  super(option);
1588
2608
  }
1589
2609
  /** 版本号 */
1590
2610
  version = version;
1591
- attr(element, attrName, attrValue) {
1592
- const DOMUtilsContext = this;
1593
- if (typeof element === "string") {
1594
- element = DOMUtilsContext.selectorAll(element);
2611
+ /**
2612
+ * 取消挂载在window下的DOMUtils并返回DOMUtils
2613
+ * @example
2614
+ * let DOMUtils = window.DOMUtils.noConflict()
2615
+ */
2616
+ noConflict() {
2617
+ const that = this;
2618
+ if (that.windowApi.window.DOMUtils) {
2619
+ CommonUtils.delete(window, "DOMUtils");
1595
2620
  }
1596
- if (element == null) {
2621
+ that.windowApi.window.DOMUtils = this;
2622
+ return this;
2623
+ }
2624
+ attr($el, attrName, attrValue) {
2625
+ const that = this;
2626
+ if (typeof $el === "string") {
2627
+ $el = that.selectorAll($el);
2628
+ }
2629
+ if ($el == null) {
1597
2630
  return;
1598
2631
  }
1599
- if (DOMUtilsCommonUtils.isNodeList(element)) {
2632
+ if (CommonUtils.isNodeList($el)) {
1600
2633
  if (attrValue == null) {
1601
2634
  // 获取属性
1602
- return DOMUtilsContext.attr(element[0], attrName, attrValue);
2635
+ return that.attr($el[0], attrName, attrValue);
1603
2636
  }
1604
2637
  else {
1605
2638
  // 设置属性
1606
- element.forEach(($ele) => {
1607
- DOMUtilsContext.attr($ele, attrName, attrValue);
2639
+ $el.forEach(($elItem) => {
2640
+ that.attr($elItem, attrName, attrValue);
1608
2641
  });
1609
2642
  return;
1610
2643
  }
1611
2644
  }
1612
2645
  if (attrValue == null) {
1613
- return element.getAttribute(attrName);
2646
+ return $el.getAttribute(attrName);
1614
2647
  }
1615
2648
  else {
1616
- element.setAttribute(attrName, attrValue);
2649
+ $el.setAttribute(attrName, attrValue);
1617
2650
  }
1618
2651
  }
1619
2652
  createElement(
@@ -1623,11 +2656,11 @@
1623
2656
  property,
1624
2657
  /** 自定义属性 */
1625
2658
  attributes) {
1626
- const DOMUtilsContext = this;
1627
- const tempElement = DOMUtilsContext.windowApi.document.createElement(tagName);
2659
+ const that = this;
2660
+ const $el = that.windowApi.document.createElement(tagName);
1628
2661
  if (typeof property === "string") {
1629
- DOMUtilsContext.html(tempElement, property);
1630
- return tempElement;
2662
+ that.html($el, property);
2663
+ return $el;
1631
2664
  }
1632
2665
  if (property == null) {
1633
2666
  property = {};
@@ -1638,10 +2671,10 @@
1638
2671
  Object.keys(property).forEach((key) => {
1639
2672
  const value = property[key];
1640
2673
  if (key === "innerHTML") {
1641
- DOMUtilsContext.html(tempElement, value);
2674
+ that.html($el, value);
1642
2675
  return;
1643
2676
  }
1644
- tempElement[key] = value;
2677
+ $el[key] = value;
1645
2678
  });
1646
2679
  Object.keys(attributes).forEach((key) => {
1647
2680
  let value = attributes[key];
@@ -1653,12 +2686,12 @@
1653
2686
  /* function转字符串 */
1654
2687
  value = value.toString();
1655
2688
  }
1656
- tempElement.setAttribute(key, value);
2689
+ $el.setAttribute(key, value);
1657
2690
  });
1658
- return tempElement;
2691
+ return $el;
1659
2692
  }
1660
- css(element, property, value) {
1661
- const DOMUtilsContext = this;
2693
+ css($el, property, value) {
2694
+ const that = this;
1662
2695
  /**
1663
2696
  * 把纯数字没有px的加上
1664
2697
  */
@@ -1672,30 +2705,30 @@
1672
2705
  }
1673
2706
  return propertyValue;
1674
2707
  }
1675
- if (typeof element === "string") {
1676
- element = DOMUtilsContext.selectorAll(element);
2708
+ if (typeof $el === "string") {
2709
+ $el = that.selectorAll($el);
1677
2710
  }
1678
- if (element == null) {
2711
+ if ($el == null) {
1679
2712
  return;
1680
2713
  }
1681
- if (DOMUtilsCommonUtils.isNodeList(element)) {
2714
+ if (CommonUtils.isNodeList($el)) {
1682
2715
  if (typeof property === "string") {
1683
2716
  if (value == null) {
1684
2717
  // 获取属性
1685
- return DOMUtilsContext.css(element[0], property);
2718
+ return that.css($el[0], property);
1686
2719
  }
1687
2720
  else {
1688
2721
  // 设置属性
1689
- element.forEach(($ele) => {
1690
- DOMUtilsContext.css($ele, property);
2722
+ $el.forEach(($elItem) => {
2723
+ that.css($elItem, property);
1691
2724
  });
1692
2725
  return;
1693
2726
  }
1694
2727
  }
1695
2728
  else if (typeof property === "object") {
1696
2729
  // 设置属性
1697
- element.forEach(($ele) => {
1698
- DOMUtilsContext.css($ele, property);
2730
+ $el.forEach(($elItem) => {
2731
+ that.css($elItem, property);
1699
2732
  });
1700
2733
  return;
1701
2734
  }
@@ -1707,16 +2740,16 @@
1707
2740
  .trim()
1708
2741
  .replace(/!important$/gi, "")
1709
2742
  .trim();
1710
- element.style.setProperty(propertyName, propertyValue, "important");
2743
+ $el.style.setProperty(propertyName, propertyValue, "important");
1711
2744
  }
1712
2745
  else {
1713
2746
  propertyValue = handlePixe(propertyName, propertyValue);
1714
- element.style.setProperty(propertyName, propertyValue);
2747
+ $el.style.setProperty(propertyName, propertyValue);
1715
2748
  }
1716
2749
  };
1717
2750
  if (typeof property === "string") {
1718
2751
  if (value == null) {
1719
- return DOMUtilsContext.windowApi.globalThis.getComputedStyle(element).getPropertyValue(property);
2752
+ return that.windowApi.globalThis.getComputedStyle($el).getPropertyValue(property);
1720
2753
  }
1721
2754
  else {
1722
2755
  setStyleProperty(property, value);
@@ -1733,92 +2766,92 @@
1733
2766
  throw new TypeError("property must be string or object");
1734
2767
  }
1735
2768
  }
1736
- text(element, text) {
1737
- const DOMUtilsContext = this;
1738
- if (typeof element === "string") {
1739
- element = DOMUtilsContext.selectorAll(element);
2769
+ text($el, text) {
2770
+ const that = this;
2771
+ if (typeof $el === "string") {
2772
+ $el = that.selectorAll($el);
1740
2773
  }
1741
- if (element == null) {
2774
+ if ($el == null) {
1742
2775
  return;
1743
2776
  }
1744
- if (DOMUtilsCommonUtils.isNodeList(element)) {
2777
+ if (CommonUtils.isNodeList($el)) {
1745
2778
  if (text == null) {
1746
2779
  // 获取
1747
- return DOMUtilsContext.text(element[0]);
2780
+ return that.text($el[0]);
1748
2781
  }
1749
2782
  else {
1750
2783
  // 设置
1751
- element.forEach(($ele) => {
1752
- DOMUtilsContext.text($ele, text);
2784
+ $el.forEach(($elItem) => {
2785
+ that.text($elItem, text);
1753
2786
  });
1754
2787
  }
1755
2788
  return;
1756
2789
  }
1757
2790
  if (text == null) {
1758
- return element.textContent || element.innerText;
2791
+ return $el.textContent || $el.innerText;
1759
2792
  }
1760
2793
  else {
1761
2794
  if (text instanceof Node) {
1762
2795
  text = text.textContent || text.innerText;
1763
2796
  }
1764
- if ("textContent" in element) {
1765
- element.textContent = text;
2797
+ if ("textContent" in $el) {
2798
+ $el.textContent = text;
1766
2799
  }
1767
- else if ("innerText" in element) {
1768
- element.innerText = text;
2800
+ else if ("innerText" in $el) {
2801
+ $el.innerText = text;
1769
2802
  }
1770
2803
  }
1771
2804
  }
1772
- html(element, html) {
1773
- const DOMUtilsContext = this;
1774
- if (typeof element === "string") {
1775
- element = DOMUtilsContext.selectorAll(element);
2805
+ html($el, html) {
2806
+ const that = this;
2807
+ if (typeof $el === "string") {
2808
+ $el = that.selectorAll($el);
1776
2809
  }
1777
- if (element == null) {
2810
+ if ($el == null) {
1778
2811
  return;
1779
2812
  }
1780
- if (DOMUtilsCommonUtils.isNodeList(element)) {
2813
+ if (CommonUtils.isNodeList($el)) {
1781
2814
  if (html == null) {
1782
2815
  // 获取
1783
- return DOMUtilsContext.html(element[0]);
2816
+ return that.html($el[0]);
1784
2817
  }
1785
2818
  else {
1786
2819
  // 设置
1787
- element.forEach(($ele) => {
1788
- DOMUtilsContext.html($ele, html);
2820
+ $el.forEach(($elItem) => {
2821
+ that.html($elItem, html);
1789
2822
  });
1790
2823
  }
1791
2824
  return;
1792
2825
  }
1793
2826
  if (html == null) {
1794
2827
  // 获取
1795
- return element.innerHTML;
2828
+ return $el.innerHTML;
1796
2829
  }
1797
2830
  else {
1798
2831
  // 设置
1799
2832
  if (html instanceof Element) {
1800
2833
  html = html.innerHTML;
1801
2834
  }
1802
- if ("innerHTML" in element) {
1803
- DOMUtilsCommonUtils.setSafeHTML(element, html);
2835
+ if ("innerHTML" in $el) {
2836
+ CommonUtils.setSafeHTML($el, html);
1804
2837
  }
1805
2838
  }
1806
2839
  }
1807
2840
  /**
1808
2841
  * 获取移动元素的transform偏移
1809
2842
  */
1810
- getTransform(element, isShow = false) {
1811
- const DOMUtilsContext = this;
2843
+ getTransform($el, isShow = false) {
2844
+ const that = this;
1812
2845
  let transform_left = 0;
1813
2846
  let transform_top = 0;
1814
- if (!(isShow || (!isShow && DOMUtilsCommonUtils.isShow(element)))) {
2847
+ if (!(isShow || (!isShow && CommonUtils.isShow($el)))) {
1815
2848
  /* 未显示 */
1816
- const { recovery } = DOMUtilsCommonUtils.showElement(element);
1817
- const transformInfo = DOMUtilsContext.getTransform(element, true);
2849
+ const { recovery } = CommonUtils.forceShow($el);
2850
+ const transformInfo = that.getTransform($el, true);
1818
2851
  recovery();
1819
2852
  return transformInfo;
1820
2853
  }
1821
- const elementTransform = DOMUtilsContext.windowApi.globalThis.getComputedStyle(element).transform;
2854
+ const elementTransform = that.windowApi.globalThis.getComputedStyle($el).transform;
1822
2855
  if (elementTransform != null && elementTransform !== "none" && elementTransform !== "") {
1823
2856
  const elementTransformSplit = elementTransform.match(/\((.+)\)/)?.[1].split(",");
1824
2857
  if (elementTransformSplit) {
@@ -1835,225 +2868,189 @@
1835
2868
  transformTop: transform_top,
1836
2869
  };
1837
2870
  }
1838
- val(element, value) {
1839
- const DOMUtilsContext = this;
1840
- if (typeof element === "string") {
1841
- element = DOMUtilsContext.selectorAll(element);
2871
+ val($el, value) {
2872
+ const that = this;
2873
+ if (typeof $el === "string") {
2874
+ $el = that.selectorAll($el);
1842
2875
  }
1843
- if (element == null) {
2876
+ if ($el == null) {
1844
2877
  return;
1845
2878
  }
1846
- if (DOMUtilsCommonUtils.isNodeList(element)) {
2879
+ if (CommonUtils.isNodeList($el)) {
1847
2880
  if (value == null) {
1848
2881
  // 获取
1849
- return DOMUtilsContext.val(element[0]);
2882
+ return that.val($el[0]);
1850
2883
  }
1851
2884
  else {
1852
2885
  // 设置
1853
- element.forEach(($ele) => {
1854
- DOMUtilsContext.val($ele, value);
2886
+ $el.forEach(($elItem) => {
2887
+ that.val($elItem, value);
1855
2888
  });
1856
2889
  }
1857
2890
  return;
1858
2891
  }
1859
2892
  if (value == null) {
1860
2893
  // 获取
1861
- if (element.localName === "input" && (element.type === "checkbox" || element.type === "radio")) {
1862
- return element.checked;
2894
+ if ($el.localName === "input" && ($el.type === "checkbox" || $el.type === "radio")) {
2895
+ return $el.checked;
1863
2896
  }
1864
2897
  else {
1865
- return element.value;
2898
+ return $el.value;
1866
2899
  }
1867
2900
  }
1868
2901
  else {
1869
2902
  // 设置
1870
- if (element.localName === "input" && (element.type === "checkbox" || element.type === "radio")) {
1871
- element.checked = !!value;
2903
+ if ($el.localName === "input" && ($el.type === "checkbox" || $el.type === "radio")) {
2904
+ $el.checked = !!value;
1872
2905
  }
1873
2906
  else {
1874
- element.value = value;
2907
+ $el.value = value;
1875
2908
  }
1876
2909
  }
1877
2910
  }
1878
- prop(element, propName, propValue) {
1879
- const DOMUtilsContext = this;
1880
- if (typeof element === "string") {
1881
- element = DOMUtilsContext.selectorAll(element);
2911
+ prop($el, propName, propValue) {
2912
+ const that = this;
2913
+ if (typeof $el === "string") {
2914
+ $el = that.selectorAll($el);
1882
2915
  }
1883
- if (element == null) {
2916
+ if ($el == null) {
1884
2917
  return;
1885
2918
  }
1886
- if (DOMUtilsCommonUtils.isNodeList(element)) {
2919
+ if (CommonUtils.isNodeList($el)) {
1887
2920
  if (propValue == null) {
1888
2921
  // 获取
1889
- return DOMUtilsContext.prop(element[0], propName);
2922
+ return that.prop($el[0], propName);
1890
2923
  }
1891
2924
  else {
1892
2925
  // 设置
1893
- element.forEach(($ele) => {
1894
- DOMUtilsContext.prop($ele, propName, propValue);
2926
+ $el.forEach(($elItem) => {
2927
+ that.prop($elItem, propName, propValue);
1895
2928
  });
1896
2929
  }
1897
2930
  return;
1898
2931
  }
1899
2932
  if (propValue == null) {
1900
- return Reflect.get(element, propName);
2933
+ return Reflect.get($el, propName);
1901
2934
  }
1902
2935
  else {
1903
- if (element instanceof Element && propName === "innerHTML") {
1904
- DOMUtilsContext.html(element, propValue);
2936
+ if ($el instanceof Element && propName === "innerHTML") {
2937
+ that.html($el, propValue);
1905
2938
  }
1906
2939
  else {
1907
- Reflect.set(element, propName, propValue);
2940
+ Reflect.set($el, propName, propValue);
1908
2941
  }
1909
2942
  }
1910
2943
  }
1911
2944
  /**
1912
2945
  * 移除元素的属性
1913
- * @param element 目标元素
2946
+ * @param $el 目标元素
1914
2947
  * @param attrName 属性名
1915
2948
  * @example
1916
2949
  * // 移除元素a.xx的属性data-value
1917
2950
  * DOMUtils.removeAttr(document.querySelector("a.xx"),"data-value")
1918
2951
  * DOMUtils.removeAttr("a.xx","data-value")
1919
2952
  * */
1920
- removeAttr(element, attrName) {
1921
- const DOMUtilsContext = this;
1922
- if (typeof element === "string") {
1923
- element = DOMUtilsContext.selectorAll(element);
2953
+ removeAttr($el, attrName) {
2954
+ const that = this;
2955
+ if (typeof $el === "string") {
2956
+ $el = that.selectorAll($el);
1924
2957
  }
1925
- if (element == null) {
2958
+ if ($el == null) {
1926
2959
  return;
1927
2960
  }
1928
- if (DOMUtilsCommonUtils.isNodeList(element)) {
2961
+ if (CommonUtils.isNodeList($el)) {
1929
2962
  // 设置
1930
- element.forEach(($ele) => {
1931
- DOMUtilsContext.removeAttr($ele, attrName);
2963
+ $el.forEach(($elItem) => {
2964
+ that.removeAttr($elItem, attrName);
1932
2965
  });
1933
2966
  return;
1934
2967
  }
1935
- element.removeAttribute(attrName);
2968
+ $el.removeAttribute(attrName);
1936
2969
  }
1937
2970
  /**
1938
2971
  * 移除元素class名
1939
- * @param element 目标元素
2972
+ * @param $el 目标元素
1940
2973
  * @param className 类名
1941
2974
  * @example
1942
2975
  * // 移除元素a.xx的className为xx
1943
2976
  * DOMUtils.removeClass(document.querySelector("a.xx"),"xx")
1944
2977
  * DOMUtils.removeClass("a.xx","xx")
1945
2978
  */
1946
- removeClass(element, className) {
1947
- const DOMUtilsContext = this;
1948
- if (typeof element === "string") {
1949
- element = DOMUtilsContext.selectorAll(element);
2979
+ removeClass($el, className) {
2980
+ const that = this;
2981
+ if (typeof $el === "string") {
2982
+ $el = that.selectorAll($el);
1950
2983
  }
1951
- if (element == null) {
2984
+ if ($el == null) {
1952
2985
  return;
1953
2986
  }
1954
- if (DOMUtilsCommonUtils.isNodeList(element)) {
2987
+ if (CommonUtils.isNodeList($el)) {
1955
2988
  // 设置
1956
- element.forEach(($ele) => {
1957
- DOMUtilsContext.removeClass($ele, className);
2989
+ $el.forEach(($elItem) => {
2990
+ that.removeClass($elItem, className);
1958
2991
  });
1959
2992
  return;
1960
2993
  }
1961
2994
  if (className == null) {
1962
2995
  // 清空全部className
1963
- element.className = "";
2996
+ $el.className = "";
1964
2997
  }
1965
2998
  else {
1966
2999
  if (!Array.isArray(className)) {
1967
3000
  className = className.trim().split(" ");
1968
3001
  }
1969
3002
  className.forEach((itemClassName) => {
1970
- element.classList.remove(itemClassName);
3003
+ $el.classList.remove(itemClassName);
1971
3004
  });
1972
3005
  }
1973
3006
  }
1974
3007
  /**
1975
3008
  * 移除元素的属性
1976
- * @param element 目标元素
3009
+ * @param $el 目标元素
1977
3010
  * @param propName 属性名
1978
3011
  * @example
1979
3012
  * // 移除元素a.xx的href属性
1980
3013
  * DOMUtils.removeProp(document.querySelector("a.xx"),"href")
1981
3014
  * DOMUtils.removeProp("a.xx","href")
1982
3015
  * */
1983
- removeProp(element, propName) {
1984
- const DOMUtilsContext = this;
1985
- if (typeof element === "string") {
1986
- element = DOMUtilsContext.selectorAll(element);
1987
- }
1988
- if (element == null) {
1989
- return;
1990
- }
1991
- if (DOMUtilsCommonUtils.isNodeList(element)) {
1992
- // 设置
1993
- element.forEach(($ele) => {
1994
- DOMUtilsContext.removeProp($ele, propName);
1995
- });
1996
- return;
1997
- }
1998
- DOMUtilsCommonUtils.delete(element, propName);
1999
- }
2000
- /**
2001
- * 将一个元素替换为另一个元素
2002
- * @param element 目标元素
2003
- * @param newElement 新元素
2004
- * @example
2005
- * // 替换元素a.xx为b.xx
2006
- * DOMUtils.replaceWith(document.querySelector("a.xx"),document.querySelector("b.xx"))
2007
- * DOMUtils.replaceWith("a.xx",'<b class="xx"></b>')
2008
- */
2009
- replaceWith(element, newElement) {
2010
- const DOMUtilsContext = this;
2011
- if (typeof element === "string") {
2012
- element = DOMUtilsContext.selectorAll(element);
3016
+ removeProp($el, propName) {
3017
+ const that = this;
3018
+ if (typeof $el === "string") {
3019
+ $el = that.selectorAll($el);
2013
3020
  }
2014
- if (element == null) {
3021
+ if ($el == null) {
2015
3022
  return;
2016
3023
  }
2017
- if (DOMUtilsCommonUtils.isNodeList(element)) {
3024
+ if (CommonUtils.isNodeList($el)) {
2018
3025
  // 设置
2019
- element.forEach(($ele) => {
2020
- DOMUtilsContext.replaceWith($ele, newElement);
3026
+ $el.forEach(($elItem) => {
3027
+ that.removeProp($elItem, propName);
2021
3028
  });
2022
3029
  return;
2023
3030
  }
2024
- if (typeof newElement === "string") {
2025
- newElement = DOMUtilsContext.parseHTML(newElement, false, false);
2026
- }
2027
- const $parent = element.parentElement;
2028
- if ($parent) {
2029
- $parent.replaceChild(newElement, element);
2030
- }
2031
- else {
2032
- DOMUtilsContext.after(element, newElement);
2033
- element.remove();
2034
- }
3031
+ CommonUtils.delete($el, propName);
2035
3032
  }
2036
3033
  /**
2037
3034
  * 给元素添加class
2038
- * @param element 目标元素
3035
+ * @param $el 目标元素
2039
3036
  * @param className class名
2040
3037
  * @example
2041
3038
  * // 元素a.xx的className添加_vue_
2042
3039
  * DOMUtils.addClass(document.querySelector("a.xx"),"_vue_")
2043
3040
  * DOMUtils.addClass("a.xx","_vue_")
2044
3041
  * */
2045
- addClass(element, className) {
2046
- const DOMUtilsContext = this;
2047
- if (typeof element === "string") {
2048
- element = DOMUtilsContext.selectorAll(element);
3042
+ addClass($el, className) {
3043
+ const that = this;
3044
+ if (typeof $el === "string") {
3045
+ $el = that.selectorAll($el);
2049
3046
  }
2050
- if (element == null) {
3047
+ if ($el == null) {
2051
3048
  return;
2052
3049
  }
2053
- if (DOMUtilsCommonUtils.isNodeList(element)) {
3050
+ if (CommonUtils.isNodeList($el)) {
2054
3051
  // 设置
2055
- element.forEach(($ele) => {
2056
- DOMUtilsContext.addClass($ele, className);
3052
+ $el.forEach(($elItem) => {
3053
+ that.addClass($elItem, className);
2057
3054
  });
2058
3055
  return;
2059
3056
  }
@@ -2064,31 +3061,31 @@
2064
3061
  if (itemClassName.trim() == "") {
2065
3062
  return;
2066
3063
  }
2067
- element.classList.add(itemClassName);
3064
+ $el.classList.add(itemClassName);
2068
3065
  });
2069
3066
  }
2070
3067
  /**
2071
3068
  * 判断元素是否存在className
2072
- * @param element
3069
+ * @param $el
2073
3070
  * @param className
2074
3071
  */
2075
- hasClass(element, className) {
2076
- const DOMUtilsContext = this;
2077
- if (typeof element === "string") {
2078
- element = DOMUtilsContext.selectorAll(element);
3072
+ hasClass($el, className) {
3073
+ const that = this;
3074
+ if (typeof $el === "string") {
3075
+ $el = that.selectorAll($el);
2079
3076
  }
2080
- if (element == null) {
3077
+ if ($el == null) {
2081
3078
  return false;
2082
3079
  }
2083
- if (DOMUtilsCommonUtils.isNodeList(element)) {
3080
+ if (CommonUtils.isNodeList($el)) {
2084
3081
  let flag = true;
2085
- for (let index = 0; index < element.length; index++) {
2086
- const $ele = element[index];
2087
- flag = flag && DOMUtilsContext.hasClass($ele, className);
3082
+ for (let index = 0; index < $el.length; index++) {
3083
+ const $elItem = $el[index];
3084
+ flag = flag && that.hasClass($elItem, className);
2088
3085
  }
2089
3086
  return flag;
2090
3087
  }
2091
- if (!element?.classList) {
3088
+ if (!$el?.classList) {
2092
3089
  return false;
2093
3090
  }
2094
3091
  if (!Array.isArray(className)) {
@@ -2096,7 +3093,7 @@
2096
3093
  }
2097
3094
  for (let index = 0; index < className.length; index++) {
2098
3095
  const item = className[index].trim();
2099
- if (!element.classList.contains(item)) {
3096
+ if (!$el.classList.contains(item)) {
2100
3097
  return false;
2101
3098
  }
2102
3099
  }
@@ -2104,25 +3101,25 @@
2104
3101
  }
2105
3102
  /**
2106
3103
  * 函数在元素内部末尾添加子元素或HTML字符串
2107
- * @param element 目标元素
3104
+ * @param $el 目标元素
2108
3105
  * @param content 子元素或HTML字符串
2109
3106
  * @example
2110
3107
  * // 元素a.xx的内部末尾添加一个元素
2111
3108
  * DOMUtils.append(document.querySelector("a.xx"),document.querySelector("b.xx"))
2112
3109
  * DOMUtils.append("a.xx","'<b class="xx"></b>")
2113
3110
  * */
2114
- append(element, content) {
2115
- const DOMUtilsContext = this;
2116
- if (typeof element === "string") {
2117
- element = DOMUtilsContext.selectorAll(element);
3111
+ append($el, content) {
3112
+ const that = this;
3113
+ if (typeof $el === "string") {
3114
+ $el = that.selectorAll($el);
2118
3115
  }
2119
- if (element == null) {
3116
+ if ($el == null) {
2120
3117
  return;
2121
3118
  }
2122
- if (DOMUtilsCommonUtils.isNodeList(element)) {
3119
+ if (CommonUtils.isNodeList($el)) {
2123
3120
  // 设置
2124
- element.forEach(($ele) => {
2125
- DOMUtilsContext.append($ele, content);
3121
+ $el.forEach(($elItem) => {
3122
+ that.append($elItem, content);
2126
3123
  });
2127
3124
  return;
2128
3125
  }
@@ -2130,12 +3127,12 @@
2130
3127
  if (typeof content === "string") {
2131
3128
  if (ele instanceof DocumentFragment) {
2132
3129
  if (typeof text === "string") {
2133
- text = DOMUtilsContext.parseHTML(text, true, false);
3130
+ text = that.toElement(text, true, false);
2134
3131
  }
2135
3132
  ele.appendChild(text);
2136
3133
  }
2137
3134
  else {
2138
- ele.insertAdjacentHTML("beforeend", DOMUtilsCommonUtils.getSafeHTML(text));
3135
+ ele.insertAdjacentHTML("beforeend", CommonUtils.createSafeHTML(text));
2139
3136
  }
2140
3137
  }
2141
3138
  else {
@@ -2144,96 +3141,96 @@
2144
3141
  }
2145
3142
  if (Array.isArray(content) || content instanceof NodeList) {
2146
3143
  /* 数组 */
2147
- const fragment = DOMUtilsContext.windowApi.document.createDocumentFragment();
3144
+ const fragment = that.windowApi.document.createDocumentFragment();
2148
3145
  content.forEach((ele) => {
2149
3146
  if (typeof ele === "string") {
2150
3147
  // 转为元素
2151
- ele = DOMUtilsContext.parseHTML(ele, true, false);
3148
+ ele = that.toElement(ele, true, false);
2152
3149
  }
2153
3150
  fragment.appendChild(ele);
2154
3151
  });
2155
- element.appendChild(fragment);
3152
+ $el.appendChild(fragment);
2156
3153
  }
2157
3154
  else {
2158
- elementAppendChild(element, content);
3155
+ elementAppendChild($el, content);
2159
3156
  }
2160
3157
  }
2161
3158
  /**
2162
3159
  * 函数 在元素内部开头添加子元素或HTML字符串
2163
- * @param element 目标元素
3160
+ * @param $el 目标元素
2164
3161
  * @param content 子元素或HTML字符串
2165
3162
  * @example
2166
3163
  * // 元素a.xx内部开头添加一个元素
2167
3164
  * DOMUtils.prepend(document.querySelector("a.xx"),document.querySelector("b.xx"))
2168
3165
  * DOMUtils.prepend("a.xx","'<b class="xx"></b>")
2169
3166
  * */
2170
- prepend(element, content) {
2171
- const DOMUtilsContext = this;
2172
- if (typeof element === "string") {
2173
- element = DOMUtilsContext.selectorAll(element);
3167
+ prepend($el, content) {
3168
+ const that = this;
3169
+ if (typeof $el === "string") {
3170
+ $el = that.selectorAll($el);
2174
3171
  }
2175
- if (element == null) {
3172
+ if ($el == null) {
2176
3173
  return;
2177
3174
  }
2178
- if (DOMUtilsCommonUtils.isNodeList(element)) {
3175
+ if (CommonUtils.isNodeList($el)) {
2179
3176
  // 设置
2180
- element.forEach(($ele) => {
2181
- DOMUtilsContext.prepend($ele, content);
3177
+ $el.forEach(($elItem) => {
3178
+ that.prepend($elItem, content);
2182
3179
  });
2183
3180
  return;
2184
3181
  }
2185
3182
  if (typeof content === "string") {
2186
- if (element instanceof DocumentFragment) {
2187
- content = DOMUtilsContext.parseHTML(content, true, false);
2188
- element.prepend(content);
3183
+ if ($el instanceof DocumentFragment) {
3184
+ content = that.toElement(content, true, false);
3185
+ $el.prepend(content);
2189
3186
  }
2190
3187
  else {
2191
- element.insertAdjacentHTML("afterbegin", DOMUtilsCommonUtils.getSafeHTML(content));
3188
+ $el.insertAdjacentHTML("afterbegin", CommonUtils.createSafeHTML(content));
2192
3189
  }
2193
3190
  }
2194
3191
  else {
2195
- const $firstChild = element.firstChild;
3192
+ const $firstChild = $el.firstChild;
2196
3193
  if ($firstChild == null) {
2197
- element.prepend(content);
3194
+ $el.prepend(content);
2198
3195
  }
2199
3196
  else {
2200
- element.insertBefore(content, $firstChild);
3197
+ $el.insertBefore(content, $firstChild);
2201
3198
  }
2202
3199
  }
2203
3200
  }
2204
3201
  /**
2205
3202
  * 在元素后面添加兄弟元素或HTML字符串
2206
- * @param element 目标元素
3203
+ * @param $el 目标元素
2207
3204
  * @param content 兄弟元素或HTML字符串
2208
3205
  * @example
2209
3206
  * // 元素a.xx后面添加一个元素
2210
3207
  * DOMUtils.after(document.querySelector("a.xx"),document.querySelector("b.xx"))
2211
3208
  * DOMUtils.after("a.xx","'<b class="xx"></b>")
2212
3209
  * */
2213
- after(element, content) {
2214
- const DOMUtilsContext = this;
2215
- if (typeof element === "string") {
2216
- element = DOMUtilsContext.selectorAll(element);
3210
+ after($el, content) {
3211
+ const that = this;
3212
+ if (typeof $el === "string") {
3213
+ $el = that.selectorAll($el);
2217
3214
  }
2218
- if (element == null) {
3215
+ if ($el == null) {
2219
3216
  return;
2220
3217
  }
2221
- if (DOMUtilsCommonUtils.isNodeList(element)) {
3218
+ if (CommonUtils.isNodeList($el)) {
2222
3219
  // 设置
2223
- element.forEach(($ele) => {
2224
- DOMUtilsContext.after($ele, content);
3220
+ $el.forEach(($elItem) => {
3221
+ that.after($elItem, content);
2225
3222
  });
2226
3223
  return;
2227
3224
  }
2228
3225
  if (typeof content === "string") {
2229
- element.insertAdjacentHTML("afterend", DOMUtilsCommonUtils.getSafeHTML(content));
3226
+ $el.insertAdjacentHTML("afterend", CommonUtils.createSafeHTML(content));
2230
3227
  }
2231
3228
  else {
2232
- const $parent = element.parentElement;
2233
- const $nextSlibling = element.nextSibling;
3229
+ const $parent = $el.parentElement;
3230
+ const $nextSlibling = $el.nextSibling;
2234
3231
  if (!$parent || $nextSlibling) {
2235
3232
  // 任意一个不行
2236
- element.after(content);
3233
+ $el.after(content);
2237
3234
  }
2238
3235
  else {
2239
3236
  $parent.insertBefore(content, $nextSlibling);
@@ -2242,157 +3239,157 @@
2242
3239
  }
2243
3240
  /**
2244
3241
  * 在元素前面添加兄弟元素或HTML字符串
2245
- * @param element 目标元素
3242
+ * @param $el 目标元素
2246
3243
  * @param content 兄弟元素或HTML字符串
2247
3244
  * @example
2248
3245
  * // 元素a.xx前面添加一个元素
2249
3246
  * DOMUtils.before(document.querySelector("a.xx"),document.querySelector("b.xx"))
2250
3247
  * DOMUtils.before("a.xx","'<b class="xx"></b>")
2251
3248
  * */
2252
- before(element, content) {
2253
- const DOMUtilsContext = this;
2254
- if (typeof element === "string") {
2255
- element = DOMUtilsContext.selectorAll(element);
3249
+ before($el, content) {
3250
+ const that = this;
3251
+ if (typeof $el === "string") {
3252
+ $el = that.selectorAll($el);
2256
3253
  }
2257
- if (element == null) {
3254
+ if ($el == null) {
2258
3255
  return;
2259
3256
  }
2260
- if (DOMUtilsCommonUtils.isNodeList(element)) {
3257
+ if (CommonUtils.isNodeList($el)) {
2261
3258
  // 设置
2262
- element.forEach(($ele) => {
2263
- DOMUtilsContext.before($ele, content);
3259
+ $el.forEach(($elItem) => {
3260
+ that.before($elItem, content);
2264
3261
  });
2265
3262
  return;
2266
3263
  }
2267
3264
  if (typeof content === "string") {
2268
- element.insertAdjacentHTML("beforebegin", DOMUtilsCommonUtils.getSafeHTML(content));
3265
+ $el.insertAdjacentHTML("beforebegin", CommonUtils.createSafeHTML(content));
2269
3266
  }
2270
3267
  else {
2271
- const $parent = element.parentElement;
3268
+ const $parent = $el.parentElement;
2272
3269
  if (!$parent) {
2273
- element.before(content);
3270
+ $el.before(content);
2274
3271
  }
2275
3272
  else {
2276
- $parent.insertBefore(content, element);
3273
+ $parent.insertBefore(content, $el);
2277
3274
  }
2278
3275
  }
2279
3276
  }
2280
3277
  /**
2281
3278
  * 移除元素
2282
- * @param element 目标元素
3279
+ * @param $el 目标元素
2283
3280
  * @example
2284
3281
  * // 元素a.xx前面添加一个元素
2285
3282
  * DOMUtils.remove(document.querySelector("a.xx"))
2286
3283
  * DOMUtils.remove(document.querySelectorAll("a.xx"))
2287
3284
  * DOMUtils.remove("a.xx")
2288
3285
  * */
2289
- remove(element) {
2290
- const DOMUtilsContext = this;
2291
- if (typeof element === "string") {
2292
- element = DOMUtilsContext.selectorAll(element);
3286
+ remove($el) {
3287
+ const that = this;
3288
+ if (typeof $el === "string") {
3289
+ $el = that.selectorAll($el);
2293
3290
  }
2294
- if (element == null) {
3291
+ if ($el == null) {
2295
3292
  return;
2296
3293
  }
2297
- if (DOMUtilsCommonUtils.isNodeList(element)) {
2298
- element.forEach(($ele) => {
2299
- DOMUtilsContext.remove($ele);
3294
+ if (CommonUtils.isNodeList($el)) {
3295
+ $el.forEach(($elItem) => {
3296
+ that.remove($elItem);
2300
3297
  });
2301
3298
  return;
2302
3299
  }
2303
- if (typeof element.remove === "function") {
2304
- element.remove();
3300
+ if (typeof $el.remove === "function") {
3301
+ $el.remove();
2305
3302
  }
2306
- else if (element.parentElement) {
2307
- element.parentElement.removeChild(element);
3303
+ else if ($el.parentElement) {
3304
+ $el.parentElement.removeChild($el);
2308
3305
  }
2309
- else if (element.parentNode) {
2310
- element.parentNode.removeChild(element);
3306
+ else if ($el.parentNode) {
3307
+ $el.parentNode.removeChild($el);
2311
3308
  }
2312
3309
  }
2313
3310
  /**
2314
3311
  * 移除元素的所有子元素
2315
- * @param element 目标元素
3312
+ * @param $el 目标元素
2316
3313
  * @example
2317
3314
  * // 移除元素a.xx元素的所有子元素
2318
3315
  * DOMUtils.empty(document.querySelector("a.xx"))
2319
3316
  * DOMUtils.empty("a.xx")
2320
3317
  * */
2321
- empty(element) {
2322
- const DOMUtilsContext = this;
2323
- if (typeof element === "string") {
2324
- element = DOMUtilsContext.selectorAll(element);
3318
+ empty($el) {
3319
+ const that = this;
3320
+ if (typeof $el === "string") {
3321
+ $el = that.selectorAll($el);
2325
3322
  }
2326
- if (element == null) {
3323
+ if ($el == null) {
2327
3324
  return;
2328
3325
  }
2329
- if (DOMUtilsCommonUtils.isNodeList(element)) {
3326
+ if (CommonUtils.isNodeList($el)) {
2330
3327
  // 设置
2331
- element.forEach(($ele) => {
2332
- DOMUtilsContext.empty($ele);
3328
+ $el.forEach(($elItem) => {
3329
+ that.empty($elItem);
2333
3330
  });
2334
3331
  return;
2335
3332
  }
2336
- if (element.innerHTML) {
2337
- element.innerHTML = "";
3333
+ if ($el.innerHTML) {
3334
+ $el.innerHTML = "";
2338
3335
  }
2339
- else if (element.textContent) {
2340
- element.textContent = "";
3336
+ else if ($el.textContent) {
3337
+ $el.textContent = "";
2341
3338
  }
2342
3339
  }
2343
3340
  /**
2344
3341
  * 获取元素相对于文档的偏移坐标(加上文档的滚动条)
2345
- * @param element 目标元素
3342
+ * @param $el 目标元素
2346
3343
  * @example
2347
3344
  * // 获取元素a.xx的对于文档的偏移坐标
2348
3345
  * DOMUtils.offset(document.querySelector("a.xx"))
2349
3346
  * DOMUtils.offset("a.xx")
2350
3347
  * > 0
2351
3348
  */
2352
- offset(element) {
2353
- const DOMUtilsContext = this;
2354
- if (typeof element === "string") {
2355
- element = DOMUtilsContext.selector(element);
3349
+ offset($el) {
3350
+ const that = this;
3351
+ if (typeof $el === "string") {
3352
+ $el = that.selector($el);
2356
3353
  }
2357
- if (element == null) {
3354
+ if ($el == null) {
2358
3355
  return;
2359
3356
  }
2360
- const rect = element.getBoundingClientRect();
3357
+ const rect = $el.getBoundingClientRect();
2361
3358
  return {
2362
3359
  /** y轴偏移 */
2363
- top: rect.top + DOMUtilsContext.windowApi.globalThis.scrollY,
3360
+ top: rect.top + that.windowApi.globalThis.scrollY,
2364
3361
  /** x轴偏移 */
2365
- left: rect.left + DOMUtilsContext.windowApi.globalThis.scrollX,
3362
+ left: rect.left + that.windowApi.globalThis.scrollX,
2366
3363
  };
2367
3364
  }
2368
- width(element, isShow = false) {
2369
- const DOMUtilsContext = this;
2370
- if (typeof element === "string") {
2371
- element = DOMUtilsContext.selector(element);
3365
+ width($el, isShow = false) {
3366
+ const that = this;
3367
+ if (typeof $el === "string") {
3368
+ $el = that.selector($el);
2372
3369
  }
2373
- if (DOMUtilsCommonUtils.isWin(element)) {
2374
- return DOMUtilsContext.windowApi.window.document.documentElement.clientWidth;
3370
+ if (CommonUtils.isWin($el)) {
3371
+ return that.windowApi.window.document.documentElement.clientWidth;
2375
3372
  }
2376
- if (element.nodeType === 9) {
3373
+ if ($el.nodeType === 9) {
2377
3374
  /* Document文档节点 */
2378
- element = element;
2379
- return Math.max(element.body.scrollWidth, element.documentElement.scrollWidth, element.body.offsetWidth, element.documentElement.offsetWidth, element.documentElement.clientWidth);
3375
+ $el = $el;
3376
+ return Math.max($el.body.scrollWidth, $el.documentElement.scrollWidth, $el.body.offsetWidth, $el.documentElement.offsetWidth, $el.documentElement.clientWidth);
2380
3377
  }
2381
- if (isShow || (!isShow && DOMUtilsCommonUtils.isShow(element))) {
3378
+ if (isShow || (!isShow && CommonUtils.isShow($el))) {
2382
3379
  /* 已显示 */
2383
3380
  /* 不从style中获取对应的宽度,因为可能使用了class定义了width !important */
2384
- element = element;
3381
+ $el = $el;
2385
3382
  /* 如果element.style.width为空 则从css里面获取是否定义了width信息如果定义了 则读取css里面定义的宽度width */
2386
- if (parseFloat(DOMUtilsCommonUtils.getStyleValue(element, "width").toString()) > 0) {
2387
- return parseFloat(DOMUtilsCommonUtils.getStyleValue(element, "width").toString());
3383
+ if (parseFloat(CommonUtils.getStyleValue($el, "width").toString()) > 0) {
3384
+ return parseFloat(CommonUtils.getStyleValue($el, "width").toString());
2388
3385
  }
2389
3386
  /* 如果从css里获取到的值不是大于0 可能是auto 则通过offsetWidth来进行计算 */
2390
- if (element.offsetWidth > 0) {
2391
- const borderLeftWidth = DOMUtilsCommonUtils.getStyleValue(element, "borderLeftWidth");
2392
- const borderRightWidth = DOMUtilsCommonUtils.getStyleValue(element, "borderRightWidth");
2393
- const paddingLeft = DOMUtilsCommonUtils.getStyleValue(element, "paddingLeft");
2394
- const paddingRight = DOMUtilsCommonUtils.getStyleValue(element, "paddingRight");
2395
- const backHeight = parseFloat(element.offsetWidth.toString()) -
3387
+ if ($el.offsetWidth > 0) {
3388
+ const borderLeftWidth = CommonUtils.getStyleValue($el, "borderLeftWidth");
3389
+ const borderRightWidth = CommonUtils.getStyleValue($el, "borderRightWidth");
3390
+ const paddingLeft = CommonUtils.getStyleValue($el, "paddingLeft");
3391
+ const paddingRight = CommonUtils.getStyleValue($el, "paddingRight");
3392
+ const backHeight = parseFloat($el.offsetWidth.toString()) -
2396
3393
  parseFloat(borderLeftWidth.toString()) -
2397
3394
  parseFloat(borderRightWidth.toString()) -
2398
3395
  parseFloat(paddingLeft.toString()) -
@@ -2403,41 +3400,41 @@
2403
3400
  }
2404
3401
  else {
2405
3402
  /* 未显示 */
2406
- element = element;
2407
- const { recovery } = DOMUtilsCommonUtils.showElement(element);
2408
- const width = DOMUtilsContext.width(element, true);
3403
+ $el = $el;
3404
+ const { recovery } = CommonUtils.forceShow($el);
3405
+ const width = that.width($el, true);
2409
3406
  recovery();
2410
3407
  return width;
2411
3408
  }
2412
3409
  }
2413
- height(element, isShow = false) {
2414
- const DOMUtilsContext = this;
2415
- if (DOMUtilsCommonUtils.isWin(element)) {
2416
- return DOMUtilsContext.windowApi.window.document.documentElement.clientHeight;
3410
+ height($el, isShow = false) {
3411
+ const that = this;
3412
+ if (CommonUtils.isWin($el)) {
3413
+ return that.windowApi.window.document.documentElement.clientHeight;
2417
3414
  }
2418
- if (typeof element === "string") {
2419
- element = DOMUtilsContext.selector(element);
3415
+ if (typeof $el === "string") {
3416
+ $el = that.selector($el);
2420
3417
  }
2421
- if (element.nodeType === 9) {
2422
- element = element;
3418
+ if ($el.nodeType === 9) {
3419
+ $el = $el;
2423
3420
  /* Document文档节点 */
2424
- return Math.max(element.body.scrollHeight, element.documentElement.scrollHeight, element.body.offsetHeight, element.documentElement.offsetHeight, element.documentElement.clientHeight);
3421
+ return Math.max($el.body.scrollHeight, $el.documentElement.scrollHeight, $el.body.offsetHeight, $el.documentElement.offsetHeight, $el.documentElement.clientHeight);
2425
3422
  }
2426
- if (isShow || (!isShow && DOMUtilsCommonUtils.isShow(element))) {
2427
- element = element;
3423
+ if (isShow || (!isShow && CommonUtils.isShow($el))) {
3424
+ $el = $el;
2428
3425
  /* 已显示 */
2429
3426
  /* 从style中获取对应的高度,因为可能使用了class定义了width !important */
2430
3427
  /* 如果element.style.height为空 则从css里面获取是否定义了height信息如果定义了 则读取css里面定义的高度height */
2431
- if (parseFloat(DOMUtilsCommonUtils.getStyleValue(element, "height").toString()) > 0) {
2432
- return parseFloat(DOMUtilsCommonUtils.getStyleValue(element, "height").toString());
3428
+ if (parseFloat(CommonUtils.getStyleValue($el, "height").toString()) > 0) {
3429
+ return parseFloat(CommonUtils.getStyleValue($el, "height").toString());
2433
3430
  }
2434
3431
  /* 如果从css里获取到的值不是大于0 可能是auto 则通过offsetHeight来进行计算 */
2435
- if (element.offsetHeight > 0) {
2436
- const borderTopWidth = DOMUtilsCommonUtils.getStyleValue(element, "borderTopWidth");
2437
- const borderBottomWidth = DOMUtilsCommonUtils.getStyleValue(element, "borderBottomWidth");
2438
- const paddingTop = DOMUtilsCommonUtils.getStyleValue(element, "paddingTop");
2439
- const paddingBottom = DOMUtilsCommonUtils.getStyleValue(element, "paddingBottom");
2440
- const backHeight = parseFloat(element.offsetHeight.toString()) -
3432
+ if ($el.offsetHeight > 0) {
3433
+ const borderTopWidth = CommonUtils.getStyleValue($el, "borderTopWidth");
3434
+ const borderBottomWidth = CommonUtils.getStyleValue($el, "borderBottomWidth");
3435
+ const paddingTop = CommonUtils.getStyleValue($el, "paddingTop");
3436
+ const paddingBottom = CommonUtils.getStyleValue($el, "paddingBottom");
3437
+ const backHeight = parseFloat($el.offsetHeight.toString()) -
2441
3438
  parseFloat(borderTopWidth.toString()) -
2442
3439
  parseFloat(borderBottomWidth.toString()) -
2443
3440
  parseFloat(paddingTop.toString()) -
@@ -2448,200 +3445,160 @@
2448
3445
  }
2449
3446
  else {
2450
3447
  /* 未显示 */
2451
- element = element;
2452
- const { recovery } = DOMUtilsCommonUtils.showElement(element);
2453
- const height = DOMUtilsContext.height(element, true);
3448
+ $el = $el;
3449
+ const { recovery } = CommonUtils.forceShow($el);
3450
+ const height = that.height($el, true);
2454
3451
  recovery();
2455
3452
  return height;
2456
3453
  }
2457
3454
  }
2458
- outerWidth(element, isShow = false) {
2459
- const DOMUtilsContext = this;
2460
- if (DOMUtilsCommonUtils.isWin(element)) {
2461
- return DOMUtilsContext.windowApi.window.innerWidth;
3455
+ outerWidth($el, isShow = false) {
3456
+ const that = this;
3457
+ if (CommonUtils.isWin($el)) {
3458
+ return that.windowApi.window.innerWidth;
2462
3459
  }
2463
- if (typeof element === "string") {
2464
- element = DOMUtilsContext.selector(element);
3460
+ if (typeof $el === "string") {
3461
+ $el = that.selector($el);
2465
3462
  }
2466
- element = element;
2467
- if (isShow || (!isShow && DOMUtilsCommonUtils.isShow(element))) {
2468
- const style = DOMUtilsContext.windowApi.globalThis.getComputedStyle(element, null);
2469
- const marginLeft = DOMUtilsCommonUtils.getStyleValue(style, "marginLeft");
2470
- const marginRight = DOMUtilsCommonUtils.getStyleValue(style, "marginRight");
2471
- return element.offsetWidth + marginLeft + marginRight;
3463
+ $el = $el;
3464
+ if (isShow || (!isShow && CommonUtils.isShow($el))) {
3465
+ const style = that.windowApi.globalThis.getComputedStyle($el, null);
3466
+ const marginLeft = CommonUtils.getStyleValue(style, "marginLeft");
3467
+ const marginRight = CommonUtils.getStyleValue(style, "marginRight");
3468
+ return $el.offsetWidth + marginLeft + marginRight;
2472
3469
  }
2473
3470
  else {
2474
- const { recovery } = DOMUtilsCommonUtils.showElement(element);
2475
- const outerWidth = DOMUtilsContext.outerWidth(element, true);
3471
+ const { recovery } = CommonUtils.forceShow($el);
3472
+ const outerWidth = that.outerWidth($el, true);
2476
3473
  recovery();
2477
3474
  return outerWidth;
2478
3475
  }
2479
3476
  }
2480
- outerHeight(element, isShow = false) {
2481
- const DOMUtilsContext = this;
2482
- if (DOMUtilsCommonUtils.isWin(element)) {
2483
- return DOMUtilsContext.windowApi.window.innerHeight;
3477
+ outerHeight($el, isShow = false) {
3478
+ const that = this;
3479
+ if (CommonUtils.isWin($el)) {
3480
+ return that.windowApi.window.innerHeight;
2484
3481
  }
2485
- if (typeof element === "string") {
2486
- element = DOMUtilsContext.selector(element);
3482
+ if (typeof $el === "string") {
3483
+ $el = that.selector($el);
2487
3484
  }
2488
- element = element;
2489
- if (isShow || (!isShow && DOMUtilsCommonUtils.isShow(element))) {
2490
- const style = DOMUtilsContext.windowApi.globalThis.getComputedStyle(element, null);
2491
- const marginTop = DOMUtilsCommonUtils.getStyleValue(style, "marginTop");
2492
- const marginBottom = DOMUtilsCommonUtils.getStyleValue(style, "marginBottom");
2493
- return element.offsetHeight + marginTop + marginBottom;
3485
+ $el = $el;
3486
+ if (isShow || (!isShow && CommonUtils.isShow($el))) {
3487
+ const style = that.windowApi.globalThis.getComputedStyle($el, null);
3488
+ const marginTop = CommonUtils.getStyleValue(style, "marginTop");
3489
+ const marginBottom = CommonUtils.getStyleValue(style, "marginBottom");
3490
+ return $el.offsetHeight + marginTop + marginBottom;
2494
3491
  }
2495
3492
  else {
2496
- const { recovery } = DOMUtilsCommonUtils.showElement(element);
2497
- const outerHeight = DOMUtilsContext.outerHeight(element, true);
3493
+ const { recovery } = CommonUtils.forceShow($el);
3494
+ const outerHeight = that.outerHeight($el, true);
2498
3495
  recovery();
2499
3496
  return outerHeight;
2500
3497
  }
2501
3498
  }
2502
3499
  /**
2503
- * 在一定时间内改变元素的样式属性,实现动画效果
2504
- * @param element 需要进行动画的元素
2505
- * @param styles 动画结束时元素的样式属性
2506
- * @param duration 动画持续时间,单位为毫秒
2507
- * @param callback 动画结束后执行的函数
3500
+ * 将一个元素替换为另一个元素
3501
+ * @param $el 目标元素
3502
+ * @param $newEl 新元素
2508
3503
  * @example
2509
- * // 监听元素a.xx的从显示变为隐藏
2510
- * DOMUtils.animate(document.querySelector("a.xx"),{ top:100},1000,function(){
2511
- * console.log("已往上位移100px")
2512
- * })
3504
+ * // 替换元素a.xx为b.xx
3505
+ * DOMUtils.replaceWith(document.querySelector("a.xx"),document.querySelector("b.xx"))
3506
+ * DOMUtils.replaceWith("a.xx",'<b class="xx"></b>')
2513
3507
  */
2514
- animate(element, styles, duration = 1000, callback = null) {
2515
- const DOMUtilsContext = this;
2516
- if (typeof element === "string") {
2517
- element = DOMUtilsContext.selectorAll(element);
3508
+ replaceWith($el, $newEl) {
3509
+ const that = this;
3510
+ if (typeof $el === "string") {
3511
+ $el = that.selectorAll($el);
2518
3512
  }
2519
- if (element == null) {
3513
+ if ($el == null) {
2520
3514
  return;
2521
3515
  }
2522
- if (DOMUtilsCommonUtils.isNodeList(element)) {
3516
+ if (CommonUtils.isNodeList($el)) {
2523
3517
  // 设置
2524
- element.forEach(($ele) => {
2525
- DOMUtilsContext.animate($ele, styles, duration, callback);
3518
+ $el.forEach(($elItem) => {
3519
+ that.replaceWith($elItem, $newEl);
2526
3520
  });
2527
3521
  return;
2528
3522
  }
2529
- if (typeof duration !== "number" || duration <= 0) {
2530
- throw new TypeError("duration must be a positive number");
2531
- }
2532
- if (typeof callback !== "function" && callback !== void 0) {
2533
- throw new TypeError("callback must be a function or null");
2534
- }
2535
- if (typeof styles !== "object" || styles === void 0) {
2536
- throw new TypeError("styles must be an object");
3523
+ if (typeof $newEl === "string") {
3524
+ $newEl = that.toElement($newEl, false, false);
2537
3525
  }
2538
- if (Object.keys(styles).length === 0) {
2539
- throw new Error("styles must contain at least one property");
3526
+ const $parent = $el.parentElement;
3527
+ if ($parent) {
3528
+ $parent.replaceChild($newEl, $el);
2540
3529
  }
2541
- const start = performance.now();
2542
- const from = {};
2543
- const to = {};
2544
- for (const prop in styles) {
2545
- from[prop] = element.style[prop] || DOMUtilsContext.windowApi.globalThis.getComputedStyle(element)[prop];
2546
- to[prop] = styles[prop];
3530
+ else {
3531
+ that.after($el, $newEl);
3532
+ $el.remove();
2547
3533
  }
2548
- const timer = DOMUtilsCommonUtils.setInterval(function () {
2549
- const timePassed = performance.now() - start;
2550
- let progress = timePassed / duration;
2551
- if (progress > 1) {
2552
- progress = 1;
2553
- }
2554
- for (const prop in styles) {
2555
- element.style[prop] = from[prop] + (to[prop] - from[prop]) * progress + "px";
2556
- }
2557
- if (progress === 1) {
2558
- DOMUtilsCommonUtils.clearInterval(timer);
2559
- if (callback) {
2560
- callback();
2561
- }
2562
- }
2563
- }, 10);
2564
3534
  }
2565
3535
  /**
2566
3536
  * 将一个元素包裹在指定的HTML元素中
2567
- * @param element 要包裹的元素
3537
+ * @param $el 要包裹的元素
2568
3538
  * @param wrapperHTML 要包裹的HTML元素的字符串表示形式
2569
3539
  * @example
2570
3540
  * // 将a.xx元素外面包裹一层div
2571
3541
  * DOMUtils.wrap(document.querySelector("a.xx"),"<div></div>")
2572
3542
  */
2573
- wrap(element, wrapperHTML) {
2574
- const DOMUtilsContext = this;
2575
- if (typeof element === "string") {
2576
- element = DOMUtilsContext.selectorAll(element);
3543
+ wrap($el, wrapperHTML) {
3544
+ const that = this;
3545
+ if (typeof $el === "string") {
3546
+ $el = that.selectorAll($el);
2577
3547
  }
2578
- if (element == null) {
3548
+ if ($el == null) {
2579
3549
  return;
2580
3550
  }
2581
- if (DOMUtilsCommonUtils.isNodeList(element)) {
3551
+ if (CommonUtils.isNodeList($el)) {
2582
3552
  // 设置
2583
- element.forEach(($ele) => {
2584
- DOMUtilsContext.wrap($ele, wrapperHTML);
3553
+ $el.forEach(($elItem) => {
3554
+ that.wrap($elItem, wrapperHTML);
2585
3555
  });
2586
3556
  return;
2587
3557
  }
2588
- element = element;
3558
+ $el = $el;
2589
3559
  // 创建一个新的div元素,并将wrapperHTML作为其innerHTML
2590
- const wrapper = DOMUtilsContext.windowApi.document.createElement("div");
2591
- DOMUtilsContext.html(wrapper, wrapperHTML);
2592
- const wrapperFirstChild = wrapper.firstChild;
3560
+ const $wrapper = that.windowApi.document.createElement("div");
3561
+ that.html($wrapper, wrapperHTML);
3562
+ const wrapperFirstChild = $wrapper.firstChild;
2593
3563
  // 将要包裹的元素插入目标元素前面
2594
- const parentElement = element.parentElement;
2595
- parentElement.insertBefore(wrapperFirstChild, element);
3564
+ const parentElement = $el.parentElement;
3565
+ parentElement.insertBefore(wrapperFirstChild, $el);
2596
3566
  // 将要包裹的元素移动到wrapper中
2597
- wrapperFirstChild.appendChild(element);
3567
+ wrapperFirstChild.appendChild($el);
2598
3568
  }
2599
- prev(element) {
2600
- const DOMUtilsContext = this;
2601
- if (typeof element === "string") {
2602
- element = DOMUtilsContext.selector(element);
3569
+ prev($el) {
3570
+ const that = this;
3571
+ if (typeof $el === "string") {
3572
+ $el = that.selector($el);
2603
3573
  }
2604
- if (element == null) {
3574
+ if ($el == null) {
2605
3575
  return;
2606
3576
  }
2607
- return element.previousElementSibling;
3577
+ return $el.previousElementSibling;
2608
3578
  }
2609
- next(element) {
2610
- const DOMUtilsContext = this;
2611
- if (typeof element === "string") {
2612
- element = DOMUtilsContext.selector(element);
3579
+ next($el) {
3580
+ const that = this;
3581
+ if (typeof $el === "string") {
3582
+ $el = that.selector($el);
2613
3583
  }
2614
- if (element == null) {
3584
+ if ($el == null) {
2615
3585
  return;
2616
3586
  }
2617
- return element.nextElementSibling;
2618
- }
2619
- /**
2620
- * 取消挂载在window下的DOMUtils并返回DOMUtils
2621
- * @example
2622
- * let DOMUtils = window.DOMUtils.noConflict()
2623
- */
2624
- noConflict() {
2625
- const DOMUtilsContext = this;
2626
- if (DOMUtilsContext.windowApi.window.DOMUtils) {
2627
- DOMUtilsCommonUtils.delete(window, "DOMUtils");
2628
- }
2629
- DOMUtilsContext.windowApi.window.DOMUtils = this;
2630
- return this;
3587
+ return $el.nextElementSibling;
2631
3588
  }
2632
- siblings(element) {
2633
- const DOMUtilsContext = this;
2634
- if (typeof element === "string") {
2635
- element = DOMUtilsContext.selector(element);
3589
+ siblings($el) {
3590
+ const that = this;
3591
+ if (typeof $el === "string") {
3592
+ $el = that.selector($el);
2636
3593
  }
2637
- if (element == null) {
3594
+ if ($el == null) {
2638
3595
  return;
2639
3596
  }
2640
- return Array.from(element.parentElement.children).filter((child) => child !== element);
3597
+ return Array.from($el.parentElement.children).filter(($child) => $child !== $el);
2641
3598
  }
2642
3599
  /**
2643
3600
  * 获取当前元素的父元素
2644
- * @param element 当前元素
3601
+ * @param $el 当前元素
2645
3602
  * @returns 父元素
2646
3603
  * @example
2647
3604
  * // 获取a.xx元素的父元素
@@ -2649,27 +3606,27 @@
2649
3606
  * DOMUtils.parent("a.xx")
2650
3607
  * > <div ...>....</div>
2651
3608
  */
2652
- parent(element) {
2653
- const DOMUtilsContext = this;
2654
- if (typeof element === "string") {
2655
- element = DOMUtilsContext.selector(element);
3609
+ parent($el) {
3610
+ const that = this;
3611
+ if (typeof $el === "string") {
3612
+ $el = that.selector($el);
2656
3613
  }
2657
- if (element == null) {
3614
+ if ($el == null) {
2658
3615
  return;
2659
3616
  }
2660
- if (DOMUtilsCommonUtils.isNodeList(element)) {
3617
+ if (CommonUtils.isNodeList($el)) {
2661
3618
  const resultArray = [];
2662
- element.forEach(($ele) => {
2663
- resultArray.push(DOMUtilsContext.parent($ele));
3619
+ $el.forEach(($elItem) => {
3620
+ resultArray.push(that.parent($elItem));
2664
3621
  });
2665
3622
  return resultArray;
2666
3623
  }
2667
3624
  else {
2668
- return element.parentElement;
3625
+ return $el.parentElement;
2669
3626
  }
2670
3627
  }
2671
- parseHTML(html, useParser = false, isComplete = false) {
2672
- const DOMUtilsContext = this;
3628
+ toElement(html, useParser = false, isComplete = false) {
3629
+ const that = this;
2673
3630
  // 去除html前后的空格
2674
3631
  html = html.trim();
2675
3632
  function parseHTMLByDOMParser() {
@@ -2682,13 +3639,13 @@
2682
3639
  }
2683
3640
  }
2684
3641
  function parseHTMLByCreateDom() {
2685
- const tempDIV = DOMUtilsContext.windowApi.document.createElement("div");
2686
- DOMUtilsContext.html(tempDIV, html);
3642
+ const $el = that.windowApi.document.createElement("div");
3643
+ that.html($el, html);
2687
3644
  if (isComplete) {
2688
- return tempDIV;
3645
+ return $el;
2689
3646
  }
2690
3647
  else {
2691
- return tempDIV.firstChild;
3648
+ return $el.firstElementChild ?? $el.firstChild;
2692
3649
  }
2693
3650
  }
2694
3651
  if (useParser) {
@@ -2733,213 +3690,6 @@
2733
3690
  .map((item) => `${encodeURIComponent(item.name)}=${encodeURIComponent(item.value)}`)
2734
3691
  .join("&");
2735
3692
  }
2736
- /**
2737
- * 显示元素
2738
- * @param target 当前元素
2739
- * @param checkVisiblie 是否检测元素是否显示
2740
- * + true (默认)如果检测到还未显示,则强制使用display: unset !important;
2741
- * + false 不检测,直接设置display属性为空
2742
- * @example
2743
- * // 显示a.xx元素
2744
- * DOMUtils.show(document.querySelector("a.xx"))
2745
- * DOMUtils.show(document.querySelectorAll("a.xx"))
2746
- * DOMUtils.show("a.xx")
2747
- */
2748
- show(target, checkVisiblie = true) {
2749
- const DOMUtilsContext = this;
2750
- if (target == null) {
2751
- return;
2752
- }
2753
- if (typeof target === "string") {
2754
- target = DOMUtilsContext.selectorAll(target);
2755
- }
2756
- if (target instanceof NodeList || target instanceof Array) {
2757
- target = target;
2758
- for (const element of target) {
2759
- DOMUtilsContext.show(element, checkVisiblie);
2760
- }
2761
- }
2762
- else {
2763
- target = target;
2764
- target.style.display = "";
2765
- if (checkVisiblie) {
2766
- if (!DOMUtilsCommonUtils.isShow(target)) {
2767
- /* 仍然是不显示,尝试使用强覆盖 */
2768
- target.style.setProperty("display", "unset", "important");
2769
- }
2770
- }
2771
- }
2772
- }
2773
- /**
2774
- * 隐藏元素
2775
- * @param target 当前元素
2776
- * @param checkVisiblie 是否检测元素是否显示
2777
- * + true (默认)如果检测到显示,则强制使用display: none !important;
2778
- * + false 不检测,直接设置display属性为none
2779
- * @example
2780
- * // 隐藏a.xx元素
2781
- * DOMUtils.hide(document.querySelector("a.xx"))
2782
- * DOMUtils.hide(document.querySelectorAll("a.xx"))
2783
- * DOMUtils.hide("a.xx")
2784
- */
2785
- hide(target, checkVisiblie = true) {
2786
- const DOMUtilsContext = this;
2787
- if (target == null) {
2788
- return;
2789
- }
2790
- if (typeof target === "string") {
2791
- target = DOMUtilsContext.selectorAll(target);
2792
- }
2793
- if (target instanceof NodeList || target instanceof Array) {
2794
- target = target;
2795
- for (const element of target) {
2796
- DOMUtilsContext.hide(element, checkVisiblie);
2797
- }
2798
- }
2799
- else {
2800
- target = target;
2801
- target.style.display = "none";
2802
- if (checkVisiblie) {
2803
- if (DOMUtilsCommonUtils.isShow(target)) {
2804
- /* 仍然是显示,尝试使用强覆盖 */
2805
- target.style.setProperty("display", "none", "important");
2806
- }
2807
- }
2808
- }
2809
- }
2810
- /**
2811
- * 淡入元素
2812
- * @param element 当前元素
2813
- * @param duration 动画持续时间(毫秒),默认400毫秒
2814
- * @param callback 动画结束的回调
2815
- * @example
2816
- * // 元素a.xx淡入
2817
- * DOMUtils.fadeIn(document.querySelector("a.xx"),2500,()=>{
2818
- * console.log("淡入完毕");
2819
- * })
2820
- * DOMUtils.fadeIn("a.xx",undefined,()=>{
2821
- * console.log("淡入完毕");
2822
- * })
2823
- */
2824
- fadeIn(element, duration = 400, callback) {
2825
- if (element == null) {
2826
- return;
2827
- }
2828
- const DOMUtilsContext = this;
2829
- if (typeof element === "string") {
2830
- element = DOMUtilsContext.selectorAll(element);
2831
- }
2832
- if (DOMUtilsCommonUtils.isNodeList(element)) {
2833
- // 设置
2834
- element.forEach(($ele) => {
2835
- DOMUtilsContext.fadeIn($ele, duration, callback);
2836
- });
2837
- return;
2838
- }
2839
- element.style.opacity = "0";
2840
- element.style.display = "";
2841
- let start = null;
2842
- let timer = null;
2843
- function step(timestamp) {
2844
- if (!start)
2845
- start = timestamp;
2846
- const progress = timestamp - start;
2847
- element = element;
2848
- element.style.opacity = Math.min(progress / duration, 1).toString();
2849
- if (progress < duration) {
2850
- DOMUtilsContext.windowApi.window.requestAnimationFrame(step);
2851
- }
2852
- else {
2853
- if (callback && typeof callback === "function") {
2854
- callback();
2855
- }
2856
- DOMUtilsContext.windowApi.window.cancelAnimationFrame(timer);
2857
- }
2858
- }
2859
- timer = DOMUtilsContext.windowApi.window.requestAnimationFrame(step);
2860
- }
2861
- /**
2862
- * 淡出元素
2863
- * @param element 当前元素
2864
- * @param duration 动画持续时间(毫秒),默认400毫秒
2865
- * @param callback 动画结束的回调
2866
- * @example
2867
- * // 元素a.xx淡出
2868
- * DOMUtils.fadeOut(document.querySelector("a.xx"),2500,()=>{
2869
- * console.log("淡出完毕");
2870
- * })
2871
- * DOMUtils.fadeOut("a.xx",undefined,()=>{
2872
- * console.log("淡出完毕");
2873
- * })
2874
- */
2875
- fadeOut(element, duration = 400, callback) {
2876
- const DOMUtilsContext = this;
2877
- if (element == null) {
2878
- return;
2879
- }
2880
- if (typeof element === "string") {
2881
- element = DOMUtilsContext.selectorAll(element);
2882
- }
2883
- if (DOMUtilsCommonUtils.isNodeList(element)) {
2884
- // 设置
2885
- element.forEach(($ele) => {
2886
- DOMUtilsContext.fadeOut($ele, duration, callback);
2887
- });
2888
- return;
2889
- }
2890
- element.style.opacity = "1";
2891
- let start = null;
2892
- let timer = null;
2893
- function step(timestamp) {
2894
- if (!start)
2895
- start = timestamp;
2896
- const progress = timestamp - start;
2897
- element = element;
2898
- element.style.opacity = Math.max(1 - progress / duration, 0).toString();
2899
- if (progress < duration) {
2900
- DOMUtilsContext.windowApi.window.requestAnimationFrame(step);
2901
- }
2902
- else {
2903
- element.style.display = "none";
2904
- if (typeof callback === "function") {
2905
- callback();
2906
- }
2907
- DOMUtilsContext.windowApi.window.cancelAnimationFrame(timer);
2908
- }
2909
- }
2910
- timer = DOMUtilsContext.windowApi.window.requestAnimationFrame(step);
2911
- }
2912
- /**
2913
- * 切换元素的显示和隐藏状态
2914
- * @param element 当前元素
2915
- * @param checkVisiblie 是否检测元素是否显示
2916
- * @example
2917
- * // 如果元素a.xx当前是隐藏,则显示,如果是显示,则隐藏
2918
- * DOMUtils.toggle(document.querySelector("a.xx"))
2919
- * DOMUtils.toggle("a.xx")
2920
- */
2921
- toggle(element, checkVisiblie) {
2922
- const DOMUtilsContext = this;
2923
- if (typeof element === "string") {
2924
- element = DOMUtilsContext.selectorAll(element);
2925
- }
2926
- if (element == null) {
2927
- return;
2928
- }
2929
- if (DOMUtilsCommonUtils.isNodeList(element)) {
2930
- // 设置
2931
- element.forEach(($ele) => {
2932
- DOMUtilsContext.toggle($ele);
2933
- });
2934
- return;
2935
- }
2936
- if (DOMUtilsContext.windowApi.globalThis.getComputedStyle(element).getPropertyValue("display") === "none") {
2937
- DOMUtilsContext.show(element, checkVisiblie);
2938
- }
2939
- else {
2940
- DOMUtilsContext.hide(element, checkVisiblie);
2941
- }
2942
- }
2943
3693
  /**
2944
3694
  * 创建一个新的DOMUtils实例
2945
3695
  * @param option
@@ -2957,7 +3707,7 @@
2957
3707
  * DOMUtils.getTextBoundingRect(document.querySelector("input"));
2958
3708
  */
2959
3709
  getTextBoundingRect($input, selectionStart, selectionEnd) {
2960
- const DOMUtilsContext = this;
3710
+ const that = this;
2961
3711
  // Basic parameter validation
2962
3712
  if (!$input || !("value" in $input))
2963
3713
  return $input;
@@ -3026,7 +3776,7 @@
3026
3776
  }
3027
3777
  // End of CSS variable checks
3028
3778
  // 不能为空,不然获取不到高度
3029
- const text = $input.value || "G", textLen = text.length, fakeClone = DOMUtilsContext.windowApi.document.createElement("div");
3779
+ const text = $input.value || "G", textLen = text.length, fakeClone = that.windowApi.document.createElement("div");
3030
3780
  if (selectionStart > 0)
3031
3781
  appendPart(0, selectionStart);
3032
3782
  const fakeRange = appendPart(selectionStart, selectionEnd);
@@ -3040,7 +3790,7 @@
3040
3790
  fakeClone.style.left = leftPos + "px";
3041
3791
  fakeClone.style.width = width + "px";
3042
3792
  fakeClone.style.height = height + "px";
3043
- DOMUtilsContext.windowApi.document.body.appendChild(fakeClone);
3793
+ that.windowApi.document.body.appendChild(fakeClone);
3044
3794
  const returnValue = fakeRange.getBoundingClientRect(); //Get rect
3045
3795
  fakeClone?.parentNode?.removeChild(fakeClone); //Remove temp
3046
3796
  return returnValue;
@@ -3052,7 +3802,7 @@
3052
3802
  * @returns
3053
3803
  */
3054
3804
  function appendPart(start, end) {
3055
- const span = DOMUtilsContext.windowApi.document.createElement("span");
3805
+ const span = that.windowApi.document.createElement("span");
3056
3806
  span.style.cssText = cssDefaultStyles; //Force styles to prevent unexpected results
3057
3807
  span.textContent = text.substring(start, end);
3058
3808
  fakeClone.appendChild(span);
@@ -3060,7 +3810,7 @@
3060
3810
  }
3061
3811
  // Computing offset position
3062
3812
  function getInputOffset() {
3063
- const body = DOMUtilsContext.windowApi.document.body, win = DOMUtilsContext.windowApi.document.defaultView, docElem = DOMUtilsContext.windowApi.document.documentElement, $box = DOMUtilsContext.windowApi.document.createElement("div");
3813
+ const body = that.windowApi.document.body, win = that.windowApi.document.defaultView, docElem = that.windowApi.document.documentElement, $box = that.windowApi.document.createElement("div");
3064
3814
  $box.style.paddingLeft = $box.style.width = "1px";
3065
3815
  body.appendChild($box);
3066
3816
  const isBoxModel = $box.offsetWidth == 2;
@@ -3079,7 +3829,7 @@
3079
3829
  * @returns
3080
3830
  */
3081
3831
  function getInputCSS(prop, isNumber) {
3082
- const val = DOMUtilsContext.windowApi.document.defaultView.getComputedStyle($input, null).getPropertyValue(prop);
3832
+ const val = that.windowApi.document.defaultView.getComputedStyle($input, null).getPropertyValue(prop);
3083
3833
  if (isNumber) {
3084
3834
  return parseFloat(val);
3085
3835
  }
@@ -3088,13 +3838,163 @@
3088
3838
  }
3089
3839
  }
3090
3840
  }
3091
- /** 获取 animationend 在各个浏览器的兼容名 */
3092
- getAnimationEndNameList() {
3093
- return DOMUtilsCommonUtils.getAnimationEndNameList();
3841
+ addStyle(cssText) {
3842
+ if (typeof cssText !== "string") {
3843
+ throw new Error("DOMUtils.addStyle 参数cssText 必须为String类型");
3844
+ }
3845
+ const $css = this.createElement("style", {
3846
+ type: "text/css",
3847
+ innerHTML: cssText,
3848
+ });
3849
+ if (this.windowApi.document.head) {
3850
+ /* 插入head最后 */
3851
+ this.windowApi.document.head.appendChild($css);
3852
+ }
3853
+ else if (this.windowApi.document.documentElement.childNodes.length === 0) {
3854
+ /* 插入#html后 */
3855
+ this.windowApi.document.documentElement.appendChild($css);
3856
+ }
3857
+ else {
3858
+ /* 插入#html第一个元素前 */
3859
+ this.windowApi.document.documentElement.insertBefore($css, this.windowApi.document.documentElement.childNodes[0]);
3860
+ }
3861
+ return $css;
3094
3862
  }
3095
- /** 获取 transitionend 在各个浏览器的兼容名 */
3096
- getTransitionEndNameList() {
3097
- return DOMUtilsCommonUtils.getTransitionEndNameList();
3863
+ /**
3864
+ * 检测点击的地方是否在该元素区域内
3865
+ * @param $el 需要检测的元素
3866
+ * @returns
3867
+ * + true 点击在元素上
3868
+ * + false 未点击在元素上
3869
+ * @example
3870
+ * DOMUtils.checkUserClickInNode(document.querySelector(".xxx"));
3871
+ * > false
3872
+ **/
3873
+ checkUserClickInNode($el) {
3874
+ const that = this;
3875
+ if (!CommonUtils.isDOM($el)) {
3876
+ throw new Error("Utils.checkUserClickInNode 参数 targetNode 必须为 Element|Node 类型");
3877
+ }
3878
+ const clickEvent = that.windowApi.window.event;
3879
+ const touchEvent = that.windowApi.window.event;
3880
+ const $click = clickEvent?.composedPath()?.[0];
3881
+ // 点击的x坐标
3882
+ const clickPosX = clickEvent?.clientX != null ? clickEvent.clientX : touchEvent.touches[0].clientX;
3883
+ // 点击的y坐标
3884
+ const clickPosY = clickEvent?.clientY != null ? clickEvent.clientY : touchEvent.touches[0].clientY;
3885
+ const {
3886
+ /* 要检测的元素的相对屏幕的横坐标最左边 */
3887
+ left: elementPosXLeft,
3888
+ /* 要检测的元素的相对屏幕的横坐标最右边 */
3889
+ right: elementPosXRight,
3890
+ /* 要检测的元素的相对屏幕的纵坐标最上边 */
3891
+ top: elementPosYTop,
3892
+ /* 要检测的元素的相对屏幕的纵坐标最下边 */
3893
+ bottom: elementPosYBottom, } = $el.getBoundingClientRect();
3894
+ if (clickPosX >= elementPosXLeft &&
3895
+ clickPosX <= elementPosXRight &&
3896
+ clickPosY >= elementPosYTop &&
3897
+ clickPosY <= elementPosYBottom) {
3898
+ return true;
3899
+ }
3900
+ else if (($click && $el.contains($click)) || $click == $el) {
3901
+ /* 这种情况是应对在界面中隐藏的元素,getBoundingClientRect获取的都是0 */
3902
+ return true;
3903
+ }
3904
+ else {
3905
+ return false;
3906
+ }
3907
+ }
3908
+ deleteParentNode($el, parentSelector) {
3909
+ if ($el == null) {
3910
+ return;
3911
+ }
3912
+ if (!CommonUtils.isDOM($el)) {
3913
+ throw new Error("DOMUtils.deleteParentNode 参数 target 必须为 Node|HTMLElement 类型");
3914
+ }
3915
+ if (typeof parentSelector !== "string") {
3916
+ throw new Error("DOMUtils.deleteParentNode 参数 targetSelector 必须为 string 类型");
3917
+ }
3918
+ let result = false;
3919
+ const $parent = domUtils.closest($el, parentSelector);
3920
+ if ($parent) {
3921
+ this.remove($parent);
3922
+ result = true;
3923
+ }
3924
+ return result;
3925
+ }
3926
+ *findElementsWithText($el, text, filter) {
3927
+ const that = this;
3928
+ if ($el.outerHTML.includes(text)) {
3929
+ if ($el.children.length === 0) {
3930
+ const filterResult = typeof filter === "function" ? filter($el) : false;
3931
+ if (!filterResult) {
3932
+ yield $el;
3933
+ }
3934
+ }
3935
+ else {
3936
+ const $text = Array.from($el.childNodes).filter(($child) => $child.nodeType === Node.TEXT_NODE);
3937
+ for (const $child of $text) {
3938
+ if ($child.textContent.includes(text)) {
3939
+ const filterResult = typeof filter === "function" ? filter($el) : false;
3940
+ if (!filterResult) {
3941
+ yield $child;
3942
+ }
3943
+ }
3944
+ }
3945
+ }
3946
+ }
3947
+ for (let index = 0; index < $el.children.length; index++) {
3948
+ const $child = $el.children[index];
3949
+ yield* that.findElementsWithText($child, text, filter);
3950
+ }
3951
+ }
3952
+ /**
3953
+ * 寻找可见元素,如果元素不可见,则向上找它的父元素直至找到,如果父元素不存在则返回null
3954
+ * @param $el
3955
+ * @example
3956
+ * let visibleElement = DOMUtils.findVisibleElement(document.querySelector("a.xx"));
3957
+ * > <HTMLElement>
3958
+ */
3959
+ findVisibleElement($el) {
3960
+ let $current = $el;
3961
+ while ($current) {
3962
+ const rect = $current.getBoundingClientRect();
3963
+ if (rect.length) {
3964
+ return $current;
3965
+ }
3966
+ $current = $current.parentElement;
3967
+ }
3968
+ return null;
3969
+ }
3970
+ /**
3971
+ * 将元素上的文本或元素使用光标进行选中
3972
+ *
3973
+ * 注意,如果设置startIndex和endIndex,且元素上并无可选则的坐标,那么会报错
3974
+ * @param $el 目标元素
3975
+ * @param childTextNode 目标元素下的#text元素
3976
+ * @param startIndex (可选)开始坐标,可为空
3977
+ * @param endIndex (可选)结束坐标,可为空
3978
+ * @example
3979
+ * DOMUtils.setElementSelection(document.querySelector("span"));
3980
+ */
3981
+ setElementSelection($el, childTextNode, startIndex, endIndex) {
3982
+ const range = this.windowApi.document.createRange();
3983
+ range.selectNodeContents($el);
3984
+ if (childTextNode) {
3985
+ if (childTextNode.nodeType !== Node.TEXT_NODE) {
3986
+ throw new TypeError("childTextNode必须是#text元素");
3987
+ }
3988
+ if (startIndex != null && endIndex != null) {
3989
+ range.setStart(childTextNode, startIndex);
3990
+ range.setEnd(childTextNode, endIndex);
3991
+ }
3992
+ }
3993
+ const selection = this.windowApi.globalThis.getSelection();
3994
+ if (selection) {
3995
+ selection.removeAllRanges();
3996
+ selection.addRange(range);
3997
+ }
3098
3998
  }
3099
3999
  }
3100
4000
  const domUtils = new DOMUtils();