0
0
mirror of https://github.com/twbs/bootstrap.git synced 2024-11-29 11:24:18 +01:00

use get selector from element only when needed

This commit is contained in:
Johann-S 2019-07-23 21:15:00 +02:00
parent f4dbffe93a
commit fc02932946
8 changed files with 82 additions and 49 deletions

View File

@ -9,7 +9,7 @@ import {
jQuery as $,
TRANSITION_END,
emulateTransitionEnd,
getSelectorFromElement,
getElementFromSelector,
getTransitionDurationFromElement
} from '../util/index'
import Data from '../dom/data'
@ -90,12 +90,7 @@ class Alert {
// Private
_getRootElement(element) {
const selector = getSelectorFromElement(element)
let parent = false
if (selector) {
parent = SelectorEngine.findOne(selector)
}
let parent = getElementFromSelector(element)
if (!parent) {
parent = SelectorEngine.closest(element, `.${ClassName.ALERT}`)

View File

@ -9,7 +9,7 @@ import {
jQuery as $,
TRANSITION_END,
emulateTransitionEnd,
getSelectorFromElement,
getElementFromSelector,
getTransitionDurationFromElement,
isVisible,
makeArray,
@ -569,13 +569,7 @@ class Carousel {
}
static _dataApiClickHandler(event) {
const selector = getSelectorFromElement(this)
if (!selector) {
return
}
const target = SelectorEngine.findOne(selector)
const target = getElementFromSelector(this)
if (!target || !target.classList.contains(ClassName.CAROUSEL)) {
return

View File

@ -10,6 +10,7 @@ import {
TRANSITION_END,
emulateTransitionEnd,
getSelectorFromElement,
getElementFromSelector,
getTransitionDurationFromElement,
isElement,
makeArray,
@ -244,15 +245,11 @@ class Collapse {
if (triggerArrayLength > 0) {
for (let i = 0; i < triggerArrayLength; i++) {
const trigger = this._triggerArray[i]
const selector = getSelectorFromElement(trigger)
const elem = getElementFromSelector(trigger)
if (selector !== null) {
const elem = SelectorEngine.findOne(selector)
if (!elem.classList.contains(ClassName.SHOW)) {
trigger.classList.add(ClassName.COLLAPSED)
trigger.setAttribute('aria-expanded', false)
}
if (elem && !elem.classList.contains(ClassName.SHOW)) {
trigger.classList.add(ClassName.COLLAPSED)
trigger.setAttribute('aria-expanded', false)
}
}
}
@ -320,8 +317,7 @@ class Collapse {
makeArray(SelectorEngine.find(selector, parent))
.forEach(element => {
const selector = getSelectorFromElement(element)
const selected = selector ? SelectorEngine.findOne(selector) : null
const selected = getElementFromSelector(element)
this._addAriaAndCollapsedClass(
selected,

View File

@ -7,7 +7,7 @@
import {
jQuery as $,
getSelectorFromElement,
getElementFromSelector,
isElement,
makeArray,
noop,
@ -442,14 +442,7 @@ class Dropdown {
}
static _getParentFromElement(element) {
let parent
const selector = getSelectorFromElement(element)
if (selector) {
parent = SelectorEngine.findOne(selector)
}
return parent || element.parentNode
return getElementFromSelector(element) || element.parentNode
}
static _dataApiKeydownHandler(event) {

View File

@ -9,7 +9,7 @@ import {
jQuery as $,
TRANSITION_END,
emulateTransitionEnd,
getSelectorFromElement,
getElementFromSelector,
getTransitionDurationFromElement,
isVisible,
makeArray,
@ -549,8 +549,7 @@ class Modal {
*/
EventHandler.on(document, Event.CLICK_DATA_API, Selector.DATA_TOGGLE, function (event) {
const selector = getSelectorFromElement(this)
const target = SelectorEngine.findOne(selector)
const target = getElementFromSelector(this)
if (this.tagName === 'A' || this.tagName === 'AREA') {
event.preventDefault()

View File

@ -9,7 +9,7 @@ import {
jQuery as $,
TRANSITION_END,
emulateTransitionEnd,
getSelectorFromElement,
getElementFromSelector,
getTransitionDurationFromElement,
makeArray,
reflow
@ -85,10 +85,9 @@ class Tab {
return
}
let target
let previous
const target = getElementFromSelector(this._element)
const listElement = SelectorEngine.closest(this._element, Selector.NAV_LIST_GROUP)
const selector = getSelectorFromElement(this._element)
if (listElement) {
const itemSelector = listElement.nodeName === 'UL' || listElement.nodeName === 'OL' ? Selector.ACTIVE_UL : Selector.ACTIVE
@ -113,10 +112,6 @@ class Tab {
return
}
if (selector) {
target = SelectorEngine.findOne(selector)
}
this._activate(
this._element,
listElement

View File

@ -28,20 +28,32 @@ const getUID = prefix => {
return prefix
}
const getSelectorFromElement = element => {
const getSelector = element => {
let selector = element.getAttribute('data-target')
if (!selector || selector === '#') {
const hrefAttr = element.getAttribute('href')
selector = hrefAttr && hrefAttr !== '#' ? hrefAttr.trim() : ''
selector = hrefAttr && hrefAttr !== '#' ? hrefAttr.trim() : null
}
try {
return selector
}
const getSelectorFromElement = element => {
const selector = getSelector(element)
if (selector) {
return document.querySelector(selector) ? selector : null
} catch (error) {
return null
}
return null
}
const getElementFromSelector = element => {
const selector = getSelector(element)
return selector ? document.querySelector(selector) : null
}
const getTransitionDurationFromElement = element => {
@ -169,6 +181,7 @@ export {
TRANSITION_END,
getUID,
getSelectorFromElement,
getElementFromSelector,
getTransitionDurationFromElement,
triggerTransitionEnd,
isElement,

View File

@ -64,6 +64,54 @@ describe('Util', () => {
expect(Util.getSelectorFromElement(testEl)).toBeNull()
})
it('should return null if no selector', () => {
fixtureEl.innerHTML = '<div></div>'
const testEl = fixtureEl.querySelector('div')
expect(Util.getSelectorFromElement(testEl)).toBeNull()
})
})
describe('getElementFromSelector', () => {
it('should get element from data-target', () => {
fixtureEl.innerHTML = [
'<div id="test" data-target=".target"></div>',
'<div class="target"></div>'
].join('')
const testEl = fixtureEl.querySelector('#test')
expect(Util.getElementFromSelector(testEl)).toEqual(fixtureEl.querySelector('.target'))
})
it('should get element from href if no data-target set', () => {
fixtureEl.innerHTML = [
'<a id="test" href=".target"></a>',
'<div class="target"></div>'
].join('')
const testEl = fixtureEl.querySelector('#test')
expect(Util.getElementFromSelector(testEl)).toEqual(fixtureEl.querySelector('.target'))
})
it('should return null if element not found', () => {
fixtureEl.innerHTML = '<a id="test" href=".target"></a>'
const testEl = fixtureEl.querySelector('#test')
expect(Util.getElementFromSelector(testEl)).toBeNull()
})
it('should return null if no selector', () => {
fixtureEl.innerHTML = '<div></div>'
const testEl = fixtureEl.querySelector('div')
expect(Util.getElementFromSelector(testEl)).toBeNull()
})
})
describe('getTransitionDurationFromElement', () => {