Merge pull request #3407 from dpalou/MOBILE-4152
MOBILE-4152 core: Fix link handlers when protocols don't matchmain
commit
134a6ab79e
|
@ -19,6 +19,7 @@ import { CoreUrlUtils } from '@services/utils/url';
|
||||||
import { CoreUtils } from '@services/utils/utils';
|
import { CoreUtils } from '@services/utils/utils';
|
||||||
import { makeSingleton } from '@singletons';
|
import { makeSingleton } from '@singletons';
|
||||||
import { CoreText } from '@singletons/text';
|
import { CoreText } from '@singletons/text';
|
||||||
|
import { CoreUrl } from '@singletons/url';
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Interface that all handlers must implement.
|
* Interface that all handlers must implement.
|
||||||
|
@ -174,7 +175,7 @@ export class CoreContentLinksDelegateService {
|
||||||
const linkActions: CoreContentLinksHandlerActions[] = [];
|
const linkActions: CoreContentLinksHandlerActions[] = [];
|
||||||
const promises: Promise<void>[] = [];
|
const promises: Promise<void>[] = [];
|
||||||
const params = CoreUrlUtils.extractUrlParams(url);
|
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) {
|
for (const name in this.handlers) {
|
||||||
const handler = this.handlers[name];
|
const handler = this.handlers[name];
|
||||||
|
|
|
@ -29,6 +29,13 @@ describe('CoreText singleton', () => {
|
||||||
expect(CoreText.removeEndingSlash('foo//')).toEqual('foo/');
|
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', () => {
|
it('concatenates paths', () => {
|
||||||
expect(CoreText.concatenatePaths('', 'foo/bar')).toEqual('foo/bar');
|
expect(CoreText.concatenatePaths('', 'foo/bar')).toEqual('foo/bar');
|
||||||
expect(CoreText.concatenatePaths('foo/bar', '')).toEqual('foo/bar');
|
expect(CoreText.concatenatePaths('foo/bar', '')).toEqual('foo/bar');
|
||||||
|
|
|
@ -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');
|
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');
|
||||||
|
});
|
||||||
|
|
||||||
});
|
});
|
||||||
|
|
|
@ -54,6 +54,20 @@ export class CoreText {
|
||||||
return text;
|
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.
|
* Concatenate two paths, adding a slash between them if needed.
|
||||||
*
|
*
|
||||||
|
|
|
@ -280,4 +280,21 @@ export class CoreUrl {
|
||||||
return CoreText.concatenatePaths(treatedParentUrl, url);
|
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, ''));
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue