@whitesev/domutils 1.9.10 → 1.9.11

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.11";
2
2
 
3
3
  class WindowApi {
4
4
  /** 默认的配置 */
@@ -1455,91 +1455,91 @@ 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
+ try {
1516
+ OriginPrototype.Object.defineProperty(event, "target", {
1517
+ get() {
1518
+ return $target;
1519
+ },
1520
+ });
1521
+ // oxlint-disable-next-line no-empty
1522
+ }
1523
+ catch { }
1524
+ execCallback = true;
1525
+ call_this = $target;
1526
+ call_event = event;
1527
+ call_$selector = $target;
1520
1528
  }
1521
- catch { }
1529
+ }
1530
+ else {
1522
1531
  execCallback = true;
1523
- call_this = $target;
1532
+ call_this = $elItem;
1524
1533
  call_event = event;
1525
- call_$selector = $target;
1526
1534
  }
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;
1535
+ if (execCallback) {
1536
+ const result = listenerCallBack.call(call_this, call_event, call_$selector);
1537
+ checkOptionOnceToRemoveEventListener();
1538
+ if (typeof result === "boolean" && !result) {
1539
+ return false;
1540
+ }
1538
1541
  }
1539
- }
1540
- };
1541
- // 遍历事件名设置元素事件
1542
- eventTypeList.forEach((eventName) => {
1542
+ };
1543
1543
  // add listener
1544
1544
  $elItem.addEventListener(eventName, handlerCallBack, listenerOption);
1545
1545
  // 获取对象上的事件
@@ -1652,31 +1652,34 @@ class ElementEvent extends ElementAnimate {
1652
1652
  const elementEvents = Reflect.get($elItem, GlobalData.domEventSymbol) || {};
1653
1653
  eventTypeList.forEach((eventName) => {
1654
1654
  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];
1655
+ // 过滤出需要删除的事件
1656
+ const handlersFiltered = typeof filter === "function" ? handlers.filter(filter) : handlers;
1657
+ for (let index = 0; index < handlersFiltered.length; index++) {
1658
+ const handler = handlersFiltered[index];
1659
+ // 过滤出的事件再根据下面的条件进行判断处理移除
1660
+ // 1. callback内存地址必须相同
1661
+ // 2. selector必须相同
1662
+ // 3. option.capture必须相同
1658
1663
  let flag = true;
1659
1664
  if (flag && listenerCallBack && handler.callback !== listenerCallBack) {
1660
- // callback不同
1661
1665
  flag = false;
1662
1666
  }
1663
1667
  if (flag && selectorList.length && Array.isArray(handler.selector)) {
1664
1668
  if (JSON.stringify(handler.selector) !== JSON.stringify(selectorList)) {
1665
- // 子元素选择器不同
1666
1669
  flag = false;
1667
1670
  }
1668
1671
  }
1669
1672
  if (flag &&
1670
1673
  typeof handler.option.capture === "boolean" &&
1671
1674
  listenerOption.capture !== handler.option.capture) {
1672
- // 事件的配置项不同
1673
1675
  flag = false;
1674
1676
  }
1675
1677
  if (flag) {
1676
1678
  $elItem.removeEventListener(eventName, handler.handlerCallBack, handler.option);
1677
- const findIndex = handlers.findIndex((item) => item === handler);
1678
- if (findIndex !== -1) {
1679
- handlers.splice(findIndex, 1);
1679
+ for (let i = handlers.length - 1; i >= 0; i--) {
1680
+ if (handlers[i] === handler) {
1681
+ handlers.splice(i, 1);
1682
+ }
1680
1683
  }
1681
1684
  }
1682
1685
  }