1
0
mirror of https://github.com/twbs/bootstrap.git synced 2025-09-29 23:09:05 +02:00

Fix TypeError when Bootstrap is included in head (#32024)

* extend jquery after domContentLoaded event is fired

* add unittest for util onDOMContentLoaded

* wait for trigger jquery event after domContentLoaded

* remove domcontentready from eventHandler

* move istanbul ignore statements to correct line

Co-authored-by: XhmikosR <xhmikosr@gmail.com>
This commit is contained in:
Sascha
2020-11-01 14:32:36 +01:00
committed by GitHub
parent 3a5f9f5cf0
commit c21506d499
14 changed files with 184 additions and 126 deletions

View File

@@ -7,6 +7,7 @@
import { import {
getjQuery, getjQuery,
onDOMContentLoaded,
TRANSITION_END, TRANSITION_END,
emulateTransitionEnd, emulateTransitionEnd,
getElementFromSelector, getElementFromSelector,
@@ -146,8 +147,6 @@ class Alert {
*/ */
EventHandler.on(document, EVENT_CLICK_DATA_API, SELECTOR_DISMISS, Alert.handleDismiss(new Alert())) EventHandler.on(document, EVENT_CLICK_DATA_API, SELECTOR_DISMISS, Alert.handleDismiss(new Alert()))
const $ = getjQuery()
/** /**
* ------------------------------------------------------------------------ * ------------------------------------------------------------------------
* jQuery * jQuery
@@ -155,8 +154,10 @@ const $ = getjQuery()
* add .alert to jQuery only if jQuery is present * add .alert to jQuery only if jQuery is present
*/ */
/* istanbul ignore if */ onDOMContentLoaded(() => {
if ($) { const $ = getjQuery()
/* istanbul ignore if */
if ($) {
const JQUERY_NO_CONFLICT = $.fn[NAME] const JQUERY_NO_CONFLICT = $.fn[NAME]
$.fn[NAME] = Alert.jQueryInterface $.fn[NAME] = Alert.jQueryInterface
$.fn[NAME].Constructor = Alert $.fn[NAME].Constructor = Alert
@@ -164,6 +165,7 @@ if ($) {
$.fn[NAME] = JQUERY_NO_CONFLICT $.fn[NAME] = JQUERY_NO_CONFLICT
return Alert.jQueryInterface return Alert.jQueryInterface
} }
} }
})
export default Alert export default Alert

View File

@@ -5,7 +5,7 @@
* -------------------------------------------------------------------------- * --------------------------------------------------------------------------
*/ */
import { getjQuery } from './util/index' import { getjQuery, onDOMContentLoaded } from './util/index'
import Data from './dom/data' import Data from './dom/data'
import EventHandler from './dom/event-handler' import EventHandler from './dom/event-handler'
@@ -97,16 +97,17 @@ EventHandler.on(document, EVENT_CLICK_DATA_API, SELECTOR_DATA_TOGGLE, event => {
data.toggle() data.toggle()
}) })
const $ = getjQuery()
/** /**
* ------------------------------------------------------------------------ * ------------------------------------------------------------------------
* jQuery * jQuery
* ------------------------------------------------------------------------ * ------------------------------------------------------------------------
* add .button to jQuery only if jQuery is present * add .button to jQuery only if jQuery is present
*/ */
/* istanbul ignore if */
if ($) { onDOMContentLoaded(() => {
const $ = getjQuery()
/* istanbul ignore if */
if ($) {
const JQUERY_NO_CONFLICT = $.fn[NAME] const JQUERY_NO_CONFLICT = $.fn[NAME]
$.fn[NAME] = Button.jQueryInterface $.fn[NAME] = Button.jQueryInterface
$.fn[NAME].Constructor = Button $.fn[NAME].Constructor = Button
@@ -115,6 +116,7 @@ if ($) {
$.fn[NAME] = JQUERY_NO_CONFLICT $.fn[NAME] = JQUERY_NO_CONFLICT
return Button.jQueryInterface return Button.jQueryInterface
} }
} }
})
export default Button export default Button

View File

