Last active
January 5, 2017 15:05
-
-
Save thure/8fb3e403c4408979732f2ee058867c1a to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* Climbs up the DOM up to but not including the limit element (or | |
* `body` if not specified) looking for and returning the first | |
* element that passes the predicate, or `null` if nothing does. | |
* | |
* @param {HTMLElement} start | |
* @param {function} predicate | |
* @param {HTMLElement} limit | |
* @returns {*} | |
*/ | |
export default function(start, predicate, limit){ | |
if(!start) return null; | |
var cursor = start, | |
lim = limit || document.body; | |
while(cursor !== lim) { | |
if(cursor === document.body) break; | |
if(predicate(cursor)){ | |
return cursor | |
}else{ | |
cursor = cursor.parentNode; | |
} | |
} | |
return null; | |
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{ | |
"name": "climb-es6", | |
"version": "0.0.1", | |
"main": "climb.js", | |
"author": "Will Shown <[email protected]>", | |
"license": "MIT" | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment