Skip to content

Commit

Permalink
Use utils.define* sted __define*
Browse files Browse the repository at this point in the history
Replace all instances of `__defineGetter__` and `__defineSetter__` with the
respective method from `jsdom/utils`. This is work toward jsdom#711.

For posterity, the vim regex I used is:
`\(\s*\)\([^_]\+\)\.__\([^_]\+\)__(\([^,]\+\),/\1\3(\2, \4,`
  • Loading branch information
lawnsea committed Jan 3, 2014
1 parent 3806eee commit 51cbc39
Show file tree
Hide file tree
Showing 7 changed files with 69 additions and 55 deletions.
8 changes: 5 additions & 3 deletions lib/jsdom.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ var request = require('request');
var pkg = require('../package.json');

var toFileUrl = require('./jsdom/utils').toFileUrl;
var defineGetter = require('./jsdom/utils').defineGetter;
var defineSetter = require('./jsdom/utils').defineSetter;
var style = require('./jsdom/level2/style');
var features = require('./jsdom/browser/documentfeatures');
var dom = exports.dom = require('./jsdom/level3/index').dom;
Expand All @@ -18,17 +20,17 @@ exports.windowAugmentation = require('./jsdom/browser/index').windowAugmentation
['availableDocumentFeatures',
'defaultDocumentFeatures',
'applyDocumentFeatures'].forEach(function (propName) {
exports.__defineGetter__(propName, function () {
defineGetter(exports, propName, function () {
return features[propName];
});
exports.__defineSetter__(propName, function (val) {
defineSetter(exports, propName, function (val) {
return features[propName] = val;
});
});

exports.debugMode = false;

exports.__defineGetter__('version', function() {
defineGetter(exports, 'version', function() {
return pkg.version;
});

Expand Down
32 changes: 17 additions & 15 deletions lib/jsdom/browser/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ var http = require('http'),
NOT_IMPLEMENTED = require('./utils').NOT_IMPLEMENTED,
CSSStyleDeclaration = require('cssstyle').CSSStyleDeclaration,
toFileUrl = require('../utils').toFileUrl,
defineGetter = require('../utils').defineGetter,
defineSetter = require('../utils').defineSetter,
Contextify = require('contextify');

function matchesDontThrow(el, selector) {
Expand Down Expand Up @@ -406,7 +408,7 @@ var browserAugmentation = exports.browserAugmentation = function(dom, options) {
return new dom.NodeList(this.ownerDocument || this, dom.mapper(this, filterByClassName));
};

dom.Element.prototype.__defineGetter__('sourceIndex', function() {
defineGetter(dom.Element.prototype, 'sourceIndex', function() {
/*
* According to QuirksMode:
* Get the sourceIndex of element x. This is also the index number for
Expand All @@ -423,15 +425,15 @@ var browserAugmentation = exports.browserAugmentation = function(dom, options) {
}
});

dom.Document.prototype.__defineGetter__('outerHTML', function() {
defineGetter(dom.Document.prototype, 'outerHTML', function() {
return domToHtml(this, true);
});

dom.Element.prototype.__defineGetter__('outerHTML', function() {
defineGetter(dom.Element.prototype, 'outerHTML', function() {
return domToHtml(this, true);
});

dom.Element.prototype.__defineGetter__('innerHTML', function() {
defineGetter(dom.Element.prototype, 'innerHTML', function() {
if (/^(?:script|style)$/.test(this._tagName)) {
var type = this.getAttribute('type');
if (!type || /^text\//i.test(type) || /\/javascript$/i.test(type)) {
Expand All @@ -442,10 +444,10 @@ var browserAugmentation = exports.browserAugmentation = function(dom, options) {
return domToHtml(this._childNodes, true);
});

dom.Element.prototype.__defineSetter__('doctype', function() {
defineSetter(dom.Element.prototype, 'doctype', function() {
throw new dom.DOMException(dom.NO_MODIFICATION_ALLOWED_ERR);
});
dom.Element.prototype.__defineGetter__('doctype', function() {
defineGetter(dom.Element.prototype, 'doctype', function() {
var r = null;
if (this.nodeName == '#document') {
if (this._doctype) {
Expand All @@ -455,7 +457,7 @@ var browserAugmentation = exports.browserAugmentation = function(dom, options) {
return r;
});

dom.Element.prototype.__defineSetter__('innerHTML', function(html) {
defineSetter(dom.Element.prototype, 'innerHTML', function(html) {
//Clear the children first:
var child;
while ((child = this._childNodes[0])) {
Expand All @@ -472,11 +474,11 @@ var browserAugmentation = exports.browserAugmentation = function(dom, options) {
});


dom.Document.prototype.__defineGetter__('innerHTML', function() {
defineGetter(dom.Document.prototype, 'innerHTML', function() {
return domToHtml(this._childNodes, true);
});

dom.Document.prototype.__defineSetter__('innerHTML', function(html) {
defineSetter(dom.Document.prototype, 'innerHTML', function(html) {
//Clear the children first:
var child;
while ((child = this._childNodes[0])) {
Expand Down Expand Up @@ -585,11 +587,11 @@ var browserAugmentation = exports.browserAugmentation = function(dom, options) {
return new dom.NodeList(this.ownerDocument || this, dom.mapper(this, filterByClassName));
};

dom.Element.prototype.__defineGetter__('nodeName', function(val) {
defineGetter(dom.Element.prototype, 'nodeName', function(val) {
return this._nodeName.toUpperCase();
});

dom.Element.prototype.__defineGetter__('tagName', function(val) {
defineGetter(dom.Element.prototype, 'tagName', function(val) {
var t = this._tagName.toUpperCase();
//Document should not return a tagName
if (this.nodeName === '#document') {
Expand All @@ -601,27 +603,27 @@ var browserAugmentation = exports.browserAugmentation = function(dom, options) {
dom.Element.prototype.scrollTop = 0;
dom.Element.prototype.scrollLeft = 0;

dom.Document.prototype.__defineGetter__('parentWindow', function() {
defineGetter(dom.Document.prototype, 'parentWindow', function() {
if (!this._parentWindow) {
this.parentWindow = exports.windowAugmentation(dom, {document: this, url: this.URL});
}
return this._parentWindow;
});

dom.Document.prototype.__defineSetter__('parentWindow', function(window) {
defineSetter(dom.Document.prototype, 'parentWindow', function(window) {
// Contextify does not support getters and setters, so we have to set them
// on the original object instead.
window._frame = function (name, frame) {
if (typeof frame === 'undefined') {
delete window[name];
} else {
window.__defineGetter__(name, function () { return frame.contentWindow; });
defineGetter(window, name, function () { return frame.contentWindow; });
}
};
this._parentWindow = window.getGlobal();
});

dom.Document.prototype.__defineGetter__('defaultView', function() {
defineGetter(dom.Document.prototype, 'defaultView', function() {
return this.parentWindow;
});

Expand Down
30 changes: 16 additions & 14 deletions lib/jsdom/level2/core.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
var core = require("../level1/core").dom.level1.core;
var defineGetter = require('../utils').defineGetter;
var defineSetter = require('../utils').defineSetter;

// modify cloned instance for more info check: https://github.com/tmpvar/jsdom/issues/325
core = Object.create(core);
Expand Down Expand Up @@ -103,7 +105,7 @@ core.DOMImplementation.prototype.createDocument = function(/* String */ na
return document;
};

core.Node.prototype.__defineGetter__("ownerDocument", function() {
defineGetter(core.Node.prototype, "ownerDocument", function() {
return this._ownerDocument || null;
});

Expand All @@ -114,19 +116,19 @@ core.Node.prototype.isSupported = function(/* string */ feature,
};

core.Node.prototype._namespaceURI = null;
core.Node.prototype.__defineGetter__("namespaceURI", function() {
defineGetter(core.Node.prototype, "namespaceURI", function() {
return this._namespaceURI || null;
});

core.Node.prototype.__defineSetter__("namespaceURI", function(value) {
defineSetter(core.Node.prototype, "namespaceURI", function(value) {
this._namespaceURI = value;
});

core.Node.prototype.__defineGetter__("prefix", function() {
defineGetter(core.Node.prototype, "prefix", function() {
return this._prefix || null;
});

core.Node.prototype.__defineSetter__("prefix", function(value) {
defineSetter(core.Node.prototype, "prefix", function(value) {

if (this.readonly) {
throw new core.DOMException(core.NO_MODIFICATION_ALLOWED_ERR);
Expand All @@ -148,7 +150,7 @@ core.Node.prototype.__defineSetter__("prefix", function(value) {
this._prefix = value;
});

core.Node.prototype.__defineGetter__("localName", function() {
defineGetter(core.Node.prototype, "localName", function() {
return this._localName || null;
});

Expand Down Expand Up @@ -290,22 +292,22 @@ core.NamedNodeMap.prototype.removeNamedItemNS = function(/*string */ namespaceUR
return found;
};

core.Attr.prototype.__defineGetter__("ownerElement", function() {
defineGetter(core.Attr.prototype, "ownerElement", function() {
return this._ownerElement || null;
});


core.Node.prototype._prefix = false;

core.Node.prototype.__defineSetter__("qualifiedName", function(qualifiedName) {
defineSetter(core.Node.prototype, "qualifiedName", function(qualifiedName) {
ns.validate(qualifiedName, this._namespaceURI);
qualifiedName = qualifiedName || "";
this._localName = qualifiedName.split(":")[1] || null;
this.prefix = qualifiedName.split(":")[0] || null;
this._nodeName = qualifiedName;
});

core.Node.prototype.__defineGetter__("qualifiedName", function() {
defineGetter(core.Node.prototype, "qualifiedName", function() {
return this._nodeName;
});

Expand Down Expand Up @@ -473,15 +475,15 @@ core.Element.prototype.hasAttributeNS = function(/* string */namespaceURI,
return false;
};

core.DocumentType.prototype.__defineGetter__("publicId", function() {
defineGetter(core.DocumentType.prototype, "publicId", function() {
return this._publicId || "";
});

core.DocumentType.prototype.__defineGetter__("systemId", function() {
defineGetter(core.DocumentType.prototype, "systemId", function() {
return this._systemId || "";
});

core.DocumentType.prototype.__defineGetter__("internalSubset", function() {
defineGetter(core.DocumentType.prototype, "internalSubset", function() {
return this._internalSubset || null;
});

Expand Down Expand Up @@ -621,11 +623,11 @@ core.Document.prototype.getElementsByTagNameNS = function(/* String */ namespace
localName);
};

core.Element.prototype.__defineSetter__("id", function(id) {
defineSetter(core.Element.prototype, "id", function(id) {
this.setAttribute("id", id);
});

core.Element.prototype.__defineGetter__("id",function() {
defineGetter(core.Element.prototype, "id", function() {
return this.getAttribute("id");
});

Expand Down
8 changes: 5 additions & 3 deletions lib/jsdom/level2/events.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,9 @@
*
*/
var core = require("./core").dom.level2.core,
utils = require("../utils");
utils = require("../utils"),
defineGetter = utils.defineGetter,
defineSetter = utils.defineSetter;

// modify cloned instance for more info check: https://github.com/tmpvar/jsdom/issues/325
core = Object.create(core);
Expand Down Expand Up @@ -378,10 +380,10 @@ function dispatchAttrEvent(change) {
utils.intercept(core.AttrNodeMap, 'removeNamedItem', dispatchAttrEvent('REMOVAL'));
utils.intercept(core.AttrNodeMap, 'setNamedItem', dispatchAttrEvent('ADDITION'));

core.CharacterData.prototype.__defineGetter__("_nodeValue", function() {
defineGetter(core.CharacterData.prototype, "_nodeValue", function() {
return this.__nodeValue;
});
core.CharacterData.prototype.__defineSetter__("_nodeValue", function(value) {
defineSetter(core.CharacterData.prototype, "_nodeValue", function(value) {
var oldValue = this.__nodeValue;
this.__nodeValue = value;
if (this._ownerDocument && this._parentNode && mutationEventsEnabled(this)) {
Expand Down
12 changes: 7 additions & 5 deletions lib/jsdom/level2/html.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
var core = require("./core").dom.level2.core,
events = require("./core").dom.level2.events,
applyDocumentFeatures = require('../browser/documentfeatures').applyDocumentFeatures,
defineGetter = require('../utils').defineGetter,
defineSetter = require('../utils').defineSetter,
URL = require("url"),
Path = require('path'),
fs = require("fs"),
Expand Down Expand Up @@ -194,7 +196,7 @@ function define(elementClass, def) {
attr = n.attr || prop.toLowerCase();

if (!n.prop || n.read !== false) {
elem.prototype.__defineGetter__(prop, function() {
defineGetter(elem.prototype, prop, function() {
var s = this.getAttribute(attr);
if (n.type && n.type === 'boolean') {
return s !== null;
Expand All @@ -210,7 +212,7 @@ function define(elementClass, def) {
}

if (!n.prop || n.write !== false) {
elem.prototype.__defineSetter__(prop, function(val) {
defineSetter(elem.prototype, prop, function(val) {
if (!val) {
this.removeAttribute(attr);
}
Expand Down Expand Up @@ -729,10 +731,10 @@ define('HTMLBodyElement', {
'onfocus', 'onhashchange', 'onload', 'onmessage', 'onoffline', 'ononline',
'onpagehide', 'onpageshow', 'onpopstate', 'onresize', 'onscroll',
'onstorage', 'onunload'].forEach(function (name) {
proto.__defineSetter__(name, function (handler) {
defineSetter(proto, name, function (handler) {
this._ownerDocument.parentWindow[name] = handler;
});
proto.__defineGetter__(name, function () {
defineGetter(proto, name, function () {
return this._ownerDocument.parentWindow[name];
});
});
Expand Down Expand Up @@ -1862,7 +1864,7 @@ define('HTMLFrameElement', {
var parent = this._ownerDocument.parentWindow;
var frameID = parent._length++;
var self = this;
parent.__defineGetter__(frameID, function () {
defineGetter(parent, frameID, function () {
return self.contentWindow;
});

Expand Down
10 changes: 6 additions & 4 deletions lib/jsdom/level2/style.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
var core = require("./core").dom.level2.core,
html = require("./html").dom.level2.html,
utils = require("../utils"),
defineGetter = utils.defineGetter,
defineSetter = utils.defineSetter,
cssom = require("cssom"),
cssstyle = require("cssstyle"),
assert = require('assert');
Expand Down Expand Up @@ -76,7 +78,7 @@ core.StyleSheetList.prototype = {
}
};

core.Document.prototype.__defineGetter__('styleSheets', function() {
defineGetter(core.Document.prototype, 'styleSheets', function() {
if (!this._styleSheets) {
this._styleSheets = new core.StyleSheetList();
}
Expand Down Expand Up @@ -183,7 +185,7 @@ utils.intercept(core.AttrNodeMap, 'setNamedItem', function(_super, args, attr) {
/**
* Lazily create a CSSStyleDeclaration.
*/
html.HTMLElement.prototype.__defineGetter__('style', function() {
defineGetter(html.HTMLElement.prototype, 'style', function() {
if (typeof this._style === 'string') {
// currently, cssom's parse doesn't really work if you pass in
// {state: 'name'}, so instead we just build a dummy sheet.
Expand Down Expand Up @@ -237,7 +239,7 @@ var getOrCreateSheet = function() {
}
return this._cssStyleSheet;
};
html.HTMLLinkElement.prototype.__defineGetter__('sheet', getOrCreateSheet);
defineGetter(html.HTMLLinkElement.prototype, 'sheet', getOrCreateSheet);

assert.equal(undefined, html.HTMLStyleElement._init);
html.HTMLStyleElement._init = function() {
Expand All @@ -258,7 +260,7 @@ html.HTMLStyleElement._init = function() {
evaluateStylesheet.call(this, content, this.sheet, this._ownerDocument.URL);
});
};
html.HTMLStyleElement.prototype.__defineGetter__('sheet', getOrCreateSheet);
defineGetter(html.HTMLStyleElement.prototype, 'sheet', getOrCreateSheet);

exports.dom = {
level2 : {
Expand Down
Loading

0 comments on commit 51cbc39

Please sign in to comment.