Skip to content

Commit

Permalink
Merge pull request #5538 from Polymer/cl245273850
Browse files Browse the repository at this point in the history
More fixes for resolveUrl
  • Loading branch information
aomarks authored May 9, 2019
2 parents 3b6f334 + 3db5608 commit 1c10f0c
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 1 deletion.
7 changes: 6 additions & 1 deletion lib/utils/resolve-url.js
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
24 changes: 24 additions & 0 deletions test/unit/resolveurl.html
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 1c10f0c

Please sign in to comment.