@@ -7,6 +7,7 @@
import { import {
getjQuery, getjQuery,
onDOMContentLoaded,
TRANSITION_END, TRANSITION_END,
emulateTransitionEnd, emulateTransitionEnd,
getElementFromSelector, getElementFromSelector,
@@ -611,16 +612,17 @@ EventHandler.on(window, EVENT_LOAD_DATA_API, () => {
} }
}) })
const $ = getjQuery()
/** /**
* ------------------------------------------------------------------------ * ------------------------------------------------------------------------
* jQuery * jQuery
* ------------------------------------------------------------------------ * ------------------------------------------------------------------------
* add .carousel to jQuery only if jQuery is present * add .carousel to jQuery only if jQuery is present
*/ */
/* istanbul ignore if */
if ($) { onDOMContentLoaded(() => {
const $ = getjQuery()
/* istanbul ignore if */
if ($) {
const JQUERY_NO_CONFLICT = $.fn[NAME] const JQUERY_NO_CONFLICT = $.fn[NAME]
$.fn[NAME] = Carousel.jQueryInterface $.fn[NAME] = Carousel.jQueryInterface
$.fn[NAME].Constructor = Carousel $.fn[NAME].Constructor = Carousel
@@ -628,6 +630,7 @@ if ($) {
$.fn[NAME] = JQUERY_NO_CONFLICT $.fn[NAME] = JQUERY_NO_CONFLICT
return Carousel.jQueryInterface return Carousel.jQueryInterface
} }
} }
})
export default Carousel export default Carousel

View File

@@ -7,6 +7,7 @@
import { import {
getjQuery, getjQuery,
onDOMContentLoaded,
TRANSITION_END, TRANSITION_END,
emulateTransitionEnd, emulateTransitionEnd,
getSelectorFromElement, getSelectorFromElement,
@@ -408,16 +409,17 @@ EventHandler.on(document, EVENT_CLICK_DATA_API, SELECTOR_DATA_TOGGLE, function (
}) })
}) })
const $ = getjQuery()
/** /**
* ------------------------------------------------------------------------ * ------------------------------------------------------------------------
* jQuery * jQuery
* ------------------------------------------------------------------------ * ------------------------------------------------------------------------
* add .collapse to jQuery only if jQuery is present * add .collapse to jQuery only if jQuery is present
*/ */
/* istanbul ignore if */
if ($) { onDOMContentLoaded(() => {
const $ = getjQuery()
/* istanbul ignore if */
if ($) {
const JQUERY_NO_CONFLICT = $.fn[NAME] const JQUERY_NO_CONFLICT = $.fn[NAME]
$.fn[NAME] = Collapse.jQueryInterface $.fn[NAME] = Collapse.jQueryInterface
$.fn[NAME].Constructor = Collapse $.fn[NAME].Constructor = Collapse
@@ -425,6 +427,7 @@ if ($) {
$.fn[NAME] = JQUERY_NO_CONFLICT $.fn[NAME] = JQUERY_NO_CONFLICT
return Collapse.jQueryInterface return Collapse.jQueryInterface
} }
} }
})
export default Collapse export default Collapse

View File

@@ -14,7 +14,6 @@ import { defaultPreventedPreservedOnDispatch } from './polyfill'
* ------------------------------------------------------------------------ * ------------------------------------------------------------------------
*/ */
const $ = getjQuery()
const namespaceRegex = /[^.]*(?=\..*)\.|.*/ const namespaceRegex = /[^.]*(?=\..*)\.|.*/
const stripNameRegex = /\..*/ const stripNameRegex = /\..*/
const stripUidRegex = /::\d+$/ const stripUidRegex = /::\d+$/
@@ -272,6 +271,7 @@ const EventHandler = {
return null return null
} }
const $ = getjQuery()
const typeEvent = event.replace(stripNameRegex, '') const typeEvent = event.replace(stripNameRegex, '')
const inNamespace = event !== typeEvent const inNamespace = event !== typeEvent
const isNative = nativeEvents.indexOf(typeEvent) > -1 const isNative = nativeEvents.indexOf(typeEvent) > -1

View File

