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

fix(skip-link): work with absolute and relative paths #2875

Merged
merged 15 commits into from
Jan 20, 2022
5 changes: 3 additions & 2 deletions lib/commons/dom/is-current-page-link.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,9 @@ const angularRouterLinkRegex = /^#[!/]/;
* @return {Boolean|null}
*/
export default function isCurrentPageLink(anchor) {
// jsdom can have window.location.origin set to null
if (!window?.location.origin) {
// jsdom can have window.location.origin set to "null" (the string)
// if the url option is not set when parsing the dom string
if (window.location.origin === 'null') {
straker marked this conversation as resolved.
Show resolved Hide resolved
return null;
}

Expand Down
64 changes: 60 additions & 4 deletions test/node/jsdom.js
Original file line number Diff line number Diff line change
Expand Up @@ -61,16 +61,72 @@ describe('jsdom axe-core', function() {
});

describe('isCurrentPageLink', function() {
// because axe only sets the window global when calling axe.run,
// we'll have to create a custom rule that calls
// isCurrentPageLink to gain access to the middle of a run with
// the proper window object
afterEach(function() {
axe.teardown();
});

it('should return null when url is not set', function() {
var dom = new jsdom.JSDOM(domStr);
var anchor = dom.window.document.getElementById('#skip');
assert.strictEqual(axe.commons.dom.isCurrentPageLink(anchor), null);
var anchor = dom.window.document.getElementById('skip');

axe.configure({
checks: [
{
id: 'check-current-page-link',
evaluate: function() {
return axe.commons.dom.isCurrentPageLink(anchor) === null;
}
}
],
rules: [
{
id: 'check-current-page-link',
any: ['check-current-page-link']
}
]
});

return axe
.run(dom.window.document.documentElement, {
runOnly: ['check-current-page-link']
})
.then(function(results) {
assert.strictEqual(results.passes.length, 1);
});
});

it('should return true when url is set', function() {
straker marked this conversation as resolved.
Show resolved Hide resolved
var dom = new jsdom.JSDOM(domStr, { url: 'https://page.com' });
var anchor = dom.window.document.getElementById('#skip');
assert.strictEqual(axe.commons.dom.isCurrentPageLink(anchor), null);
var anchor = dom.window.document.getElementById('skip');

axe.configure({
checks: [
{
id: 'check-current-page-link',
evaluate: function() {
return axe.commons.dom.isCurrentPageLink(anchor) === true;
}
}
],
rules: [
{
id: 'check-current-page-link',
any: ['check-current-page-link']
}
]
});

return axe
.run(dom.window.document.documentElement, {
runOnly: ['check-current-page-link']
})
.then(function(results) {
assert.strictEqual(results.passes.length, 1);
});
});
});
});