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: 1 addition & 1 deletion lib/checks/aria/required-children.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ function ariaOwns(nodes, role) {
if (nodes[index] === null) {
continue;
}
let virtualTree = axe.utils.getNodeFromTree(axe._tree[0], nodes[index]);
let virtualTree = axe.utils.getNodeFromTree(nodes[index]);
straker marked this conversation as resolved.
Show resolved Hide resolved
if (owns(nodes[index], virtualTree, role, true)) {
return true;
}
Expand Down
2 changes: 1 addition & 1 deletion lib/checks/aria/required-parent.js
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ var owners = getAriaOwners(node);
if (owners) {
for (var i = 0, l = owners.length; i < l; i++) {
missingParents = getMissingContext(
axe.utils.getNodeFromTree(axe._tree[0], owners[i]),
axe.utils.getNodeFromTree(owners[i]),
missingParents,
true
);
Expand Down
2 changes: 1 addition & 1 deletion lib/commons/aria/get-owned-virtual.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ aria.getOwnedVirtual = function getOwned({ actualNode, children }) {

return dom.idrefs(actualNode, 'aria-owns').reduce((ownedElms, element) => {
if (element) {
const virtualNode = axe.utils.getNodeFromTree(axe._tree[0], element);
const virtualNode = axe.utils.getNodeFromTree(element);
ownedElms.push(virtualNode);
}
return ownedElms;
Expand Down
40 changes: 8 additions & 32 deletions lib/commons/aria/is-accessible-ref.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,39 +17,15 @@ function findDomNode(node, functor) {
* @returns {Boolean}
*/
aria.isAccessibleRef = function isAccessibleRef(node) {


// if while we generated the virtual node for each node we looked at
// the attributes of the node to see if it had any idref type attributes,
// we could store all those ids into a map and then this function would
// just have to look up the info in the map instead of the entire tree

node = node.actualNode || node;
let root = dom.getRootNode(node);
root = root.documentElement || root; // account for shadow roots
const id = node.id;

// Get all idref(s) attributes on the lookup table
const refAttrs = Object.keys(aria.lookupTable.attributes).filter(attr => {
const { type } = aria.lookupTable.attributes[attr];
return /^idrefs?$/.test(type);
});

// Find the first element that IDREF(S) the node
let refElm = findDomNode(root, elm => {
if (elm.nodeType !== 1) {
// Elements only
return;
}
if (
elm.nodeName.toUpperCase() === 'LABEL' &&
elm.getAttribute('for') === id
) {
return true;
}
// See if there are any aria attributes that reference the node
return refAttrs
.filter(attr => elm.hasAttribute(attr))
.some(attr => {
const attrValue = elm.getAttribute(attr);
if (aria.lookupTable.attributes[attr].type === 'idref') {
return attrValue === id;
}
return axe.utils.tokenList(attrValue).includes(id);
});
});
return typeof refElm !== 'undefined';
return typeof axe._idRefCache[id] !== 'undefined';
};
4 changes: 2 additions & 2 deletions lib/commons/aria/label-virtual.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ aria.labelVirtual = function({ actualNode }) {
ref = dom.idrefs(actualNode, 'aria-labelledby');
candidate = ref
.map(function(thing) {
const vNode = axe.utils.getNodeFromTree(axe._tree[0], thing);
const vNode = axe.utils.getNodeFromTree(thing);
return vNode ? text.visibleVirtual(vNode, true) : '';
})
.join(' ')
Expand Down Expand Up @@ -49,6 +49,6 @@ aria.labelVirtual = function({ actualNode }) {
* @return {Mixed} String of visible text, or `null` if no label is found
*/
aria.label = function(node) {
node = axe.utils.getNodeFromTree(axe._tree[0], node);
node = axe.utils.getNodeFromTree(node);
return aria.labelVirtual(node);
};
2 changes: 1 addition & 1 deletion lib/commons/dom/find-up.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
*/
dom.findUp = function(element, target) {
return dom.findUpVirtual(
axe.utils.getNodeFromTree(axe._tree[0], element),
axe.utils.getNodeFromTree(element),
target
);
};
Expand Down
2 changes: 1 addition & 1 deletion lib/commons/dom/has-content-virtual.js
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ dom.hasContentVirtual = function(elm, noRecursion) {
* @return {Boolean}
*/
dom.hasContent = function hasContent(elm, noRecursion) {
elm = axe.utils.getNodeFromTree(axe._tree[0], elm);
elm = axe.utils.getNodeFromTree(elm);
return dom.hasContentVirtual(elm, noRecursion);
};

Expand Down
2 changes: 1 addition & 1 deletion lib/commons/dom/is-in-text-block.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ function getBlockParent(node) {
while (parentBlock && !isBlock(parentBlock)) {
parentBlock = dom.getComposedParent(parentBlock);
}
return axe.utils.getNodeFromTree(axe._tree[0], parentBlock);
return axe.utils.getNodeFromTree(parentBlock);
}

/**
Expand Down
2 changes: 1 addition & 1 deletion lib/commons/text/accessible-text-virtual.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
* @return {string}
*/
text.accessibleText = function accessibleText(element, context) {
let virtualNode = axe.utils.getNodeFromTree(axe._tree[0], element); // throws an exception on purpose if axe._tree not correct
let virtualNode = axe.utils.getNodeFromTree(element); // throws an exception on purpose if axe._tree not correct
straker marked this conversation as resolved.
Show resolved Hide resolved
return text.accessibleTextVirtual(virtualNode, context);
};

Expand Down
2 changes: 1 addition & 1 deletion lib/commons/text/label-virtual.js
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,6 @@ text.labelVirtual = function(node) {
* @return {Mixed} String of visible text, or `null` if no label is found
*/
text.label = function(node) {
node = axe.utils.getNodeFromTree(axe._tree[0], node);
node = axe.utils.getNodeFromTree(node);
return text.labelVirtual(node);
};
2 changes: 1 addition & 1 deletion lib/commons/text/visible-virtual.js
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,6 @@ text.visibleVirtual = function(element, screenReader, noRecursing) {
* @return {String}
*/
text.visible = function(element, screenReader, noRecursing) {
element = axe.utils.getNodeFromTree(axe._tree[0], element);
element = axe.utils.getNodeFromTree(element);
return text.visibleVirtual(element, screenReader, noRecursing);
};
6 changes: 3 additions & 3 deletions lib/core/base/context.js
Original file line number Diff line number Diff line change
Expand Up @@ -135,7 +135,7 @@ function parseSelectorArray(context, type) {
//eslint no-loop-func:0
result = result.concat(
nodeList.map(node => {
return axe.utils.getNodeFromTree(context.flatTree[0], node);
return axe.utils.getNodeFromTree(node);
})
);
break;
Expand All @@ -147,15 +147,15 @@ function parseSelectorArray(context, type) {
//eslint no-loop-func:0
result = result.concat(
nodeList.map(node => {
return axe.utils.getNodeFromTree(context.flatTree[0], node);
return axe.utils.getNodeFromTree(node);
})
);
}
} else if (item instanceof Node) {
if (item.documentElement instanceof Node) {
result.push(context.flatTree[0]);
} else {
result.push(axe.utils.getNodeFromTree(context.flatTree[0], item));
result.push(axe.utils.getNodeFromTree(item));
}
}
}
Expand Down
8 changes: 6 additions & 2 deletions lib/core/public/run-rules.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

// Clean up after resolve / reject
function cleanup() {
axe._nodeMap = undefined;
axe._cache = undefined;
axe._tree = undefined;
axe._selectorData = undefined;
}
Expand All @@ -19,7 +19,11 @@ function cleanup() {
function runRules(context, options, resolve, reject) {
'use strict';
try {
// axe._nodeMap = new WeakMap();
axe._cache = {
nodeMap: new WeakMap(),
idRefs: {}
};

context = new Context(context);
axe._tree = context.flatTree;
axe._selectorData = axe.utils.getSelectorData(context.flatTree);
Expand Down
32 changes: 25 additions & 7 deletions lib/core/utils/flattened-tree.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
/*eslint no-use-before-define: 0*/
var axe = axe || { utils: {} };

const idRefRegex = /^idrefs?$/;

/**
* This implemnts the flatten-tree algorithm specified:
* Originally here https://drafts.csswg.org/css-scoping/#flat-tree
Expand All @@ -27,6 +29,25 @@ var axe = axe || { utils: {} };
* @return {Object} - the wrapped node
*/
function virtualDOMfromNode(node, shadowId) {
const refAttrs = Object.keys(axe.commons.aria.lookupTable.attributes)
.filter(attr => {
const { type } = axe.commons.aria.lookupTable.attributes[attr];
return /^idrefs?$/.test(type);
});

// cache all idref attribute values so we can look them up in
// aria.isAccessibleRef
if (node.hasAttribute) {
refAttrs
.filter(attr => node.hasAttribute(attr))
.forEach(attr => {
const attrValue = elm.getAttribute(attr);
axe.utils.tokenList(attrValue).forEach(id => {
axe._cache.idRefs[id] = 1;
});
});
}

const vNodeCache = {};
const vNode = {
shadowId: shadowId,
Expand All @@ -47,9 +68,7 @@ function virtualDOMfromNode(node, shadowId) {
return vNodeCache._tabbableElements;
}
};
if (axe._nodeMap) {
axe._nodeMap.set(node, vNode);
}
axe._cahce.nodeMap.set(node, vNode);
straker marked this conversation as resolved.
Show resolved Hide resolved
return vNode;
}

Expand Down Expand Up @@ -149,11 +168,10 @@ axe.utils.getFlattenedTree = function(node, shadowId) {
};

/**
* Recursively return a single node from a virtual dom tree
* Return a single node from the virtual dom tree
*
* @param {Object} vNode The flattened, virtual DOM tree
* @param {Node} node The HTML DOM node
*/
axe.utils.getNodeFromTree = function(vNode, node) {
return axe._nodeMap.get(node);
axe.utils.getNodeFromTree = function(node) {
return axe._cache.nodeMap.get(node);
};
2 changes: 1 addition & 1 deletion lib/core/utils/is-hidden.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
*/
axe.utils.isHidden = function isHidden(el, recursed) {
'use strict';
var node = axe.utils.getNodeFromTree(axe._tree[0], el);
var node = axe.utils.getNodeFromTree(el);
var parent, isHidden;

// 9 === Node.DOCUMENT
Expand Down
5 changes: 1 addition & 4 deletions lib/rules/color-contrast-matches.js
Original file line number Diff line number Diff line change
Expand Up @@ -53,10 +53,7 @@ if (nodeName === 'LABEL' || nodeParentLabel) {
if (nodeParentLabel) {
relevantNode = nodeParentLabel;
// we need an input candidate from a parent to account for label children
relevantVirtualNode = axe.utils.getNodeFromTree(
axe._tree[0],
nodeParentLabel
);
relevantVirtualNode = axe.utils.getNodeFromTree(nodeParentLabel);
}
// explicit label of disabled input
let doc = axe.commons.dom.getRootNode(relevantNode);
Expand Down
4 changes: 4 additions & 0 deletions test/testutils.js
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,10 @@ testUtils.fixtureSetup = function(content) {
}
axe._tree = axe.utils.getFlattenedTree(fixture);
axe._selectorData = axe.utils.getSelectorData(axe._tree);
axe._cache = {
nodeMap: new WeakMap(),
idRefs: {}
};

return fixture;
};
Expand Down