0
0
mirror of https://github.com/twbs/bootstrap.git synced 2024-12-12 00:08:59 +01:00
Bootstrap/js/dist/dom/selectorengine.js

284 lines
7.5 KiB
JavaScript
Raw Normal View History

2019-03-01 17:31:34 +01:00
/*!
* 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) {
2019-04-18 13:47:52 +02:00
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';
2019-03-01 17:31:34 +01:00
/**
* --------------------------------------------------------------------------
* Bootstrap (v4.3.1): util/index.js
* Licensed under MIT (https://github.com/twbs/bootstrap/blob/master/LICENSE)
* --------------------------------------------------------------------------
*/
2019-04-18 13:47:52 +02:00
var MAX_UID = 1000000;
2019-03-11 16:13:30 +01:00
var _window = window,
jQuery = _window.jQuery; // Shoutout AngusCroll (https://goo.gl/pxwQGp)
2019-04-18 13:47:52 +02:00
/**
* --------------------------------------------------------------------------
* 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;
};
2019-03-01 17:31:34 +01:00
var makeArray = function makeArray(nodeList) {
if (!nodeList) {
return [];
}
return [].slice.call(nodeList);
};
2019-04-18 13:47:52 +02:00
/* 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;
};
}
2019-03-01 17:31:34 +01:00
/**
* --------------------------------------------------------------------------
* 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 = {
2019-04-18 13:47:52 +02:00
matches: function matches$1(element, selector) {
return matches.call(element, selector);
2019-03-01 17:31:34 +01:00
},
2019-04-18 13:47:52 +02:00
find: function find$1(selector, element) {
2019-03-01 17:31:34 +01:00
if (element === void 0) {
element = document.documentElement;
}
if (typeof selector !== 'string') {
return null;
}
2019-04-18 13:47:52 +02:00
return find.call(element, selector);
2019-03-01 17:31:34 +01:00
},
2019-04-18 13:47:52 +02:00
findOne: function findOne$1(selector, element) {
2019-03-01 17:31:34 +01:00
if (element === void 0) {
element = document.documentElement;
}
if (typeof selector !== 'string') {
return null;
}
2019-04-18 13:47:52 +02:00
return findOne.call(element, selector);
2019-03-01 17:31:34 +01:00
},
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;
},
2019-04-18 13:47:52 +02:00
closest: function closest$1(element, selector) {
2019-03-01 17:31:34 +01:00
if (typeof selector !== 'string') {
return null;
}
2019-04-18 13:47:52 +02:00
return closest.call(element, selector);
2019-03-01 17:31:34 +01:00
},
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