Skip to content

Commit

Permalink
Remove the old defineSetter utility
Browse files Browse the repository at this point in the history
  • Loading branch information
domenic committed Feb 26, 2017
1 parent 79f5522 commit 2161116
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 91 deletions.
28 changes: 12 additions & 16 deletions lib/jsdom/living/nodes/HTMLBodyElement-impl.js
@@ -1,29 +1,25 @@
"use strict";

const HTMLElementImpl = require("./HTMLElement-impl").implementation;

const defineGetter = require("../../utils").defineGetter;
const defineSetter = require("../../utils").defineSetter;
const proxiedWindowEventHandlers = require("../helpers/proxied-window-event-handlers");

class HTMLBodyElementImpl extends HTMLElementImpl {
class HTMLBodyElementImpl extends HTMLElementImpl {}

}

(function () {
proxiedWindowEventHandlers.forEach(name => {
defineSetter(HTMLBodyElementImpl.prototype, name, function (handler) {
for (const name of proxiedWindowEventHandlers) {
Object.defineProperty(HTMLBodyElementImpl.prototype, name, {
configurable: true,
enumerable: true,
get() {
const window = this._ownerDocument._defaultView;
return window ? window[name] : null;
},
set(handler) {
const window = this._ownerDocument._defaultView;
if (window) {
window[name] = handler;
}
});
defineGetter(HTMLBodyElementImpl.prototype, name, function () {
const window = this._ownerDocument._defaultView;
return window ? window[name] : null;
});
}
});
}());
}

module.exports = {
implementation: HTMLBodyElementImpl
Expand Down
20 changes: 0 additions & 20 deletions lib/jsdom/utils.js
Expand Up @@ -19,26 +19,6 @@ exports.toFileUrl = function (fileName) {
return "file://" + encodeURI(pathname);
};

/**
* Define a setter on an object
*
* This method replaces any existing setter but leaves getters in place.
*
* - `object` {Object} the object to define the setter on
* - `property` {String} the name of the setter
* - `setterFn` {Function} the setter
*/
exports.defineSetter = function defineSetter(object, property, setterFn) {
const descriptor = Object.getOwnPropertyDescriptor(object, property) || {
configurable: true,
enumerable: true
};

descriptor.set = setterFn;

Object.defineProperty(object, property, descriptor);
};

/**
* Define a getter on an object
*
Expand Down
63 changes: 8 additions & 55 deletions test/jsdom/utils.js
Expand Up @@ -7,58 +7,6 @@ const specify = require("mocha-sugar-free").specify;
const utils = require("../../lib/jsdom/utils");

describe("jsdom/utils", () => {
specify("defineSetter defines a setter", () => {
const o = {};
let called = false;
const expected = "bar";
let actual;

utils.defineSetter(o, "foo", val => {
called = true;
actual = val;
});

o.foo = expected;
assert.equal(called, true);
assert.equal(actual, expected);
});

specify("defineSetter replaces existing setters", () => {
const o = {};
let originalCalled = false;
let newCalled = false;

utils.defineSetter(o, "foo", () => {
originalCalled = true;
});
utils.defineSetter(o, "foo", () => {
newCalled = true;
});

o.foo = true;
assert.equal(originalCalled, false);
assert.equal(newCalled, true);
});

specify("defineSetter does not remove existing getters", () => {
const o = {};
let called = false;
const expected = "bar";

utils.defineGetter(o, "foo", () => {
called = true;
return expected;
});

utils.defineSetter(o, "foo", () => {
// doesn't matter for this test
});

const actual = o.foo;
assert.equal(called, true);
assert.equal(actual, expected);
});

specify("defineGetter defines a getter", () => {
const o = {};
let called = false;
Expand Down Expand Up @@ -100,10 +48,15 @@ describe("jsdom/utils", () => {
const expected = "bar";
let actual;

utils.defineSetter(o, "foo", val => {
called = true;
actual = val;
/* eslint-disable accessor-pairs */
Object.defineProperty(o, "foo", {
configurable: true,
set(val) {
called = true;
actual = val;
}
});
/* eslint-enable accessor-pairs */

utils.defineGetter(o, "foo", () => {
// doesn't matter for this test
Expand Down

0 comments on commit 2161116

Please sign in to comment.