@whitesev/domutils 1.8.8 → 1.8.9

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.
@@ -1,6 +1,6 @@
1
+ import { ElementWait } from "./ElementWait";
1
2
  import type { DOMUtilsTargetElementType } from "./types/global";
2
3
  import type { WindowApiOption } from "./types/WindowApi";
3
- import { ElementWait } from "./ElementWait";
4
4
  import { WindowApi } from "./WindowApi";
5
5
  declare class ElementAnimate extends ElementWait {
6
6
  windowApi: typeof WindowApi.prototype;
@@ -1,8 +1,8 @@
1
- import type { DOMUtilsCreateElementAttributesMap } from "./types/DOMUtilsEvent";
2
- import { type DOMUtilsCSSProperty, type DOMUtilsCSSPropertyType } from "./types/DOMUtilsCSSProperty";
3
- import type { WindowApiOption } from "./types/WindowApi";
4
1
  import { ElementHandler } from "./ElementHandler";
2
+ import { type DOMUtilsCSSProperty, type DOMUtilsCSSPropertyType } from "./types/DOMUtilsCSSProperty";
3
+ import type { DOMUtilsCreateElementAttributesMap } from "./types/DOMUtilsEvent";
5
4
  import type { DOMUtilsTargetElementType } from "./types/global";
5
+ import type { WindowApiOption } from "./types/WindowApi";
6
6
  declare class DOMUtils extends ElementHandler {
7
7
  constructor(option?: WindowApiOption);
8
8
  /** 版本号 */
@@ -304,43 +304,52 @@ declare class DOMUtils extends ElementHandler {
304
304
  /**
305
305
  * 函数在元素内部末尾添加子元素或HTML字符串
306
306
  * @param $el 目标元素
307
- * @param content 子元素或HTML字符串
307
+ * @param args 子元素或HTML字符串
308
308
  * @example
309
309
  * // 元素a.xx的内部末尾添加一个元素
310
- * DOMUtils.append(document.querySelector("a.xx"),document.querySelector("b.xx"))
311
- * DOMUtils.append("a.xx","'<b class="xx"></b>")
310
+ * DOMUtils.append(document.querySelector("a.xx"), document.querySelector("b.xx"))
311
+ * DOMUtils.append("a.xx", "<b class="xx"></b>")
312
+ * DOMUtils.append(document, [document.querySelector("b.xx"), document.querySelector("c.xx"), document.querySelector("d.xx")])
313
+ * DOMUtils.append(document, document.querySelector("b.xx"), document.querySelector("c.xx"), document.querySelector("d.xx"))
312
314
  * */
313
- append($el: DOMUtilsTargetElementType | DocumentFragment, content: HTMLElement | string | (HTMLElement | string | Element)[] | NodeList): void;
315
+ append($el: DOMUtilsTargetElementType | DocumentFragment, ...args: (HTMLElement | string | (HTMLElement | string | Element)[] | NodeList)[]): void;
314
316
  /**
315
317
  * 函数 在元素内部开头添加子元素或HTML字符串
316
318
  * @param $el 目标元素
317
- * @param content 子元素或HTML字符串
319
+ * @param args 子元素或HTML字符串
318
320
  * @example
319
321
  * // 元素a.xx内部开头添加一个元素
320
322
  * DOMUtils.prepend(document.querySelector("a.xx"),document.querySelector("b.xx"))
321
323
  * DOMUtils.prepend("a.xx","'<b class="xx"></b>")
324
+ * DOMUtils.prepend(document, [document.querySelector("b.xx"), document.querySelector("c.xx"), document.querySelector("d.xx")])
325
+ * DOMUtils.prepend(document, document.querySelector("b.xx"), document.querySelector("c.xx"), document.querySelector("d.xx"))
322
326
  * */
323
- prepend($el: DOMUtilsTargetElementType | DocumentFragment, content: HTMLElement | string): void;
327
+ prepend($el: DOMUtilsTargetElementType | DocumentFragment, ...args: (HTMLElement | string | (HTMLElement | string | Element)[] | NodeList)[]): void;
324
328
  /**
325
329
  * 在元素后面添加兄弟元素或HTML字符串
326
330
  * @param $el 目标元素
327
- * @param content 兄弟元素或HTML字符串
331
+ * @param args 兄弟元素或HTML字符串
328
332
  * @example
329
333
  * // 元素a.xx后面添加一个元素
330
334
  * DOMUtils.after(document.querySelector("a.xx"),document.querySelector("b.xx"))
331
335
  * DOMUtils.after("a.xx","'<b class="xx"></b>")
336
+ * DOMUtils.after(document, [document.querySelector("b.xx"), document.querySelector("c.xx"), document.querySelector("d.xx")])
337
+ * DOMUtils.after(document, document.querySelector("b.xx"), document.querySelector("c.xx"), document.querySelector("d.xx"))
332
338
  * */
333
- after($el: DOMUtilsTargetElementType, content: HTMLElement | string): void;
339
+ after($el: DOMUtilsTargetElementType, ...args: (HTMLElement | string | (HTMLElement | string | Element)[] | NodeList)[]): void;
334
340
  /**
335
341
  * 在元素前面添加兄弟元素或HTML字符串
336
342
  * @param $el 目标元素
337
- * @param content 兄弟元素或HTML字符串
343
+ * @param args 兄弟元素或HTML字符串
338
344
  * @example
339
345
  * // 元素a.xx前面添加一个元素
340
346
  * DOMUtils.before(document.querySelector("a.xx"),document.querySelector("b.xx"))
341
347
  * DOMUtils.before("a.xx","'<b class="xx"></b>")
348
+ * DOMUtils.before(document, [document.querySelector("b.xx"), document.querySelector("c.xx"), document.querySelector("d.xx")])
349
+ * DOMUtils.before(document, document.querySelector("b.xx"), document.querySelector("c.xx"), document.querySelector("d.xx"))
350
+ *
342
351
  * */
343
- before($el: DOMUtilsTargetElementType, content: HTMLElement | string): void;
352
+ before($el: DOMUtilsTargetElementType, ...args: (HTMLElement | string | (HTMLElement | string | Element)[] | NodeList)[]): void;
344
353
  /**
345
354
  * 移除元素
346
355
  * @param $el 目标元素,可以是数组、单个元素、NodeList、元素选择器
@@ -352,7 +361,7 @@ declare class DOMUtils extends ElementHandler {
352
361
  * */
353
362
  remove($el: DOMUtilsTargetElementType | Element): void;
354
363
  /**
355
- * 移除元素的所有子元素
364
+ * 移除元素内所有的子元素
356
365
  * @param $el 目标元素
357
366
  * @example
358
367
  * // 移除元素a.xx元素的所有子元素
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "$schema": "https://json.schemastore.org/package.json",
3
3
  "name": "@whitesev/domutils",
4
- "version": "1.8.8",
4
+ "version": "1.8.9",
5
5
  "type": "module",
6
6
  "description": "适合在浏览器中操作DOM的常用工具类",
7
7
  "main": "dist/index.cjs.js",
@@ -58,7 +58,7 @@
58
58
  "lint": "eslint .",
59
59
  "lint:fix": "eslint . --fix",
60
60
  "format": "prettier . --write",
61
- "dev": "pnpm run format && pnpm run lint && rollup -c -w",
61
+ "dev": "vue-tsc --noEmit && vite --force",
62
62
  "build": "pnpm run format && pnpm run lint && rollup -c"
63
63
  }
64
64
  }
@@ -1,10 +1,4 @@
1
1
  import { WindowApi } from "./WindowApi";
2
- import {
3
- clearInterval as WorkerClearInterval,
4
- clearTimeout as WorkerClearTimeout,
5
- setInterval as WorkerSetInterval,
6
- setTimeout as WorkerSetTimeout,
7
- } from "worker-timers";
8
2
 
9
3
  /** 通用工具类 */
10
4
  export const CommonUtils = {
@@ -151,54 +145,6 @@ export const CommonUtils = {
151
145
  delete target[propName];
152
146
  }
153
147
  },
154
- /**
155
- * 自动使用 Worker 执行 setTimeout
156
- */
157
- setTimeout(callback: (...args: any[]) => any, timeout: number = 0) {
158
- try {
159
- return WorkerSetTimeout(callback, timeout);
160
- } catch {
161
- return this.windowApi.setTimeout(callback, timeout);
162
- }
163
- },
164
- /**
165
- * 配合 .setTimeout 使用
166
- */
167
- clearTimeout(timeId: number | undefined) {
168
- try {
169
- if (timeId != null) {
170
- WorkerClearTimeout(timeId);
171
- }
172
- } catch {
173
- // TODO
174
- } finally {
175
- this.windowApi.clearTimeout(timeId);
176
- }
177
- },
178
- /**
179
- * 自动使用 Worker 执行 setInterval
180
- */
181
- setInterval(callback: (...args: any[]) => any, timeout: number = 0) {
182
- try {
183
- return WorkerSetInterval(callback, timeout);
184
- } catch {
185
- return this.windowApi.setInterval(callback, timeout);
186
- }
187
- },
188
- /**
189
- * 配合 .setInterval 使用
190
- */
191
- clearInterval(timeId: number | undefined) {
192
- try {
193
- if (timeId != null) {
194
- WorkerClearInterval(timeId);
195
- }
196
- } catch {
197
- // TODO
198
- } finally {
199
- this.windowApi.clearInterval(timeId);
200
- }
201
- },
202
148
  /**
203
149
  * 判断是否是元素列表
204
150
  * @param $ele
@@ -1,8 +1,8 @@
1
1
  import { CommonUtils } from "./CommonUtils";
2
2
  import { elementSelector } from "./ElementSelector";
3
+ import { ElementWait } from "./ElementWait";
3
4
  import type { DOMUtilsTargetElementType } from "./types/global";
4
5
  import type { WindowApiOption } from "./types/WindowApi";
5
- import { ElementWait } from "./ElementWait";
6
6
  import { WindowApi } from "./WindowApi";
7
7
 
8
8
  class ElementAnimate extends ElementWait {
@@ -66,7 +66,7 @@ class ElementAnimate extends ElementWait {
66
66
  from[prop] = element.style[prop] || context.windowApi.globalThis.getComputedStyle(element)[prop];
67
67
  to[prop] = styles[prop];
68
68
  }
69
- const timer = CommonUtils.setInterval(function () {
69
+ const timer = setInterval(function () {
70
70
  const timePassed = performance.now() - start;
71
71
  let progress = timePassed / duration;
72
72
  if (progress > 1) {
@@ -76,7 +76,7 @@ class ElementAnimate extends ElementWait {
76
76
  element.style[prop] = from[prop] + (to[prop] - from[prop]) * progress + "px";
77
77
  }
78
78
  if (progress === 1) {
79
- CommonUtils.clearInterval(timer);
79
+ clearInterval(timer);
80
80
  if (callback) {
81
81
  callback();
82
82
  }
@@ -743,7 +743,7 @@ class ElementEvent extends ElementAnimate {
743
743
  function check() {
744
744
  if (checkDOMReadyState()) {
745
745
  /* 检查document状态 */
746
- CommonUtils.setTimeout(completed, 0);
746
+ setTimeout(completed, 0);
747
747
  } else {
748
748
  /* 添加监听 */
749
749
  addDomReadyListener();
@@ -1440,7 +1440,7 @@ class ElementEvent extends ElementAnimate {
1440
1440
  if (evt.pointerType === "touch") {
1441
1441
  isMobileTouch = true;
1442
1442
  }
1443
- CommonUtils.clearTimeout(timer);
1443
+ clearTimeout(timer);
1444
1444
  timer = void 0;
1445
1445
  if (isDoubleClick && $click === selectorTarget) {
1446
1446
  isDoubleClick = false;
@@ -1450,7 +1450,7 @@ class ElementEvent extends ElementAnimate {
1450
1450
  isDoubleClick: true,
1451
1451
  });
1452
1452
  } else {
1453
- timer = CommonUtils.setTimeout(() => {
1453
+ timer = setTimeout(() => {
1454
1454
  isDoubleClick = false;
1455
1455
  // 判断为单击
1456
1456
  dblclick_handler(evt, {
@@ -1,4 +1,3 @@
1
- import { CommonUtils } from "./CommonUtils";
2
1
  import { ElementSelector, elementSelector } from "./ElementSelector";
3
2
  import type { WindowApiOption } from "./types/WindowApi";
4
3
  import { utils } from "./Utils";
@@ -90,7 +89,7 @@ class ElementWait extends ElementSelector {
90
89
  },
91
90
  });
92
91
  if (__timeout__ > 0) {
93
- CommonUtils.setTimeout(() => {
92
+ setTimeout(() => {
94
93
  // 取消观察器
95
94
  if (typeof observer?.disconnect === "function") {
96
95
  observer.disconnect();
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);
948
1007
  } else {
949
- $parent.insertBefore(content, $el);
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
+ }
950
1018
  }
951
- }
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
+ });
1027
+ } else {
1028
+ handler($fragment, argItem);
1029
+ }
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元素的所有子元素