From fcf82349cfd5f933fed01d10cc861e97b7917e43 Mon Sep 17 00:00:00 2001 From: Dani Palou Date: Tue, 21 Jun 2022 15:48:34 +0200 Subject: [PATCH] MOBILE-4097 core: Don't convert to address URL if it's already a URL --- src/core/services/tests/utils/text.test.ts | 10 ++++++++++ src/core/services/utils/text.ts | 7 +++++++ 2 files changed, 17 insertions(+) diff --git a/src/core/services/tests/utils/text.test.ts b/src/core/services/tests/utils/text.test.ts index 1ac99af3d..6d11a1050 100644 --- a/src/core/services/tests/utils/text.test.ts +++ b/src/core/services/tests/utils/text.test.ts @@ -76,6 +76,16 @@ describe('CoreTextUtilsProvider', () => { expect(CoreApp.isAndroid).toHaveBeenCalled(); }); + it('doesn\'t build address if it\'s already a URL', () => { + const address = 'https://moodle.org'; + + const url = textUtils.buildAddressURL(address); + + expect(url).toEqual(address); + + expect(DomSanitizer.bypassSecurityTrustUrl).toHaveBeenCalled(); + }); + it('matches glob patterns', () => { expect(textUtils.matchesGlob('/foo/bar', '/foo/bar')).toBe(true); expect(textUtils.matchesGlob('/foo/bar', '/foo/bar/')).toBe(false); diff --git a/src/core/services/utils/text.ts b/src/core/services/utils/text.ts index b54f6d575..723b3574c 100644 --- a/src/core/services/utils/text.ts +++ b/src/core/services/utils/text.ts @@ -25,6 +25,7 @@ import { CoreViewerTextComponent } from '@features/viewer/components/text/text'; import { CoreFileHelper } from '@services/file-helper'; import { CoreDomUtils } from './dom'; import { CoreText } from '@singletons/text'; +import { CoreUrl } from '@singletons/url'; /** * Different type of errors the app can treat. @@ -155,6 +156,12 @@ export class CoreTextUtilsProvider { * @return URL to view the address. */ buildAddressURL(address: string): SafeUrl { + const parsedUrl = CoreUrl.parse(address); + if (parsedUrl?.protocol) { + // It's already a URL, don't convert it. + return DomSanitizer.bypassSecurityTrustUrl(address); + } + return DomSanitizer.bypassSecurityTrustUrl((CoreApp.isAndroid() ? 'geo:0,0?q=' : 'http://maps.google.com?q=') + encodeURIComponent(address)); }