Skip to content

Commit

Permalink
Merge pull request #2617 from IntersectMBO/fix/fix-opening-relative-p…
Browse files Browse the repository at this point in the history
…aths-in-external-links

fix: opening relative paths in external links
  • Loading branch information
MSzalowski authored Jan 10, 2025
2 parents 6895ddd + 02fdc96 commit a06cbf4
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 2 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ changes.
### Fixed

- Fix counting submitted votes [Issue 2609](https://github.com/IntersectMBO/govtool/issues/2609)
- Fix opening relative paths in external links

### Changed

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,9 @@ export const ExternalLinkModal = forwardRef<HTMLDivElement>((_, ref) => {
<Button
data-testid="continue-modal-button"
onClick={() => {
openInNewTab(state?.externalLink || "#");
if (state?.externalLink) {
openInNewTab(state?.externalLink);
}
closeModal();
}}
sx={{
Expand Down
8 changes: 7 additions & 1 deletion govtool/frontend/src/utils/openInNewTab.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,10 @@
export const openInNewTab = (url: string) => {
const newWindow = window.open(url, "_blank", "noopener,noreferrer");
// Ensure the URL is absolute
const fullUrl =
url.startsWith("http://") || url.startsWith("https://")
? url
: `https://${url}`;

const newWindow = window.open(fullUrl, "_blank", "noopener,noreferrer");
if (newWindow) newWindow.opener = null;
};

0 comments on commit a06cbf4

Please sign in to comment.