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

PLATUI-2829: Do not remove back link when referrer is empty #359

Merged
merged 1 commit into from Mar 27, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
6 changes: 6 additions & 0 deletions CHANGELOG.md
Expand Up @@ -7,6 +7,12 @@ and this project adheres to [Semantic Versioning](http://semver.org/).

## [Unreleased]

## [6.12.0] - 2024-03-26

### Changed

- Retain the back link even when the referrer is empty

## [6.11.0] - 2024-03-25

### Changed
Expand Down
2 changes: 1 addition & 1 deletion package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
@@ -1,6 +1,6 @@
{
"name": "hmrc-frontend",
"version": "6.11.0",
"version": "6.12.0",
"description": "Design patterns for HMRC frontends",
"scripts": {
"start": "gulp dev",
Expand Down
24 changes: 19 additions & 5 deletions src/components/back-link-helper/back-link-helper.js
Expand Up @@ -5,13 +5,27 @@ function BackLinkHelper($module, window, document) {
}

BackLinkHelper.prototype.init = function init() {
const isReferrerExternal = () => {
// If there is no referrer, consider it on the same domain
if (!this.document.referrer) {
return false;
}

try {
const currentDomain = new URL(this.window.location.href).hostname;
const referrerDomain = new URL(this.document.referrer).hostname;

// Check if the referrer is not the same as the current domain
return currentDomain !== referrerDomain;
} catch (err) {
return false;
}
};

// do nothing if History API is absent
if (this.window.history) {
// store referrer value to cater for IE
const docReferrer = this.document.referrer;

// hide the backlink if the referrer is on a different domain or the referrer is not set
if (docReferrer === '' || docReferrer.indexOf(this.window.location.host) === -1) {
// hide the backlink if the referrer is on a different domain
if (isReferrerExternal()) {
this.$module.classList.add('hmrc-hidden-backlink');
} else {
// prevent resubmit warning
Expand Down
4 changes: 2 additions & 2 deletions src/components/back-link-helper/back-link-helper.test.js
Expand Up @@ -71,11 +71,11 @@ describe('/components/back-link-helper', () => {
expect(mockWindow.history.back).toHaveBeenCalled();
});

it('adds .hmrc-hidden-backlink class to backlink when document referer is empty', () => {
it('does not add .hmrc-hidden-backlink class to backlink when document referer is empty', () => {
const sut = new BackLinkHelper(mockAnchorTag, mockWindow, { referrer: '' });
sut.init();

expect(mockAnchorTag.classList.add.mock.calls[0][0]).toBe('hmrc-hidden-backlink');
expect(mockAnchorTag.classList.add.mock.calls).toHaveLength(0);
});

it('adds .hmrc-hidden-backlink class to backlink if document referer is on a different domain', () => {
Expand Down