diff --git a/js/src/dom/polyfill.js b/js/src/dom/polyfill.js index 834a1d6610..8644dc7022 100644 --- a/js/src/dom/polyfill.js +++ b/js/src/dom/polyfill.js @@ -23,40 +23,13 @@ const Polyfill = (() => { return e.defaultPrevented })() - // Event constructor shim - if (!window.Event || typeof window.Event !== 'function') { - const origEvent = window.Event - window.Event = (inType, params) => { - params = params || {} - const e = document.createEvent('Event') - e.initEvent(inType, Boolean(params.bubbles), Boolean(params.cancelable)) - return e - } - window.Event.prototype = origEvent.prototype - } - - // closest polyfill (see: https://mzl.la/2vXggaI) - let closest - if (!Element.prototype.closest) { - const nodeText = 3 - closest = (element, selector) => { - let ancestor = element - do { - if (ancestor.matches(selector)) { - return ancestor - } - - ancestor = ancestor.parentElement - } while (ancestor !== null && ancestor.nodeType === Node.ELEMENT_NODE && ancestor.nodeType !== nodeText) - - return null - } - } else { - closest = (element, selector) => element.closest(selector) - } + let find = Element.prototype.querySelectorAll + let findOne = Element.prototype.querySelector + const scopeSelectorRegex = /:scope\b/ const supportScopeQuery = (() => { const element = document.createElement('div') + try { element.querySelectorAll(':scope *') } catch (e) { @@ -66,10 +39,6 @@ const Polyfill = (() => { return true })() - const scopeSelectorRegex = /:scope\b/ - let find = Element.prototype.querySelectorAll - let findOne = Element.prototype.querySelector - if (!supportScopeQuery) { find = function (selector) { if (!scopeSelectorRegex.test(selector)) { @@ -77,6 +46,7 @@ const Polyfill = (() => { } const hasId = Boolean(this.id) + if (!hasId) { this.id = Util.getUID('scope') } @@ -100,6 +70,7 @@ const Polyfill = (() => { } const matches = find.call(this, selector) + if (typeof matches[0] !== 'undefined') { return matches[0] } @@ -108,37 +79,8 @@ const Polyfill = (() => { } } - if (typeof Object.assign !== 'function') { - Object.defineProperty(Object, 'assign', { - value: (target, ...args) => { - if (target === null || typeof target === 'undefined') { - throw new TypeError('Cannot convert undefined or null to object') - } - - const to = Object(target) - - for (let index = 1; index < args.length; index++) { - const nextSource = args[index] - - if (nextSource !== null || !nextSource) { - for (const nextKey in nextSource) { - if (Object.prototype.hasOwnProperty.call(nextSource, nextKey)) { - to[nextKey] = nextSource[nextKey] - } - } - } - } - return to - }, - writable: true, - configurable: true - }) - } - return { defaultPreventedPreservedOnDispatch, - focusIn: typeof window.onfocusin === 'undefined', - closest, find, findOne } diff --git a/js/src/dom/selectorEngine.js b/js/src/dom/selectorEngine.js index 52e2b93d61..95b5a9fb50 100644 --- a/js/src/dom/selectorEngine.js +++ b/js/src/dom/selectorEngine.js @@ -1,6 +1,3 @@ -import Polyfill from './polyfill' -import Util from '../util' - /** * -------------------------------------------------------------------------- * Bootstrap (v4.3.1): dom/selectorEngine.js @@ -8,21 +5,22 @@ import Util from '../util' * -------------------------------------------------------------------------- */ +import Polyfill from './polyfill' +import Util from '../util' + /** * ------------------------------------------------------------------------ * Constants * ------------------------------------------------------------------------ */ -const closest = Polyfill.closest -const matchesFn = Element.prototype.matches -const find = Polyfill.find +const findFn = Polyfill.find const findOne = Polyfill.findOne -const nodeText = 3 +const NODE_TEXT = 3 const SelectorEngine = { matches(element, selector) { - return matchesFn.call(element, selector) + return element.matches(selector) }, find(selector, element = document.documentElement) { @@ -30,7 +28,7 @@ const SelectorEngine = { return null } - return find.call(element, selector) + return findFn.call(element, selector) }, findOne(selector, element = document.documentElement) { @@ -47,6 +45,7 @@ const SelectorEngine = { } const children = Util.makeArray(element.children) + return children.filter((child) => this.matches(child, selector)) }, @@ -56,9 +55,9 @@ const SelectorEngine = { } const parents = [] - let ancestor = element.parentNode - while (ancestor && ancestor.nodeType === Node.ELEMENT_NODE && ancestor.nodeType !== nodeText) { + + while (ancestor && ancestor.nodeType === Node.ELEMENT_NODE && ancestor.nodeType !== NODE_TEXT) { if (this.matches(ancestor, selector)) { parents.push(ancestor) } @@ -74,7 +73,7 @@ const SelectorEngine = { return null } - return closest(element, selector) + return element.closest(selector) }, prev(element, selector) { @@ -83,9 +82,9 @@ const SelectorEngine = { } const siblings = [] - let previous = element.previousSibling - while (previous && previous.nodeType === Node.ELEMENT_NODE && previous.nodeType !== nodeText) { + + while (previous && previous.nodeType === Node.ELEMENT_NODE && previous.nodeType !== NODE_TEXT) { if (this.matches(previous, selector)) { siblings.push(previous) } diff --git a/js/tests/visual/modal.html b/js/tests/visual/modal.html index b37beeef38..f9d8b3f890 100644 --- a/js/tests/visual/modal.html +++ b/js/tests/visual/modal.html @@ -230,13 +230,13 @@ } } - document.querySelectorAll('[data-toggle="popover"]') + [].slice.call(document.querySelectorAll('[data-toggle="popover"]')) .forEach(function (popover) { new Popover(popover) }) - document.querySelectorAll('[data-toggle="tooltip"]') - .forEach(function (tooltip) { + var tooltipList = [].slice.call(document.querySelectorAll('[data-toggle="tooltip"]')) + tooltipList.forEach(function (tooltip) { new Tooltip(tooltip) }) diff --git a/js/tests/visual/popover.html b/js/tests/visual/popover.html index d408dbb903..86916c3d42 100644 --- a/js/tests/visual/popover.html +++ b/js/tests/visual/popover.html @@ -42,7 +42,7 @@