Skip to content

Commit bd7a19b

Browse files
committed
fix block of protocol change for file with empty host in the URL polyfill
1 parent 2c8da3d commit bd7a19b

File tree

4 files changed

+15
-1
lines changed

4 files changed

+15
-1
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@
3434
- Fixed some percent encode cases / character sets in the `URL` polyfill
3535
- Fixed some cases of `''` and `null` host handling in the `URL` polyfill
3636
- Fixed host parsing with `hostname = host:port` in the `URL` polyfill
37+
- Fixed block of protocol change for file with empty host in the `URL` polyfill
3738
- Fixed invalid code points handling in UTF-8 decode in the `URLSearchParams` polyfill
3839
- Fixed some cases of serialization in `URL` polyfill (`/.` prefix for non-special URLs with `null` host and path starting with empty segment)
3940
- Fixed `URL` polyfill `.origin` getter with `blob` scheme

packages/core-js/modules/web.url.constructor.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -602,6 +602,7 @@ URLState.prototype = {
602602

603603
case FILE:
604604
url.scheme = 'file';
605+
url.host = '';
605606
if (chr === '/' || chr === '\\') state = FILE_SLASH;
606607
else if (base && base.scheme === 'file') {
607608
switch (chr) {
@@ -624,8 +625,8 @@ URLState.prototype = {
624625
state = FRAGMENT;
625626
break;
626627
default:
628+
url.host = base.host;
627629
if (!startsWithWindowsDriveLetter(join(arraySlice(codePoints, pointer), ''))) {
628-
url.host = base.host;
629630
url.path = arraySlice(base.path);
630631
url.shortenPath();
631632
}

tests/unit-global/web.url.js

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,8 @@ QUnit.test('URL constructor', assert => {
4242
assert.same(String(new URL('http://0300.168.0xG0')), 'http://0300.168.0xg0/', 'incorrect IPv4 parsed as host');
4343

4444
assert.same(String(new URL('file:///var/log/system.log')), 'file:///var/log/system.log', 'file scheme');
45+
assert.same(String(new URL('file:foo')), 'file:///foo', 'file scheme without slashes');
46+
assert.same(new URL('file:foo').host, '', 'file scheme without slashes: host');
4547
// assert.same(String(new URL('file://nnsc.nsf.net/bar/baz')), 'file://nnsc.nsf.net/bar/baz', 'file scheme'); // 'file:///bar/baz' in FF
4648
// assert.same(String(new URL('file://localhost/bar/baz')), 'file:///bar/baz', 'file scheme'); // 'file://localhost/bar/baz' in Chrome
4749

@@ -199,6 +201,10 @@ QUnit.test('URL#protocol', assert => {
199201
assert.same(url.protocol, 'http:');
200202
assert.same(url.href, 'http://zloirock.ru/', 'incorrect scheme');
201203
assert.same(String(url), 'http://zloirock.ru/', 'incorrect scheme');
204+
205+
url = new URL('file:foo');
206+
url.protocol = 'http:';
207+
assert.same(url.protocol, 'file:', 'file with empty host: protocol change blocked');
202208
}
203209
});
204210

tests/unit-pure/web.url.js

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,8 @@ QUnit.test('URL constructor', assert => {
4343
assert.same(String(new URL('http://0300.168.0xG0')), 'http://0300.168.0xg0/', 'incorrect IPv4 parsed as host');
4444

4545
assert.same(String(new URL('file:///var/log/system.log')), 'file:///var/log/system.log', 'file scheme');
46+
assert.same(String(new URL('file:foo')), 'file:///foo', 'file scheme without slashes');
47+
assert.same(new URL('file:foo').host, '', 'file scheme without slashes: host');
4648
// assert.same(String(new URL('file://nnsc.nsf.net/bar/baz')), 'file://nnsc.nsf.net/bar/baz', 'file scheme'); // 'file:///bar/baz' in FF
4749
// assert.same(String(new URL('file://localhost/bar/baz')), 'file:///bar/baz', 'file scheme'); // 'file://localhost/bar/baz' in Chrome
4850

@@ -200,6 +202,10 @@ QUnit.test('URL#protocol', assert => {
200202
assert.same(url.protocol, 'http:');
201203
assert.same(url.href, 'http://zloirock.ru/', 'incorrect scheme');
202204
assert.same(String(url), 'http://zloirock.ru/', 'incorrect scheme');
205+
206+
url = new URL('file:foo');
207+
url.protocol = 'http:';
208+
assert.same(url.protocol, 'file:', 'file with empty host: protocol change blocked');
203209
}
204210
});
205211

0 commit comments

Comments
 (0)