Skip to content

Commit 4eceb27

Browse files
VIA-627 SB & EO Better name and docstring for linksOpenCorrectly().
1 parent c59f9c5 commit 4eceb27

File tree

3 files changed

+11
-5
lines changed

3 files changed

+11
-5
lines changed

src/app/_components/content/Overview.tsx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,14 @@
11
import { VaccineType } from "@src/models/vaccine";
22
import { Overview as StyledOverview } from "@src/services/content-api/types";
3-
import { fixupHtmlFragment } from "@src/utils/html";
3+
import { linksOpenCorrectly } from "@src/utils/html";
44
import { JSX } from "react";
55

66
const Overview = (props: { overview: StyledOverview | undefined; vaccineType: VaccineType }): JSX.Element => {
77
const element = props.overview ? (
88
props.overview.containsHtml ? (
99
<div
1010
data-testid="overview-text"
11-
dangerouslySetInnerHTML={{ __html: fixupHtmlFragment(props.overview.content) || "" }}
11+
dangerouslySetInnerHTML={{ __html: linksOpenCorrectly(props.overview.content) || "" }}
1212
/>
1313
) : (
1414
<p data-testid="overview-text">{props.overview.content}</p>

src/services/content-api/parsers/content-styling-service.tsx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ import {
1616
VaccinePageSection,
1717
VaccinePageSubsection,
1818
} from "@src/services/content-api/types";
19-
import { fixupHtmlFragment } from "@src/utils/html";
19+
import { linksOpenCorrectly } from "@src/utils/html";
2020
import sanitiseHtml from "@src/utils/sanitise-html";
2121
import { InsetText, WarningCallout } from "nhsuk-react-components";
2222
import React, { JSX } from "react";
@@ -184,7 +184,7 @@ function styleCallout(callout: HeadingWithTypedContent | undefined): StyledPageS
184184
component: (
185185
<div
186186
data-testid="callout-html"
187-
dangerouslySetInnerHTML={{ __html: fixupHtmlFragment(callout.content) || "" }}
187+
dangerouslySetInnerHTML={{ __html: linksOpenCorrectly(callout.content) || "" }}
188188
/>
189189
),
190190
};

src/utils/html.ts

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,12 @@
11
import { load as loadHtml } from "cheerio";
22

3-
export function fixupHtmlFragment(fragment: string) {
3+
/**
4+
* Update HTML fragment to ensure that anchor tags have the target="_blank"
5+
* attribute, so links open in a new window.
6+
* * @param {string} fragment - the original HTML fragment.
7+
* * @returns {string} The HTML fragment with fixed anchor tags.
8+
*/
9+
export function linksOpenCorrectly(fragment: string) {
410
const $ = loadHtml(fragment, null, false);
511
$("a").attr("target", "_blank");
612
return $.html();

0 commit comments

Comments
 (0)