diff --git a/src/_h5ai/public/js/lib/util/dom.js b/src/_h5ai/public/js/lib/util/dom.js index e3c8fca6..51f08590 100644 --- a/src/_h5ai/public/js/lib/util/dom.js +++ b/src/_h5ai/public/js/lib/util/dom.js @@ -3,41 +3,41 @@ const {each, filter, hasLength, is, isStr, map, isInstanceOf, toArray} = require const doc = win.document; -const createElement = name => doc.createElement(name); +const parseHtml = (() => { + const create = name => doc.createElement(name); + const rules = [ + [/^ { - for (const {re, el} of containers) { - if (re.test(str)) { - return el; + const findContainer = str => { + for (const [re, el] of rules) { + if (re.test(str)) { + return el; + } } - } - return containerDiv; -}; + return div; + }; -const parseHtml = str => { - const container = findContainer(str); - container.innerHTML = str; - const res = toArray(container.childNodes); - each(res, el => container.removeChild(el)); - container.innerHTML = ''; - return res; -}; + return str => { + const container = findContainer(str); + container.innerHTML = str; + const res = toArray(container.childNodes); + each(res, el => container.removeChild(el)); + container.innerHTML = ''; + return res; + }; +})(); const queryAll = (selector, context = doc) => { try { return toArray(context.querySelectorAll(selector)); - } catch (err) {/* ignore */} - return []; + } catch (err) { + return []; + } }; 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 removeListener = (el, type, fn) => el.removeEventListener(type, fn); +const reReady = /^(i|c|loade)/; const onReady = fn => { if (reReady.test(doc.readyState)) { fn(); @@ -58,20 +59,6 @@ const onReady = 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 => { if (isInstanceOf(arg, dom)) { return arg; @@ -281,13 +268,7 @@ dom.prototype = { }; module.exports = { - isElement, - isDocument, - isWindow, - isElDocWin, onReady, onLoad, - onResize, - onPrint, dom };