diff --git a/lib/utils/resolve-url.js b/lib/utils/resolve-url.js index ae5e5e2ad5..5dd89cbcf2 100644 --- a/lib/utils/resolve-url.js +++ b/lib/utils/resolve-url.js @@ -46,7 +46,12 @@ export function resolveUrl(url, baseURI) { baseURI = document.baseURI || window.location.href; } if (workingURL) { - return (new URL(url, baseURI)).href; + try { + return (new URL(url, baseURI)).href; + } catch (e) { + // Bad url or baseURI structure. Do not attempt to resolve. + return url; + } } // Fallback to creating an anchor into a disconnected document. if (!resolveDoc) { diff --git a/test/unit/resolveurl.html b/test/unit/resolveurl.html index 26d708b682..2ce6c8b27f 100644 --- a/test/unit/resolveurl.html +++ b/test/unit/resolveurl.html @@ -170,6 +170,30 @@ assert.equal(actual, expected); }); + test('resolveUrl when called with relative url and a bad baseURI', function () { + const el = document.querySelector('x-resolve'); + const expected = 'relative/path.png'; + const actual = + el.resolveUrl('relative/path.png', '/not/a/full/uri'); + assert.equal(actual, expected); + }); + + test('resolveUrl when called with a full url and a bad baseURI', function () { + const el = document.querySelector('x-resolve'); + const expected = 'https://example.com/foo.png'; + const actual = + el.resolveUrl('https://example.com/foo.png', '/not/a/full/uri'); + assert.equal(actual, expected); + }); + + test('resolveUrl when called with a protocol-relative url and a bad baseURI', function () { + const el = document.querySelector('x-resolve'); + const expected = '//example.com/foo.png'; + const actual = + el.resolveUrl('//example.com/foo.png', '/not/a/full/uri'); + assert.equal(actual, expected); + }); + test('resolveUrl api with assetpath', function() { var el = document.createElement('p-r-ap'); // Manually calculate expected URL, to avoid dependence on