diff options
Diffstat (limited to 'js/src/dom/selectorEngine.js')
| -rw-r--r-- | js/src/dom/selectorEngine.js | 42 |
1 files changed, 42 insertions, 0 deletions
diff --git a/js/src/dom/selectorEngine.js b/js/src/dom/selectorEngine.js new file mode 100644 index 000000000..f6bcf6da2 --- /dev/null +++ b/js/src/dom/selectorEngine.js @@ -0,0 +1,42 @@ +/** + * -------------------------------------------------------------------------- + * Bootstrap (v4.0.0-beta): dom/selectorEngine.js + * Licensed under MIT (https://github.com/twbs/bootstrap/blob/master/LICENSE) + * -------------------------------------------------------------------------- + */ + +const SelectorEngine = { + matches: Element.prototype.msMatchesSelector || Element.prototype.webkitMatchesSelector, + + find(selector) { + if (typeof selector !== 'string') { + return null + } + + let selectorType = 'querySelectorAll' + if (selector.indexOf('#') === 0) { + selectorType = 'getElementById' + selector = selector.substr(1, selector.length) + } + return document[selectorType](selector) + }, + + closest(element, selector) { + let ancestor = element + if (!document.documentElement.contains(element)) { + return null + } + + do { + if (SelectorEngine.matches.call(ancestor, selector)) { + return ancestor + } + + ancestor = ancestor.parentElement + } while (ancestor !== null) + + return null + } +} + +export default SelectorEngine |
