Author: felix8a
Date: Mon Jul 13 19:28:54 2009
New Revision: 3568

Modified:
   trunk/src/com/google/caja/plugin/domita.js
   trunk/tests/com/google/caja/plugin/domita_test_untrusted.html

Log:
fix return value of appendChild, etc.
http://codereview.appspot.com/88157

currently, appendChild returns null.
it's supposed to return the appended child.
similarly for insertChild, removeChild, and replaceChild.

[email protected]


Modified: trunk/src/com/google/caja/plugin/domita.js
==============================================================================
--- trunk/src/com/google/caja/plugin/domita.js  (original)
+++ trunk/src/com/google/caja/plugin/domita.js  Mon Jul 13 19:28:54 2009
@@ -1330,16 +1330,23 @@
         throw new Error(NOT_EDITABLE);
       }
       this.node___.appendChild(child.node___);
+      return child;
     };
     TameBackedNode.prototype.insertBefore = function (toInsert, child) {
       cajita.guard(tameNodeTrademark, toInsert);
       if (child === void 0) { child = null; }
-      if (child !== null) { cajita.guard(tameNodeTrademark, child); }
+      if (child !== null) {
+        cajita.guard(tameNodeTrademark, child);
+        if (!child.editable___) {
+          throw new Error(NOT_EDITABLE);
+        }
+      }
       if (!this.childrenEditable___ || !toInsert.editable___) {
         throw new Error(NOT_EDITABLE);
       }
       this.node___.insertBefore(
           toInsert.node___, child !== null ? child.node___ : null);
+      return toInsert;
     };
     TameBackedNode.prototype.removeChild = function (child) {
       cajita.guard(tameNodeTrademark, child);
@@ -1347,14 +1354,17 @@
         throw new Error(NOT_EDITABLE);
       }
       this.node___.removeChild(child.node___);
+      return child;
     };
-    TameBackedNode.prototype.replaceChild = function (child, replacement) {
-      cajita.guard(tameNodeTrademark, child);
-      cajita.guard(tameNodeTrademark, replacement);
-      if (!this.childrenEditable___ || !replacement.editable___) {
+    TameBackedNode.prototype.replaceChild = function (newChild, oldChild) {
+      cajita.guard(tameNodeTrademark, newChild);
+      cajita.guard(tameNodeTrademark, oldChild);
+      if (!this.childrenEditable___ || !newChild.editable___
+          || !oldChild.editable___) {
         throw new Error(NOT_EDITABLE);
       }
-      this.node___.replaceChild(child.node___, replacement.node___);
+      this.node___.replaceChild(newChild.node___, oldChild.node___);
+      return oldChild;
     };
     TameBackedNode.prototype.getFirstChild = function () {
return defaultTameNode(this.node___.firstChild, this.childrenEditable___);
@@ -1563,8 +1573,9 @@
     TamePseudoNode.prototype.appendChild =
     TamePseudoNode.prototype.insertBefore =
     TamePseudoNode.prototype.removeChild =
-    TamePseudoNode.prototype.replaceChild = function (child) {
+    TamePseudoNode.prototype.replaceChild = function () {
       cajita.log("Node not editable; no action performed.");
+      return void 0;
     };
     TamePseudoNode.prototype.getFirstChild = function () {
       var children = this.getChildNodes();

Modified: trunk/tests/com/google/caja/plugin/domita_test_untrusted.html
==============================================================================
--- trunk/tests/com/google/caja/plugin/domita_test_untrusted.html       
(original)
+++ trunk/tests/com/google/caja/plugin/domita_test_untrusted.html Mon Jul 13 19:28:54 2009
@@ -143,6 +143,13 @@
   <span id="indelible">I am indelible</span>
 </p>

+<p class="testcontainer" id="test-remove-child"
+  >hello<span id="test-remove-child-1">world</span></p>
+
+<p class="testcontainer" id="test-replace-child"
+  ><span id="test-replace-child-1">alpha</span
+  >bravo<span id="test-replace-child-2">charlie</span></p>
+
 <p class="testcontainer" id="test-insert-before">zero</p>

 <ul class="testcontainer" id="test-node-lists"
@@ -812,11 +819,13 @@
   // Some nodes that are not under the document element.
   var disconnectedEl = document.createElement('DIV');
   var disconnectedChild = document.createElement('B');
-  disconnectedEl.appendChild(disconnectedChild);
+  var result = disconnectedEl.appendChild(disconnectedChild);
+  assertEquals(result, disconnectedChild);

   var testContainer = document.getElementById('test-contains');
   var testContainerChild = document.createElement('SPAN');
-  testContainer.appendChild(testContainerChild);
+  result = testContainer.appendChild(testContainerChild);
+  assertEquals(result, testContainerChild);
// This test assume that test-forms follows test-contains in the HTML above.
   var follower = document.getElementById('test-forms');

@@ -1165,6 +1174,29 @@
   pass('test-read-only');
 });

+jsunitRegister('testRemoveChild',
+               function testRemoveChild() {
+  var parent = document.getElementById('test-remove-child');
+  var child = document.getElementById('test-remove-child-1');
+  var result = parent.removeChild(child);
+  assertEquals(child, result);
+  assertEquals('hello', canonInnerHtml(parent.innerHTML));
+  pass('test-remove-child');
+});
+
+jsunitRegister('testReplaceChild',
+               function testReplaceChild() {
+  var parent = document.getElementById('test-replace-child');
+  var newChild = document.getElementById('test-replace-child-1');
+  var oldChild = document.getElementById('test-replace-child-2');
+  var result = parent.replaceChild(newChild, oldChild);
+  assertEquals(oldChild, result);
+  assertEquals(
+    'bravo<span id="test-replace-child-1">alpha</span>',
+    canonInnerHtml(parent.innerHTML));
+  pass('test-replace-child');
+});
+
 jsunitRegister('testInsertBefore',
                function testInsertBefore() {
   var el = document.getElementById('test-insert-before');
@@ -1179,7 +1211,8 @@
   var two = document.createTextNode('two');
   var three = document.createTextNode('three');
   var four = document.createTextNode('four');
-  el.insertBefore(three, null);
+  var result = el.insertBefore(three, null);
+  assertEquals(three, result);
   assertChildren('zero', 'three');
   el.insertBefore(one, three);
   assertChildren('zero', 'one', 'three');

Reply via email to