Skip to content

Commit b94eec1

Browse files
committed
url: define @@toStringTag as a data property
Even though this is not fully Web IDL spec-compliant, it is arguably the best we can do. Following the spec would mean non-trivial performance deterioration (10% when parsing a medium-length URL), while the current getter behavior is not adopted by any implementer, and it causes some spec ambiguity when the getter is called with !(this instanceof URL). This commit adopts Chrome's behavior, and is consistent with ECMAScript-defined classes while providing reasonable behaviors for corner cases as well. Until the Web IDL spec is changed one way or another, this is the way to go. PR-URL: #10906 Reviewed-By: James M Snell <jasnell@gmail.com> Reviewed-By: Joyee Cheung <joyeec9h3@gmail.com>
1 parent 499e75a commit b94eec1

File tree

3 files changed

+11
-8
lines changed

3 files changed

+11
-8
lines changed

lib/internal/url.js

+4-4
Original file line numberDiff line numberDiff line change
@@ -223,10 +223,6 @@ class URL {
223223
parse(this, input, base);
224224
}
225225

226-
get [Symbol.toStringTag]() {
227-
return this instanceof URL ? 'URL' : 'URLPrototype';
228-
}
229-
230226
get [special]() {
231227
return (this[context].flags & binding.URL_FLAGS_SPECIAL) !== 0;
232228
}
@@ -314,6 +310,10 @@ Object.defineProperties(URL.prototype, {
314310
return ret;
315311
}
316312
},
313+
[Symbol.toStringTag]: {
314+
configurable: true,
315+
value: 'URL'
316+
},
317317
href: {
318318
enumerable: true,
319319
configurable: true,

test/parallel/test-whatwg-url-properties.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ assert.strictEqual(url.searchParams, oldParams); // [SameObject]
4545
// Note: this error message is subject to change in V8 updates
4646
assert.throws(() => url.origin = 'http://foo.bar.com:22',
4747
new RegExp('TypeError: Cannot set property origin of' +
48-
' \\[object Object\\] which has only a getter'));
48+
' \\[object URL\\] which has only a getter'));
4949
assert.strictEqual(url.origin, 'http://foo.bar.com:21');
5050
assert.strictEqual(url.toString(),
5151
'http://user:pass@foo.bar.com:21/aaa/zzz?l=25#test');
@@ -121,7 +121,7 @@ assert.strictEqual(url.hash, '#abcd');
121121
// Note: this error message is subject to change in V8 updates
122122
assert.throws(() => url.searchParams = '?k=88',
123123
new RegExp('TypeError: Cannot set property searchParams of' +
124-
' \\[object Object\\] which has only a getter'));
124+
' \\[object URL\\] which has only a getter'));
125125
assert.strictEqual(url.searchParams, oldParams);
126126
assert.strictEqual(url.toString(),
127127
'https://user2:pass2@foo.bar.org:23/aaa/bbb?k=99#abcd');

test/parallel/test-whatwg-url-tostringtag.js

+5-2
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,12 @@ const sp = url.searchParams;
1010

1111
const test = [
1212
[toString.call(url), 'URL'],
13-
[toString.call(Object.getPrototypeOf(url)), 'URLPrototype'],
1413
[toString.call(sp), 'URLSearchParams'],
15-
[toString.call(Object.getPrototypeOf(sp)), 'URLSearchParamsPrototype']
14+
[toString.call(Object.getPrototypeOf(sp)), 'URLSearchParamsPrototype'],
15+
// Web IDL spec says we have to return 'URLPrototype', but it is too
16+
// expensive to implement; therefore, use Chrome's behavior for now, until
17+
// spec is changed.
18+
[toString.call(Object.getPrototypeOf(url)), 'URL']
1619
];
1720

1821
test.forEach((row) => {

0 commit comments

Comments
 (0)