Skip to content

Instantly share code, notes, and snippets.

@Osmose
Created November 6, 2015 08:51
Show Gist options
  • Save Osmose/dbc26c58259172e84cee to your computer and use it in GitHub Desktop.
Save Osmose/dbc26c58259172e84cee to your computer and use it in GitHub Desktop.
/**
* 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