From 2d501024a068dda87a036da05b7d396b987aad81 Mon Sep 17 00:00:00 2001 From: Dani Palou Date: Thu, 13 Oct 2022 17:05:34 +0200 Subject: [PATCH] MOBILE-4152 core: Fix link handlers when protocols don't match --- .../services/contentlinks-delegate.ts | 3 ++- src/core/singletons/tests/text.test.ts | 7 +++++++ src/core/singletons/tests/url.test.ts | 9 +++++++++ src/core/singletons/text.ts | 14 ++++++++++++++ src/core/singletons/url.ts | 17 +++++++++++++++++ 5 files changed, 49 insertions(+), 1 deletion(-) diff --git a/src/core/features/contentlinks/services/contentlinks-delegate.ts b/src/core/features/contentlinks/services/contentlinks-delegate.ts index 8f6823193..7311e1225 100644 --- a/src/core/features/contentlinks/services/contentlinks-delegate.ts +++ b/src/core/features/contentlinks/services/contentlinks-delegate.ts @@ -19,6 +19,7 @@ import { CoreUrlUtils } from '@services/utils/url'; import { CoreUtils } from '@services/utils/utils'; import { makeSingleton } from '@singletons'; import { CoreText } from '@singletons/text'; +import { CoreUrl } from '@singletons/url'; /** * Interface that all handlers must implement. @@ -174,7 +175,7 @@ export class CoreContentLinksDelegateService { const linkActions: CoreContentLinksHandlerActions[] = []; const promises: Promise[] = []; const params = CoreUrlUtils.extractUrlParams(url); - const relativeUrl = CoreText.addStartingSlash(url.replace(site.getURL(), '')); + const relativeUrl = CoreText.addStartingSlash(CoreUrl.toRelativeURL(site.getURL(), url)); for (const name in this.handlers) { const handler = this.handlers[name]; diff --git a/src/core/singletons/tests/text.test.ts b/src/core/singletons/tests/text.test.ts index 10994b35e..b4a6fff8a 100644 --- a/src/core/singletons/tests/text.test.ts +++ b/src/core/singletons/tests/text.test.ts @@ -29,6 +29,13 @@ describe('CoreText singleton', () => { expect(CoreText.removeEndingSlash('foo//')).toEqual('foo/'); }); + it('remove starting slash if needed', () => { + expect(CoreText.removeStartingSlash('/')).toEqual(''); + expect(CoreText.removeStartingSlash('foo')).toEqual('foo'); + expect(CoreText.removeStartingSlash('/foo')).toEqual('foo'); + expect(CoreText.removeStartingSlash('//foo')).toEqual('/foo'); + }); + it('concatenates paths', () => { expect(CoreText.concatenatePaths('', 'foo/bar')).toEqual('foo/bar'); expect(CoreText.concatenatePaths('foo/bar', '')).toEqual('foo/bar'); diff --git a/src/core/singletons/tests/url.test.ts b/src/core/singletons/tests/url.test.ts index cc9c410ff..40cb31a96 100644 --- a/src/core/singletons/tests/url.test.ts +++ b/src/core/singletons/tests/url.test.ts @@ -119,4 +119,13 @@ describe('CoreUrl singleton', () => { expect(CoreUrl.toAbsoluteURL('https://school.edu/foo.php', 'image.png')).toBe('https://school.edu/foo.php/image.png'); }); + it('converts to relative URLs', () => { + expect(CoreUrl.toRelativeURL('https://school.edu/foo/bar', 'https://mysite.edu')).toBe('https://mysite.edu'); + expect(CoreUrl.toRelativeURL('https://school.edu/', 'https://school.edu/image.png')).toBe('image.png'); + expect(CoreUrl.toRelativeURL('http://school.edu/', 'https://school.edu/image.png')).toBe('image.png'); + expect(CoreUrl.toRelativeURL('https://school.edu/', 'http://school.edu/image.png')).toBe('image.png'); + expect(CoreUrl.toRelativeURL('https://school.edu/foo/bar', 'https://school.edu/foo/bar/image.png')).toBe('image.png'); + expect(CoreUrl.toRelativeURL('https://school.edu', 'school.edu/image.png')).toBe('image.png'); + }); + }); diff --git a/src/core/singletons/text.ts b/src/core/singletons/text.ts index f94e5996a..8255e0588 100644 --- a/src/core/singletons/text.ts +++ b/src/core/singletons/text.ts @@ -54,6 +54,20 @@ export class CoreText { return text; } + /** + * Remove starting slash from a string if needed. + * + * @param text Text to treat. + * @return Treated text. + */ + static removeStartingSlash(text = ''): string { + if (text[0] !== '/') { + return text; + } + + return text.substring(1); + } + /** * Concatenate two paths, adding a slash between them if needed. * diff --git a/src/core/singletons/url.ts b/src/core/singletons/url.ts index 7a4bab437..9b69cba3b 100644 --- a/src/core/singletons/url.ts +++ b/src/core/singletons/url.ts @@ -280,4 +280,21 @@ export class CoreUrl { return CoreText.concatenatePaths(treatedParentUrl, url); } + /** + * Convert a URL to a relative URL (if it isn't already). + * + * @param parentUrl The parent URL. + * @param url The url to convert. + * @return Relative URL. + */ + static toRelativeURL(parentUrl: string, url: string): string { + parentUrl = CoreUrl.removeProtocol(parentUrl); + + if (!url.includes(parentUrl)) { + return url; // Already relative URL. + } + + return CoreText.removeStartingSlash(CoreUrl.removeProtocol(url).replace(parentUrl, '')); + } + }