MOBILE-4152 core: Fix link handlers when protocols don't match

main
Dani Palou 2022-10-13 17:05:34 +02:00
parent e6308c2869
commit 2d501024a0
5 changed files with 49 additions and 1 deletions

View File

@ -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<void>[] = [];
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];

View File

@ -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');

View File

@ -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');
});
});

View File

@ -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.
*

View File

@ -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, ''));
}
}