diff --git a/lib/jsdom/level1/core.js b/lib/jsdom/level1/core.js index ee852abbdf..19df04cc5d 100644 --- a/lib/jsdom/level1/core.js +++ b/lib/jsdom/level1/core.js @@ -12,7 +12,7 @@ var core = { // Returns Array mapDOMNodes : function(parent, recursive, callback) { function visit(parent, result) { - return parent.childNodes.toArray().reduce(reducer, result); + return parent.childNodes.reduce(reducer, result); } function reducer(array, child) { @@ -139,6 +139,17 @@ core.DOMException.prototype = { core.DOMException.prototype.__proto__ = Error.prototype; core.NodeList = function NodeList(element, query) { + if (!query) { + // Non-live NodeList + var list = element || []; + list.item = function(i) { + return this[i]; + }; + list.toArray = function() { + return this; + }; + return list; + } Object.defineProperties(this, { _element: {value: element}, _query: {value: query}, @@ -150,13 +161,14 @@ core.NodeList = function NodeList(element, query) { }; core.NodeList.prototype = { update: function() { + var i; if (this._element && this._version < this._element._version) { - for (var i = 0; i < this._length; i++) { + for (i = 0; i < this._length; i++) { this[i] = undefined; } var nodes = this._snapshot = this._query(); this._length = nodes.length; - for (var i = 0; i < nodes.length; i++) { + for (i = 0; i < nodes.length; i++) { this[i] = nodes[i]; } this._version = this._element._version; @@ -291,27 +303,17 @@ var attrCopy = function(src, dest, fn) { }; core.Node = function Node(ownerDocument) { - var self = this; - - this._childNodes = []; + this._childNodes = new core.NodeList(); this._ownerDocument = ownerDocument; this._attributes = new core.AttrNodeMap(ownerDocument, this); - - this._childrenList = new core.NodeList(this, function() { - return self._childNodes.filter(function(node) { - return node.tagName; - }); - }); - - this._childNodesList = new core.NodeList(this, function() { - return self._childNodes; - }); - + this._nodeName = null; + this._childrenList = null; this._version = 0; this._nodeValue = null; this._parentNode = null; this._nodeName = null; this._readonly = false; + this.style = null; }; core.Node.ELEMENT_NODE = ELEMENT_NODE; @@ -328,18 +330,6 @@ core.Node.DOCUMENT_FRAGMENT_NODE = DOCUMENT_FRAGMENT_NODE; core.Node.NOTATION_NODE = NOTATION_NODE; core.Node.prototype = { - _attributes: null, - _childNodes: null, - _childNodesList: null, - _childrenList: null, - _version: 0, - _nodeValue: null, - _parentNode: null, - _ownerDocument: null, - _attributes: null, - _nodeName: null, - _readonly: false, - style: null, ELEMENT_NODE : ELEMENT_NODE, ATTRIBUTE_NODE : ATTRIBUTE_NODE, TEXT_NODE : TEXT_NODE, @@ -354,6 +344,14 @@ core.Node.prototype = { NOTATION_NODE : NOTATION_NODE, get children() { + if (!this._childrenList) { + var self = this; + this._childrenList = new core.NodeList(this, function() { + return self._childNodes.filter(function(node) { + return node.tagName; + }); + }); + } return this._childrenList; }, get nodeValue() { @@ -396,7 +394,7 @@ core.Node.prototype = { set lastChild() { throw new core.DOMException();}, get childNodes() { - return this._childNodesList; + return this._childNodes; }, set childNodes() { throw new core.DOMException();}, @@ -520,8 +518,8 @@ core.Node.prototype = { this._ownerDocument._version++; } - this._childrenList.update(); - this._childNodesList.update(); + if (this._childrenList) this._childrenList.update(); + //this._childNodesList.update(); }, _attrModified: function(name, value, oldValue) { @@ -1508,7 +1506,7 @@ core.Attr.prototype = { for (var i=0,len=this._childNodes.length;i