Comparing version 0.3.4 to 0.4.0
@@ -159,5 +159,5 @@ //List from node-htmlparser | ||
noformat ? "" : "\n"); | ||
if (dom.toArray) { | ||
if (dom._toArray) { | ||
// node list | ||
dom = dom.toArray(); | ||
dom = dom._toArray(); | ||
} | ||
@@ -164,0 +164,0 @@ if (typeof dom.length !== 'undefined') { |
@@ -43,3 +43,3 @@ /* | ||
function visit(parent, result) { | ||
return parent.childNodes.reduce(reducer, result); | ||
return Array.prototype.reduce.call(parent.childNodes, reducer, result); | ||
} | ||
@@ -170,52 +170,72 @@ | ||
var NodeArray = function() {}; | ||
NodeArray.prototype = new Array(); | ||
NodeArray.prototype.item = function(i) { | ||
return this[i]; | ||
}; | ||
core.NodeList = function NodeList(element, query) { | ||
if (!query) { | ||
// Non-live NodeList | ||
var list = new NodeArray(); | ||
if (Array.isArray(element)) { | ||
Array.prototype.push.apply(list, element); | ||
Array.prototype.push.apply(this, element); | ||
} | ||
Object.defineProperties(this, { | ||
_length: {value: element ? element.length : 0, writable:true} | ||
}); | ||
} else { | ||
Object.defineProperties(this, { | ||
_element: {value: element}, | ||
_query: {value: query}, | ||
_snapshot: {writable: true}, | ||
_length: {value: 0, writable: true}, | ||
_version: {value: -1, writable: true} | ||
}); | ||
this._update(); | ||
} | ||
}; | ||
return list; | ||
function lengthFromProperties(arrayLike) { | ||
var max = -1; | ||
for (var i in arrayLike) { | ||
var asNumber = +i; | ||
if (!isNaN(asNumber) && asNumber > max) { | ||
max = asNumber; | ||
} | ||
} | ||
Object.defineProperties(this, { | ||
_element: {value: element}, | ||
_query: {value: query}, | ||
_snapshot: {writable: true}, | ||
_length: {value: 0, writable: true}, | ||
_version: {value: -1, writable: true} | ||
}); | ||
this.update(); | ||
}; | ||
return max + 1; | ||
} | ||
core.NodeList.prototype = { | ||
update: function() { | ||
_update: function() { | ||
var i; | ||
if (this._element && this._version < this._element._version) { | ||
for (i = 0; i < this._length; i++) { | ||
this[i] = undefined; | ||
if (!this._element) { | ||
this._length = lengthFromProperties(this); | ||
} else { | ||
if (this._version < this._element._version) { | ||
var nodes = this._snapshot = this._query(); | ||
this._resetTo(nodes); | ||
this._version = this._element._version; | ||
} | ||
var nodes = this._snapshot = this._query(); | ||
this._length = nodes.length; | ||
for (i = 0; i < nodes.length; i++) { | ||
this[i] = nodes[i]; | ||
} | ||
this._version = this._element._version; | ||
} | ||
return this._snapshot; | ||
}, | ||
toArray: function() { | ||
return this.update() || []; | ||
_resetTo: function(array) { | ||
var startingLength = lengthFromProperties(this); | ||
for (var i = 0; i < startingLength; ++i) { | ||
delete this[i]; | ||
} | ||
for (var j = 0; j < array.length; ++j) { | ||
this[j] = array[j]; | ||
} | ||
this._length = array.length; | ||
}, | ||
_toArray: function() { | ||
if (this._element) { | ||
this._update(); | ||
return this._snapshot; | ||
} | ||
return Array.prototype.slice.call(this); | ||
}, | ||
get length() { | ||
this.update(); | ||
this._update(); | ||
return this._length || 0; | ||
}, | ||
item: function(index) { | ||
this.update(); | ||
this._update(); | ||
return this[index] || null; | ||
@@ -225,15 +245,9 @@ }, | ||
return '[ jsdom NodeList ]: contains ' + this.length + ' items'; | ||
}, | ||
indexOf: function(node) { | ||
var len = this.update().length; | ||
for (var i = 0; i < len; i++) { | ||
if (this[i] == node) { | ||
return i; | ||
} | ||
} | ||
return -1; // not found | ||
} | ||
}; | ||
Object.defineProperty(core.NodeList.prototype, 'constructor', { | ||
value: core.NodeList, | ||
writable: true, | ||
configurable: true | ||
}); | ||
@@ -390,3 +404,3 @@ core.DOMImplementation = function DOMImplementation(document, /* Object */ features) { | ||
this._childrenList = new core.NodeList(this, function() { | ||
return self._childNodes.filter(function(node) { | ||
return Array.prototype.filter.call(self._childNodes, function(node) { | ||
return node.tagName; | ||
@@ -544,3 +558,4 @@ }); | ||
this._childNodes.splice(refChildIndex, 0, newChild); | ||
Array.prototype.splice.call(this._childNodes, refChildIndex, 0, newChild); | ||
newChild._parentNode = this; | ||
@@ -563,4 +578,5 @@ if (this._attached && newChild._attach) { | ||
if (this._childrenList) this._childrenList.update(); | ||
//this._childNodesList.update(); | ||
if (this._childrenList) { | ||
this._childrenList._update(); | ||
} | ||
}, | ||
@@ -618,3 +634,3 @@ | ||
this._childNodes.splice(oldChildIndex, 1); | ||
Array.prototype.splice.call(this._childNodes, oldChildIndex, 1); | ||
oldChild._parentNode = null; | ||
@@ -1015,3 +1031,3 @@ this._modified(); | ||
} | ||
return ""; | ||
return null; | ||
}, | ||
@@ -1548,3 +1564,3 @@ | ||
if (child.nodeType === ENTITY_REFERENCE_NODE) { | ||
val += child.childNodes.reduce(function(prev, c) { | ||
val += Array.prototype.reduce.call(child.childNodes, function(prev, c) { | ||
return prev += (c.nodeValue || c); | ||
@@ -1564,4 +1580,3 @@ }, ''); | ||
this._childNodes.length = 0; | ||
this._childNodes.push(this._ownerDocument.createTextNode(value)); | ||
this._childNodes._resetTo([this._ownerDocument.createTextNode(value)]); | ||
this._modified(); | ||
@@ -1568,0 +1583,0 @@ this._specified = true; |
@@ -218,4 +218,4 @@ var core = require("./core").dom.level2.core, | ||
core.HTMLCollection.prototype = { | ||
namedItem : function(name) { | ||
var results = this.toArray(), | ||
namedItem: function(name) { | ||
var results = this._toArray(), | ||
l = results.length, | ||
@@ -239,2 +239,8 @@ node, | ||
}; | ||
Object.defineProperty(core.HTMLCollection.prototype, 'constructor', { | ||
value: core.NodeList, | ||
writable: true, | ||
configurable: true | ||
}); | ||
core.HTMLCollection.prototype.__proto__ = core.NodeList.prototype; | ||
@@ -384,3 +390,3 @@ | ||
open : function() { | ||
this._childNodes = []; | ||
this._childNodes = new core.NodeList(); | ||
this._documentElement = null; | ||
@@ -545,3 +551,3 @@ this._modified(); | ||
reset: function() { | ||
this.elements.toArray().forEach(function(el) { | ||
this.elements._toArray().forEach(function(el) { | ||
el.value = el.defaultValue; | ||
@@ -694,3 +700,3 @@ }); | ||
get selectedIndex() { | ||
return this.options.toArray().reduceRight(function(prev, option, i) { | ||
return this.options._toArray().reduceRight(function(prev, option, i) { | ||
return option.selected ? i : prev; | ||
@@ -701,3 +707,3 @@ }, -1); | ||
set selectedIndex(index) { | ||
this.options.toArray().forEach(function(option, i) { | ||
this.options._toArray().forEach(function(option, i) { | ||
option.selected = i === index; | ||
@@ -720,3 +726,3 @@ }); | ||
var self = this; | ||
this.options.toArray().forEach(function(option) { | ||
this.options._toArray().forEach(function(option) { | ||
if (option.value === val) { | ||
@@ -752,3 +758,3 @@ option.selected = true; | ||
remove: function(index) { | ||
var opts = this.options.toArray(); | ||
var opts = this.options._toArray(); | ||
if (index >= 0 && index < opts.length) { | ||
@@ -814,3 +820,3 @@ var el = opts[index]; | ||
get index() { | ||
return closest(this, 'SELECT').options.toArray().indexOf(this); | ||
return closest(this, 'SELECT').options._toArray().indexOf(this); | ||
}, | ||
@@ -1388,3 +1394,3 @@ get selected() { | ||
this._rows = new core.HTMLCollection(this._ownerDocument, function() { | ||
var sections = [table.tHead].concat(table.tBodies.toArray(), table.tFoot).filter(function(s) { return !!s }); | ||
var sections = [table.tHead].concat(table.tBodies._toArray(), table.tFoot).filter(function(s) { return !!s }); | ||
@@ -1398,3 +1404,3 @@ if (sections.length === 0) { | ||
return sections.reduce(function(prev, s) { | ||
return prev.concat(s.rows.toArray()); | ||
return prev.concat(s.rows._toArray()); | ||
}, []); | ||
@@ -1459,3 +1465,3 @@ | ||
} | ||
var rows = this.rows.toArray(); | ||
var rows = this.rows._toArray(); | ||
if (index < -1 || index > rows.length) { | ||
@@ -1478,3 +1484,3 @@ throw new core.DOMException(core.INDEX_SIZE_ERR); | ||
deleteRow: function(index) { | ||
var rows = this.rows.toArray(), l = rows.length; | ||
var rows = this.rows._toArray(), l = rows.length; | ||
if (index === -1) { | ||
@@ -1533,3 +1539,3 @@ index = l-1; | ||
var tr = this._ownerDocument.createElement('TR'); | ||
var rows = this.rows.toArray(); | ||
var rows = this.rows._toArray(); | ||
if (index < -1 || index > rows.length) { | ||
@@ -1548,3 +1554,3 @@ throw new core.DOMException(core.INDEX_SIZE_ERR); | ||
deleteRow: function(index) { | ||
var rows = this.rows.toArray(); | ||
var rows = this.rows._toArray(); | ||
if (index === -1) { | ||
@@ -1582,11 +1588,11 @@ index = rows.length-1; | ||
get rowIndex() { | ||
return closest(this, 'TABLE').rows.toArray().indexOf(this); | ||
return closest(this, 'TABLE').rows._toArray().indexOf(this); | ||
}, | ||
get sectionRowIndex() { | ||
return this._parentNode.rows.toArray().indexOf(this); | ||
return this._parentNode.rows._toArray().indexOf(this); | ||
}, | ||
insertCell: function(index) { | ||
var td = this._ownerDocument.createElement('TD'); | ||
var cells = this.cells.toArray(); | ||
var cells = this.cells._toArray(); | ||
if (index < -1 || index > cells.length) { | ||
@@ -1605,3 +1611,3 @@ throw new core.DOMException(core.INDEX_SIZE_ERR); | ||
deleteCell: function(index) { | ||
var cells = this.cells.toArray(); | ||
var cells = this.cells._toArray(); | ||
if (index === -1) { | ||
@@ -1659,3 +1665,3 @@ index = cells.length-1; | ||
get cellIndex() { | ||
return closest(this, 'TR').cells.toArray().indexOf(this); | ||
return closest(this, 'TR').cells._toArray().indexOf(this); | ||
} | ||
@@ -1662,0 +1668,0 @@ }, |
@@ -106,6 +106,6 @@ var core = require("../level2/core").dom.level2.core, | ||
// Text nodes for attributes does not have a _parentNode. So we need to find them as attribute child. | ||
if( this.nodeType === this.ATTRIBUTE_NODE && this._childNodes && this._childNodes.indexOf(otherNode) !== -1) | ||
if( this.nodeType === this.ATTRIBUTE_NODE && this._childNodes && this._childNodes._toArray().indexOf(otherNode) !== -1) | ||
return DOCUMENT_POSITION_FOLLOWING + DOCUMENT_POSITION_CONTAINED_BY | ||
if( otherNode.nodeType === this.ATTRIBUTE_NODE && otherNode._childNodes && otherNode._childNodes.indexOf(this) !== -1) | ||
if( otherNode.nodeType === this.ATTRIBUTE_NODE && otherNode._childNodes && otherNode._childNodes._toArray().indexOf(this) !== -1) | ||
return DOCUMENT_POSITION_PRECEDING + DOCUMENT_POSITION_CONTAINS | ||
@@ -128,4 +128,4 @@ | ||
var smallest_common_ancestor = parents[ location_index ] | ||
var this_index = smallest_common_ancestor._childNodes.indexOf( parents[location_index - 1] ) | ||
var other_index = smallest_common_ancestor._childNodes.indexOf( previous ) | ||
var this_index = smallest_common_ancestor._childNodes._toArray().indexOf( parents[location_index - 1] ) | ||
var other_index = smallest_common_ancestor._childNodes._toArray().indexOf( previous ) | ||
if( this_index > other_index ) { | ||
@@ -132,0 +132,0 @@ return DOCUMENT_POSITION_PRECEDING |
{ | ||
"name": "jsdom", | ||
"version": "0.3.4", | ||
"version": "0.4.0", | ||
"description": "A JavaScript implementation of the W3C DOM", | ||
@@ -5,0 +5,0 @@ "keywords": [ |
@@ -279,3 +279,3 @@ # jsdom | ||
level2/html 697/697 100% | ||
level2/style 10/10 100% | ||
level2/style 11/11 100% | ||
level2/extra 4/4 100% | ||
@@ -287,3 +287,3 @@ level2/events 24/24 100% | ||
window/frame 14/14 100% | ||
sizzle/index 9/14 64% | ||
sizzle/index 10/14 71% | ||
jsdom/index 86/86 100% | ||
@@ -294,3 +294,3 @@ jsonp/jsonp 1/1 100% | ||
------------------------------------------------------ | ||
TOTALS: 5/2564 failed; 99% success | ||
TOTALS: 4/2565 failed; 99% success | ||
``` | ||
@@ -297,0 +297,0 @@ |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
344065
6
25