You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

104 lines
4.1 KiB

8 months ago
  1. /*!
  2. * Bootstrap selector-engine.js v5.3.3 (https://getbootstrap.com/)
  3. * Copyright 2011-2024 The Bootstrap Authors (https://github.com/twbs/bootstrap/graphs/contributors)
  4. * Licensed under MIT (https://github.com/twbs/bootstrap/blob/main/LICENSE)
  5. */
  6. (function (global, factory) {
  7. typeof exports === 'object' && typeof module !== 'undefined' ? module.exports = factory(require('../util/index.js')) :
  8. typeof define === 'function' && define.amd ? define(['../util/index'], factory) :
  9. (global = typeof globalThis !== 'undefined' ? globalThis : global || self, global.SelectorEngine = factory(global.Index));
  10. })(this, (function (index_js) { 'use strict';
  11. /**
  12. * --------------------------------------------------------------------------
  13. * Bootstrap dom/selector-engine.js
  14. * Licensed under MIT (https://github.com/twbs/bootstrap/blob/main/LICENSE)
  15. * --------------------------------------------------------------------------
  16. */
  17. const getSelector = element => {
  18. let selector = element.getAttribute('data-bs-target');
  19. if (!selector || selector === '#') {
  20. let hrefAttribute = element.getAttribute('href');
  21. // The only valid content that could double as a selector are IDs or classes,
  22. // so everything starting with `#` or `.`. If a "real" URL is used as the selector,
  23. // `document.querySelector` will rightfully complain it is invalid.
  24. // See https://github.com/twbs/bootstrap/issues/32273
  25. if (!hrefAttribute || !hrefAttribute.includes('#') && !hrefAttribute.startsWith('.')) {
  26. return null;
  27. }
  28. // Just in case some CMS puts out a full URL with the anchor appended
  29. if (hrefAttribute.includes('#') && !hrefAttribute.startsWith('#')) {
  30. hrefAttribute = `#${hrefAttribute.split('#')[1]}`;
  31. }
  32. selector = hrefAttribute && hrefAttribute !== '#' ? hrefAttribute.trim() : null;
  33. }
  34. return selector ? selector.split(',').map(sel => index_js.parseSelector(sel)).join(',') : null;
  35. };
  36. const SelectorEngine = {
  37. find(selector, element = document.documentElement) {
  38. return [].concat(...Element.prototype.querySelectorAll.call(element, selector));
  39. },
  40. findOne(selector, element = document.documentElement) {
  41. return Element.prototype.querySelector.call(element, selector);
  42. },
  43. children(element, selector) {
  44. return [].concat(...element.children).filter(child => child.matches(selector));
  45. },
  46. parents(element, selector) {
  47. const parents = [];
  48. let ancestor = element.parentNode.closest(selector);
  49. while (ancestor) {
  50. parents.push(ancestor);
  51. ancestor = ancestor.parentNode.closest(selector);
  52. }
  53. return parents;
  54. },
  55. prev(element, selector) {
  56. let previous = element.previousElementSibling;
  57. while (previous) {
  58. if (previous.matches(selector)) {
  59. return [previous];
  60. }
  61. previous = previous.previousElementSibling;
  62. }
  63. return [];
  64. },
  65. // TODO: this is now unused; remove later along with prev()
  66. next(element, selector) {
  67. let next = element.nextElementSibling;
  68. while (next) {
  69. if (next.matches(selector)) {
  70. return [next];
  71. }
  72. next = next.nextElementSibling;
  73. }
  74. return [];
  75. },
  76. focusableChildren(element) {
  77. const focusables = ['a', 'button', 'input', 'textarea', 'select', 'details', '[tabindex]', '[contenteditable="true"]'].map(selector => `${selector}:not([tabindex^="-"])`).join(',');
  78. return this.find(focusables, element).filter(el => !index_js.isDisabled(el) && index_js.isVisible(el));
  79. },
  80. getSelectorFromElement(element) {
  81. const selector = getSelector(element);
  82. if (selector) {
  83. return SelectorEngine.findOne(selector) ? selector : null;
  84. }
  85. return null;
  86. },
  87. getElementFromSelector(element) {
  88. const selector = getSelector(element);
  89. return selector ? SelectorEngine.findOne(selector) : null;
  90. },
  91. getMultipleElementsFromSelector(element) {
  92. const selector = getSelector(element);
  93. return selector ? SelectorEngine.find(selector) : [];
  94. }
  95. };
  96. return SelectorEngine;
  97. }));
  98. //# sourceMappingURL=selector-engine.js.map