/** * Copyright 2015 The Incremental DOM Authors. * Copyright 2017 trivago N.V. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS-IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ /** * @param {!Node} node * @return {boolean} True if the node the root of a document, false otherwise. */ const isDocumentRoot = function(node) { // For ShadowRoots, check if they are a DocumentFragment instead of if they // are a ShadowRoot so that this can work in 'use strict' if ShadowRoots are // not supported. return node instanceof Document || node instanceof DocumentFragment; }; /** * @param {!Node} node The node to start at, inclusive. * @param {?Node} root The root ancestor to get until, exclusive. * @return {!Array} The ancestry of DOM nodes. */ const getAncestry = function(node, root) { const ancestry = []; let cur = node; while (cur !== root) { ancestry.push(cur); cur = cur.parentNode; } return ancestry; }; /** * @param {!Node} node * @return {!Node} The root node of the DOM tree that contains node. */ const getRoot = function(node) { let cur = node; let prev = cur; while (cur) { prev = cur; cur = cur.parentNode; } return prev; }; /** * @param {!Node} node The node to get the activeElement for. * @return {?Element} The activeElement in the Document or ShadowRoot * corresponding to node, if present. */ const getActiveElement = function(node) { const root = getRoot(node); return isDocumentRoot(root) ? root.activeElement : null; }; /** * Function that checks whether some element is contained inside a node. This function * has a fallback implementation for cases where Node.prototype.contains is * not implemented (e.g. IE11 for SVGElements). * @param {!Node} node The node that should be parent of elm. * @param {?Element} elm The element that should be child of elm. * @return {Boolean} whether or not elm is contained within node. */ const nodeContainsElement = function(node, elm) { if (node.contains) { return node.contains(elm); } while (elm && elm !== node) { elm = elm.parentNode; } return elm === node; }; /** * Gets the path of nodes that contain the focused node in the same document as * a reference node, up until the root. * @param {!Node} node The reference node to get the activeElement for. * @param {?Node} root The root to get the focused path until. * @return {!Array} */ const getFocusedPath = function(node, root) { const activeElement = getActiveElement(node); if (!activeElement || !nodeContainsElement(node, activeElement)) { return []; } return getAncestry(activeElement, root); }; /** * Like insertBefore, but instead instead of moving the desired node, instead * moves all the other nodes after. * @param {?Node} parentNode * @param {!Node} node * @param {?Node} referenceNode */ const moveBefore = function(parentNode, node, referenceNode) { const insertReferenceNode = node.nextSibling; let cur = referenceNode; while (cur && cur !== node) { const next = cur.nextSibling; parentNode.insertBefore(cur, insertReferenceNode); cur = next; } }; /** */ export { getFocusedPath, moveBefore };