Skip to content

Commit

Permalink
Clarify Skip link URL error with href= attribute value
Browse files Browse the repository at this point in the history
Some pages can have multiple Skip link components and our error messages don’t explain which one has the problem

The error message now includes the exact `getAttribute('href')` HTML attribute value to help identify the invalid link
  • Loading branch information
colinrotherham committed Dec 7, 2023
1 parent 158d9f5 commit e2163d3
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -37,12 +37,15 @@ export class SkipLink extends GOVUKFrontendComponent {

this.$module = $module

const linkedElementId = getFragmentFromUrl(this.$module.hash)
const hash = this.$module.hash
const href = this.$module.getAttribute('href') ?? ''

const linkedElementId = getFragmentFromUrl(hash)

// Check for link hash fragment
if (!linkedElementId) {
throw new ElementError(
'Skip link: Root element (`$module`) attribute (`href`) has no URL fragment'
`Skip link: Target link (\`href="${href}"\`) has no hash fragment`
)
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -144,7 +144,7 @@ describe('Skip Link', () => {
cause: {
name: 'ElementError',
message:
'Skip link: Root element (`$module`) attribute (`href`) has no URL fragment'
'Skip link: Target link (`href="this-element-does-not-exist"`) has no hash fragment'
}
})
})
Expand Down

0 comments on commit e2163d3

Please sign in to comment.