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

fix: properly handle data URIs #27

Merged
merged 6 commits into from
May 20, 2021
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
5 changes: 5 additions & 0 deletions src/resolve-url.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,11 @@ const resolveUrl = (baseUrl, relativeUrl) => {
return relativeUrl;
}

// if baseUrl is a data URI, ignore it and resolve everything relative to window.location
if ((/^data:/).test(baseUrl)) {
baseUrl = window.location && window.location.href || '';
}

// IE11 supports URL but not the URL constructor
// feature detect the behavior we want
const nativeURL = typeof window.URL === 'function';
Expand Down
21 changes: 21 additions & 0 deletions test/resolve-url.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,11 @@ QUnit.module('URL resolver');

QUnit.test('works with a selection of valid urls', function(assert) {
let currentLocation = '';
let currentPath = '';

if (window.location && window.location.protocol) {
currentLocation = window.location.protocol + '//' + window.location.host;
currentPath = window.location.pathname.split('/').slice(0, -1).join('/');
}

assert.equal(
Expand All @@ -25,4 +27,23 @@ QUnit.test('works with a selection of valid urls', function(assert) {
);
assert.equal(resolveUrl('/a/b/cd/e.m3u8', 'z.ts'), currentLocation + '/a/b/cd/z.ts');
assert.equal(resolveUrl('/a/b/cd/e.m3u8', '../../../z.ts'), currentLocation + '/z.ts');

assert.equal(
resolveUrl('data:application/dash+xml;charset=utf-8,http%3A%2F%2Fexample.com', 'hello.m3u8'),
// we need to add the currentPath because we're actually working relative to window.location
currentLocation + currentPath + '/hello.m3u8',
'resolves urls relative to window when given a data base url'
);

assert.equal(
resolveUrl('data:application/dash+xml;charset=utf-8,http%3A%2F%2Fexample.com', 'http://example.com/hello.m3u8'),
'http://example.com/hello.m3u8',
'absolute urls should still be absolute even with a data uri'
);

assert.equal(
resolveUrl('http://example.com', 'data:application/dash+xml;charset=utf-8,http%3A%2F%2Fexample.com'),
'data:application/dash+xml;charset=utf-8,http%3A%2F%2Fexample.com',
'data uri is treated as an absolute url'
);
});