@whitesev/domutils 1.9.10 → 1.9.12

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.umd.js CHANGED
@@ -4,7 +4,7 @@
4
4
  (global = typeof globalThis !== 'undefined' ? globalThis : global || self, global.DOMUtils = factory());
5
5
  })(this, (function () { 'use strict';
6
6
 
7
- const version = "1.9.10";
7
+ const version = "1.9.12";
8
8
 
9
9
  class WindowApi {
10
10
  /** 默认的配置 */
@@ -1461,91 +1461,97 @@
1461
1461
  // 这是存在selector的情况
1462
1462
  listenerOption = getOption(args, 4, listenerOption);
1463
1463
  }
1464
- /**
1465
- * 如果是once,那么删除该监听和元素上的事件和监听
1466
- */
1467
- const checkOptionOnceToRemoveEventListener = ($el) => {
1468
- if (listenerOption.once) {
1469
- this.off($el, eventTypeList, selector, callback, option);
1470
- }
1471
- };
1472
1464
  $elList.forEach(($elItem) => {
1473
- /**
1474
- * 事件回调
1475
- * @param event
1476
- */
1477
- const handlerCallBack = function (event) {
1478
- let call_this = void 0;
1479
- let call_event = void 0;
1480
- let call_$selector = void 0;
1481
- let execCallback = false;
1482
- if (selectorList.length) {
1483
- // 存在子元素选择器
1484
- // 这时候的this和target都是子元素选择器的元素
1485
- let $target;
1486
- if (listenerOption.isComposedPath) {
1487
- // 可能为空
1488
- const composedPath = event.composedPath();
1489
- if (!composedPath.length && event.target) {
1490
- composedPath.push(event.target);
1491
- }
1492
- $target = composedPath[0];
1493
- }
1494
- else {
1495
- $target = event.target;
1496
- }
1497
- let $parent = $elItem;
1498
- if (CommonUtils.isWin($parent)) {
1499
- // window和document共用一个对象
1500
- // 这样就能处理子元素选择器无法匹配的问题
1501
- $parent = that.windowApi.document.documentElement;
1465
+ // 遍历事件名设置元素事件
1466
+ eventTypeList.forEach((eventName) => {
1467
+ /**
1468
+ * 如果是option.once,那么删除该监听和元素上的事件和监听
1469
+ */
1470
+ const checkOptionOnceToRemoveEventListener = () => {
1471
+ if (listenerOption.once) {
1472
+ this.off($elItem, eventName, selector, callback, option);
1502
1473
  }
1503
- const findValue = selectorList.find((selectors) => {
1504
- // 判断目标元素是否匹配选择器
1505
- if (that.matches($target, selectors)) {
1506
- // 当前目标可以被selector所匹配到
1507
- return true;
1474
+ };
1475
+ /**
1476
+ * 事件回调
1477
+ * @param event
1478
+ */
1479
+ const handlerCallBack = function (event) {
1480
+ let call_this = void 0;
1481
+ let call_event = void 0;
1482
+ let call_$selector = void 0;
1483
+ let execCallback = false;
1484
+ if (selectorList.length) {
1485
+ // 存在子元素选择器
1486
+ // 这时候的this和target都是子元素选择器的元素
1487
+ let $target;
1488
+ if (listenerOption.isComposedPath) {
1489
+ // 可能为空
1490
+ const composedPath = event.composedPath();
1491
+ if (!composedPath.length && event.target) {
1492
+ composedPath.push(event.target);
1493
+ }
1494
+ $target = composedPath[0];
1508
1495
  }
1509
- // 在上层与主元素之间寻找可以被selector所匹配到的
1510
- const $closestMatches = that.closest($target, selectors);
1511
- if ($closestMatches && $parent?.contains?.($closestMatches)) {
1512
- $target = $closestMatches;
1513
- return true;
1496
+ else {
1497
+ $target = event.target;
1514
1498
  }
1515
- return false;
1516
- });
1517
- if (findValue) {
1518
- // 这里尝试使用defineProperty修改event的target值
1519
- try {
1520
- OriginPrototype.Object.defineProperty(event, "target", {
1521
- get() {
1522
- return $target;
1523
- },
1524
- });
1525
- // oxlint-disable-next-line no-empty
1499
+ let $parent = $elItem;
1500
+ if (CommonUtils.isWin($parent)) {
1501
+ // window和document共用一个对象
1502
+ // 这样就能处理子元素选择器无法匹配的问题
1503
+ $parent = that.windowApi.document.documentElement;
1504
+ }
1505
+ const findValue = selectorList.find((selectors) => {
1506
+ // 判断目标元素是否匹配选择器
1507
+ if (that.matches($target, selectors)) {
1508
+ // 当前目标可以被selector所匹配到
1509
+ return true;
1510
+ }
1511
+ // 在上层与主元素之间寻找可以被selector所匹配到的
1512
+ const $closestMatches = that.closest($target, selectors);
1513
+ if ($closestMatches && $parent?.contains?.($closestMatches)) {
1514
+ $target = $closestMatches;
1515
+ return true;
1516
+ }
1517
+ return false;
1518
+ });
1519
+ if (findValue) {
1520
+ // 这里尝试使用defineProperty修改event的target值
1521
+ const originTarget = event.target;
1522
+ try {
1523
+ OriginPrototype.Object.defineProperty(event, "target", {
1524
+ get() {
1525
+ return $target;
1526
+ },
1527
+ });
1528
+ OriginPrototype.Object.defineProperty(event, "originTarget", {
1529
+ get() {
1530
+ return originTarget;
1531
+ },
1532
+ });
1533
+ // oxlint-disable-next-line no-empty
1534
+ }
1535
+ catch { }
1536
+ execCallback = true;
1537
+ call_this = $target;
1538
+ call_event = event;
1539
+ call_$selector = $target;
1526
1540
  }
1527
- catch { }
1541
+ }
1542
+ else {
1528
1543
  execCallback = true;
1529
- call_this = $target;
1544
+ call_this = $elItem;
1530
1545
  call_event = event;
1531
- call_$selector = $target;
1532
1546
  }
1533
- }
1534
- else {
1535
- execCallback = true;
1536
- call_this = $elItem;
1537
- call_event = event;
1538
- }
1539
- if (execCallback) {
1540
- const result = listenerCallBack.call(call_this, call_event, call_$selector);
1541
- checkOptionOnceToRemoveEventListener($elItem);
1542
- if (typeof result === "boolean" && !result) {
1543
- return false;
1547
+ if (execCallback) {
1548
+ const result = listenerCallBack.call(call_this, call_event, call_$selector);
1549
+ checkOptionOnceToRemoveEventListener();
1550
+ if (typeof result === "boolean" && !result) {
1551
+ return false;
1552
+ }
1544
1553
  }
1545
- }
1546
- };
1547
- // 遍历事件名设置元素事件
1548
- eventTypeList.forEach((eventName) => {
1554
+ };
1549
1555
  // add listener
1550
1556
  $elItem.addEventListener(eventName, handlerCallBack, listenerOption);
1551
1557
  // 获取对象上的事件
@@ -1658,31 +1664,34 @@
1658
1664
  const elementEvents = Reflect.get($elItem, GlobalData.domEventSymbol) || {};
1659
1665
  eventTypeList.forEach((eventName) => {
1660
1666
  const handlers = elementEvents[eventName] || [];
1661
- const filterHandler = typeof filter === "function" ? handlers.filter(filter) : handlers;
1662
- for (let index = 0; index < filterHandler.length; index++) {
1663
- const handler = filterHandler[index];
1667
+ // 过滤出需要删除的事件
1668
+ const handlersFiltered = typeof filter === "function" ? handlers.filter(filter) : handlers;
1669
+ for (let index = 0; index < handlersFiltered.length; index++) {
1670
+ const handler = handlersFiltered[index];
1671
+ // 过滤出的事件再根据下面的条件进行判断处理移除
1672
+ // 1. callback内存地址必须相同
1673
+ // 2. selector必须相同
1674
+ // 3. option.capture必须相同
1664
1675
  let flag = true;
1665
1676
  if (flag && listenerCallBack && handler.callback !== listenerCallBack) {
1666
- // callback不同
1667
1677
  flag = false;
1668
1678
  }
1669
1679
  if (flag && selectorList.length && Array.isArray(handler.selector)) {
1670
1680
  if (JSON.stringify(handler.selector) !== JSON.stringify(selectorList)) {
1671
- // 子元素选择器不同
1672
1681
  flag = false;
1673
1682
  }
1674
1683
  }
1675
1684
  if (flag &&
1676
1685
  typeof handler.option.capture === "boolean" &&
1677
1686
  listenerOption.capture !== handler.option.capture) {
1678
- // 事件的配置项不同
1679
1687
  flag = false;
1680
1688
  }
1681
1689
  if (flag) {
1682
1690
  $elItem.removeEventListener(eventName, handler.handlerCallBack, handler.option);
1683
- const findIndex = handlers.findIndex((item) => item === handler);
1684
- if (findIndex !== -1) {
1685
- handlers.splice(findIndex, 1);
1691
+ for (let i = handlers.length - 1; i >= 0; i--) {
1692
+ if (handlers[i] === handler) {
1693
+ handlers.splice(i, 1);
1694
+ }
1686
1695
  }
1687
1696
  }
1688
1697
  }