@@ -7,6 +7,7 @@
import { import {
getjQuery, getjQuery,
onDOMContentLoaded,
getElementFromSelector, getElementFromSelector,
isElement, isElement,
isVisible, isVisible,
@@ -512,16 +513,17 @@ EventHandler.on(document, EVENT_CLICK_DATA_API, SELECTOR_DATA_TOGGLE, function (
}) })
EventHandler.on(document, EVENT_CLICK_DATA_API, SELECTOR_FORM_CHILD, e => e.stopPropagation()) EventHandler.on(document, EVENT_CLICK_DATA_API, SELECTOR_FORM_CHILD, e => e.stopPropagation())
const $ = getjQuery()
/** /**
* ------------------------------------------------------------------------ * ------------------------------------------------------------------------
* jQuery * jQuery
* ------------------------------------------------------------------------ * ------------------------------------------------------------------------
* add .dropdown to jQuery only if jQuery is present * add .dropdown to jQuery only if jQuery is present
*/ */
/* istanbul ignore if */
if ($) { onDOMContentLoaded(() => {
const $ = getjQuery()
/* istanbul ignore if */
if ($) {
const JQUERY_NO_CONFLICT = $.fn[NAME] const JQUERY_NO_CONFLICT = $.fn[NAME]
$.fn[NAME] = Dropdown.jQueryInterface $.fn[NAME] = Dropdown.jQueryInterface
$.fn[NAME].Constructor = Dropdown $.fn[NAME].Constructor = Dropdown
@@ -529,6 +531,7 @@ if ($) {
$.fn[NAME] = JQUERY_NO_CONFLICT $.fn[NAME] = JQUERY_NO_CONFLICT
return Dropdown.jQueryInterface return Dropdown.jQueryInterface
} }
} }
})
export default Dropdown export default Dropdown

View File

@@ -7,6 +7,7 @@
import { import {
getjQuery, getjQuery,
onDOMContentLoaded,
TRANSITION_END, TRANSITION_END,
emulateTransitionEnd, emulateTransitionEnd,
getElementFromSelector, getElementFromSelector,
@@ -607,16 +608,17 @@ EventHandler.on(document, EVENT_CLICK_DATA_API, SELECTOR_DATA_TOGGLE, function (
data.show(this) data.show(this)
}) })
const $ = getjQuery()
/** /**
* ------------------------------------------------------------------------ * ------------------------------------------------------------------------
* jQuery * jQuery
* ------------------------------------------------------------------------ * ------------------------------------------------------------------------
* add .modal to jQuery only if jQuery is present * add .modal to jQuery only if jQuery is present
*/ */
/* istanbul ignore if */
if ($) { onDOMContentLoaded(() => {
const $ = getjQuery()
/* istanbul ignore if */
if ($) {
const JQUERY_NO_CONFLICT = $.fn[NAME] const JQUERY_NO_CONFLICT = $.fn[NAME]
$.fn[NAME] = Modal.jQueryInterface $.fn[NAME] = Modal.jQueryInterface
$.fn[NAME].Constructor = Modal $.fn[NAME].Constructor = Modal
@@ -624,6 +626,7 @@ if ($) {
$.fn[NAME] = JQUERY_NO_CONFLICT $.fn[NAME] = JQUERY_NO_CONFLICT
return Modal.jQueryInterface return Modal.jQueryInterface
} }
} }
})
export default Modal export default Modal

View File

@@ -5,7 +5,7 @@
* -------------------------------------------------------------------------- * --------------------------------------------------------------------------
*/ */
import { getjQuery } from './util/index' import { getjQuery, onDOMContentLoaded } from './util/index'
import Data from './dom/data' import Data from './dom/data'
import SelectorEngine from './dom/selector-engine' import SelectorEngine from './dom/selector-engine'
import Tooltip from './tooltip' import Tooltip from './tooltip'
@@ -167,15 +167,16 @@ class Popover extends Tooltip {
} }
} }
const $ = getjQuery()
/** /**
* ------------------------------------------------------------------------ * ------------------------------------------------------------------------
* jQuery * jQuery
* ------------------------------------------------------------------------ * ------------------------------------------------------------------------
*/ */
/* istanbul ignore if */
if ($) { onDOMContentLoaded(() => {
const $ = getjQuery()
/* istanbul ignore if */
if ($) {
const JQUERY_NO_CONFLICT = $.fn[NAME] const JQUERY_NO_CONFLICT = $.fn[NAME]
$.fn[NAME] = Popover.jQueryInterface $.fn[NAME] = Popover.jQueryInterface
$.fn[NAME].Constructor = Popover $.fn[NAME].Constructor = Popover
@@ -183,6 +184,7 @@ if ($) {
$.fn[NAME] = JQUERY_NO_CONFLICT $.fn[NAME] = JQUERY_NO_CONFLICT
return Popover.jQueryInterface return Popover.jQueryInterface
} }
} }
})
export default Popover export default Popover

