Update dom.

This commit is contained in:
Lars Jung 2016-07-22 20:15:14 +02:00
parent 20e080751d
commit 1d4b4b8a02

View file

@ -3,41 +3,41 @@ const {each, filter, hasLength, is, isStr, map, isInstanceOf, toArray} = require
const doc = win.document; const doc = win.document;
const createElement = name => doc.createElement(name); const parseHtml = (() => {
const create = name => doc.createElement(name);
const rules = [
[/^<t(head|body|foot)|^<c(ap|olg)/i, create('table')],
[/^<col/i, create('colgroup')],
[/^<tr/i, create('tbody')],
[/^<t[dh]/i, create('tr')]
];
const div = create('div');
const reReady = /^(i|c|loade)/; const findContainer = str => {
for (const [re, el] of rules) {
const containers = [ if (re.test(str)) {
{re: /^<t(head|body|foot)|^<c(ap|olg)/i, el: createElement('table')}, return el;
{re: /^<col/i, el: createElement('colgroup')}, }
{re: /^<tr/i, el: createElement('tbody')},
{re: /^<t[dh]/i, el: createElement('tr')}
];
const containerDiv = createElement('div');
const findContainer = str => {
for (const {re, el} of containers) {
if (re.test(str)) {
return el;
} }
} return div;
return containerDiv; };
};
const parseHtml = str => { return str => {
const container = findContainer(str); const container = findContainer(str);
container.innerHTML = str; container.innerHTML = str;
const res = toArray(container.childNodes); const res = toArray(container.childNodes);
each(res, el => container.removeChild(el)); each(res, el => container.removeChild(el));
container.innerHTML = ''; container.innerHTML = '';
return res; return res;
}; };
})();
const queryAll = (selector, context = doc) => { const queryAll = (selector, context = doc) => {
try { try {
return toArray(context.querySelectorAll(selector)); return toArray(context.querySelectorAll(selector));
} catch (err) {/* ignore */} } catch (err) {
return []; return [];
}
}; };
const isElement = x => isInstanceOf(x, win.Element); const isElement = x => isInstanceOf(x, win.Element);
@ -48,6 +48,7 @@ const isElDocWin = x => isElement(x) || isDocument(x) || isWindow(x);
const addListener = (el, type, fn) => el.addEventListener(type, fn); const addListener = (el, type, fn) => el.addEventListener(type, fn);
const removeListener = (el, type, fn) => el.removeEventListener(type, fn); const removeListener = (el, type, fn) => el.removeEventListener(type, fn);
const reReady = /^(i|c|loade)/;
const onReady = fn => { const onReady = fn => {
if (reReady.test(doc.readyState)) { if (reReady.test(doc.readyState)) {
fn(); fn();
@ -58,20 +59,6 @@ const onReady = fn => {
const onLoad = fn => addListener(win, 'load', fn); const onLoad = fn => addListener(win, 'load', fn);
const onResize = fn => {
addListener(win, 'resize', fn);
};
const onPrint = (before, after) => {
win.matchMedia('print').addListener(mql => {
if (mql.matches) {
before();
} else {
after();
}
});
};
const dom = arg => { const dom = arg => {
if (isInstanceOf(arg, dom)) { if (isInstanceOf(arg, dom)) {
return arg; return arg;
@ -281,13 +268,7 @@ dom.prototype = {
}; };
module.exports = { module.exports = {
isElement,
isDocument,
isWindow,
isElDocWin,
onReady, onReady,
onLoad, onLoad,
onResize,
onPrint,
dom dom
}; };