Skip to content

Commit d762e44

Browse files
committed
fix some cases of serialization in URL polyfill
1 parent 766ff3b commit d762e44

File tree

4 files changed

+14
-0
lines changed

4 files changed

+14
-0
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
- Fixed some percent encode cases / character sets in the `URL` polyfill
2525
- Fixed host parsing with `hostname = host:port` in the `URL` polyfill
2626
- Fixed invalid code points handling in UTF-8 decode in the `URLSearchParams` polyfill
27+
- Fixed some cases of serialization in `URL` polyfill ([step 3](https://url.spec.whatwg.org/#url-serializing))
2728
- Fixed `URL` polyfill `.origin` getter with `blob` scheme
2829
- Fixed a lack of error in `URLSearchParams.prototype.set` polyfill on calling only with 1 argument
2930
- Fixed handling invalid UTF-8 continuation bytes in `URLSearchParams` polyfill

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -814,6 +814,7 @@ URLState.prototype = {
814814
output += serializeHost(host);
815815
if (port !== null) output += ':' + port;
816816
} else if (scheme === 'file') output += '//';
817+
if (host === null && !url.cannotBeABaseURL && path.length > 1 && path[0] === '') output += '/.';
817818
output += url.cannotBeABaseURL ? path[0] : path.length ? '/' + join(path, '/') : '';
818819
if (query !== null) output += '?' + query;
819820
if (fragment !== null) output += '#' + fragment;

tests/unit-global/web.url.js

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -139,6 +139,12 @@ QUnit.test('URL#href', assert => {
139139
// assert.throws(() => new URL('http://zloirock.ru/').href = 'http://a%b', 'forbidden host code point'); // no error in Chrome and FF
140140
// assert.throws(() => new URL('http://zloirock.ru/').href = '1http://zloirock.ru', 'incorrect scheme'); // no error in Chrome
141141
}
142+
143+
// URL serializing step 3 - /. prefix for non-special URLs with null host and path starting with empty segment
144+
assert.same(new URL('x:/a/..//b').href, 'x:/.//b', '/. prefix prevents ambiguous serialization');
145+
assert.same(new URL('x:/a/..//b').pathname, '//b', 'pathname is not affected by /. prefix');
146+
assert.same(new URL('x:/.//b').href, 'x:/.//b', '/. prefix is idempotent');
147+
assert.same(new URL(new URL('x:/a/..//b').href).pathname, '//b', '/. prefix round-trips correctly');
142148
});
143149

144150
QUnit.test('URL#origin', assert => {

tests/unit-pure/web.url.js

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -140,6 +140,12 @@ QUnit.test('URL#href', assert => {
140140
// assert.throws(() => new URL('http://zloirock.ru/').href = 'http://a%b', 'forbidden host code point'); // no error in Chrome and FF
141141
// assert.throws(() => new URL('http://zloirock.ru/').href = '1http://zloirock.ru', 'incorrect scheme'); // no error in Chrome
142142
}
143+
144+
// URL serializing step 3 - /. prefix for non-special URLs with null host and path starting with empty segment
145+
assert.same(new URL('x:/a/..//b').href, 'x:/.//b', '/. prefix prevents ambiguous serialization');
146+
assert.same(new URL('x:/a/..//b').pathname, '//b', 'pathname is not affected by /. prefix');
147+
assert.same(new URL('x:/.//b').href, 'x:/.//b', '/. prefix is idempotent');
148+
assert.same(new URL(new URL('x:/a/..//b').href).pathname, '//b', '/. prefix round-trips correctly');
143149
});
144150

145151
QUnit.test('URL#origin', assert => {

0 commit comments

Comments
 (0)