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