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

url: use symbol properties correctly in URL class #10906

Closed
wants to merge 3 commits into from
Closed
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
12 changes: 6 additions & 6 deletions lib/internal/url.js
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ class TupleOrigin {
return result;
}

inspect() {
[util.inspect.custom]() {
return `TupleOrigin {
scheme: ${this[kScheme]},
host: ${this[kHost]},
Expand Down Expand Up @@ -223,10 +223,6 @@ class URL {
parse(this, input, base);
}

get [Symbol.toStringTag]() {
return this instanceof URL ? 'URL' : 'URLPrototype';
}

get [special]() {
return (this[context].flags & binding.URL_FLAGS_SPECIAL) !== 0;
}
Expand All @@ -235,7 +231,7 @@ class URL {
return (this[context].flags & binding.URL_FLAGS_CANNOT_BE_BASE) !== 0;
}

inspect(depth, opts) {
[util.inspect.custom](depth, opts) {
const ctx = this[context];
var ret = 'URL {\n';
ret += ` href: ${this.href}\n`;
Expand Down Expand Up @@ -314,6 +310,10 @@ Object.defineProperties(URL.prototype, {
return ret;
}
},
[Symbol.toStringTag]: {
configurable: true,
value: 'URL'
},
href: {
enumerable: true,
configurable: true,
Expand Down
5 changes: 3 additions & 2 deletions test/parallel/test-whatwg-url-parsing.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
'use strict';

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

if (!common.hasIntl) {
// A handful of the tests fail when ICU is not included.
Expand Down Expand Up @@ -144,8 +145,8 @@ for (const test of allTests) {
const url = test.url ? new URL(test.url) : new URL(test.input, test.base);

for (const showHidden of [true, false]) {
const res = url.inspect(null, {
showHidden: showHidden
const res = util.inspect(url, {
showHidden
});

const lines = res.split('\n');
Expand Down
4 changes: 2 additions & 2 deletions test/parallel/test-whatwg-url-properties.js
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ assert.strictEqual(url.searchParams, oldParams); // [SameObject]
// Note: this error message is subject to change in V8 updates
assert.throws(() => url.origin = 'http://foo.bar.com:22',
new RegExp('TypeError: Cannot set property origin of' +
' \\[object Object\\] which has only a getter'));
' \\[object URL\\] which has only a getter'));
assert.strictEqual(url.origin, 'http://foo.bar.com:21');
assert.strictEqual(url.toString(),
'http://user:pass@foo.bar.com:21/aaa/zzz?l=25#test');
Expand Down Expand Up @@ -121,7 +121,7 @@ assert.strictEqual(url.hash, '#abcd');
// Note: this error message is subject to change in V8 updates
assert.throws(() => url.searchParams = '?k=88',
new RegExp('TypeError: Cannot set property searchParams of' +
' \\[object Object\\] which has only a getter'));
' \\[object URL\\] which has only a getter'));
assert.strictEqual(url.searchParams, oldParams);
assert.strictEqual(url.toString(),
'https://user2:pass2@foo.bar.org:23/aaa/bbb?k=99#abcd');
Expand Down
7 changes: 5 additions & 2 deletions test/parallel/test-whatwg-url-tostringtag.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,12 @@ const sp = url.searchParams;

const test = [
[toString.call(url), 'URL'],
[toString.call(Object.getPrototypeOf(url)), 'URLPrototype'],
[toString.call(sp), 'URLSearchParams'],
[toString.call(Object.getPrototypeOf(sp)), 'URLSearchParamsPrototype']
[toString.call(Object.getPrototypeOf(sp)), 'URLSearchParamsPrototype'],
// Web IDL spec says we have to return 'URLPrototype', but it is too
// expensive to implement; therefore, use Chrome's behavior for now, until
// spec is changed.
[toString.call(Object.getPrototypeOf(url)), 'URL']
];

test.forEach((row) => {
Expand Down