Browse files

Dealt with a bug where code would crash when a document fragment with…

… more than two elements was appended to a document
  • Loading branch information...
1 parent 9f9e2d2 commit 65be3bd87bea96d92115b1a034f2797c73b87e91 Jos Shepherd committed Jun 15, 2010
Showing with 29 additions and 7 deletions.
  1. +12 −5 lib/level1/core.js
  2. +17 −2 test/level1/core.js
View
17 lib/level1/core.js
@@ -449,22 +449,29 @@ core.Node.prototype = {
newChild.ownerDocument !== this.ownerDocument) {
throw new core.DOMException(WRONG_DOCUMENT_ERR);
}
-
+
try {
this.removeChild(newChild);
} catch (e) { /* do nothing */ }
// fragments are merged into the element
if (newChild.nodeType === this.DOCUMENT_FRAGMENT_NODE) {
+
+ var transfer = [];
var length = newChild.children.length;
- var i = 0;
+ var i = length-1;
var tmpNode;
var child;
- for (; i<length; i++)
+ // remove backwards so that item indexes stay stable while nodes are removed
+ for (; i> -1; i--)
{
- tmpNode = newChild.removeChild(newChild.children.item(i));
- this.appendChild(tmpNode);
+ transfer.unshift(newChild.removeChild(newChild.children.item(i)));
}
+ for (i=0; i<transfer.length; i++)
+ {
+ this.appendChild(transfer[i]);
+ }
+
} else {
if (newChild && newChild.parentNode) {
newChild.parentNode.removeChild(newChild);
View
19 test/level1/core.js
@@ -6609,7 +6609,9 @@ exports.tests = {
docFragment = doc.createDocumentFragment();
newOne = doc.createElement("html");
newOne.setAttribute("lang","EN");
+
appendedChild = docFragment.appendChild(newOne);
+
domesticNode = docFragment.firstChild;
attributes = domesticNode.attributes;
@@ -12547,6 +12549,7 @@ exports.tests = {
appendedChild = newdocFragment.appendChild(newChild1);
appendedChild = newdocFragment.appendChild(newChild2);
appendedChild = employeeNode.appendChild(newdocFragment);
+
for(var indexN100A2 = 0;indexN100A2 < childList.length; indexN100A2++) {
child = childList.item(indexN100A2);
nodeType = child.nodeType;
@@ -17845,11 +17848,12 @@ exports.tests = {
append it to the second employee. After the
"appendChild(newChild)" method is invoked retrieve the
new nodes at the end of the list, they should be the
- two Element nodes from the DocumentFragment.
+ three (Jos: was two) Element nodes from the DocumentFragment.
* @author NIST
* @author Mary Brady
* @see http://www.w3.org/TR/1998/REC-DOM-Level-1-19981001/level-one-core#ID-184E7107
+ * MODIFIED BY Jos Shepherd - added a third new child
*/
nodeappendchilddocfragment : function () {
var success;
@@ -17861,6 +17865,8 @@ exports.tests = {
var newdocFragment;
var newChild1;
var newChild2;
+ var newChild3;
+
var child;
var childName;
var result = new Array();
@@ -17876,6 +17882,7 @@ exports.tests = {
expected[5] = "address";
expected[6] = "newChild1";
expected[7] = "newChild2";
+ expected[8] = "newChild3";
var docRef = null;
@@ -17890,9 +17897,16 @@ exports.tests = {
newdocFragment = doc.createDocumentFragment();
newChild1 = doc.createElement("newChild1");
newChild2 = doc.createElement("newChild2");
+ newChild3 = doc.createElement("newChild3");
+
appendedChild = newdocFragment.appendChild(newChild1);
appendedChild = newdocFragment.appendChild(newChild2);
+ appendedChild = newdocFragment.appendChild(newChild3);
+
+ require('sys').debug('appends');
appendedChild = employeeNode.appendChild(newdocFragment);
+ require('sys').debug('appende');
+
for(var indexN1009F = 0;indexN1009F < childList.length; indexN1009F++) {
child = childList.item(indexN1009F);
nodeType = child.nodeType;
@@ -24340,4 +24354,5 @@ exports.tests = {
assertEquals("textNodeValue","Roger\n Jones",value);
- }};
+ }
+ };

0 comments on commit 65be3bd

Please sign in to comment.