All files / js/utils dom.js

30.77% Statements 12/39
28.07% Branches 16/57
7.69% Functions 1/13
28.95% Lines 11/38

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87      1x                                         1x         1x                             1x 1x                         1x 1x             1x 1x                         1x 1x          
import * as type from './type';
import * as utils from './index';
 
export const querySelector = (selector, node) => {
	if (type.isFunction(selector)) {
		return querySelector(selector(), node);
	}
 
	if (type.isNode(selector)) {
		return selector;
	}
 
	if (type.isString(selector)) {
		if (node && type.isNode(node)) {
			return node.querySelector(selector);
		} else {
			return document.querySelector(selector);
		}
	}
 
	if (Array.isArray(selector) || NodeList.prototype.isPrototypeOf(selector)) {
		return selector[0];
	}
};
export const querySelectorAll = (selector, node) => type.isFunction(selector) ?
	selector(node ? node : document) : (type.isString(selector) ?
		((node && type.isNode(node)) ? node.querySelectorAll(selector) : document.querySelectorAll(selector)) : (type.isNode(selector) ? [selector] : (NodeList.prototype.isPrototypeOf(selector) ?
			selector : null)));
 
export const optionsFromDataset = (node, defaultOptions = {}) => {
	if (type.isNode(node)) {
		return node.dataset ? Object.keys(node.dataset)
			.filter(key => Object.keys(defaultOptions).includes(key))
			.reduce((obj, key) => {
				return {
					...obj,
					[key]: node.dataset[key]
				};
			}, {}) : {};
	} else {
		return {};
	}
};
 
Eif (window.Node && !Node.prototype.on) {
	Node.prototype.on = function (events, handler) {
		if (!Array.isArray(events)) {
			events = events.split(' ');
		}
 
		events.forEach(event => {
			this.addEventListener(event, handler, utils.detectSupportsPassive() ? {
				passive: false
			} : false);
		});
	};
}
 
Eif (window.NodeList && !NodeList.prototype.on) {
	NodeList.prototype.on = function (events, handler) {
		this.forEach(node => {
			node.on(events, handler);
		});
	};
}
 
Eif (window.Node && !Node.prototype.off) {
	Node.prototype.off = function (events, handler) {
		if (!Array.isArray(events)) {
			events = events.split(' ');
		}
 
		events.forEach(event => {
			this.removeEventListener(event, handler, utils.detectSupportsPassive() ? {
				passive: false
			} : false);
		});
	};
}
 
Eif (window.NodeList && !NodeList.prototype.off) {
	NodeList.prototype.off = function (events, handler) {
		this.forEach(node => {
			node.off(events, handler);
		});
	};
}