@whitesev/domutils 1.8.8 → 1.9.0

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 (40) hide show
  1. package/dist/index.amd.js +3913 -4128
  2. package/dist/index.amd.js.map +1 -1
  3. package/dist/index.amd.min.js +1 -1
  4. package/dist/index.amd.min.js.map +1 -1
  5. package/dist/index.cjs.js +195 -410
  6. package/dist/index.cjs.js.map +1 -1
  7. package/dist/index.cjs.min.js +1 -1
  8. package/dist/index.cjs.min.js.map +1 -1
  9. package/dist/index.esm.js +195 -410
  10. package/dist/index.esm.js.map +1 -1
  11. package/dist/index.esm.min.js +1 -1
  12. package/dist/index.esm.min.js.map +1 -1
  13. package/dist/index.iife.js +3914 -4129
  14. package/dist/index.iife.js.map +1 -1
  15. package/dist/index.iife.min.js +1 -1
  16. package/dist/index.iife.min.js.map +1 -1
  17. package/dist/index.system.js +3918 -4133
  18. package/dist/index.system.js.map +1 -1
  19. package/dist/index.system.min.js +1 -1
  20. package/dist/index.system.min.js.map +1 -1
  21. package/dist/index.umd.js +3916 -4131
  22. package/dist/index.umd.js.map +1 -1
  23. package/dist/index.umd.min.js +1 -1
  24. package/dist/index.umd.min.js.map +1 -1
  25. package/dist/types/src/CommonUtils.d.ts +0 -16
  26. package/dist/types/src/ElementAnimate.d.ts +1 -1
  27. package/dist/types/src/ElementEvent.d.ts +51 -23
  28. package/dist/types/src/ElementSelector.d.ts +2 -2
  29. package/dist/types/src/index.d.ts +23 -14
  30. package/dist/types/src/types/DOMUtilsEvent.d.ts +3 -3
  31. package/dist/types/src/types/env.d.ts +1 -0
  32. package/package.json +2 -2
  33. package/src/CommonUtils.ts +0 -54
  34. package/src/ElementAnimate.ts +3 -3
  35. package/src/ElementEvent.ts +99 -76
  36. package/src/ElementSelector.ts +8 -6
  37. package/src/ElementWait.ts +1 -2
  38. package/src/index.ts +164 -81
  39. package/src/types/DOMUtilsEvent.d.ts +3 -3
  40. package/src/types/env.d.ts +1 -0
package/src/index.ts CHANGED
@@ -1,10 +1,10 @@
1
- import { CommonUtils } from "./CommonUtils";
2
- import type { DOMUtilsCreateElementAttributesMap } from "./types/DOMUtilsEvent";
3
- import { type DOMUtilsCSSProperty, type DOMUtilsCSSPropertyType } from "./types/DOMUtilsCSSProperty";
4
- import type { WindowApiOption } from "./types/WindowApi";
5
1
  import { version } from "../package.json";
2
+ import { CommonUtils } from "./CommonUtils";
6
3
  import { ElementHandler } from "./ElementHandler";
4
+ import { type DOMUtilsCSSProperty, type DOMUtilsCSSPropertyType } from "./types/DOMUtilsCSSProperty";
5
+ import type { DOMUtilsCreateElementAttributesMap } from "./types/DOMUtilsEvent";
7
6
  import type { DOMUtilsTargetElementType } from "./types/global";
7
+ import type { WindowApiOption } from "./types/WindowApi";
8
8
 
