Skip to content

Commit

Permalink
Use Symbol.toStringTag to make Object.prototype.toString work
Browse files Browse the repository at this point in the history
Closes #1809; closes #1136. Most of the changes are due to jsdom/webidl2js@0590e5a, but the non-webidl2js classes needed to be updated manually.
  • Loading branch information
domenic committed Apr 29, 2017
1 parent 4591c3c commit 9d0c03e
Show file tree
Hide file tree
Showing 10 changed files with 13 additions and 15 deletions.
1 change: 1 addition & 0 deletions lib/jsdom/browser/Window.js
Original file line number Diff line number Diff line change
Expand Up @@ -503,6 +503,7 @@ function Window(options) {

Object.setPrototypeOf(Window, EventTarget.interface);
Object.setPrototypeOf(Window.prototype, EventTarget.interface.prototype);
Window.prototype[Symbol.toStringTag] = "Window";

function matchesDontThrow(el, selector) {
try {
Expand Down
2 changes: 2 additions & 0 deletions lib/jsdom/living/dom-token-list.js
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,8 @@ class DOMTokenList {
}
}

DOMTokenList.prototype[Symbol.toStringTag] = "DOMTokenList";

function serialize(list) {
const value = list[INTERNAL].element.getAttribute(list[INTERNAL].attribute);
return value === null ? "" : value;
Expand Down
1 change: 1 addition & 0 deletions lib/jsdom/living/html-collection.js
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ class HTMLCollection {
}

HTMLCollection.prototype[Symbol.iterator] = Array.prototype[Symbol.iterator];
HTMLCollection.prototype[Symbol.toStringTag] = "HTMLCollection";

function updateHTMLCollection(collection) {
if (collection[privates].version < collection[privates].element._version) {
Expand Down
4 changes: 1 addition & 3 deletions lib/jsdom/living/node-iterator.js
Original file line number Diff line number Diff line change
Expand Up @@ -226,7 +226,5 @@ module.exports = function (core) {
// noop
};

core.NodeIterator.prototype.toString = function () {
return "[object NodeIterator]";
};
core.NodeIterator.prototype[Symbol.toStringTag] = "NodeIterator";
};
1 change: 1 addition & 0 deletions lib/jsdom/living/node-list.js
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,7 @@ class NodeList {
}

NodeList.prototype[Symbol.iterator] = NodeList.prototype.values = Array.prototype[Symbol.iterator];
NodeList.prototype[Symbol.toStringTag] = "NodeList";

function updateNodeList(nodeList) {
if (nodeList[privates].isLive) {
Expand Down
3 changes: 2 additions & 1 deletion lib/jsdom/living/xmlhttprequest-event-target.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
const EventTarget = require("./generated/EventTarget");

function XMLHttpRequestEventTarget() {
if (!(this instanceof XMLHttpRequestEventTarget)) {
if (!new.target) {
throw new TypeError("DOM object constructor cannot be called as a function.");
}
EventTarget.setup(this);
Expand All @@ -17,6 +17,7 @@ function XMLHttpRequestEventTarget() {
}

XMLHttpRequestEventTarget.prototype = Object.create(EventTarget.interface.prototype);
XMLHttpRequestEventTarget.prototype[Symbol.toStringTag] = "XMLHttpRequestEventTarget";

module.exports = function (core) {
core.XMLHttpRequestEventTarget = XMLHttpRequestEventTarget;
Expand Down
3 changes: 2 additions & 1 deletion lib/jsdom/living/xmlhttprequest-upload.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,12 @@ module.exports = function (core) {
class XMLHttpRequestUpload extends XMLHttpRequestEventTarget {
constructor() {
super();
if (!(this instanceof XMLHttpRequestUpload)) {
if (!new.target) {
throw new TypeError("DOM object constructor cannot be called as a function.");
}
}
}
XMLHttpRequestUpload.prototype[Symbol.toStringTag] = "XMLHttpRequestUpload";

core.XMLHttpRequestUpload = XMLHttpRequestUpload;
};
Expand Down
6 changes: 2 additions & 4 deletions lib/jsdom/living/xmlhttprequest.js
Original file line number Diff line number Diff line change
Expand Up @@ -801,15 +801,13 @@ module.exports = function createXMLHttpRequest(window) {
flag.requestHeaders[lcHeader] = value;
}

toString() {
return "[object XMLHttpRequest]";
}

get _ownerDocument() {
return idlUtils.implForWrapper(window.document);
}
}

XMLHttpRequest.prototype[Symbol.toStringTag] = "XMLHttpRequest";

addConstants(XMLHttpRequest, {
UNSENT: 0,
OPENED: 1,
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@
"st": "^1.2.0",
"watchify": "^3.9.0",
"wd": "^1.1.3",
"webidl2js": "^5.1.1"
"webidl2js": "^6.0.3"
},
"browser": {
"canvas": false,
Expand Down
5 changes: 0 additions & 5 deletions test/to-port-to-wpts/misc2.js
Original file line number Diff line number Diff line change
Expand Up @@ -812,11 +812,6 @@ describe("jsdom/miscellaneous", () => {
assert.ok(iframeDocument.body);
});

specify("issue_935_document_tostring_returns_null", () => {
const document = jsdom.jsdom();
assert.equal(document.toString(), "[object HTMLDocument]");
});

specify("addmetatohead", () => {
const window = jsdom.jsdom().defaultView;
const meta = window.document.createElement("meta");
Expand Down

0 comments on commit 9d0c03e

Please sign in to comment.