Skip to content

Commit

Permalink
url: set toStringTag for the URL class
Browse files Browse the repository at this point in the history
PR-URL: #10562
Fixes: #10554
Reviewed-By: Michaël Zasso <targos@protonmail.com>
Reviewed-By: Anna Henningsen <anna@addaleax.net>
Reviewed-By: Colin Ihrig <cjihrig@gmail.com>
Reviewed-By: Benjamin Gruenbaum <benjamingr@gmail.com>
  • Loading branch information
jasnell committed Jan 3, 2017
1 parent 134481d commit f5d92f3
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 14 deletions.
22 changes: 8 additions & 14 deletions lib/internal/url.js
Original file line number Diff line number Diff line change
Expand Up @@ -222,6 +222,10 @@ 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 @@ -641,15 +645,11 @@ class URLSearchParams {

// "associated url object"
this[context] = null;
}

// Class string for an instance of URLSearchParams. This is different from
// the class string of the prototype object (set below).
Object.defineProperty(this, Symbol.toStringTag, {
value: 'URLSearchParams',
writable: false,
enumerable: false,
configurable: true
});
get [Symbol.toStringTag]() {
return this instanceof URLSearchParams ?
'URLSearchParams' : 'URLSearchParamsPrototype';
}

append(name, value) {
Expand Down Expand Up @@ -804,12 +804,6 @@ class URLSearchParams {
}
// https://heycam.github.io/webidl/#es-iterable-entries
URLSearchParams.prototype[Symbol.iterator] = URLSearchParams.prototype.entries;
Object.defineProperty(URLSearchParams.prototype, Symbol.toStringTag, {
value: 'URLSearchParamsPrototype',
writable: false,
enumerable: false,
configurable: true
});

// https://heycam.github.io/webidl/#dfn-default-iterator-object
function createSearchParamsIterator(target, kind) {
Expand Down
21 changes: 21 additions & 0 deletions test/parallel/test-whatwg-url-tostringtag.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
'use strict';

require('../common');
const assert = require('assert');
const URL = require('url').URL;
const toString = Object.prototype.toString;

const url = new URL('http://example.org');
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']
];

test.forEach((row) => {
assert.strictEqual(row[0], `[object ${row[1]}]`,
`${row[0]} !== [object ${row[1]}]`);
});

0 comments on commit f5d92f3

Please sign in to comment.