9
9
  class DOMUtils extends ElementHandler {
10
10
  constructor(option?: WindowApiOption) {
@@ -784,20 +784,22 @@ class DOMUtils extends ElementHandler {
784
784
  /**
785
785
  * 函数在元素内部末尾添加子元素或HTML字符串
786
786
  * @param $el 目标元素
787
- * @param content 子元素或HTML字符串
787
+ * @param args 子元素或HTML字符串
788
788
  * @example
789
789
  * // 元素a.xx的内部末尾添加一个元素
790
- * DOMUtils.append(document.querySelector("a.xx"),document.querySelector("b.xx"))
791
- * DOMUtils.append("a.xx","'<b class="xx"></b>")
790
+ * DOMUtils.append(document.querySelector("a.xx"), document.querySelector("b.xx"))
791
+ * DOMUtils.append("a.xx", "<b class="xx"></b>")
792
+ * DOMUtils.append(document, [document.querySelector("b.xx"), document.querySelector("c.xx"), document.querySelector("d.xx")])
793
+ * DOMUtils.append(document, document.querySelector("b.xx"), document.querySelector("c.xx"), document.querySelector("d.xx"))
792
794
  * */
793
795
  append(
794
796
  $el: DOMUtilsTargetElementType | DocumentFragment,
795
- content: HTMLElement | string | (HTMLElement | string | Element)[] | NodeList
797
+ ...args: (HTMLElement | string | (HTMLElement | string | Element)[] | NodeList)[]
796
798
  ) {
797
- const that = this;
798
799
  if (typeof $el === "string") {
799
- $el = that.selectorAll($el);
800
+ $el = this.selectorAll($el);
800
801
  }
802
+
801
803
  if ($el == null) {
802
804
  return;
803
805
  }
@@ -805,150 +807,228 @@ class DOMUtils extends ElementHandler {
805
807
  if (CommonUtils.isNodeList($el)) {
806
808
  // 设置
807
809
  $el.forEach(($elItem) => {
808
- that.append($elItem as HTMLElement, content);
810
+ this.append($elItem as HTMLElement, ...args);
809
811
  });
810
812
  return;
811
813
  }
812
- function elementAppendChild(ele: HTMLElement | DocumentFragment, text: HTMLElement | string) {
813
- if (typeof content === "string") {
814
- if (ele instanceof DocumentFragment) {
815
- if (typeof text === "string") {
816
- text = that.toElement(text, true, false);
817
- }
818
- ele.appendChild(text);
814
+ const handler = ($ele: HTMLElement | DocumentFragment, $target: HTMLElement | string | Node) => {
815
+ if ($ele instanceof DocumentFragment) {
816
+ if (typeof $target === "string") {
817
+ // 字符串转元素
818
+ $target = this.toElement($target, true, false);
819
+ }
820
+ $ele.appendChild($target);
821
+ } else {
822
+ if (typeof $target === "string") {
823
+ $ele.insertAdjacentHTML("beforeend", CommonUtils.createSafeHTML($target));
819
824
  } else {
820
- ele.insertAdjacentHTML("beforeend", CommonUtils.createSafeHTML(text as string));
825
+ $ele.appendChild($target);
821
826
  }
827
+ }
828
+ };
829
+ const $fragment = this.windowApi.document.createDocumentFragment();
830
+ args.forEach((argItem) => {
831
+ if (CommonUtils.isNodeList(argItem)) {
832
+ // 数组
833
+ argItem.forEach((it) => {
834
+ handler($fragment, it);
835
+ });
822
836
  } else {
823
- ele.appendChild(text as HTMLElement);
837
+ handler($fragment, argItem);
824
838
  }
825
- }
826
- if (Array.isArray(content) || content instanceof NodeList) {
827
- /* 数组 */
828
- const fragment = that.windowApi.document.createDocumentFragment();
829
- content.forEach((ele) => {
830
- if (typeof ele === "string") {
831
- // 转为元素
832
- ele = that.toElement(ele, true, false);
833
- }
834
- fragment.appendChild(ele);
835
- });
836
- $el.appendChild(fragment);
837
- } else {
838
- elementAppendChild($el, content);
839
- }
839
+ });
840
+ handler($el, $fragment);
840
841
  }
841
842
  /**
842
843
  * 函数 在元素内部开头添加子元素或HTML字符串
843
844
  * @param $el 目标元素
844
- * @param content 子元素或HTML字符串
845
+ * @param args 子元素或HTML字符串
845
846
  * @example
846
847
  * // 元素a.xx内部开头添加一个元素
847
848
  * DOMUtils.prepend(document.querySelector("a.xx"),document.querySelector("b.xx"))
848
849
  * DOMUtils.prepend("a.xx","'<b class="xx"></b>")
850
+ * DOMUtils.prepend(document, [document.querySelector("b.xx"), document.querySelector("c.xx"), document.querySelector("d.xx")])
851
+ * DOMUtils.prepend(document, document.querySelector("b.xx"), document.querySelector("c.xx"), document.querySelector("d.xx"))
849
852
  * */
850
- prepend($el: DOMUtilsTargetElementType | DocumentFragment, content: HTMLElement | string) {
851
- const that = this;
853
+ prepend(
854
+ $el: DOMUtilsTargetElementType | DocumentFragment,
855
+ ...args: (HTMLElement | string | (HTMLElement | string | Element)[] | NodeList)[]
856
+ ) {
852
857
  if (typeof $el === "string") {
853
- $el = that.selectorAll($el);
858
+ $el = this.selectorAll($el);
854
859
  }
860
+
855
861
  if ($el == null) {
856
862
  return;
857
863
  }
864
+
858
865
  if (CommonUtils.isNodeList($el)) {
859
866
  // 设置
860
867
  $el.forEach(($elItem) => {
861
- that.prepend($elItem as HTMLElement, content);
868
+ this.prepend($elItem as HTMLElement, ...args);
862
869
  });
863
870
  return;
864
871
  }
865
- if (typeof content === "string") {
866
- if ($el instanceof DocumentFragment) {
867
- content = that.toElement(content, true, false);
868
- $el.prepend(content);
872
+ const handler = ($ele: HTMLElement | DocumentFragment, $target: HTMLElement | string | Node) => {
873
+ if ($ele instanceof DocumentFragment) {
874
+ if (typeof $target === "string") {
875
+ // 字符串转元素
876
+ $target = this.toElement($target, true, false);
877
+ }
878
+ $ele.appendChild($target);
869
879
  } else {
870
- $el.insertAdjacentHTML("afterbegin", CommonUtils.createSafeHTML(content));
880
+ if (typeof $target === "string") {
881
+ $ele.insertAdjacentHTML("afterbegin", CommonUtils.createSafeHTML($target));
882
+ } else {
883
+ const $firstChild = $ele.firstChild;
884
+ if ($firstChild) {
885
+ $ele.insertBefore($target, $firstChild);
886
+ } else {
887
+ $ele.prepend($target);
888
+ }
889
+ }
871
890
  }
872
- } else {
873
- const $firstChild = $el.firstChild;
874
- if ($firstChild == null) {
875
- $el.prepend(content);
891
+ };
892
+ const $fragment = this.windowApi.document.createDocumentFragment();
893
+ args.forEach((argItem) => {
894
+ if (CommonUtils.isNodeList(argItem)) {
895
+ // 数组
896
+ argItem.forEach((it) => {
897
+ handler($fragment, it);
898
+ });
876
899
  } else {
877
- $el.insertBefore(content, $firstChild);
900
+ handler($fragment, argItem);
878
901
  }
879
- }
902
+ });
903
+ handler($el, $fragment);
880
904
  }
881
905
  /**
882
906
  * 在元素后面添加兄弟元素或HTML字符串
883
907
  * @param $el 目标元素
884
- * @param content 兄弟元素或HTML字符串
908
+ * @param args 兄弟元素或HTML字符串
885
909
  * @example
886
910
  * // 元素a.xx后面添加一个元素
887
911
  * DOMUtils.after(document.querySelector("a.xx"),document.querySelector("b.xx"))
888
912
  * DOMUtils.after("a.xx","'<b class="xx"></b>")
913
+ * DOMUtils.after(document, [document.querySelector("b.xx"), document.querySelector("c.xx"), document.querySelector("d.xx")])
914
+ * DOMUtils.after(document, document.querySelector("b.xx"), document.querySelector("c.xx"), document.querySelector("d.xx"))
889
915
  * */
890
- after($el: DOMUtilsTargetElementType, content: HTMLElement | string) {
891
- const that = this;
916
+ after(
917
+ $el: DOMUtilsTargetElementType,
918
+ ...args: (HTMLElement | string | (HTMLElement | string | Element)[] | NodeList)[]
919
+ ) {
892
920
  if (typeof $el === "string") {
893
- $el = that.selectorAll($el);
921
+ $el = this.selectorAll($el);
894
922
  }
923
+
895
924
  if ($el == null) {
896
925
  return;
897
926
  }
927
+
898
928
  if (CommonUtils.isNodeList($el)) {
899
929
  // 设置
900
930
  $el.forEach(($elItem) => {
901
- that.after($elItem as HTMLElement, content);
931
+ this.after($elItem as HTMLElement, ...args);
902
932
  });
903
933
  return;
904
934
  }
905
- if (typeof content === "string") {
906
- $el.insertAdjacentHTML("afterend", CommonUtils.createSafeHTML(content));
907
- } else {
908
- const $parent = $el.parentElement;
909
- const $nextSlibling = $el.nextSibling;
910
- if (!$parent || $nextSlibling) {
911
- // 任意一个不行
912
- $el.after(content);
935
+ const handler = ($ele: HTMLElement | DocumentFragment, $target: HTMLElement | string | Node) => {
936
+ if ($ele instanceof DocumentFragment) {
937
+ if (typeof $target === "string") {
938
+ // 字符串转元素
939
+ $target = this.toElement($target, true, false);
940
+ }
941
+ $ele.appendChild($target);
913
942
  } else {
914
- $parent.insertBefore(content, $nextSlibling);
943
+ if (typeof $target === "string") {
944
+ $ele.insertAdjacentHTML("afterend", CommonUtils.createSafeHTML($target));
945
+ } else {
946
+ const $parent = $el.parentElement;
947
+ const $nextSlibling = $el.nextSibling;
948
+ if ($parent && $nextSlibling) {
949
+ $parent.insertBefore($target, $nextSlibling);
950
+ } else {
951
+ $el.after($target);
952
+ }
953
+ }
915
954
  }
916
- }
955
+ };
956
+ const $fragment = this.windowApi.document.createDocumentFragment();
957
+ args.forEach((argItem) => {
958
+ if (CommonUtils.isNodeList(argItem)) {
959
+ // 数组
960
+ argItem.forEach((it) => {
961
+ handler($fragment, it);
962
+ });
963
+ } else {
964
+ handler($fragment, argItem);
965
+ }
966
+ });
967
+ handler($el, $fragment);
917
968
  }
918
969
  /**
919
970
  * 在元素前面添加兄弟元素或HTML字符串
920
971
  * @param $el 目标元素
921
- * @param content 兄弟元素或HTML字符串
972
+ * @param args 兄弟元素或HTML字符串
922
973
  * @example
923
974
  * // 元素a.xx前面添加一个元素
924
975
  * DOMUtils.before(document.querySelector("a.xx"),document.querySelector("b.xx"))
925
976
  * DOMUtils.before("a.xx","'<b class="xx"></b>")
977
+ * DOMUtils.before(document, [document.querySelector("b.xx"), document.querySelector("c.xx"), document.querySelector("d.xx")])
978
+ * DOMUtils.before(document, document.querySelector("b.xx"), document.querySelector("c.xx"), document.querySelector("d.xx"))
979
+ *
926
980
  * */
927
- before($el: DOMUtilsTargetElementType, content: HTMLElement | string) {
928
- const that = this;
981
+ before(
982
+ $el: DOMUtilsTargetElementType,
983
+ ...args: (HTMLElement | string | (HTMLElement | string | Element)[] | NodeList)[]
984
+ ) {
929
985
  if (typeof $el === "string") {
930
- $el = that.selectorAll($el);
986
+ $el = this.selectorAll($el);
931
987
  }
988
+
932
989
  if ($el == null) {
933
990
  return;
934
991
  }
992
+
935
993
  if (CommonUtils.isNodeList($el)) {
936
994
  // 设置
937
995
  $el.forEach(($elItem) => {
938
- that.before($elItem as HTMLElement, content);
996
+ this.before($elItem as HTMLElement, ...args);
939
997
  });
940
998
  return;
941
999
  }
942
- if (typeof content === "string") {
943
- $el.insertAdjacentHTML("beforebegin", CommonUtils.createSafeHTML(content));
944
- } else {
945
- const $parent = $el.parentElement;
946
- if (!$parent) {
947
- $el.before(content);
1000
+ const handler = ($ele: HTMLElement | DocumentFragment, $target: HTMLElement | string | Node) => {
1001
+ if ($ele instanceof DocumentFragment) {
1002
+ if (typeof $target === "string") {
1003
+ // 字符串转元素
1004
+ $target = this.toElement($target, true, false);
1005
+ }
1006
+ $ele.appendChild($target);
1007
+ } else {
1008
+ if (typeof $target === "string") {
1009
+ $el.insertAdjacentHTML("beforebegin", CommonUtils.createSafeHTML($target));
1010
+ } else {
1011
+ const $parent = $el.parentElement;
1012
+ if ($parent) {
1013
+ $parent.insertBefore($target, $el);
1014
+ } else {
1015
+ $el.before($target);
1016
+ }
1017
+ }
1018
+ }
1019
+ };
1020
+ const $fragment = this.windowApi.document.createDocumentFragment();
1021
+ args.forEach((argItem) => {
1022
+ if (CommonUtils.isNodeList(argItem)) {
1023
+ // 数组
1024
+ argItem.forEach((it) => {
1025
+ handler($fragment, it);
1026
+ });
948
1027
  } else {
949
- $parent.insertBefore(content, $el);
1028
+ handler($fragment, argItem);
950
1029
  }
951
- }
1030
+ });
1031
+ handler($el, $fragment);
952
1032
  }
953
1033
  /**
954
1034
  * 移除元素
@@ -982,7 +1062,7 @@ class DOMUtils extends ElementHandler {
982
1062
  }
983
1063
  }
984
1064
  /**
985
- * 移除元素的所有子元素
1065
+ * 移除元素内所有的子元素
986
1066
  * @param $el 目标元素
987
1067
  * @example
988
1068
  * // 移除元素a.xx元素的所有子元素
@@ -1903,5 +1983,8 @@ class DOMUtils extends ElementHandler {
1903
1983
  }
1904
1984
 
1905
1985
  const domUtils = new DOMUtils();
1906
-
1986
+ domUtils.emit(document, "test");
1987
+ domUtils.emit(document, "click");
1988
+ domUtils.emit(document, ["test", "click"]);
1989
+ domUtils.emit(document, ["test", "click"], true);
1907
1990
  export { domUtils as DOMUtils };
@@ -339,9 +339,9 @@ export declare type DOMUtils_EventType = keyof DOMUtils_Event;
339
339
  */
340
340
  export declare interface DOMUtilsEventListenerOptionsAttribute {
341
341
  /**
342
- * DOMUtils的ownCallBack,元素上的监听事件就是它,移除事件时也需要传入这个函数
342
+ * DOMUtils的handlerCallBack,元素上的监听事件就是它,移除事件时也需要传入这个函数
343
343
  */
344
- callback: (event: Event) => void;
344
+ handlerCallBack: (event: Event, $selector?: HTMLElement) => void;
345
345
  /**
346
346
  * 属性配置
347
347
  */
@@ -349,7 +349,7 @@ export declare interface DOMUtilsEventListenerOptionsAttribute {
349
349
  /**
350
350
  * 用户添加的事件
351
351
  */
352
- originCallBack: (event: Event, selectorTarget?: HTMLElement) => void;
352
+ callback: (event: Event, $selector?: HTMLElement) => void;
353
353
  /**
354
354
  * 子元素选择器
355
355
  */
@@ -7,3 +7,4 @@ declare interface Window {
7
7
  DOMUtils: typeof import("./../index").DOMUtils;
8
8
  trustedTypes: any;
9
9
  }
10
+ declare type IArray<T> = T | T[];