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