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