Skip to content

Commit

Permalink
Provide methods to define ES5 getters and setters
Browse files Browse the repository at this point in the history
To allow jsdom to run in environments that don't support `__defineGetter__` and
`__defineSetter__`, implement equivalents in terms of ES5's
`Object.defineProperty`.
  • Loading branch information
lawnsea committed Jan 3, 2014
1 parent 721251d commit 3806eee
Show file tree
Hide file tree
Showing 3 changed files with 160 additions and 0 deletions.
40 changes: 40 additions & 0 deletions lib/jsdom/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -39,3 +39,43 @@ exports.toFileUrl = function (fileName) {

return 'file://' + 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) {
var descriptor = Object.getOwnPropertyDescriptor(object, property) || {
configurable: true,
enumerable: true
};

descriptor.set = setterFn;

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

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

descriptor.get = getterFn;

Object.defineProperty(object, property, descriptor);
};
119 changes: 119 additions & 0 deletions test/jsdom/utils.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,119 @@
"use strict";

var utils = require("../../lib/jsdom/utils");

exports["defineSetter defines a setter"] = function (t) {
var o = {};
var called = false;
var expected = 'bar';
var actual;

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

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

t.done();
};

exports["defineSetter replaces existing setters"] = function (t) {
var o = {};
var originalCalled = false;
var newCalled = false;

utils.defineSetter(o, 'foo', function (val) {
originalCalled = true;
});

utils.defineSetter(o, 'foo', function (val) {
newCalled = true;
});

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

t.done();
};

exports["defineSetter does not remove existing getters"] = function (t) {
var o = {};
var called = false;
var expected = 'bar';
var actual;

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

utils.defineSetter(o, 'foo', function (val) { /* NOP */ });

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

t.done();
};

exports["defineGetter defines a getter"] = function (t) {
var o = {};
var called = false;
var expected = 'bar';
var actual;

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

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

t.done();
};

exports["defineGetter replaces existing getters"] = function (t) {
var o = {};
var originalCalled = false;
var newCalled = false;

utils.defineGetter(o, 'foo', function (val) {
originalCalled = true;
});

utils.defineGetter(o, 'foo', function (val) {
newCalled = true;
});

var actual = o.foo;
t.equal(originalCalled, false);
t.equal(newCalled, true);

t.done();
};

exports["defineGetter does not remove existing setters"] = function (t) {
var o = {};
var called = false;
var expected = 'bar';
var actual;

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

utils.defineGetter(o, 'foo', function () { /* NOP */ });

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

t.done();
};
1 change: 1 addition & 0 deletions test/runner
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,7 @@ var files = [
"jsdom/index.js",
"jsdom/parsing.js",
"jsdom/env.js",
"jsdom/utils.js",
"jsonp/jsonp.js",
"browser/css.js",
"browser/index.js"
Expand Down

0 comments on commit 3806eee

Please sign in to comment.