Skip to content

Commit

Permalink
Check Skip link URL is valid by parsing with new URL()
Browse files Browse the repository at this point in the history
  • Loading branch information
colinrotherham committed Dec 7, 2023
1 parent 4e5cb52 commit b58d909
Showing 1 changed file with 18 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,24 @@ export class SkipLink extends GOVUKFrontendComponent {

const hash = this.$module.hash
const href = this.$module.getAttribute('href') ?? ''
const url = new window.URL(this.$module.href)

/** @type {URL | undefined} */
let url

/**
* Check for valid link URL
*
* {@link https://caniuse.com/url}
* {@link https://url.spec.whatwg.org}
*
*/
try {
url = new window.URL(this.$module.href)
} catch (error) {
throw new ElementError(
`Skip link: Target link (\`href="${href}"\`) is invalid`
)
}

// Check for external link URL and return early
if (url.origin !== window.location.origin) {
Expand Down

0 comments on commit b58d909

Please sign in to comment.