Skip to content

Commit

Permalink
url: define @@toStringTag as a data property
Browse files Browse the repository at this point in the history
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>
  • Loading branch information
TimothyGu authored and evanlucas committed Jan 31, 2017
1 parent f1851cb commit 2bfd58a
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 8 deletions.
8 changes: 4 additions & 4 deletions lib/internal/url.js
Original file line number Diff line number Diff line change
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 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
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

0 comments on commit 2bfd58a

Please sign in to comment.