-
Notifications
You must be signed in to change notification settings - Fork 779
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix(skip-link): work with absolute and relative paths (#2875)
* fix(skip-link): work with absoulte and relative paths * revert playground * fix ie11 * fix ie11 again * Update lib/commons/dom/is-skip-link.js Co-authored-by: Wilco Fiers <[email protected]> * finalize * fix * fix ie11 (always) * typo * fix jsdom test * Update lib/commons/dom/is-current-page-link.js Co-authored-by: Wilco Fiers <[email protected]> * fix issue with usemap * fix ie11 Co-authored-by: Wilco Fiers <[email protected]>
- Loading branch information
1 parent
2e27dca
commit ee49d3e
Showing
8 changed files
with
361 additions
and
20 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
// angular skip links start with /# | ||
const angularSkipLinkRegex = /^\/\#/; | ||
|
||
// angular router link uses #! or #/ | ||
const angularRouterLinkRegex = /^#[!/]/; | ||
|
||
/** | ||
* Determine if an anchor elements href attribute references the current page. | ||
* @method isCurrentPageLink | ||
* @memberof axe.commons.dom | ||
* @param {HTMLAnchorElement} anchor | ||
* @return {Boolean|null} | ||
*/ | ||
export default function isCurrentPageLink(anchor) { | ||
const href = anchor.getAttribute('href'); | ||
if (!href || href === '#') { | ||
return false; | ||
} | ||
|
||
if (angularSkipLinkRegex.test(href)) { | ||
return true; | ||
} | ||
|
||
const { hash, protocol, hostname, port, pathname } = anchor; | ||
if (angularRouterLinkRegex.test(hash)) { | ||
return false; | ||
} | ||
|
||
if (href.charAt(0) === '#') { | ||
return true; | ||
} | ||
|
||
// jsdom can have window.location.origin set to "null" (the string) | ||
// if the url option is not set when parsing the dom string | ||
if ( | ||
typeof window.location?.origin !== 'string' || | ||
window.location.origin.indexOf('://') === -1 | ||
) { | ||
return null; | ||
} | ||
|
||
// ie11 does not support window.origin | ||
const currentPageUrl = window.location.origin + window.location.pathname; | ||
|
||
// ie11 does not have anchor.origin so we need to construct | ||
// it ourselves | ||
// also ie11 has empty protocol, hostname, and port when the | ||
// link is relative, so use window.location.origin in these cases | ||
let url; | ||
if (!hostname) { | ||
url = window.location.origin; | ||
} else { | ||
url = `${protocol}//${hostname}${port ? `:${port}` : ''}`; | ||
} | ||
|
||
// ie11 has empty pathname if link is just a hash, so use | ||
// window.location.pathname in these cases | ||
if (!pathname) { | ||
url += window.location.pathname; | ||
} else { | ||
// ie11 pathname does not start with / but chrome and firefox do | ||
url += (pathname[0] !== '/' ? '/' : '') + pathname; | ||
} | ||
|
||
return url === currentPageUrl; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
describe('is-current-page-link', function() { | ||
var isCurrentPageLink = axe.commons.dom.isCurrentPageLink; | ||
var currentPage = window.location.origin + window.location.pathname; | ||
var base; | ||
|
||
afterEach(function() { | ||
if (base) { | ||
document.head.removeChild(base); | ||
} | ||
}); | ||
|
||
it('should return true for hash links', function() { | ||
var anchor = document.createElement('a'); | ||
anchor.href = '#main'; | ||
document.body.appendChild(anchor); | ||
assert.isTrue(isCurrentPageLink(anchor)); | ||
}); | ||
|
||
it('should return true for relative links to the same page', function() { | ||
var anchor = document.createElement('a'); | ||
anchor.href = window.location.pathname; | ||
assert.isTrue(isCurrentPageLink(anchor)); | ||
}); | ||
|
||
it('should return true for absolute links to the same page', function() { | ||
var anchor = document.createElement('a'); | ||
anchor.href = currentPage; | ||
assert.isTrue(isCurrentPageLink(anchor)); | ||
}); | ||
|
||
it('should return true for angular skip links', function() { | ||
var anchor = document.createElement('a'); | ||
anchor.href = '/#main'; | ||
assert.isTrue(isCurrentPageLink(anchor)); | ||
}); | ||
|
||
it('should return false for just "#"', function() { | ||
var anchor = document.createElement('a'); | ||
anchor.href = '#'; | ||
assert.isFalse(isCurrentPageLink(anchor)); | ||
}); | ||
|
||
it('should return false for relative links to a different page', function() { | ||
var anchor = document.createElement('a'); | ||
anchor.href = '/foo/bar/index.html'; | ||
assert.isFalse(isCurrentPageLink(anchor)); | ||
}); | ||
|
||
it('should return false for absolute links to a different page', function() { | ||
var anchor = document.createElement('a'); | ||
anchor.href = 'https://my-page.com/foo/bar/index.html'; | ||
assert.isFalse(isCurrentPageLink(anchor)); | ||
}); | ||
|
||
it('should return false for angular router links (#!)', function() { | ||
var anchor = document.createElement('a'); | ||
anchor.href = '#!main'; | ||
assert.isFalse(isCurrentPageLink(anchor)); | ||
}); | ||
|
||
it('should return false for angular router links (#/)', function() { | ||
var anchor = document.createElement('a'); | ||
anchor.href = '#/main'; | ||
assert.isFalse(isCurrentPageLink(anchor)); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.