Skip to content

Commit

Permalink
perf: Improve performance of various functions (#1838)
Browse files Browse the repository at this point in the history
  • Loading branch information
sorin-davidoi authored and stephenmathieson committed Oct 10, 2019
1 parent 5c7370a commit 8df4fb5
Show file tree
Hide file tree
Showing 9 changed files with 55 additions and 50 deletions.
27 changes: 18 additions & 9 deletions lib/commons/aria/is-accessible-ref.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,18 +3,27 @@ const idRefsRegex = /^idrefs?$/;

function cacheIdRefs(node, refAttrs) {
if (node.hasAttribute) {
const idRefs = axe._cache.get('idRefs');

if (node.nodeName.toUpperCase() === 'LABEL' && node.hasAttribute('for')) {
axe._cache.get('idRefs')[node.getAttribute('for')] = true;
idRefs[node.getAttribute('for')] = true;
}

refAttrs
.filter(attr => node.hasAttribute(attr))
.forEach(attr => {
const attrValue = node.getAttribute(attr);
axe.utils.tokenList(attrValue).forEach(id => {
axe._cache.get('idRefs')[id] = true;
});
});
for (let i = 0; i < refAttrs.length; ++i) {
const attr = refAttrs[i];

if (!node.hasAttribute(attr)) {
continue;
}

const attrValue = node.getAttribute(attr);

const tokens = axe.utils.tokenList(attrValue);

for (let k = 0; k < tokens.length; ++k) {
idRefs[tokens[k]] = true;
}
}
}

for (let i = 0; i < node.children.length; i++) {
Expand Down
2 changes: 1 addition & 1 deletion lib/commons/dom/is-focusable.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
function focusDisabled(el) {
return (
el.disabled ||
(dom.isHiddenWithCSS(el) && el.nodeName.toUpperCase() !== 'AREA')
(el.nodeName.toUpperCase() !== 'AREA' && dom.isHiddenWithCSS(el))
);
}

Expand Down
20 changes: 11 additions & 9 deletions lib/core/base/context.js
Original file line number Diff line number Diff line change
Expand Up @@ -195,17 +195,19 @@ function validateContext(context) {
function getRootNode({ include, exclude }) {
const selectors = Array.from(include).concat(Array.from(exclude));
// Find the first Element.ownerDocument or Document
const localDocument = selectors.reduce((result, item) => {
if (result) {
return result;
} else if (item instanceof Element) {
return item.ownerDocument;
} else if (item instanceof Document) {
return item;
for (var i = 0; i < selectors.length; ++i) {
var item = selectors[i];

if (item instanceof Element) {
return item.ownerDocument.documentElement;
}

if (item instanceof Document) {
return item.documentElement;
}
}, null);
}

return (localDocument || document).documentElement;
return document.documentElement;
}

/**
Expand Down
11 changes: 4 additions & 7 deletions lib/core/utils/are-styles-set.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,19 +3,16 @@
function areStylesSet(el, styles, stopAt) {
'use strict';
var styl = window.getComputedStyle(el, null);
var set = false;
if (!styl) {
return false;
}
styles.forEach(function(att) {
for (var i = 0; i < styles.length; ++i) {
var att = styles[i];
if (styl.getPropertyValue(att.property) === att.value) {
set = true;
return true;
}
});
if (set) {
return true;
}
if (el.nodeName.toUpperCase() === stopAt.toUpperCase() || !el.parentNode) {
if (!el.parentNode || el.nodeName.toUpperCase() === stopAt.toUpperCase()) {
return false;
}
return areStylesSet(el.parentNode, styles, stopAt);
Expand Down
10 changes: 5 additions & 5 deletions lib/core/utils/is-html-element.js
Original file line number Diff line number Diff line change
Expand Up @@ -127,9 +127,9 @@ const htmlTags = [
* @return {Boolean} true/ false
*/
axe.utils.isHtmlElement = function isHtmlElement(node) {
const tagName = node.nodeName.toLowerCase();
return (
htmlTags.includes(tagName) &&
node.namespaceURI !== 'http://www.w3.org/2000/svg'
);
if (node.namespaceURI === 'http://www.w3.org/2000/svg') {
return false;
}

return htmlTags.includes(node.nodeName.toLowerCase());
};
6 changes: 3 additions & 3 deletions lib/core/utils/is-shadow-root.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,11 +27,11 @@ const possibleShadowRoots = [
* @return {Boolean}
*/
axe.utils.isShadowRoot = function isShadowRoot(node) {
const nodeName = node.nodeName.toLowerCase();
if (node.shadowRoot) {
const nodeName = node.nodeName.toLowerCase();
if (
/^[a-z][a-z0-9_.-]*-[a-z0-9_.-]*$/.test(nodeName) ||
possibleShadowRoots.includes(nodeName)
possibleShadowRoots.includes(nodeName) ||
/^[a-z][a-z0-9_.-]*-[a-z0-9_.-]*$/.test(nodeName)
) {
return true;
}
Expand Down
23 changes: 10 additions & 13 deletions lib/core/utils/qsa.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,10 @@ function matchesClasses(vNode, exp) {
function matchesAttributes(vNode, exp) {
return (
!exp.attributes ||
exp.attributes.reduce((result, att) => {
exp.attributes.every(att => {
var nodeAtt = vNode.attr(att.key);
return result && nodeAtt !== null && (!att.value || att.test(nodeAtt));
}, true)
return nodeAtt !== null && (!att.value || att.test(nodeAtt));
})
);
}

Expand All @@ -32,17 +32,14 @@ function matchesId(vNode, exp) {
function matchesPseudos(target, exp) {
if (
!exp.pseudos ||
exp.pseudos.reduce((result, pseudo) => {
exp.pseudos.every(pseudo => {
if (pseudo.name === 'not') {
return (
result &&
!matchExpressions([target], pseudo.expressions, false).length
);
return !matchExpressions([target], pseudo.expressions, false).length;
}
throw new Error(
'the pseudo selector ' + pseudo.name + ' has not yet been implemented'
);
}, true)
})
) {
return true;
}
Expand Down Expand Up @@ -231,8 +228,8 @@ matchExpressions = function(domTree, expressions, recurse, filter) {
for (let i = 0; i < combined.length; i++) {
let exp = combined[i];
if (
matchesSelector(vNode, exp) &&
(!exp[0].id || vNode.shadowId === currentLevel.parentShadowId)
(!exp[0].id || vNode.shadowId === currentLevel.parentShadowId) &&
matchesSelector(vNode, exp)
) {
if (exp.length === 1) {
if (!added && (!filter || filter(vNode))) {
Expand All @@ -257,8 +254,8 @@ matchExpressions = function(domTree, expressions, recurse, filter) {
}
}
if (
currentLevel.anyLevel.includes(exp) &&
(!exp[0].id || vNode.shadowId === currentLevel.parentShadowId)
(!exp[0].id || vNode.shadowId === currentLevel.parentShadowId) &&
currentLevel.anyLevel.includes(exp)
) {
childAny.push(exp);
}
Expand Down
2 changes: 1 addition & 1 deletion lib/rules/duplicate-id-active-matches.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,4 @@ const idMatchingElms = Array.from(
dom.getRootNode(node).querySelectorAll(idSelector)
);

return idMatchingElms.some(dom.isFocusable) && !aria.isAccessibleRef(node);
return !aria.isAccessibleRef(node) && idMatchingElms.some(dom.isFocusable);
4 changes: 2 additions & 2 deletions lib/rules/duplicate-id-misc-matches.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,6 @@ const idMatchingElms = Array.from(
);

return (
idMatchingElms.every(elm => !dom.isFocusable(elm)) &&
!aria.isAccessibleRef(node)
!aria.isAccessibleRef(node) &&
idMatchingElms.every(elm => !dom.isFocusable(elm))
);

0 comments on commit 8df4fb5

Please sign in to comment.