Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Improve perf of axe.run [WWD-1821] #1503

Merged
merged 18 commits into from
Apr 30, 2019
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions lib/core/public/run-rules.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

// Clean up after resolve / reject
function cleanup() {
axe._nodeMap = undefined;
axe._tree = undefined;
axe._selectorData = undefined;
}
Expand All @@ -18,6 +19,7 @@ function cleanup() {
function runRules(context, options, resolve, reject) {
'use strict';
try {
// axe._nodeMap = new WeakMap();
straker marked this conversation as resolved.
Show resolved Hide resolved
context = new Context(context);
axe._tree = context.flatTree;
axe._selectorData = axe.utils.getSelectorData(context.flatTree);
Expand Down
23 changes: 6 additions & 17 deletions lib/core/utils/flattened-tree.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ var axe = axe || { utils: {} };
*/
function virtualDOMfromNode(node, shadowId) {
const vNodeCache = {};
return {
const vNode = {
shadowId: shadowId,
children: [],
actualNode: node,
Expand All @@ -47,6 +47,10 @@ function virtualDOMfromNode(node, shadowId) {
return vNodeCache._tabbableElements;
}
};
if (axe._nodeMap) {
axe._nodeMap.set(node, vNode);
}
return vNode;
}

/**
Expand Down Expand Up @@ -151,20 +155,5 @@ axe.utils.getFlattenedTree = function(node, shadowId) {
* @param {Node} node The HTML DOM node
*/
axe.utils.getNodeFromTree = function(vNode, node) {
var found;

if (vNode.actualNode === node) {
return vNode;
}
vNode.children.forEach(candidate => {
if (found) {
return;
}
if (candidate.actualNode === node) {
found = candidate;
} else {
found = axe.utils.getNodeFromTree(candidate, node);
}
});
return found;
return axe._nodeMap.get(node);
};
12 changes: 9 additions & 3 deletions lib/core/utils/is-hidden.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,8 @@
*/
axe.utils.isHidden = function isHidden(el, recursed) {
'use strict';
var parent;
var node = axe.utils.getNodeFromTree(axe._tree[0], el);
var parent, isHidden;

// 9 === Node.DOCUMENT
if (el.nodeType === 9) {
Expand All @@ -20,6 +21,10 @@ axe.utils.isHidden = function isHidden(el, recursed) {
el = el.host; // grab the host Node
}

if (typeof node.isHidden !== 'undefined') {
return node.isHidden;
}

var style = window.getComputedStyle(el, null);

if (
Expand All @@ -35,6 +40,7 @@ axe.utils.isHidden = function isHidden(el, recursed) {
}

parent = el.assignedSlot ? el.assignedSlot : el.parentNode;

return axe.utils.isHidden(parent, true);
isHidden = axe.utils.isHidden(parent, true);
node.isHidden = isHidden;
return isHidden;
};