@whitesev/domutils 1.9.9 → 1.9.10

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.umd.js CHANGED
@@ -4,7 +4,7 @@
4
4
  (global = typeof globalThis !== 'undefined' ? globalThis : global || self, global.DOMUtils = factory());
5
5
  })(this, (function () { 'use strict';
6
6
 
7
- const version = "1.9.9";
7
+ const version = "1.9.10";
8
8
 
9
9
  class WindowApi {
10
10
  /** 默认的配置 */
@@ -1384,7 +1384,7 @@
1384
1384
  * @param startIndex
1385
1385
  * @param option
1386
1386
  */
1387
- function getOption(args, startIndex, option) {
1387
+ const getOption = function (args, startIndex, option) {
1388
1388
  const currentParam = args[startIndex];
1389
1389
  if (typeof currentParam === "boolean") {
1390
1390
  option.capture = currentParam;
@@ -1406,7 +1406,7 @@
1406
1406
  option.isComposedPath = currentParam.isComposedPath;
1407
1407
  }
1408
1408
  return option;
1409
- }
1409
+ };
1410
1410
  const that = this;
1411
1411
  // eslint-disable-next-line prefer-rest-params
1412
1412
  const args = arguments;
@@ -1464,39 +1464,52 @@
1464
1464
  /**
1465
1465
  * 如果是once,那么删除该监听和元素上的事件和监听
1466
1466
  */
1467
- const checkOptionOnceToRemoveEventListener = () => {
1467
+ const checkOptionOnceToRemoveEventListener = ($el) => {
1468
1468
  if (listenerOption.once) {
1469
- that.off(element, eventType, selector, callback, option);
1469
+ this.off($el, eventTypeList, selector, callback, option);
1470
1470
  }
1471
1471
  };
1472
- $elList.forEach((elementItem) => {
1472
+ $elList.forEach(($elItem) => {
1473
1473
  /**
1474
1474
  * 事件回调
1475
1475
  * @param event
1476
1476
  */
1477
1477
  const handlerCallBack = function (event) {
1478
+ let call_this = void 0;
1479
+ let call_event = void 0;
1480
+ let call_$selector = void 0;
1481
+ let execCallback = false;
1478
1482
  if (selectorList.length) {
1479
- /* 存在子元素选择器 */
1483
+ // 存在子元素选择器
1480
1484
  // 这时候的this和target都是子元素选择器的元素
1481
- let eventTarget = listenerOption.isComposedPath
1482
- ? event.composedPath()[0]
1483
- : event.target;
1484
- let totalParent = elementItem;
1485
- if (CommonUtils.isWin(totalParent)) {
1486
- if (totalParent === that.windowApi.document) {
1487
- totalParent = that.windowApi.document.documentElement;
1485
+ let $target;
1486
+ if (listenerOption.isComposedPath) {
1487
+ // 可能为空
1488
+ const composedPath = event.composedPath();
1489
+ if (!composedPath.length && event.target) {
1490
+ composedPath.push(event.target);
1488
1491
  }
1492
+ $target = composedPath[0];
1493
+ }
1494
+ else {
1495
+ $target = event.target;
1496
+ }
1497
+ let $parent = $elItem;
1498
+ if (CommonUtils.isWin($parent)) {
1499
+ // window和document共用一个对象
1500
+ // 这样就能处理子元素选择器无法匹配的问题
1501
+ $parent = that.windowApi.document.documentElement;
1489
1502
  }
1490
- const findValue = selectorList.find((selectorItem) => {
1503
+ const findValue = selectorList.find((selectors) => {
1491
1504
  // 判断目标元素是否匹配选择器
1492
- if (that.matches(eventTarget, selectorItem)) {
1493
- /* 当前目标可以被selector所匹配到 */
1505
+ if (that.matches($target, selectors)) {
1506
+ // 当前目标可以被selector所匹配到
1494
1507
  return true;
1495
1508
  }
1496
- /* 在上层与主元素之间寻找可以被selector所匹配到的 */
1497
- const $closestMatches = that.closest(eventTarget, selectorItem);
1498
- if ($closestMatches && totalParent?.contains?.($closestMatches)) {
1499
- eventTarget = $closestMatches;
1509
+ // 在上层与主元素之间寻找可以被selector所匹配到的
1510
+ const $closestMatches = that.closest($target, selectors);
1511
+ if ($closestMatches && $parent?.contains?.($closestMatches)) {
1512
+ $target = $closestMatches;
1500
1513
  return true;
1501
1514
  }
1502
1515
  return false;
@@ -1506,29 +1519,38 @@
1506
1519
  try {
1507
1520
  OriginPrototype.Object.defineProperty(event, "target", {
1508
1521
  get() {
1509
- return eventTarget;
1522
+ return $target;
1510
1523
  },
1511
1524
  });
1525
+ // oxlint-disable-next-line no-empty
1512
1526
  }
1513
- catch {
1514
- // TODO
1515
- }
1516
- listenerCallBack.call(eventTarget, event, eventTarget);
1517
- checkOptionOnceToRemoveEventListener();
1527
+ catch { }
1528
+ execCallback = true;
1529
+ call_this = $target;
1530
+ call_event = event;
1531
+ call_$selector = $target;
1518
1532
  }
1519
1533
  }
1520
1534
  else {
1521
- // 这时候的this指向监听的元素
1522
- listenerCallBack.call(elementItem, event);
1523
- checkOptionOnceToRemoveEventListener();
1535
+ execCallback = true;
1536
+ call_this = $elItem;
1537
+ call_event = event;
1538
+ }
1539
+ if (execCallback) {
1540
+ const result = listenerCallBack.call(call_this, call_event, call_$selector);
1541
+ checkOptionOnceToRemoveEventListener($elItem);
1542
+ if (typeof result === "boolean" && !result) {
1543
+ return false;
1544
+ }
1524
1545
  }
1525
1546
  };
1526
- /* 遍历事件名设置元素事件 */
1547
+ // 遍历事件名设置元素事件
1527
1548
  eventTypeList.forEach((eventName) => {
1528
- elementItem.addEventListener(eventName, handlerCallBack, listenerOption);
1529
- /* 获取对象上的事件 */
1530
- const elementEvents = Reflect.get(elementItem, GlobalData.domEventSymbol) || {};
1531
- /* 初始化对象上的xx事件 */
1549
+ // add listener
1550
+ $elItem.addEventListener(eventName, handlerCallBack, listenerOption);
1551
+ // 获取对象上的事件
1552
+ const elementEvents = Reflect.get($elItem, GlobalData.domEventSymbol) || {};
1553
+ // 初始化对象上的xx事件
1532
1554
  elementEvents[eventName] = elementEvents[eventName] || [];
1533
1555
  elementEvents[eventName].push({
1534
1556
  selector: selectorList,
@@ -1536,8 +1558,8 @@
1536
1558
  handlerCallBack: handlerCallBack,
1537
1559
  callback: listenerCallBack,
1538
1560
  });
1539
- /* 覆盖事件 */
1540
- Reflect.set(elementItem, GlobalData.domEventSymbol, elementEvents);
1561
+ // 覆盖事件
1562
+ Reflect.set($elItem, GlobalData.domEventSymbol, elementEvents);
1541
1563
  });
1542
1564
  });
1543
1565
  return {
@@ -1551,7 +1573,7 @@
1551
1573
  /**
1552
1574
  * 主动触发事件
1553
1575
  * @param extraDetails 赋予触发的Event的额外属性,如果是Event类型,那么将自动代替默认new的Event对象
1554
- * @param useDispatchToTriggerEvent 是否使用dispatchEvent来触发事件,默认true,如果为false,则直接调用callback,但是这种会让使用了$selector的没有值
1576
+ * @param useDispatchToTriggerEvent 是否使用dispatchEvent来触发事件,默认true,如果为false,则直接调用callback,但是这种会让使用了`$selector`的没有值
1555
1577
  */
1556
1578
  emit: (extraDetails, useDispatchToTriggerEvent) => {
1557
1579
  that.emit($elList, eventTypeList, extraDetails, useDispatchToTriggerEvent);
@@ -1565,7 +1587,7 @@
1565
1587
  * @param startIndex
1566
1588
  * @param option
1567
1589
  */
1568
- function getOption(args1, startIndex, option) {
1590
+ const getOption = function (args1, startIndex, option) {
1569
1591
  const currentParam = args1[startIndex];
1570
1592
  if (typeof currentParam === "boolean") {
1571
1593
  option.capture = currentParam;
@@ -1574,7 +1596,7 @@
1574
1596
  option.capture = currentParam.capture;
1575
1597
  }
1576
1598
  return option;
1577
- }
1599
+ };
1578
1600
  const that = this;
1579
1601
  // eslint-disable-next-line prefer-rest-params
1580
1602
  const args = arguments;
@@ -1632,7 +1654,7 @@
1632
1654
  filter = option;
1633
1655
  }
1634
1656
  $elList.forEach(($elItem) => {
1635
- /* 获取对象上的事件 */
1657
+ // 获取对象上的事件
1636
1658
  const elementEvents = Reflect.get($elItem, GlobalData.domEventSymbol) || {};
1637
1659
  eventTypeList.forEach((eventName) => {
1638
1660
  const handlers = elementEvents[eventName] || [];
@@ -1665,7 +1687,7 @@
1665
1687
  }
1666
1688
  }
1667
1689
  if (handlers.length === 0) {
1668
- /* 如果没有任意的handler,那么删除该属性 */
1690
+ // 如果没有任意的handler,那么删除该属性
1669
1691
  CommonUtils.delete(elementEvents, eventType);
1670
1692
  }
1671
1693
  });
@@ -2426,11 +2448,11 @@
2426
2448
  * 阻止事件的默认行为发生,并阻止事件传播
2427
2449
  */
2428
2450
  const stopEvent = (event, onlyStopPropagation) => {
2451
+ // 停止事件的传播,阻止它继续向更上层的元素冒泡,事件将不会再传播给其他的元素
2452
+ event?.stopPropagation();
2453
+ // 阻止事件传播,并且还能阻止元素上的其他事件处理程序被触发
2454
+ event?.stopImmediatePropagation();
2429
2455
  if (typeof onlyStopPropagation === "boolean" && onlyStopPropagation) {
2430
- // 停止事件的传播,阻止它继续向更上层的元素冒泡,事件将不会再传播给其他的元素
2431
- event?.stopPropagation();
2432
- // 阻止事件传播,并且还能阻止元素上的其他事件处理程序被触发
2433
- event?.stopImmediatePropagation();
2434
2456
  return;
2435
2457
  }
2436
2458
  // 阻止事件的默认行为发生。例如,当点击一个链接时,浏览器会默认打开链接的URL,或者在输入框内输入文字