From d48b11f74d622b660e38b1105a2288a4020e9f47 Mon Sep 17 00:00:00 2001 From: Steven Lambert Date: Tue, 30 Apr 2019 09:52:53 -0600 Subject: [PATCH] fix(isSkipLink): cache first page link --- lib/commons/dom/is-skip-link.js | 22 +++++++++++++++------- 1 file changed, 15 insertions(+), 7 deletions(-) diff --git a/lib/commons/dom/is-skip-link.js b/lib/commons/dom/is-skip-link.js index e9fd5a100a..e45525f5a6 100644 --- a/lib/commons/dom/is-skip-link.js +++ b/lib/commons/dom/is-skip-link.js @@ -16,13 +16,21 @@ dom.isSkipLink = function(element) { return false; } - // define a skip link as any anchor element whose href starts with `#...` - // and which precedes the first anchor element whose href doesn't start - // with `#...` (that is, a link to a page) - const firstPageLink = axe.utils.querySelectorAll( - axe._tree, - 'a:not([href^="#"]):not([href^="/#"]):not([href^="javascript"])' - )[0]; + let firstPageLink; + if (typeof axe._cache.firstPageLink !== 'undefined') { + firstPageLink = axe._cache.firstPageLink; + } else { + // define a skip link as any anchor element whose href starts with `#...` + // and which precedes the first anchor element whose href doesn't start + // with `#...` (that is, a link to a page) + firstPageLink = axe.utils.querySelectorAll( + axe._tree, + 'a:not([href^="#"]):not([href^="/#"]):not([href^="javascript"])' + )[0]; + + // null will signify no first page link + axe._cache.firstPageLink = firstPageLink || null; + } // if there are no page links then all all links will need to be // considered as skip links