mirror of
https://github.com/twbs/bootstrap.git
synced 2025-02-13 10:54:24 +01:00
The `prev()` function doesn't take nodes other than elements into account. Also we could simplify things a lot using the `previousElementSibling` property. This property isn't fully supported in IE, it only works for elements, but since the `element` variable is an element, we can safely use it here. I've also added an additional test. I don't think we had this issue in v4, since we relied on jQuery back then. Ref. https://developer.mozilla.org/en-US/docs/Web/API/NonDocumentTypeChildNode/nextElementSibling
74 lines
1.7 KiB
JavaScript
74 lines
1.7 KiB
JavaScript
/**
|
|
* --------------------------------------------------------------------------
|
|
* Bootstrap (v4.3.1): dom/selector-engine.js
|
|
* Licensed under MIT (https://github.com/twbs/bootstrap/blob/master/LICENSE)
|
|
* --------------------------------------------------------------------------
|
|
*/
|
|
|
|
import { find as findFn, findOne, matches, closest } from './polyfill'
|
|
import { makeArray } from '../util/index'
|
|
|
|
/**
|
|
* ------------------------------------------------------------------------
|
|
* Constants
|
|
* ------------------------------------------------------------------------
|
|
*/
|
|
|
|
const NODE_TEXT = 3
|
|
|
|
const SelectorEngine = {
|
|
matches(element, selector) {
|
|
return matches.call(element, selector)
|
|
},
|
|
|
|
find(selector, element = document.documentElement) {
|
|
return findFn.call(element, selector)
|
|
},
|
|
|
|
findOne(selector, element = document.documentElement) {
|
|
return findOne.call(element, selector)
|
|
},
|
|
|
|
children(element, selector) {
|
|
const children = makeArray(element.children)
|
|
|
|
return children.filter(child => this.matches(child, selector))
|
|
},
|
|
|
|
parents(element, selector) {
|
|
const parents = []
|
|
|
|
let 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
|
|
},
|
|
|
|
closest(element, selector) {
|
|
return closest.call(element, selector)
|
|
},
|
|
|
|
prev(element, selector) {
|
|
let previous = element.previousElementSibling
|
|
|
|
while (previous) {
|
|
if (this.matches(previous, selector)) {
|
|
return [previous]
|
|
}
|
|
|
|
previous = previous.previousElementSibling
|
|
}
|
|
|
|
return []
|
|
}
|
|
}
|
|
|
|
export default SelectorEngine
|