Skip to content

Commit 1fe4a16

Browse files
committed
fix host inheritance in some cases of file scheme in the URL polyfill
1 parent e810209 commit 1fe4a16

File tree

4 files changed

+17
-3
lines changed

4 files changed

+17
-3
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@
4141
- Fixed some percent encode cases / character sets in the `URL` polyfill
4242
- Fixed some cases of `''` and `null` host handling in the `URL` polyfill
4343
- Fixed host parsing with `hostname = host:port` in the `URL` polyfill
44+
- Fixed host inheritance in some cases of file scheme in the `URL` polyfill
4445
- Fixed block of protocol change for file with empty host in the `URL` polyfill
4546
- Fixed invalid code points handling in UTF-8 decode in the `URLSearchParams` polyfill
4647
- Fixed some cases of serialization in `URL` polyfill (`/.` prefix for non-special URLs with `null` host and path starting with empty segment)

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

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -643,9 +643,10 @@ URLState.prototype = {
643643
state = FILE_HOST;
644644
break;
645645
}
646-
if (base && base.scheme === 'file' && !startsWithWindowsDriveLetter(join(arraySlice(codePoints, pointer), ''))) {
647-
if (isWindowsDriveLetter(base.path[0], true)) push(url.path, base.path[0]);
648-
else url.host = base.host;
646+
if (base && base.scheme === 'file') {
647+
url.host = base.host;
648+
if (!startsWithWindowsDriveLetter(join(arraySlice(codePoints, pointer), ''))
649+
&& isWindowsDriveLetter(base.path[0], true)) push(url.path, base.path[0]);
649650
}
650651
state = PATH;
651652
continue;

tests/unit-global/web.url.js

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,12 @@ QUnit.test('URL constructor', assert => {
5050
// assert.same(String(new URL('file://nnsc.nsf.net/bar/baz')), 'file://nnsc.nsf.net/bar/baz', 'file scheme'); // 'file:///bar/baz' in FF
5151
// assert.same(String(new URL('file://localhost/bar/baz')), 'file:///bar/baz', 'file scheme'); // 'file://localhost/bar/baz' in Chrome
5252

53+
// FILE_SLASH state: host should be inherited from file: base
54+
// some browsers have a non-spec-compliant native URL implementation for this case
55+
if (new URL('file:/path', 'file://somehost/dir/file').host === 'somehost') {
56+
assert.same(new URL('file:/path', 'file://somehost/dir/file').href, 'file://somehost/path', 'file slash: href with inherited host');
57+
}
58+
5359
assert.throws(() => new URL(), 'TypeError: Failed to construct URL: 1 argument required, but only 0 present.');
5460
assert.throws(() => new URL(''), 'TypeError: Failed to construct URL: Invalid URL');
5561
// Node 19.7

tests/unit-pure/web.url.js

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,12 @@ QUnit.test('URL constructor', assert => {
5151
// assert.same(String(new URL('file://nnsc.nsf.net/bar/baz')), 'file://nnsc.nsf.net/bar/baz', 'file scheme'); // 'file:///bar/baz' in FF
5252
// assert.same(String(new URL('file://localhost/bar/baz')), 'file:///bar/baz', 'file scheme'); // 'file://localhost/bar/baz' in Chrome
5353

54+
// FILE_SLASH state: host should be inherited from file: base
55+
// some browsers have a non-spec-compliant native URL implementation for this case
56+
if (new URL('file:/path', 'file://somehost/dir/file').host === 'somehost') {
57+
assert.same(new URL('file:/path', 'file://somehost/dir/file').href, 'file://somehost/path', 'file slash: href with inherited host');
58+
}
59+
5460
assert.throws(() => new URL(), 'TypeError: Failed to construct URL: 1 argument required, but only 0 present.');
5561
assert.throws(() => new URL(''), 'TypeError: Failed to construct URL: Invalid URL');
5662
// Node 19.7

0 commit comments

Comments
 (0)