From 45d7274c66032e53c291057abe767fc255cd29b1 Mon Sep 17 00:00:00 2001 From: James M Snell Date: Sat, 31 Dec 2016 23:14:18 -0800 Subject: [PATCH] url: set toStringTag for the URL class Fixes: https://github.com/nodejs/node/issues/10554 --- lib/internal/url.js | 22 +++++++------------- test/parallel/test-whatwg-url-tostringtag.js | 21 +++++++++++++++++++ 2 files changed, 29 insertions(+), 14 deletions(-) create mode 100644 test/parallel/test-whatwg-url-tostringtag.js diff --git a/lib/internal/url.js b/lib/internal/url.js index d7f859d95f0580..e1e1c515f842d2 100644 --- a/lib/internal/url.js +++ b/lib/internal/url.js @@ -221,6 +221,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; } @@ -640,15 +644,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) { @@ -803,12 +803,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) { diff --git a/test/parallel/test-whatwg-url-tostringtag.js b/test/parallel/test-whatwg-url-tostringtag.js new file mode 100644 index 00000000000000..9e0927955feaab --- /dev/null +++ b/test/parallel/test-whatwg-url-tostringtag.js @@ -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]}]`); +});