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