Skip to content

Commit

Permalink
Merge branch 'latest' into WSTEAM1-931-article-election-banner
Browse files Browse the repository at this point in the history
  • Loading branch information
amoore108 committed Oct 18, 2024
2 parents ea2a3e7 + 68c5b92 commit 5f840ab
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 8 deletions.
28 changes: 22 additions & 6 deletions src/server/Document/Renderers/litePageTransforms/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ describe('litePageTransforms', () => {
it('should append .lite suffix to valid hrefs', () => {
const html = `
<a href="https://www.bbc.com/news">News</a>
<a href="https://www.bbc.com/serbian/lat">News</a>
<a href="https://www.bbc.com/mundo">News</a>
<a href="https://www.bbcrussian.com/news">News</a>
<a href="/news">News</a>
`;
Expand All @@ -13,31 +15,45 @@ describe('litePageTransforms', () => {

expect(modifiedHtml).toEqual(`
<a href="https://www.bbc.com/news.lite">News</a>
<a href="https://www.bbc.com/serbian/lat.lite">News</a>
<a href="https://www.bbc.com/mundo.lite">News</a>
<a href="https://www.bbcrussian.com/news.lite">News</a>
<a href="/news.lite">News</a>
`);
});

it('should not append .lite suffix to invalid hrefs', () => {
const html = `
const originalHtml = `
<a href="https://www.bbc.co.uk/news">News</a>
<a href="https://www.bbc.com/news.lite">News</a>
<a href="https://www.bbc.com/news.amp">News</a>
<a href="#news">News</a>
<a href="mailto:test@gmail.com">News</a>
`;

const modifiedHtml = litePageTransforms(html);
const modifiedHtml = litePageTransforms(originalHtml);

expect(modifiedHtml).toEqual(originalHtml);
});

expect(modifiedHtml).toEqual(html);
it('should not append .lite suffix to an invalid "service"', () => {
const originalHtml = `
<a href="https://www.bbc.co.uk/future">Future</a>
<a href="https://www.bbc.com/food">Food</a>
<a href="https://www.bbc.com/weather">Weather</a>
`;

const modifiedHtml = litePageTransforms(originalHtml);

expect(modifiedHtml).toEqual(originalHtml);
});

it('should not append .lite suffix when no anchor tags are present', () => {
const html = '<p>I am a paragraph</p>';
const originalHtml = '<p>I am a paragraph</p>';

const modifiedHtml = litePageTransforms(html);
const modifiedHtml = litePageTransforms(originalHtml);

expect(modifiedHtml).toEqual(html);
expect(modifiedHtml).toEqual(originalHtml);
});
});
});
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
import { Services } from '#app/models/types/global';
import services from '#lib/config/services/loadableConfig';

const SERVICES = Object.keys(services) as Services[];

const VALID_DOMAINS = [
'/',
'localhost',
Expand All @@ -15,9 +20,18 @@ const isValidHref = (href: string) => {
const extension = url?.pathname?.split('.')?.pop() || '';
const startsWithHash = href?.startsWith('#');

const hasReservedRouteExtension =
RESERVED_ROUTE_EXTENSIONS.includes(extension);

const isValidDomain = VALID_DOMAINS.includes(url.hostname);
const isWsService = SERVICES.includes(
url?.pathname?.split('/')?.[1] as Services,
);

return (
VALID_DOMAINS.includes(url.hostname) &&
!RESERVED_ROUTE_EXTENSIONS.includes(extension) &&
isValidDomain &&
isWsService &&
!hasReservedRouteExtension &&
!startsWithHash
);
};
Expand Down

0 comments on commit 5f840ab

Please sign in to comment.