0
0
mirror of https://github.com/twbs/bootstrap.git synced 2024-12-10 22:24:19 +01:00
Bootstrap/js/dist/dom/selectorengine.js
2019-04-18 14:47:52 +03:00

284 lines
7.5 KiB
JavaScript

/*!
* Bootstrap selectorengine.js v4.3.1 (https://getbootstrap.com/)
* Copyright 2011-2019 The Bootstrap Authors (https://github.com/twbs/bootstrap/graphs/contributors)
* Licensed under MIT (https://github.com/twbs/bootstrap/blob/master/LICENSE)
*/
(function (global, factory) {
typeof exports === 'object' && typeof module !== 'undefined' ? module.exports = factory() :
typeof define === 'function' && define.amd ? define(factory) :
(global = global || self, global.SelectorEngine = factory());
}(this, function () { 'use strict';
/**
* --------------------------------------------------------------------------
* Bootstrap (v4.3.1): util/index.js
* Licensed under MIT (https://github.com/twbs/bootstrap/blob/master/LICENSE)
* --------------------------------------------------------------------------
*/
var MAX_UID = 1000000;
var _window = window,
jQuery = _window.jQuery; // Shoutout AngusCroll (https://goo.gl/pxwQGp)
/**
* --------------------------------------------------------------------------
* Public Util Api
* --------------------------------------------------------------------------
*/
var getUID = function getUID(prefix) {
do {
// eslint-disable-next-line no-bitwise
prefix += ~~(Math.random() * MAX_UID); // "~~" acts like a faster Math.floor() here
} while (document.getElementById(prefix));
return prefix;
};
var makeArray = function makeArray(nodeList) {
if (!nodeList) {
return [];
}
return [].slice.call(nodeList);
};
/* istanbul ignore file */
var _Element$prototype = Element.prototype,
matches = _Element$prototype.matches,
closest = _Element$prototype.closest;
var find = Element.prototype.querySelectorAll;
var findOne = Element.prototype.querySelector;
var createCustomEvent = function createCustomEvent(eventName, params) {
var cEvent = new CustomEvent(eventName, params);
return cEvent;
};
if (typeof window.CustomEvent !== 'function') {
createCustomEvent = function createCustomEvent(eventName, params) {
params = params || {
bubbles: false,
cancelable: false,
detail: null
};
var evt = document.createEvent('CustomEvent');
evt.initCustomEvent(eventName, params.bubbles, params.cancelable, params.detail);
return evt;
};
}
var workingDefaultPrevented = function () {
var e = document.createEvent('CustomEvent');
e.initEvent('Bootstrap', true, true);
e.preventDefault();
return e.defaultPrevented;
}();
if (!workingDefaultPrevented) {
var origPreventDefault = Event.prototype.preventDefault;
Event.prototype.preventDefault = function () {
if (!this.cancelable) {
return;
}
origPreventDefault.call(this);
Object.defineProperty(this, 'defaultPrevented', {
get: function get() {
return true;
},
configurable: true
});
};
} // MSEdge resets defaultPrevented flag upon dispatchEvent call if at least one listener is attached
var defaultPreventedPreservedOnDispatch = function () {
var e = createCustomEvent('Bootstrap', {
cancelable: true
});
var element = document.createElement('div');
element.addEventListener('Bootstrap', function () {
return null;
});
e.preventDefault();
element.dispatchEvent(e);
return e.defaultPrevented;
}();
if (!matches) {
matches = Element.prototype.msMatchesSelector || Element.prototype.webkitMatchesSelector;
}
if (!closest) {
closest = function closest(selector) {
var element = this;
do {
if (matches.call(element, selector)) {
return element;
}
element = element.parentElement || element.parentNode;
} while (element !== null && element.nodeType === 1);
return null;
};
}
var scopeSelectorRegex = /:scope\b/;
var supportScopeQuery = function () {
var element = document.createElement('div');
try {
element.querySelectorAll(':scope *');
} catch (error) {
return false;
}
return true;
}();
if (!supportScopeQuery) {
find = function find(selector) {
if (!scopeSelectorRegex.test(selector)) {
return this.querySelectorAll(selector);
}
var hasId = Boolean(this.id);
if (!hasId) {
this.id = getUID('scope');
}
var nodeList = null;
try {
selector = selector.replace(scopeSelectorRegex, "#" + this.id);
nodeList = this.querySelectorAll(selector);
} finally {
if (!hasId) {
this.removeAttribute('id');
}
}
return nodeList;
};
findOne = function findOne(selector) {
if (!scopeSelectorRegex.test(selector)) {
return this.querySelector(selector);
}
var matches = find.call(this, selector);
if (typeof matches[0] !== 'undefined') {
return matches[0];
}
return null;
};
}
/**
* --------------------------------------------------------------------------
* Bootstrap (v4.3.1): dom/selectorEngine.js
* Licensed under MIT (https://github.com/twbs/bootstrap/blob/master/LICENSE)
* --------------------------------------------------------------------------
*/
/**
* ------------------------------------------------------------------------
* Constants
* ------------------------------------------------------------------------
*/
var NODE_TEXT = 3;
var SelectorEngine = {
matches: function matches$1(element, selector) {
return matches.call(element, selector);
},
find: function find$1(selector, element) {
if (element === void 0) {
element = document.documentElement;
}
if (typeof selector !== 'string') {
return null;
}
return find.call(element, selector);
},
findOne: function findOne$1(selector, element) {
if (element === void 0) {
element = document.documentElement;
}
if (typeof selector !== 'string') {
return null;
}
return findOne.call(element, selector);
},
children: function children(element, selector) {
var _this = this;
if (typeof selector !== 'string') {
return null;
}
var children = makeArray(element.children);
return children.filter(function (child) {
return _this.matches(child, selector);
});
},
parents: function parents(element, selector) {
if (typeof selector !== 'string') {
return null;
}
var parents = [];
var ancestor = element.parentNode;
while (ancestor && ancestor.nodeType === Node.ELEMENT_NODE && ancestor.nodeType !== NODE_TEXT) {
if (this.matches(ancestor, selector)) {
parents.push(ancestor);
}
ancestor = ancestor.parentNode;
}
return parents;
},
closest: function closest$1(element, selector) {
if (typeof selector !== 'string') {
return null;
}
return closest.call(element, selector);
},
prev: function prev(element, selector) {
if (typeof selector !== 'string') {
return null;
}
var siblings = [];
var previous = element.previousSibling;
while (previous && previous.nodeType === Node.ELEMENT_NODE && previous.nodeType !== NODE_TEXT) {
if (this.matches(previous, selector)) {
siblings.push(previous);
}
previous = previous.previousSibling;
}
return siblings;
}
};
return SelectorEngine;
}));
//# sourceMappingURL=selectorengine.js.map