View File

@@ -7,6 +7,7 @@
import { import {
getjQuery, getjQuery,
onDOMContentLoaded,
getSelectorFromElement, getSelectorFromElement,
getUID, getUID,
isElement, isElement,
@@ -317,15 +318,16 @@ EventHandler.on(window, EVENT_LOAD_DATA_API, () => {
.forEach(spy => new ScrollSpy(spy, Manipulator.getDataAttributes(spy))) .forEach(spy => new ScrollSpy(spy, Manipulator.getDataAttributes(spy)))
}) })
const $ = getjQuery()
/** /**
* ------------------------------------------------------------------------ * ------------------------------------------------------------------------
* jQuery * jQuery
* ------------------------------------------------------------------------ * ------------------------------------------------------------------------
*/ */
/* istanbul ignore if */
if ($) { onDOMContentLoaded(() => {
const $ = getjQuery()
/* istanbul ignore if */
if ($) {
const JQUERY_NO_CONFLICT = $.fn[NAME] const JQUERY_NO_CONFLICT = $.fn[NAME]
$.fn[NAME] = ScrollSpy.jQueryInterface $.fn[NAME] = ScrollSpy.jQueryInterface
$.fn[NAME].Constructor = ScrollSpy $.fn[NAME].Constructor = ScrollSpy
@@ -333,6 +335,7 @@ if ($) {
$.fn[NAME] = JQUERY_NO_CONFLICT $.fn[NAME] = JQUERY_NO_CONFLICT
return ScrollSpy.jQueryInterface return ScrollSpy.jQueryInterface
} }
} }
})
export default ScrollSpy export default ScrollSpy

View File

@@ -7,6 +7,7 @@
import { import {
getjQuery, getjQuery,
onDOMContentLoaded,
TRANSITION_END, TRANSITION_END,
emulateTransitionEnd, emulateTransitionEnd,
getElementFromSelector, getElementFromSelector,
@@ -235,16 +236,17 @@ EventHandler.on(document, EVENT_CLICK_DATA_API, SELECTOR_DATA_TOGGLE, function (
data.show() data.show()
}) })
const $ = getjQuery()
/** /**
* ------------------------------------------------------------------------ * ------------------------------------------------------------------------
* jQuery * jQuery
* ------------------------------------------------------------------------ * ------------------------------------------------------------------------
* add .tab to jQuery only if jQuery is present * add .tab to jQuery only if jQuery is present
*/ */
/* istanbul ignore if */
if ($) { onDOMContentLoaded(() => {
const $ = getjQuery()
/* istanbul ignore if */
if ($) {
const JQUERY_NO_CONFLICT = $.fn[NAME] const JQUERY_NO_CONFLICT = $.fn[NAME]
$.fn[NAME] = Tab.jQueryInterface $.fn[NAME] = Tab.jQueryInterface
$.fn[NAME].Constructor = Tab $.fn[NAME].Constructor = Tab
@@ -252,6 +254,7 @@ if ($) {
$.fn[NAME] = JQUERY_NO_CONFLICT $.fn[NAME] = JQUERY_NO_CONFLICT
return Tab.jQueryInterface return Tab.jQueryInterface
} }
} }
})
export default Tab export default Tab

View File

