Skip to content

Commit

Permalink
url: align url format behavior with browsers
Browse files Browse the repository at this point in the history
Fixes: #36887

PR-URL: #36903
Reviewed-By: Michaël Zasso <targos@protonmail.com>
Reviewed-By: Yongsheng Zhang <zyszys98@gmail.com>
Reviewed-By: Juan José Arboleda <soyjuanarbol@gmail.com>
Reviewed-By: Anna Henningsen <anna@addaleax.net>
Reviewed-By: Tiancheng "Timothy" Gu <timothygu99@gmail.com>
Reviewed-By: Daijiro Wachi <daijiro.wachi@gmail.com>
  • Loading branch information
Lxxyx authored and targos committed May 1, 2021
1 parent e990b11 commit d13fc6e
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 1 deletion.
2 changes: 1 addition & 1 deletion lib/internal/url.js
Expand Up @@ -416,7 +416,7 @@ ObjectDefineProperties(URL.prototype, {
ret += '@';
}
ret += options.unicode ?
domainToUnicode(this.hostname) : this.hostname;
domainToUnicode(ctx.host) : ctx.host;
if (ctx.port !== null)
ret += `:${ctx.port}`;
} else if (ctx.scheme === 'file:') {
Expand Down
20 changes: 20 additions & 0 deletions test/parallel/test-whatwg-url-override-hostname.js
@@ -0,0 +1,20 @@
'use strict';

require('../common');
const assert = require('assert');

{
const url = new (class extends URL { get hostname() { return 'bar.com'; } })('http://foo.com/');
assert.strictEqual(url.href, 'http://foo.com/');
assert.strictEqual(url.toString(), 'http://foo.com/');
assert.strictEqual(url.toJSON(), 'http://foo.com/');
assert.strictEqual(url.hash, '');
assert.strictEqual(url.host, 'foo.com');
assert.strictEqual(url.hostname, 'bar.com');
assert.strictEqual(url.origin, 'http://foo.com');
assert.strictEqual(url.password, '');
assert.strictEqual(url.protocol, 'http:');
assert.strictEqual(url.username, '');
assert.strictEqual(url.search, '');
assert.strictEqual(url.searchParams.toString(), '');
}

0 comments on commit d13fc6e

Please sign in to comment.