1
0
Fork 0
mirror of https://github.com/twbs/bootstrap.git synced 2022-11-09 12:25:43 -05:00
twbs--bootstrap/js/dist/dom/selector-engine.js

129 lines
3.7 KiB
JavaScript
Raw Normal View History

2019-03-01 11:31:34 -05:00
/*!
2021-08-04 11:41:51 -04:00
* Bootstrap selector-engine.js v5.1.0 (https://getbootstrap.com/)
* Copyright 2011-2021 The Bootstrap Authors (https://github.com/twbs/bootstrap/graphs/contributors)
2020-06-16 14:50:01 -04:00
* Licensed under MIT (https://github.com/twbs/bootstrap/blob/main/LICENSE)
2019-03-01 11:31:34 -05:00
*/
(function (global, factory) {
2020-11-11 12:07:37 -05:00
typeof exports === 'object' && typeof module !== 'undefined' ? module.exports = factory() :
typeof define === 'function' && define.amd ? define(factory) :
(global = typeof globalThis !== 'undefined' ? globalThis : global || self, global.SelectorEngine = factory());
}(this, (function () { 'use strict';
2019-03-01 11:31:34 -05:00
/**
* --------------------------------------------------------------------------
2021-08-04 11:41:51 -04:00
* Bootstrap (v5.1.0): util/index.js
2020-06-16 14:50:01 -04:00
* Licensed under MIT (https://github.com/twbs/bootstrap/blob/main/LICENSE)
2019-03-01 11:31:34 -05:00
* --------------------------------------------------------------------------
*/
2020-11-11 12:07:37 -05:00
2021-08-04 11:41:51 -04:00
const isElement = obj => {
if (!obj || typeof obj !== 'object') {
return false;
}
if (typeof obj.jquery !== 'undefined') {
obj = obj[0];
}
return typeof obj.nodeType !== 'undefined';
};
const isVisible = element => {
if (!isElement(element) || element.getClientRects().length === 0) {
return false;
}
return getComputedStyle(element).getPropertyValue('visibility') === 'visible';
};
const isDisabled = element => {
if (!element || element.nodeType !== Node.ELEMENT_NODE) {
return true;
}
if (element.classList.contains('disabled')) {
return true;
}
if (typeof element.disabled !== 'undefined') {
return element.disabled;
}
return element.hasAttribute('disabled') && element.getAttribute('disabled') !== 'false';
};
2019-03-01 11:31:34 -05:00
/**
2021-08-04 11:41:51 -04:00
* --------------------------------------------------------------------------
* Bootstrap (v5.1.0): dom/selector-engine.js
* Licensed under MIT (https://github.com/twbs/bootstrap/blob/main/LICENSE)
* --------------------------------------------------------------------------
2019-03-01 11:31:34 -05:00
*/
2021-03-23 12:26:54 -04:00
const NODE_TEXT = 3;
const SelectorEngine = {
find(selector, element = document.documentElement) {
return [].concat(...Element.prototype.querySelectorAll.call(element, selector));
2019-03-01 11:31:34 -05:00
},
2021-03-23 12:26:54 -04:00
findOne(selector, element = document.documentElement) {
2020-11-11 12:07:37 -05:00
return Element.prototype.querySelector.call(element, selector);
2019-03-01 11:31:34 -05:00
},
2020-03-28 06:29:08 -04:00
2021-03-23 12:26:54 -04:00
children(element, selector) {
return [].concat(...element.children).filter(child => child.matches(selector));
2019-03-01 11:31:34 -05:00
},
2021-03-23 12:26:54 -04:00
parents(element, selector) {
const parents = [];
let ancestor = element.parentNode;
2019-03-01 11:31:34 -05:00
while (ancestor && ancestor.nodeType === Node.ELEMENT_NODE && ancestor.nodeType !== NODE_TEXT) {
if (ancestor.matches(selector)) {
2019-03-01 11:31:34 -05:00
parents.push(ancestor);
}
ancestor = ancestor.parentNode;
}
return parents;
},
2021-03-23 12:26:54 -04:00
prev(element, selector) {
let previous = element.previousElementSibling;
2020-03-28 06:29:08 -04:00
while (previous) {
if (previous.matches(selector)) {
return [previous];
}
previous = previous.previousElementSibling;
}
2019-03-01 11:31:34 -05:00
2020-03-28 06:29:08 -04:00
return [];
},
2021-03-23 12:26:54 -04:00
next(element, selector) {
let next = element.nextElementSibling;
2020-03-28 06:29:08 -04:00
while (next) {
if (next.matches(selector)) {
2020-03-28 06:29:08 -04:00
return [next];
2019-03-01 11:31:34 -05:00
}
2020-03-28 06:29:08 -04:00
next = next.nextElementSibling;
2019-03-01 11:31:34 -05:00
}
2020-03-28 06:29:08 -04:00
return [];
2021-08-04 11:41:51 -04:00
},
focusableChildren(element) {
const focusables = ['a', 'button', 'input', 'textarea', 'select', 'details', '[tabindex]', '[contenteditable="true"]'].map(selector => `${selector}:not([tabindex^="-"])`).join(', ');
return this.find(focusables, element).filter(el => !isDisabled(el) && isVisible(el));
2019-03-01 11:31:34 -05:00
}
2021-03-23 12:26:54 -04:00
2019-03-01 11:31:34 -05:00
};
return SelectorEngine;
2019-11-08 03:11:23 -05:00
})));
//# sourceMappingURL=selector-engine.js.map