UNPKG

tiny-dream

Version:

A tiny Incremental DOM implementation for modern browsers.

98 lines (77 loc) 1.98 kB
function EmptyObject() {} EmptyObject.prototype = Object.create(null) const createEmptyObject = () => { return new EmptyObject() } const cachedHasOwnProperty = Object.prototype.hasOwnProperty const hasOwnProperty = (map, property) => { return cachedHasOwnProperty.call(map, property) } const nextSibling = (node) => { if (!node) { return null } if (hasOwnProperty(node, 'nextSibling')) { return node.nextSibling } const childNodes = node && node.parentNode && node.parentNode.childNodes for (var i = childNodes.length - 1; i >= 0; i--) { if (i === childNodes.length) { return null } const thisNode = childNodes[i] if (thisNode === node) { return childNodes[i + 1] } } return null } const isDocumentRoot = (node) => { // TODO check this is accurate return node === document } const detectAncestors = (node, root) => { const ancestors = [] let current = node while (current !== root) { ancestors.push(current) current = current.parentNode } return ancestors } const detectRootNode = () => { let current = this let prev = current while (current) { prev = current current = current.parentNode } return prev } const detectActiveNode = (node) => { const rootNode = detectRootNode.call(node) return isDocumentRoot(rootNode) ? rootNode.activeElement : null } const detectFocusedAncestors = (node, rootNode) => { const activeNode = detectActiveNode(node) if (!activeNode || !node.contains(activeNode)) { return [] } return detectAncestors(activeNode, rootNode) } const moveBefore = (parentNode, node, referenceNode) => { const insertReferenceNode = node.nextSibling let current = referenceNode while (current !== node) { const next = current.nextSibling parentNode.insertBefore(current, insertReferenceNode) current = next } } export { createEmptyObject, hasOwnProperty, nextSibling, detectFocusedAncestors, moveBefore }