Created
November 6, 2015 08:51
-
-
Save Osmose/dbc26c58259172e84cee to your computer and use it in GitHub Desktop.
This file contains hidden or 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
| /** | |
| * Parse the given string as HTML and return DOM nodes. Assumes a root | |
| * DOM node because, well, that's all I use it for. | |
| */ | |
| export function dom(html) { | |
| let div = document.createElement('div'); | |
| div.innerHTML = html; | |
| return div.firstElementChild; | |
| } | |
| /** | |
| * Starts at the current DOM element and moves upward in the DOM tree | |
| * until an element matching the given selector is found. | |
| * | |
| * Intended to be bound to DOM elements like so: | |
| * domNode::closest('selector'); | |
| */ | |
| export function closest(selector) { | |
| if (this.matches && this.matches(selector)) { | |
| return this; | |
| } else if (this.parentNode) { | |
| return this.parentNode::closest(selector); | |
| } else { | |
| return null; | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment