Skip to content
GitLab
Projects
Groups
Snippets
/
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
Menu
Open sidebar
Bootstrap
bootstrap
Commits
d6560bbc
Commit
d6560bbc
authored
7 years ago
by
Johann-S
Committed by
XhmikosR
6 years ago
Browse files
Options
Download
Email Patches
Plain Diff
better polyfill for closest and matches functions
parent
0b16c8c6
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
js/src/dom/selectorEngine.js
+39
-15
js/src/dom/selectorEngine.js
with
39 additions
and
15 deletions
+39
-15
js/src/dom/selectorEngine.js
+
39
-
15
View file @
d6560bbc
...
@@ -5,8 +5,45 @@
...
@@ -5,8 +5,45 @@
* --------------------------------------------------------------------------
* --------------------------------------------------------------------------
*/
*/
// matches polyfill (see: https://mzl.la/2ikXneG)
let
fnMatches
=
null
if
(
!
Element
.
prototype
.
matches
)
{
fnMatches
=
Element
.
prototype
.
msMatchesSelector
||
Element
.
prototype
.
webkitMatchesSelector
}
else
{
fnMatches
=
Element
.
prototype
.
matches
}
// closest polyfill (see: https://mzl.la/2vXggaI)
let
fnClosest
=
null
if
(
!
Element
.
prototype
.
closest
)
{
fnClosest
=
(
element
,
selector
)
=>
{
let
ancestor
=
element
if
(
!
document
.
documentElement
.
contains
(
element
))
{
return
null
}
do
{
if
(
fnMatches
.
call
(
ancestor
,
selector
))
{
return
ancestor
}
ancestor
=
ancestor
.
parentElement
}
while
(
ancestor
!==
null
)
return
null
}
}
else
{
fnClosest
=
(
element
,
selector
)
=>
{
return
element
.
closest
(
selector
)
}
}
const
SelectorEngine
=
{
const
SelectorEngine
=
{
matches
:
Element
.
prototype
.
msMatchesSelector
||
Element
.
prototype
.
webkitMatchesSelector
,
matches
(
element
,
selector
)
{
return
fnMatches
.
call
(
element
,
selector
)
},
find
(
selector
)
{
find
(
selector
)
{
if
(
typeof
selector
!==
'
string
'
)
{
if
(
typeof
selector
!==
'
string
'
)
{
...
@@ -22,20 +59,7 @@ const SelectorEngine = {
...
@@ -22,20 +59,7 @@ const SelectorEngine = {
},
},
closest
(
element
,
selector
)
{
closest
(
element
,
selector
)
{
let
ancestor
=
element
return
fnClosest
(
element
,
selector
)
if
(
!
document
.
documentElement
.
contains
(
element
))
{
return
null
}
do
{
if
(
SelectorEngine
.
matches
.
call
(
ancestor
,
selector
))
{
return
ancestor
}
ancestor
=
ancestor
.
parentElement
}
while
(
ancestor
!==
null
)
return
null
}
}
}
}
...
...
This diff is collapsed.
Click to expand it.
Write
Preview
Supports
Markdown
0%
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment