diff --git a/lib/jsdom.js b/lib/jsdom.js index 0751f7d512..2d6b7a5b13 100644 --- a/lib/jsdom.js +++ b/lib/jsdom.js @@ -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; @@ -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; }); diff --git a/lib/jsdom/browser/index.js b/lib/jsdom/browser/index.js index 31701a2ffd..cdf41ee692 100644 --- a/lib/jsdom/browser/index.js +++ b/lib/jsdom/browser/index.js @@ -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) { @@ -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 @@ -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)) { @@ -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) { @@ -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])) { @@ -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])) { @@ -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') { @@ -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; }); diff --git a/lib/jsdom/level2/core.js b/lib/jsdom/level2/core.js index 4fb5d1f480..63ce394378 100644 --- a/lib/jsdom/level2/core.js +++ b/lib/jsdom/level2/core.js @@ -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); @@ -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; }); @@ -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); @@ -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; }); @@ -290,14 +292,14 @@ 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; @@ -305,7 +307,7 @@ core.Node.prototype.__defineSetter__("qualifiedName", function(qualifiedName) { this._nodeName = qualifiedName; }); -core.Node.prototype.__defineGetter__("qualifiedName", function() { +defineGetter(core.Node.prototype, "qualifiedName", function() { return this._nodeName; }); @@ -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; }); @@ -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"); }); diff --git a/lib/jsdom/level2/events.js b/lib/jsdom/level2/events.js index 546a9be608..db44a31237 100644 --- a/lib/jsdom/level2/events.js +++ b/lib/jsdom/level2/events.js @@ -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); @@ -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)) { diff --git a/lib/jsdom/level2/html.js b/lib/jsdom/level2/html.js index 964c3da705..1673156671 100644 --- a/lib/jsdom/level2/html.js +++ b/lib/jsdom/level2/html.js @@ -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"), @@ -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; @@ -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); } @@ -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]; }); }); @@ -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; }); diff --git a/lib/jsdom/level2/style.js b/lib/jsdom/level2/style.js index bb957376c1..53ff6e4d03 100644 --- a/lib/jsdom/level2/style.js +++ b/lib/jsdom/level2/style.js @@ -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'); @@ -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(); } @@ -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. @@ -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() { @@ -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 : { diff --git a/lib/jsdom/level3/core.js b/lib/jsdom/level3/core.js index c9221e7da3..495dbc79e2 100644 --- a/lib/jsdom/level3/core.js +++ b/lib/jsdom/level3/core.js @@ -1,4 +1,6 @@ var core = require("../level2/core").dom.level2.core, + defineGetter = require('../utils').defineGetter, + defineSetter = require('../utils').defineSetter, HtmlToDom = require('../browser/htmltodom').HtmlToDom, domToHtml = require('../browser/domtohtml').domToHtml, htmlencoding = require('../browser/htmlencoding'), @@ -149,7 +151,7 @@ core.Node.prototype.isSameNode = function(other) { }; // @see http://www.w3.org/TR/2004/REC-DOM-Level-3-Core-20040407/core.html#Node3-textContent -core.Node.prototype.__defineGetter__('textContent', function() { +defineGetter(core.Node.prototype, 'textContent', function() { switch (this.nodeType) { case this.COMMENT_NODE: case this.CDATA_SECTION_NODE: @@ -176,7 +178,7 @@ core.Node.prototype.__defineGetter__('textContent', function() { } }); -core.Node.prototype.__defineSetter__('textContent', function(txt) { +defineSetter(core.Node.prototype, 'textContent', function(txt) { for (var i = this.childNodes.length; --i >=0;) { this.removeChild(this.childNodes.item(i)); } @@ -326,7 +328,7 @@ core.Node.prototype.getUserData = function(key) { */ // Introduced in DOM Level 3: -core.Attr.prototype.__defineGetter__('isId', function() { +defineGetter(core.Attr.prototype, 'isId', function() { return (this.name.toLowerCase() === 'id'); }); /* @@ -446,22 +448,22 @@ core.DOMError.prototype = {}; core.DOMError.prototype.SEVERITY_WARNING = 1; core.DOMError.prototype.SEVERITY_ERROR = 2; core.DOMError.prototype.SEVERITY_FATAL_ERROR = 3; -core.DOMError.prototype.__defineGetter__('severity', function() { +defineGetter(core.DOMError.prototype, 'severity', function() { return this._severity; }); -core.DOMError.prototype.__defineGetter__('message', function() { +defineGetter(core.DOMError.prototype, 'message', function() { return this._message; }); -core.DOMError.prototype.__defineGetter__('type', function() { +defineGetter(core.DOMError.prototype, 'type', function() { return this._type; }); -core.DOMError.prototype.__defineGetter__('relatedException', function() { +defineGetter(core.DOMError.prototype, 'relatedException', function() { return this._relatedException; }); -core.DOMError.prototype.__defineGetter__('relatedData', function() { +defineGetter(core.DOMError.prototype, 'relatedData', function() { return this._relatedData; }); -core.DOMError.prototype.__defineGetter__('location', function() { +defineGetter(core.DOMError.prototype, 'location', function() { return this._location; }); @@ -514,7 +516,7 @@ core.DOMConfiguration.prototype = { }; //core.Document.prototype._domConfig = new core.DOMConfiguration(); -core.Document.prototype.__defineGetter__('domConfig', function() { +defineGetter(core.Document.prototype, 'domConfig', function() { return this._domConfig || new core.DOMConfiguration();; }); @@ -615,7 +617,7 @@ core.DOMStringList.prototype = { // Introduced in DOM Level 3: core.Document.prototype._inputEncoding = null; -core.Document.prototype.__defineGetter__('inputEncoding', function() { +defineGetter(core.Document.prototype, 'inputEncoding', function() { return this._inputEncoding; }); /*