mirror of
https://github.com/google/blockly.git
synced 2026-01-04 15:40:08 +01:00
feat: finish core impl + tests
This adds new tests for the FocusableTreeTraverser and fixes a number of issues with the original implementation (one of which required two new API methods to be added to IFocusableTree). More tests have also been added for FocusManager, and defocusing tracked nodes/trees has been fully implemented in FocusManager.
This commit is contained in:
@@ -62,7 +62,7 @@ export class FocusManager {
|
||||
if (newNode) {
|
||||
this.focusNode(newNode);
|
||||
} else {
|
||||
// TODO: Set previous to passive if all trees are losing active focus.
|
||||
this.defocusCurrentFocusedNode();
|
||||
}
|
||||
});
|
||||
}
|
||||
@@ -259,6 +259,16 @@ export class FocusManager {
|
||||
};
|
||||
}
|
||||
|
||||
private defocusCurrentFocusedNode(): void {
|
||||
// The current node will likely be defocused while ephemeral focus is held,
|
||||
// but internal manager state shouldn't change since the node should be
|
||||
// restored upon exiting ephemeral focus mode.
|
||||
if (this.focusedNode && !this.currentlyHoldsEphemeralFocus) {
|
||||
this.setNodeToPassive(this.focusedNode);
|
||||
this.focusedNode = null;
|
||||
}
|
||||
}
|
||||
|
||||
private setNodeToActive(node: IFocusableNode): void {
|
||||
const element = node.getFocusableElement();
|
||||
dom.addClass(element, 'blocklyActiveFocus');
|
||||
|
||||
@@ -40,6 +40,28 @@ export interface IFocusableTree {
|
||||
*/
|
||||
getRootFocusableNode(): IFocusableNode;
|
||||
|
||||
/**
|
||||
* Returns all directly nested trees under this tree.
|
||||
*
|
||||
* Note that the returned list of trees doesn't need to be stable, however all
|
||||
* returned trees *do* need to be registered with FocusManager. Additionally,
|
||||
* this must return actual nested trees as omitting a nested tree will affect
|
||||
* how focus changes map to a specific node and its tree, potentially leading
|
||||
* to user confusion.
|
||||
*/
|
||||
getNestedTrees(): Array<IFocusableTree>;
|
||||
|
||||
/**
|
||||
* Returns the IFocusableNode corresponding to the specified element ID, or
|
||||
* null if there's no exact node within this tree with that ID or if the ID
|
||||
* corresponds to the root of the tree.
|
||||
*
|
||||
* This will never match against nested trees.
|
||||
*
|
||||
* @param id The ID of the node's focusable HTMLElement or SVGElement.
|
||||
*/
|
||||
lookUpFocusableNode(id: string): IFocusableNode | null;
|
||||
|
||||
/**
|
||||
* Returns the IFocusableNode corresponding to the select element, or null if
|
||||
* the element does not have such a node.
|
||||
@@ -47,7 +69,11 @@ export interface IFocusableTree {
|
||||
* The provided element must have a non-null ID that conforms to the contract
|
||||
* mentioned in IFocusableNode.
|
||||
*
|
||||
* This function may match against the root node of the tree.
|
||||
* This function may match against the root node of the tree. It will also map
|
||||
* against the nearest node to the provided element if the element does not
|
||||
* have an exact matching corresponding node. This function filters out
|
||||
* matches against nested trees, so long as they are represented in the return
|
||||
* value of getNestedTrees.
|
||||
*/
|
||||
findFocusableNodeFor(
|
||||
element: HTMLElement | SVGElement,
|
||||
|
||||
@@ -6,6 +6,7 @@
|
||||
|
||||
import type {IFocusableNode} from '../interfaces/i_focusable_node.js';
|
||||
import type {IFocusableTree} from '../interfaces/i_focusable_tree.js';
|
||||
import * as dom from '../utils/dom.js';
|
||||
|
||||
/**
|
||||
* A helper utility for IFocusableTree implementations to aid with common
|
||||
@@ -24,20 +25,29 @@ export class FocusableTreeTraverser {
|
||||
*/
|
||||
static findFocusedNode(tree: IFocusableTree): IFocusableNode | null {
|
||||
const root = tree.getRootFocusableNode().getFocusableElement();
|
||||
const activeElem = root.querySelector('.blocklyActiveFocus');
|
||||
let active: IFocusableNode | null = null;
|
||||
if (activeElem instanceof HTMLElement || activeElem instanceof SVGElement) {
|
||||
active = tree.findFocusableNodeFor(activeElem);
|
||||
if (
|
||||
dom.hasClass(root, 'blocklyActiveFocus') ||
|
||||
dom.hasClass(root, 'blocklyPassiveFocus')
|
||||
) {
|
||||
// The root has focus.
|
||||
return tree.getRootFocusableNode();
|
||||
}
|
||||
const passiveElems = Array.from(
|
||||
root.querySelectorAll('.blocklyPassiveFocus'),
|
||||
);
|
||||
const passive = passiveElems.map((elem) => {
|
||||
if (elem instanceof HTMLElement || elem instanceof SVGElement) {
|
||||
return tree.findFocusableNodeFor(elem);
|
||||
} else return null;
|
||||
});
|
||||
return active || passive.find((node) => !!node) || null;
|
||||
|
||||
const activeEl = root.querySelector('.blocklyActiveFocus');
|
||||
let active: IFocusableNode | null = null;
|
||||
if (activeEl instanceof HTMLElement || activeEl instanceof SVGElement) {
|
||||
active = tree.findFocusableNodeFor(activeEl);
|
||||
}
|
||||
|
||||
// At most there should be one passive indicator per tree (not considering
|
||||
// subtrees).
|
||||
const passiveEl = root.querySelector('.blocklyPassiveFocus');
|
||||
let passive: IFocusableNode | null = null;
|
||||
if (passiveEl instanceof HTMLElement || passiveEl instanceof SVGElement) {
|
||||
passive = tree.findFocusableNodeFor(passiveEl);
|
||||
}
|
||||
|
||||
return active ?? passive;
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -47,38 +57,42 @@ export class FocusableTreeTraverser {
|
||||
*
|
||||
* If the tree contains another nested IFocusableTree, the nested tree may be
|
||||
* traversed but its nodes will never be returned here per the contract of
|
||||
* findChildById.
|
||||
*
|
||||
* findChildById is a provided callback that takes an element ID and maps it
|
||||
* back to the corresponding IFocusableNode within the provided
|
||||
* IFocusableTree. These IDs will match the contract specified in the
|
||||
* documentation for IFocusableNode. This function must not return any node
|
||||
* that doesn't directly belong to the node's nearest parent tree.
|
||||
* IFocusableTree.lookUpFocusableNode.
|
||||
*
|
||||
* @param element The HTML or SVG element being sought.
|
||||
* @param tree The tree under which the provided element may be a descendant.
|
||||
* @param findChildById The ID->IFocusableNode mapping callback that must
|
||||
* follow the contract mentioned above.
|
||||
* @returns The matching IFocusableNode, or null if there is no match.
|
||||
*/
|
||||
static findFocusableNodeFor(
|
||||
element: HTMLElement | SVGElement,
|
||||
tree: IFocusableTree,
|
||||
findChildById: (id: string) => IFocusableNode | null,
|
||||
): IFocusableNode | null {
|
||||
// First, match against subtrees.
|
||||
const subTreeMatches = tree
|
||||
.getNestedTrees()
|
||||
.map((tree) => tree.findFocusableNodeFor(element));
|
||||
if (subTreeMatches.findIndex((match) => !!match) !== -1) {
|
||||
// At least one subtree has a match for the element so it cannot be part
|
||||
// of the outer tree.
|
||||
return null;
|
||||
}
|
||||
|
||||
// Second, check against the tree's root.
|
||||
if (element === tree.getRootFocusableNode().getFocusableElement()) {
|
||||
return tree.getRootFocusableNode();
|
||||
}
|
||||
const matchedChildNode = findChildById(element.id);
|
||||
|
||||
// Third, check if the element has a node.
|
||||
const matchedChildNode = tree.lookUpFocusableNode(element.id) ?? null;
|
||||
if (matchedChildNode) return matchedChildNode;
|
||||
|
||||
// Fourth, recurse up to find the nearest tree/node if it's possible.
|
||||
const elementParent = element.parentElement;
|
||||
if (!matchedChildNode && elementParent) {
|
||||
// Recurse up to find the nearest tree/node.
|
||||
return FocusableTreeTraverser.findFocusableNodeFor(
|
||||
elementParent,
|
||||
tree,
|
||||
findChildById,
|
||||
);
|
||||
return FocusableTreeTraverser.findFocusableNodeFor(elementParent, tree);
|
||||
}
|
||||
return matchedChildNode;
|
||||
|
||||
// Otherwise, there's no matching node.
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
File diff suppressed because it is too large
Load Diff
480
tests/mocha/focusable_tree_traverser_test.js
Normal file
480
tests/mocha/focusable_tree_traverser_test.js
Normal file
@@ -0,0 +1,480 @@
|
||||
/**
|
||||
* @license
|
||||
* Copyright 2025 Google LLC
|
||||
* SPDX-License-Identifier: Apache-2.0
|
||||
*/
|
||||
|
||||
import {FocusableTreeTraverser} from '../../build/src/core/utils/focusable_tree_traverser.js';
|
||||
import {assert} from '../../node_modules/chai/chai.js';
|
||||
import {
|
||||
sharedTestSetup,
|
||||
sharedTestTeardown,
|
||||
} from './test_helpers/setup_teardown.js';
|
||||
|
||||
suite('FocusableTreeTraverser', function () {
|
||||
setup(function () {
|
||||
sharedTestSetup.call(this);
|
||||
|
||||
const FocusableNodeImpl = function (element, tree) {
|
||||
this.getFocusableElement = function () {
|
||||
return element;
|
||||
};
|
||||
|
||||
this.getFocusableTree = function () {
|
||||
return tree;
|
||||
};
|
||||
};
|
||||
const FocusableTreeImpl = function (rootElement, nestedTrees) {
|
||||
this.idToNodeMap = {};
|
||||
|
||||
this.addNode = function (element) {
|
||||
const node = new FocusableNodeImpl(element, this);
|
||||
this.idToNodeMap[element.id] = node;
|
||||
return node;
|
||||
};
|
||||
|
||||
this.getFocusedNode = function () {
|
||||
throw Error('Unused in test suite.');
|
||||
};
|
||||
|
||||
this.getRootFocusableNode = function () {
|
||||
return this.rootNode;
|
||||
};
|
||||
|
||||
this.getNestedTrees = function () {
|
||||
return nestedTrees;
|
||||
};
|
||||
|
||||
this.lookUpFocusableNode = function (id) {
|
||||
return this.idToNodeMap[id];
|
||||
};
|
||||
|
||||
this.findFocusableNodeFor = function (element) {
|
||||
return FocusableTreeTraverser.findFocusableNodeFor(element, this);
|
||||
};
|
||||
|
||||
this.rootNode = this.addNode(rootElement);
|
||||
};
|
||||
|
||||
const createFocusableTree = function (rootElementId, nestedTrees) {
|
||||
return new FocusableTreeImpl(
|
||||
document.getElementById(rootElementId),
|
||||
nestedTrees || [],
|
||||
);
|
||||
};
|
||||
const createFocusableNode = function (tree, elementId) {
|
||||
return tree.addNode(document.getElementById(elementId));
|
||||
};
|
||||
|
||||
this.testFocusableTree1 = createFocusableTree('testFocusableTree1');
|
||||
this.testFocusableTree1Node1 = createFocusableNode(
|
||||
this.testFocusableTree1,
|
||||
'testFocusableTree1.node1',
|
||||
);
|
||||
this.testFocusableTree1Node1Child1 = createFocusableNode(
|
||||
this.testFocusableTree1,
|
||||
'testFocusableTree1.node1.child1',
|
||||
);
|
||||
this.testFocusableTree1Node2 = createFocusableNode(
|
||||
this.testFocusableTree1,
|
||||
'testFocusableTree1.node2',
|
||||
);
|
||||
this.testFocusableNestedTree4 = createFocusableTree(
|
||||
'testFocusableNestedTree4',
|
||||
);
|
||||
this.testFocusableNestedTree4Node1 = createFocusableNode(
|
||||
this.testFocusableNestedTree4,
|
||||
'testFocusableNestedTree4.node1',
|
||||
);
|
||||
this.testFocusableNestedTree5 = createFocusableTree(
|
||||
'testFocusableNestedTree5',
|
||||
);
|
||||
this.testFocusableNestedTree5Node1 = createFocusableNode(
|
||||
this.testFocusableNestedTree5,
|
||||
'testFocusableNestedTree5.node1',
|
||||
);
|
||||
this.testFocusableTree2 = createFocusableTree('testFocusableTree2', [
|
||||
this.testFocusableNestedTree4,
|
||||
this.testFocusableNestedTree5,
|
||||
]);
|
||||
this.testFocusableTree2Node1 = createFocusableNode(
|
||||
this.testFocusableTree2,
|
||||
'testFocusableTree2.node1',
|
||||
);
|
||||
});
|
||||
|
||||
teardown(function () {
|
||||
sharedTestTeardown.call(this);
|
||||
|
||||
const removeFocusIndicators = function (element) {
|
||||
element.classList.remove('blocklyActiveFocus', 'blocklyPassiveFocus');
|
||||
};
|
||||
|
||||
// Ensure all node CSS styles are reset so that state isn't leaked between tests.
|
||||
removeFocusIndicators(document.getElementById('testFocusableTree1'));
|
||||
removeFocusIndicators(document.getElementById('testFocusableTree1.node1'));
|
||||
removeFocusIndicators(
|
||||
document.getElementById('testFocusableTree1.node1.child1'),
|
||||
);
|
||||
removeFocusIndicators(document.getElementById('testFocusableTree1.node2'));
|
||||
removeFocusIndicators(document.getElementById('testFocusableTree2'));
|
||||
removeFocusIndicators(document.getElementById('testFocusableTree2.node1'));
|
||||
removeFocusIndicators(document.getElementById('testFocusableNestedTree4'));
|
||||
removeFocusIndicators(
|
||||
document.getElementById('testFocusableNestedTree4.node1'),
|
||||
);
|
||||
removeFocusIndicators(document.getElementById('testFocusableNestedTree5'));
|
||||
removeFocusIndicators(
|
||||
document.getElementById('testFocusableNestedTree5.node1'),
|
||||
);
|
||||
});
|
||||
|
||||
suite('findFocusedNode()', function () {
|
||||
test('for tree with no highlights returns null', function () {
|
||||
const tree = this.testFocusableTree1;
|
||||
|
||||
const finding = FocusableTreeTraverser.findFocusedNode(tree);
|
||||
|
||||
assert.isNull(finding);
|
||||
});
|
||||
|
||||
test('for tree with root active highlight returns root node', function () {
|
||||
const tree = this.testFocusableTree1;
|
||||
const rootNode = tree.getRootFocusableNode();
|
||||
rootNode.getFocusableElement().classList.add('blocklyActiveFocus');
|
||||
|
||||
const finding = FocusableTreeTraverser.findFocusedNode(tree);
|
||||
|
||||
assert.equal(finding, rootNode);
|
||||
});
|
||||
|
||||
test('for tree with root passive highlight returns root node', function () {
|
||||
const tree = this.testFocusableTree1;
|
||||
const rootNode = tree.getRootFocusableNode();
|
||||
rootNode.getFocusableElement().classList.add('blocklyPassiveFocus');
|
||||
|
||||
const finding = FocusableTreeTraverser.findFocusedNode(tree);
|
||||
|
||||
assert.equal(finding, rootNode);
|
||||
});
|
||||
|
||||
test('for tree with node active highlight returns node', function () {
|
||||
const tree = this.testFocusableTree1;
|
||||
const node = this.testFocusableTree1Node1;
|
||||
node.getFocusableElement().classList.add('blocklyActiveFocus');
|
||||
|
||||
const finding = FocusableTreeTraverser.findFocusedNode(tree);
|
||||
|
||||
assert.equal(finding, node);
|
||||
});
|
||||
|
||||
test('for tree with node passive highlight returns node', function () {
|
||||
const tree = this.testFocusableTree1;
|
||||
const node = this.testFocusableTree1Node1;
|
||||
node.getFocusableElement().classList.add('blocklyPassiveFocus');
|
||||
|
||||
const finding = FocusableTreeTraverser.findFocusedNode(tree);
|
||||
|
||||
assert.equal(finding, node);
|
||||
});
|
||||
|
||||
test('for tree with nested node active highlight returns node', function () {
|
||||
const tree = this.testFocusableTree1;
|
||||
const node = this.testFocusableTree1Node1Child1;
|
||||
node.getFocusableElement().classList.add('blocklyActiveFocus');
|
||||
|
||||
const finding = FocusableTreeTraverser.findFocusedNode(tree);
|
||||
|
||||
assert.equal(finding, node);
|
||||
});
|
||||
|
||||
test('for tree with nested node passive highlight returns node', function () {
|
||||
const tree = this.testFocusableTree1;
|
||||
const node = this.testFocusableTree1Node1Child1;
|
||||
node.getFocusableElement().classList.add('blocklyPassiveFocus');
|
||||
|
||||
const finding = FocusableTreeTraverser.findFocusedNode(tree);
|
||||
|
||||
assert.equal(finding, node);
|
||||
});
|
||||
|
||||
test('for tree with nested tree root active no parent highlights returns null', function () {
|
||||
const tree = this.testFocusableNestedTree4;
|
||||
const rootNode = this.testFocusableNestedTree4.getRootFocusableNode();
|
||||
rootNode.getFocusableElement().classList.add('blocklyActiveFocus');
|
||||
|
||||
const finding = FocusableTreeTraverser.findFocusedNode(tree);
|
||||
|
||||
assert.equal(finding, rootNode);
|
||||
});
|
||||
|
||||
test('for tree with nested tree root passive no parent highlights returns null', function () {
|
||||
const tree = this.testFocusableNestedTree4;
|
||||
const rootNode = this.testFocusableNestedTree4.getRootFocusableNode();
|
||||
rootNode.getFocusableElement().classList.add('blocklyPassiveFocus');
|
||||
|
||||
const finding = FocusableTreeTraverser.findFocusedNode(tree);
|
||||
|
||||
assert.equal(finding, rootNode);
|
||||
});
|
||||
|
||||
test('for tree with nested tree root active no parent highlights returns null', function () {
|
||||
const tree = this.testFocusableNestedTree4;
|
||||
const node = this.testFocusableNestedTree4Node1;
|
||||
node.getFocusableElement().classList.add('blocklyActiveFocus');
|
||||
|
||||
const finding = FocusableTreeTraverser.findFocusedNode(tree);
|
||||
|
||||
assert.equal(finding, node);
|
||||
});
|
||||
|
||||
test('for tree with nested tree root passive no parent highlights returns null', function () {
|
||||
const tree = this.testFocusableNestedTree4;
|
||||
const node = this.testFocusableNestedTree4Node1;
|
||||
node.getFocusableElement().classList.add('blocklyPassiveFocus');
|
||||
|
||||
const finding = FocusableTreeTraverser.findFocusedNode(tree);
|
||||
|
||||
assert.equal(finding, node);
|
||||
});
|
||||
|
||||
test('for tree with nested tree root active parent node passive returns parent node', function () {
|
||||
const tree = this.testFocusableNestedTree4;
|
||||
const rootNode = this.testFocusableNestedTree4.getRootFocusableNode();
|
||||
this.testFocusableTree2Node1
|
||||
.getFocusableElement()
|
||||
.classList.add('blocklyPassiveFocus');
|
||||
rootNode.getFocusableElement().classList.add('blocklyActiveFocus');
|
||||
|
||||
const finding = FocusableTreeTraverser.findFocusedNode(
|
||||
this.testFocusableTree2,
|
||||
);
|
||||
|
||||
assert.equal(finding, this.testFocusableTree2Node1);
|
||||
});
|
||||
|
||||
test('for tree with nested tree root passive parent node passive returns parent node', function () {
|
||||
const tree = this.testFocusableNestedTree4;
|
||||
const rootNode = this.testFocusableNestedTree4.getRootFocusableNode();
|
||||
this.testFocusableTree2Node1
|
||||
.getFocusableElement()
|
||||
.classList.add('blocklyPassiveFocus');
|
||||
rootNode.getFocusableElement().classList.add('blocklyPassiveFocus');
|
||||
|
||||
const finding = FocusableTreeTraverser.findFocusedNode(
|
||||
this.testFocusableTree2,
|
||||
);
|
||||
|
||||
assert.equal(finding, this.testFocusableTree2Node1);
|
||||
});
|
||||
|
||||
test('for tree with nested tree node active parent node passive returns parent node', function () {
|
||||
const tree = this.testFocusableNestedTree4;
|
||||
const node = this.testFocusableNestedTree4Node1;
|
||||
this.testFocusableTree2Node1
|
||||
.getFocusableElement()
|
||||
.classList.add('blocklyPassiveFocus');
|
||||
node.getFocusableElement().classList.add('blocklyActiveFocus');
|
||||
|
||||
const finding = FocusableTreeTraverser.findFocusedNode(
|
||||
this.testFocusableTree2,
|
||||
);
|
||||
|
||||
assert.equal(finding, this.testFocusableTree2Node1);
|
||||
});
|
||||
|
||||
test('for tree with nested tree node passive parent node passive returns parent node', function () {
|
||||
const tree = this.testFocusableNestedTree4;
|
||||
const node = this.testFocusableNestedTree4Node1;
|
||||
this.testFocusableTree2Node1
|
||||
.getFocusableElement()
|
||||
.classList.add('blocklyPassiveFocus');
|
||||
node.getFocusableElement().classList.add('blocklyPassiveFocus');
|
||||
|
||||
const finding = FocusableTreeTraverser.findFocusedNode(
|
||||
this.testFocusableTree2,
|
||||
);
|
||||
|
||||
assert.equal(finding, this.testFocusableTree2Node1);
|
||||
});
|
||||
});
|
||||
|
||||
suite('findFocusableNodeFor()', function () {
|
||||
test('for root element returns root', function () {
|
||||
const tree = this.testFocusableTree1;
|
||||
const rootNode = tree.getRootFocusableNode();
|
||||
const rootElem = rootNode.getFocusableElement();
|
||||
|
||||
const finding = FocusableTreeTraverser.findFocusableNodeFor(
|
||||
rootElem,
|
||||
tree,
|
||||
);
|
||||
|
||||
assert.equal(finding, rootNode);
|
||||
});
|
||||
|
||||
test('for element for different tree root returns null', function () {
|
||||
const tree = this.testFocusableTree1;
|
||||
const rootNode = this.testFocusableTree2.getRootFocusableNode();
|
||||
const rootElem = rootNode.getFocusableElement();
|
||||
|
||||
const finding = FocusableTreeTraverser.findFocusableNodeFor(
|
||||
rootElem,
|
||||
tree,
|
||||
);
|
||||
|
||||
assert.isNull(finding);
|
||||
});
|
||||
|
||||
test('for element for different tree node returns null', function () {
|
||||
const tree = this.testFocusableTree1;
|
||||
const nodeElem = this.testFocusableTree2Node1.getFocusableElement();
|
||||
|
||||
const finding = FocusableTreeTraverser.findFocusableNodeFor(
|
||||
nodeElem,
|
||||
tree,
|
||||
);
|
||||
|
||||
assert.isNull(finding);
|
||||
});
|
||||
|
||||
test('for node element in tree returns node', function () {
|
||||
const tree = this.testFocusableTree1;
|
||||
const nodeElem = this.testFocusableTree1Node1.getFocusableElement();
|
||||
|
||||
const finding = FocusableTreeTraverser.findFocusableNodeFor(
|
||||
nodeElem,
|
||||
tree,
|
||||
);
|
||||
|
||||
assert.equal(finding, this.testFocusableTree1Node1);
|
||||
});
|
||||
|
||||
test('for non-node element in tree returns root', function () {
|
||||
const tree = this.testFocusableTree1;
|
||||
const unregElem = document.getElementById(
|
||||
'testFocusableTree1.node2.unregisteredChild1',
|
||||
);
|
||||
|
||||
const finding = FocusableTreeTraverser.findFocusableNodeFor(
|
||||
unregElem,
|
||||
tree,
|
||||
);
|
||||
|
||||
// An unregistered element should map to the closest node.
|
||||
assert.equal(finding, this.testFocusableTree1Node2);
|
||||
});
|
||||
|
||||
test('for nested node element in tree returns node', function () {
|
||||
const tree = this.testFocusableTree1;
|
||||
const nodeElem = this.testFocusableTree1Node1Child1.getFocusableElement();
|
||||
|
||||
const finding = FocusableTreeTraverser.findFocusableNodeFor(
|
||||
nodeElem,
|
||||
tree,
|
||||
);
|
||||
|
||||
// The nested node should be returned.
|
||||
assert.equal(finding, this.testFocusableTree1Node1Child1);
|
||||
});
|
||||
|
||||
test('for nested node element in tree returns node', function () {
|
||||
const tree = this.testFocusableTree1;
|
||||
const unregElem = document.getElementById(
|
||||
'testFocusableTree1.node1.child1.unregisteredChild1',
|
||||
);
|
||||
|
||||
const finding = FocusableTreeTraverser.findFocusableNodeFor(
|
||||
unregElem,
|
||||
tree,
|
||||
);
|
||||
|
||||
// An unregistered element should map to the closest node.
|
||||
assert.equal(finding, this.testFocusableTree1Node1Child1);
|
||||
});
|
||||
|
||||
test('for nested node element in tree returns node', function () {
|
||||
const tree = this.testFocusableTree1;
|
||||
const unregElem = document.getElementById(
|
||||
'testFocusableTree1.unregisteredChild1',
|
||||
);
|
||||
|
||||
const finding = FocusableTreeTraverser.findFocusableNodeFor(
|
||||
unregElem,
|
||||
tree,
|
||||
);
|
||||
|
||||
// An unregistered element should map to the closest node (or root).
|
||||
assert.equal(finding, tree.getRootFocusableNode());
|
||||
});
|
||||
|
||||
test('for nested tree root returns nested tree root', function () {
|
||||
const tree = this.testFocusableNestedTree4;
|
||||
const rootNode = tree.getRootFocusableNode();
|
||||
const rootElem = rootNode.getFocusableElement();
|
||||
|
||||
const finding = FocusableTreeTraverser.findFocusableNodeFor(
|
||||
rootElem,
|
||||
tree,
|
||||
);
|
||||
|
||||
assert.equal(finding, rootNode);
|
||||
});
|
||||
|
||||
test('for nested tree node returns nested tree node', function () {
|
||||
const tree = this.testFocusableNestedTree4;
|
||||
const nodeElem = this.testFocusableNestedTree4Node1.getFocusableElement();
|
||||
|
||||
const finding = FocusableTreeTraverser.findFocusableNodeFor(
|
||||
nodeElem,
|
||||
tree,
|
||||
);
|
||||
|
||||
// The node of the nested tree should be returned.
|
||||
assert.equal(finding, this.testFocusableNestedTree4Node1);
|
||||
});
|
||||
|
||||
test('for nested element in nested tree node returns nearest nested node', function () {
|
||||
const tree = this.testFocusableNestedTree4;
|
||||
const unregElem = document.getElementById(
|
||||
'testFocusableNestedTree4.node1.unregisteredChild1',
|
||||
);
|
||||
|
||||
const finding = FocusableTreeTraverser.findFocusableNodeFor(
|
||||
unregElem,
|
||||
tree,
|
||||
);
|
||||
|
||||
// An unregistered element should map to the closest node.
|
||||
assert.equal(finding, this.testFocusableNestedTree4Node1);
|
||||
});
|
||||
|
||||
test('for nested tree node under root with different tree base returns null', function () {
|
||||
const tree = this.testFocusableTree2;
|
||||
const nodeElem = this.testFocusableNestedTree5Node1.getFocusableElement();
|
||||
|
||||
const finding = FocusableTreeTraverser.findFocusableNodeFor(
|
||||
nodeElem,
|
||||
tree,
|
||||
);
|
||||
|
||||
// The nested node hierarchically sits below the outer tree, but using
|
||||
// that tree as the basis should yield null since it's not a direct child.
|
||||
assert.isNull(finding);
|
||||
});
|
||||
|
||||
test('for nested tree node under node with different tree base returns null', function () {
|
||||
const tree = this.testFocusableTree2;
|
||||
const nodeElem = this.testFocusableNestedTree4Node1.getFocusableElement();
|
||||
|
||||
const finding = FocusableTreeTraverser.findFocusableNodeFor(
|
||||
nodeElem,
|
||||
tree,
|
||||
);
|
||||
|
||||
// The nested node hierarchically sits below the outer tree, but using
|
||||
// that tree as the basis should yield null since it's not a direct child.
|
||||
assert.isNull(finding);
|
||||
});
|
||||
});
|
||||
});
|
||||
@@ -35,26 +35,57 @@
|
||||
fill: #00f;
|
||||
}
|
||||
</style>
|
||||
<body>
|
||||
<body tabindex="-1">
|
||||
<div id="mocha"></div>
|
||||
<div id="failureCount" style="display: none" tests_failed="unset"></div>
|
||||
<div id="failureMessages" style="display: none"></div>
|
||||
<div id="testFocusableTree1" tabindex="-1">
|
||||
<div id="testFocusableTree1.node1" tabindex="-1">
|
||||
Focusable tree 1
|
||||
<div id="testFocusableTree1.node1" style="margin-left: 1em"; tabindex="-1">
|
||||
Tree 1 node 1
|
||||
<div id="testFocusableTree1.node1.child1" tabindex="-1">Tree 1 node 1 child 1</div>
|
||||
<div id="testFocusableTree1.node1.child1" style="margin-left: 2em"; tabindex="-1">
|
||||
Tree 1 node 1 child 1
|
||||
<div id="testFocusableTree1.node1.child1.unregisteredChild1" style="margin-left: 3em"; tabindex="-1">
|
||||
Tree 1 node 1 child 1 child 1 (unregistered)
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
<div id="testFocusableTree1.node2" tabindex="-1">
|
||||
<div id="testFocusableTree1.node2" style="margin-left: 1em"; tabindex="-1">
|
||||
Tree 1 node 2
|
||||
<div id="testFocusableTree1.node2.unregisteredChild1" tabindex="-1">Tree 1 node 2 child 2 (unregistered)</div>
|
||||
<div id="testFocusableTree1.node2.unregisteredChild1" style="margin-left: 2em"; tabindex="-1">
|
||||
Tree 1 node 2 child 2 (unregistered)
|
||||
</div>
|
||||
</div>
|
||||
<div id="testFocusableTree1.unregisteredChild1" style="margin-left: 1em"; tabindex="-1">
|
||||
Tree 1 child 1 (unregistered)
|
||||
</div>
|
||||
</div>
|
||||
<div id="testFocusableTree2" tabindex="-1">
|
||||
<div id="testFocusableTree2.node1" tabindex="-1">Tree 2 node 1</div>
|
||||
Focusable tree 2
|
||||
<div id="testFocusableTree2.node1" style="margin-left: 1em"; tabindex="-1">
|
||||
Tree 2 node 1
|
||||
<div id="testFocusableNestedTree4" style="margin-left: 2em"; tabindex="-1">
|
||||
Nested tree 4
|
||||
<div id="testFocusableNestedTree4.node1" style="margin-left: 3em"; tabindex="-1">
|
||||
Tree 4 node 1 (nested)
|
||||
<div id="testFocusableNestedTree4.node1.unregisteredChild1" style="margin-left: 4em"; tabindex="-1">
|
||||
Tree 4 node 1 child 1 (unregistered)
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
<div id="testFocusableNestedTree5" style="margin-left: 1em"; tabindex="-1">
|
||||
Nested tree 5
|
||||
<div id="testFocusableNestedTree5.node1" style="margin-left: 2em"; tabindex="-1">Tree 5 node 1 (nested)</div>
|
||||
</div>
|
||||
</div>
|
||||
<div id="testUnregisteredFocusableTree3" tabindex="-1">
|
||||
<div id="testUnregisteredFocusableTree3.node1" tabindex="-1">Tree 3 node 1 (unregistered)</div>
|
||||
Unregistered tree 3
|
||||
<div id="testUnregisteredFocusableTree3.node1" style="margin-left: 1em"; tabindex="-1">
|
||||
Tree 3 node 1 (unregistered)
|
||||
</div>
|
||||
</div>
|
||||
<div id="testUnfocusableElement">Unfocusable element</div>
|
||||
<div id="nonTreeElementForEphemeralFocus" tabindex="-1" />
|
||||
<svg width="250" height="250">
|
||||
<g id="testFocusableGroup1" tabindex="-1">
|
||||
@@ -71,7 +102,9 @@
|
||||
<text x="10" y="80" class="svgText">Group 1 node 2</text>
|
||||
<g id="testFocusableGroup1.node2.unregisteredChild1" tabindex="-1">
|
||||
<rect x="0" y="90" width="250" height="30" fill="lightgrey" />
|
||||
<text x="10" y="110" class="svgText">Tree 1 node 2 child 2 (unregistered)</text>
|
||||
<text x="10" y="110" class="svgText">
|
||||
Tree 1 node 2 child 2 (unregistered)
|
||||
</text>
|
||||
</g>
|
||||
</g>
|
||||
</g>
|
||||
@@ -80,11 +113,19 @@
|
||||
<rect x="0" y="120" width="250" height="30" fill="grey" />
|
||||
<text x="10" y="140" class="svgText">Group 2 node 1</text>
|
||||
</g>
|
||||
<g id="testFocusableNestedGroup4" tabindex="-1">
|
||||
<g id="testFocusableNestedGroup4.node1" tabindex="-1">
|
||||
<rect x="0" y="150" width="250" height="30" fill="lightgrey" />
|
||||
<text x="10" y="170" class="svgText">Group 4 node 1 (nested)</text>
|
||||
</g>
|
||||
</g>
|
||||
</g>
|
||||
<g id="testUnregisteredFocusableGroup3" tabindex="-1">
|
||||
<g id="testUnregisteredFocusableGroup3.node1" tabindex="-1">
|
||||
<rect x="0" y="150" width="250" height="30" fill="grey" />
|
||||
<text x="10" y="170" class="svgText">Tree 3 node 1 (unregistered)</text>
|
||||
<rect x="0" y="180" width="250" height="30" fill="grey" />
|
||||
<text x="10" y="200" class="svgText">
|
||||
Tree 3 node 1 (unregistered)
|
||||
</text>
|
||||
</g>
|
||||
</g>
|
||||
<g id="nonTreeGroupForEphemeralFocus" tabindex="-1"></g>
|
||||
@@ -162,6 +203,7 @@
|
||||
import './field_variable_test.js';
|
||||
import './flyout_test.js';
|
||||
import './focus_manager_test.js';
|
||||
import './focusable_tree_traverser_test.js';
|
||||
// import './test_event_reduction.js';
|
||||
import './generator_test.js';
|
||||
import './gesture_test.js';
|
||||
|
||||
Reference in New Issue
Block a user