@whitesev/domutils 1.9.0 → 1.9.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/index.amd.js CHANGED
@@ -1,6 +1,6 @@
1
1
  define((function () { 'use strict';
2
2
 
3
- const version = "1.9.0";
3
+ const version = "1.9.2";
4
4
 
5
5
  class WindowApi {
6
6
  /** 默认的配置 */
@@ -1706,7 +1706,7 @@ define((function () { 'use strict';
1706
1706
  }
1707
1707
  for (const handler of handlers) {
1708
1708
  $elItem.removeEventListener(eventName, handler.handlerCallBack, {
1709
- capture: handler["option"]["capture"],
1709
+ capture: handler.option.capture,
1710
1710
  });
1711
1711
  }
1712
1712
  const events = Reflect.get($elItem, symbolItem);
@@ -1720,36 +1720,6 @@ define((function () { 'use strict';
1720
1720
  // 异步回调
1721
1721
  let resolve = void 0;
1722
1722
  const that = this;
1723
- /**
1724
- * 检测文档是否加载完毕
1725
- */
1726
- function checkDOMReadyState() {
1727
- try {
1728
- if (that.windowApi.document.readyState === "complete" ||
1729
- (that.windowApi.document.readyState !== "loading" &&
1730
- !that.windowApi.document.documentElement.doScroll)) {
1731
- return true;
1732
- }
1733
- else {
1734
- return false;
1735
- }
1736
- }
1737
- catch {
1738
- return false;
1739
- }
1740
- }
1741
- /**
1742
- * 成功加载完毕后触发的回调函数
1743
- */
1744
- function completed() {
1745
- removeDomReadyListener();
1746
- if (typeof callback === "function") {
1747
- callback();
1748
- }
1749
- if (typeof resolve === "function") {
1750
- resolve();
1751
- }
1752
- }
1753
1723
  /**
1754
1724
  * 监听目标
1755
1725
  */
@@ -1757,52 +1727,90 @@ define((function () { 'use strict';
1757
1727
  {
1758
1728
  target: that.windowApi.document,
1759
1729
  eventType: "DOMContentLoaded",
1760
- callback: completed,
1730
+ callback: () => {
1731
+ ReadyChecker.completed();
1732
+ },
1761
1733
  },
1762
1734
  {
1763
1735
  target: that.windowApi.window,
1764
1736
  eventType: "load",
1765
- callback: completed,
1737
+ callback: () => {
1738
+ ReadyChecker.completed();
1739
+ },
1766
1740
  },
1767
1741
  ];
1768
- /**
1769
- * 添加监听
1770
- */
1771
- function addDomReadyListener() {
1772
- for (const item of listenTargetList) {
1773
- that.on(item.target, item.eventType, item.callback);
1774
- }
1775
- }
1776
- /**
1777
- * 移除监听
1778
- */
1779
- function removeDomReadyListener() {
1780
- for (const item of listenTargetList) {
1781
- that.off(item.target, item.eventType, item.callback);
1782
- }
1783
- }
1784
- /**
1785
- * 执行检查
1786
- */
1787
- function check() {
1788
- if (checkDOMReadyState()) {
1789
- /* 检查document状态 */
1790
- setTimeout(completed, 0);
1791
- }
1792
- else {
1793
- /* 添加监听 */
1794
- addDomReadyListener();
1795
- }
1796
- }
1797
- if (args.length === 0) {
1798
- return new Promise((__resolve__) => {
1799
- resolve = __resolve__;
1800
- check();
1801
- });
1802
- }
1803
- else {
1804
- check();
1805
- }
1742
+ const ReadyChecker = {
1743
+ init() {
1744
+ if (args.length === 0) {
1745
+ return new Promise((__resolve__) => {
1746
+ resolve = __resolve__;
1747
+ ReadyChecker.check();
1748
+ });
1749
+ }
1750
+ else {
1751
+ ReadyChecker.check();
1752
+ }
1753
+ },
1754
+ check() {
1755
+ if (ReadyChecker.isReady()) {
1756
+ /* 检查document状态 */
1757
+ setTimeout(() => {
1758
+ ReadyChecker.completed();
1759
+ }, 0);
1760
+ }
1761
+ else {
1762
+ /* 添加监听 */
1763
+ ReadyChecker.onCompleted();
1764
+ }
1765
+ },
1766
+ /**
1767
+ * 检测文档是否加载完毕
1768
+ */
1769
+ isReady() {
1770
+ try {
1771
+ if (that.windowApi.document.readyState === "complete" ||
1772
+ // @ts-expect-error
1773
+ (that.windowApi.document.readyState !== "loading" && !that.windowApi.document.documentElement.doScroll)) {
1774
+ return true;
1775
+ }
1776
+ else {
1777
+ return false;
1778
+ }
1779
+ }
1780
+ catch {
1781
+ return false;
1782
+ }
1783
+ },
1784
+ /**
1785
+ * 成功加载完毕后触发的回调函数
1786
+ */
1787
+ completed() {
1788
+ ReadyChecker.offCompleted();
1789
+ if (typeof callback === "function") {
1790
+ callback();
1791
+ }
1792
+ if (typeof resolve === "function") {
1793
+ resolve();
1794
+ }
1795
+ },
1796
+ /**
1797
+ * 添加监听
1798
+ */
1799
+ onCompleted() {
1800
+ for (const item of listenTargetList) {
1801
+ that.on(item.target, item.eventType, item.callback);
1802
+ }
1803
+ },
1804
+ /**
1805
+ * 移除监听
1806
+ */
1807
+ offCompleted() {
1808
+ for (const item of listenTargetList) {
1809
+ that.off(item.target, item.eventType, item.callback);
1810
+ }
1811
+ },
1812
+ };
1813
+ return ReadyChecker.init();
1806
1814
  }
1807
1815
  /**
1808
1816
  * 主动触发事件
@@ -3577,6 +3585,41 @@ define((function () { 'use strict';
3577
3585
  return parseHTMLByCreateDom();
3578
3586
  }
3579
3587
  }
3588
+ /**
3589
+ * 将字符串转为Element元素数组
3590
+ * @param html
3591
+ * @param useParser 是否使用DOMParser来生成元素,有些时候通过DOMParser生成的元素有点问题
3592
+ * + true 使用DOMPraser来转换字符串
3593
+ * + false (默认)创建一个div,里面放入字符串,然后提取childNodes
3594
+ * @example
3595
+ * // 将字符串转为Element元素数组
3596
+ * DOMUtils.toElements("<a href='xxxx'></a>")
3597
+ * > [<a href="xxxx"></a>]
3598
+ * @example
3599
+ * // 使用DOMParser将字符串转为Element元素数组
3600
+ * DOMUtils.toElements("<a href='xxxx'></a>",true)
3601
+ * > [<a href="xxxx"></a>]
3602
+ */
3603
+ toElements(html, useParser = false) {
3604
+ const that = this;
3605
+ // 去除html前后的空格
3606
+ html = html.trim();
3607
+ function parseHTMLByDOMParser() {
3608
+ const parser = new DOMParser();
3609
+ return Array.from(parser.parseFromString(html, "text/html").body.childNodes);
3610
+ }
3611
+ function parseHTMLByCreateDom() {
3612
+ const $el = that.windowApi.document.createElement("div");
3613
+ that.html($el, html);
3614
+ return Array.from($el.childNodes);
3615
+ }
3616
+ if (useParser) {
3617
+ return parseHTMLByDOMParser();
3618
+ }
3619
+ else {
3620
+ return parseHTMLByCreateDom();
3621
+ }
3622
+ }
3580
3623
  /**
3581
3624
  * 序列化表单元素
3582
3625
  * @param $form 表单元素
@@ -3585,26 +3628,29 @@ define((function () { 'use strict';
3585
3628
  * > xxx=xxx&aaa=
3586
3629
  */
3587
3630
  serialize($form) {
3631
+ if (!($form instanceof HTMLFormElement)) {
3632
+ throw new TypeError("DOMUtils.serialize 参数必须是HTMLFormElement");
3633
+ }
3588
3634
  const elements = $form.elements;
3589
3635
  const serializedArray = [];
3590
- for (let i = 0; i < elements.length; i++) {
3591
- const element = elements[i];
3592
- if (element.name &&
3593
- !element.disabled &&
3594
- (element.checked ||
3595
- ["text", "hidden", "password", "textarea", "select-one", "select-multiple"].includes(element.type))) {
3596
- if (element.type === "select-multiple") {
3597
- for (let j = 0; j < element.options.length; j++) {
3598
- if (element.options[j].selected) {
3636
+ for (let index = 0; index < elements.length; index++) {
3637
+ const $el = elements[index];
3638
+ if ($el.name &&
3639
+ !$el.disabled &&
3640
+ ($el.checked ||
3641
+ ["text", "hidden", "password", "textarea", "select-one", "select-multiple"].includes($el.type))) {
3642
+ if ($el.type === "select-multiple") {
3643
+ for (let j = 0; j < $el.options.length; j++) {
3644
+ if ($el.options[j].selected) {
3599
3645
  serializedArray.push({
3600
- name: element.name,
3601
- value: element.options[j].value,
3646
+ name: $el.name,
3647
+ value: $el.options[j].value,
3602
3648
  });
3603
3649
  }
3604
3650
  }
3605
3651
  }
3606
3652
  else {
3607
- serializedArray.push({ name: element.name, value: element.value });
3653
+ serializedArray.push({ name: $el.name, value: $el.value });
3608
3654
  }
3609
3655
  }
3610
3656
  }
@@ -3920,10 +3966,6 @@ define((function () { 'use strict';
3920
3966
  }
3921
3967
  }
3922
3968
  const domUtils = new DOMUtils();
3923
- domUtils.emit(document, "test");
3924
- domUtils.emit(document, "click");
3925
- domUtils.emit(document, ["test", "click"]);
3926
- domUtils.emit(document, ["test", "click"], true);
3927
3969
 
3928
3970
  return domUtils;
3929
3971