Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

More fixes for resolveUrl #5538

Merged
merged 4 commits into from
May 9, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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