polyfill.js 5.03 KiB
import Util from '../util'
/**
 * --------------------------------------------------------------------------
 * Bootstrap (v4.1.3): dom/polyfill.js
 * Licensed under MIT (https://github.com/twbs/bootstrap/blob/master/LICENSE)
 * --------------------------------------------------------------------------
 */
/* istanbul ignore next */
const Polyfill = (() => {
  // defaultPrevented is broken in IE
  const workingDefaultPrevented = (() => {
    const e = document.createEvent('CustomEvent')
    e.initEvent('Bootstrap', true, true)
    e.preventDefault()
    return e.defaultPrevented
  })()
  if (!workingDefaultPrevented) {
    const origPreventDefault = Event.prototype.preventDefault
    Event.prototype.preventDefault = function () {
      if (!this.cancelable) {
        return
      origPreventDefault.call(this)
      Object.defineProperty(this, 'defaultPrevented', {
        get() {
          return true
        configurable: true
  // CustomEvent polyfill for IE (see: https://mzl.la/2v76Zvn)
  if (typeof window.CustomEvent !== 'function') {
    window.CustomEvent = (event, params) => {
      params = params || {
        bubbles: false,
        cancelable: false,
        detail: null
      const evt = document.createEvent('CustomEvent')
      evt.initCustomEvent(event, params.bubbles, params.cancelable, params.detail)
      return evt
    window.CustomEvent.prototype = window.Event.prototype
  // MSEdge resets defaultPrevented flag upon dispatchEvent call if at least one listener is attached
  const defaultPreventedPreservedOnDispatch = (() => {
    const e = new CustomEvent('Bootstrap', {
      cancelable: true
    const element = document.createElement('div')
    element.addEventListener('Bootstrap', () => null)
    e.preventDefault()
    element.dispatchEvent(e)
    return e.defaultPrevented
  })()
  // Event constructor shim
  if (!window.Event || typeof window.Event !== 'function') {
    const origEvent = window.Event
    window.Event = (inType, params) => {
7172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
params = params || {} const e = document.createEvent('Event') e.initEvent(inType, Boolean(params.bubbles), Boolean(params.cancelable)) return e } window.Event.prototype = origEvent.prototype } // matches polyfill (see: https://mzl.la/2ikXneG) if (!Element.prototype.matches) { Element.prototype.matches = Element.prototype.msMatchesSelector || Element.prototype.webkitMatchesSelector } // closest polyfill (see: https://mzl.la/2vXggaI) let closest if (!Element.prototype.closest) { closest = (element, selector) => { let ancestor = element do { if (ancestor.matches(selector)) { return ancestor } ancestor = ancestor.parentElement } while (ancestor !== null && ancestor.nodeType === Node.ELEMENT_NODE) return null } } else { closest = (element, selector) => element.closest(selector) } const supportScopeQuery = (() => { const element = document.createElement('div') try { element.querySelectorAll(':scope *') } catch (e) { return false } 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)) { return this.querySelectorAll(selector) } const hasId = Boolean(this.id) if (!hasId) { this.id = Util.getUID('scope') } let nodeList = null try { selector = selector.replace(scopeSelectorRegex, `#${this.id}`) nodeList = this.querySelectorAll(selector) } finally { if (!hasId) { this.removeAttribute('id') } }
141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195
return nodeList } findOne = function (selector) { if (!scopeSelectorRegex.test(selector)) { return this.querySelector(selector) } const matches = find.call(this, selector) if (typeof matches[0] !== 'undefined') { return matches[0] } return null } } 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 } })() export default Polyfill