selector-engine.js 2.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  1. /*!
  2. * Bootstrap selector-engine.js v5.0.2 (https://getbootstrap.com/)
  3. * Copyright 2011-2021 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() :
  8. typeof define === 'function' && define.amd ? define(factory) :
  9. (global = typeof globalThis !== 'undefined' ? globalThis : global || self, global.SelectorEngine = factory());
  10. }(this, (function () { 'use strict';
  11. /**
  12. * --------------------------------------------------------------------------
  13. * Bootstrap (v5.0.2): dom/selector-engine.js
  14. * Licensed under MIT (https://github.com/twbs/bootstrap/blob/main/LICENSE)
  15. * --------------------------------------------------------------------------
  16. */
  17. /**
  18. * ------------------------------------------------------------------------
  19. * Constants
  20. * ------------------------------------------------------------------------
  21. */
  22. const NODE_TEXT = 3;
  23. const SelectorEngine = {
  24. find(selector, element = document.documentElement) {
  25. return [].concat(...Element.prototype.querySelectorAll.call(element, selector));
  26. },
  27. findOne(selector, element = document.documentElement) {
  28. return Element.prototype.querySelector.call(element, selector);
  29. },
  30. children(element, selector) {
  31. return [].concat(...element.children).filter(child => child.matches(selector));
  32. },
  33. parents(element, selector) {
  34. const parents = [];
  35. let ancestor = element.parentNode;
  36. while (ancestor && ancestor.nodeType === Node.ELEMENT_NODE && ancestor.nodeType !== NODE_TEXT) {
  37. if (ancestor.matches(selector)) {
  38. parents.push(ancestor);
  39. }
  40. ancestor = ancestor.parentNode;
  41. }
  42. return parents;
  43. },
  44. prev(element, selector) {
  45. let previous = element.previousElementSibling;
  46. while (previous) {
  47. if (previous.matches(selector)) {
  48. return [previous];
  49. }
  50. previous = previous.previousElementSibling;
  51. }
  52. return [];
  53. },
  54. next(element, selector) {
  55. let next = element.nextElementSibling;
  56. while (next) {
  57. if (next.matches(selector)) {
  58. return [next];
  59. }
  60. next = next.nextElementSibling;
  61. }
  62. return [];
  63. }
  64. };
  65. return SelectorEngine;
  66. })));
  67. //# sourceMappingURL=selector-engine.js.map