From 8d693884bd93ee067236782d21c5f5740c5ecc16 Mon Sep 17 00:00:00 2001 From: Steven Lambert Date: Wed, 3 Jun 2020 09:04:53 -0600 Subject: [PATCH] feat(utils.getFlattenTree): default to documentElement --- lib/core/utils/get-flattened-tree.js | 10 +++++----- test/core/utils/flattened-tree.js | 6 ++++++ 2 files changed, 11 insertions(+), 5 deletions(-) diff --git a/lib/core/utils/get-flattened-tree.js b/lib/core/utils/get-flattened-tree.js index 3606399d6e..c98c0e8609 100644 --- a/lib/core/utils/get-flattened-tree.js +++ b/lib/core/utils/get-flattened-tree.js @@ -1,5 +1,6 @@ import isShadowRoot from './is-shadow-root'; import VirtualNode from '../base/virtual-node/virtual-node'; +import cache from '../base/cache'; /** * This implemnts the flatten-tree algorithm specified: @@ -134,13 +135,12 @@ function flattenTree(node, shadowId, parent) { * Recursvely returns an array of the virtual DOM nodes at this level * excluding comment nodes and the shadow DOM nodes and * - * @param {Node} node the current node - * @param {String} shadowId, optional ID of the shadow DOM that is the closest shadow + * @param {Node} [node=document.documentElement] optional node. NOTE: passing in anything other than body or the documentElement may result in incomplete results. + * @param {String} [shadowId] optional ID of the shadow DOM that is the closest shadow * ancestor of the node */ -function getFlattenedTree(node, shadowId) { - // TODO: es-modules_cache - axe._cache.set('nodeMap', new WeakMap()); +function getFlattenedTree(node = document.documentElement, shadowId) { + cache.set('nodeMap', new WeakMap()); return flattenTree(node, shadowId); } diff --git a/test/core/utils/flattened-tree.js b/test/core/utils/flattened-tree.js index 66ce30968b..1c9dd8dda3 100644 --- a/test/core/utils/flattened-tree.js +++ b/test/core/utils/flattened-tree.js @@ -75,6 +75,12 @@ describe('axe.utils.getFlattenedTree', function() { ); } + it('should default to document', function() { + fixture.innerHTML = ''; + var tree = axe.utils.getFlattenedTree(); + assert(tree[0].actualNode === document.documentElement); + }); + if (shadowSupport.v0) { describe('shadow DOM v0', function() { afterEach(function() {