@@ -7,6 +7,7 @@
import { import {
getjQuery, getjQuery,
onDOMContentLoaded,
TRANSITION_END, TRANSITION_END,
emulateTransitionEnd, emulateTransitionEnd,
getTransitionDurationFromElement, getTransitionDurationFromElement,
@@ -213,16 +214,17 @@ class Toast {
} }
} }
const $ = getjQuery()
/** /**
* ------------------------------------------------------------------------ * ------------------------------------------------------------------------
* jQuery * jQuery
* ------------------------------------------------------------------------ * ------------------------------------------------------------------------
* add .toast to jQuery only if jQuery is present * add .toast to jQuery only if jQuery is present
*/ */
/* istanbul ignore if */
if ($) { onDOMContentLoaded(() => {
const $ = getjQuery()
/* istanbul ignore if */
if ($) {
const JQUERY_NO_CONFLICT = $.fn[NAME] const JQUERY_NO_CONFLICT = $.fn[NAME]
$.fn[NAME] = Toast.jQueryInterface $.fn[NAME] = Toast.jQueryInterface
$.fn[NAME].Constructor = Toast $.fn[NAME].Constructor = Toast
@@ -230,6 +232,7 @@ if ($) {
$.fn[NAME] = JQUERY_NO_CONFLICT $.fn[NAME] = JQUERY_NO_CONFLICT
return Toast.jQueryInterface return Toast.jQueryInterface
} }
} }
})
export default Toast export default Toast

View File

@@ -7,6 +7,7 @@
import { import {
getjQuery, getjQuery,
onDOMContentLoaded,
TRANSITION_END, TRANSITION_END,
emulateTransitionEnd, emulateTransitionEnd,
findShadowRoot, findShadowRoot,
@@ -793,16 +794,17 @@ class Tooltip {
} }
} }
const $ = getjQuery()
/** /**
* ------------------------------------------------------------------------ * ------------------------------------------------------------------------
* jQuery * jQuery
* ------------------------------------------------------------------------ * ------------------------------------------------------------------------
* add .tooltip to jQuery only if jQuery is present * add .tooltip to jQuery only if jQuery is present
*/ */
/* istanbul ignore if */
if ($) { onDOMContentLoaded(() => {
const $ = getjQuery()
/* istanbul ignore if */
if ($) {
const JQUERY_NO_CONFLICT = $.fn[NAME] const JQUERY_NO_CONFLICT = $.fn[NAME]
$.fn[NAME] = Tooltip.jQueryInterface $.fn[NAME] = Tooltip.jQueryInterface
$.fn[NAME].Constructor = Tooltip $.fn[NAME].Constructor = Tooltip
@@ -810,6 +812,7 @@ if ($) {
$.fn[NAME] = JQUERY_NO_CONFLICT $.fn[NAME] = JQUERY_NO_CONFLICT
return Tooltip.jQueryInterface return Tooltip.jQueryInterface
} }
} }
})
export default Tooltip export default Tooltip

View File

@@ -180,8 +180,15 @@ const getjQuery = () => {
return null return null
} }
const onDOMContentLoaded = callback => {
if (document.readyState === 'loading') {
document.addEventListener('DOMContentLoaded', callback)
} else {
callback()
}
}
export { export {
getjQuery,
TRANSITION_END, TRANSITION_END,
getUID, getUID,
getSelectorFromElement, getSelectorFromElement,
@@ -194,5 +201,7 @@ export {
isVisible, isVisible,
findShadowRoot, findShadowRoot,
noop, noop,
reflow reflow,
getjQuery,
onDOMContentLoaded
} }

View File

@@ -394,4 +394,23 @@ describe('Util', () => {
expect(Util.getjQuery()).toEqual(null) expect(Util.getjQuery()).toEqual(null)
}) })
}) })
describe('onDOMContentLoaded', () => {
it('should execute callback when DOMContentLoaded is fired', () => {
const spy = jasmine.createSpy()
spyOnProperty(document, 'readyState').and.returnValue('loading')
Util.onDOMContentLoaded(spy)
window.document.dispatchEvent(new Event('DOMContentLoaded', {
bubbles: true,
cancelable: true
}))
expect(spy).toHaveBeenCalled()
})
it('should execute callback if readyState is not "loading"', () => {
const spy = jasmine.createSpy()
Util.onDOMContentLoaded(spy)
expect(spy).toHaveBeenCalled()
})
})
}) })