Revision: 5149
Author:   [email protected]
Date:     Fri Nov  9 14:57:44 2012
Log:      Systematize taming/untaming of primitives
http://codereview.appspot.com/6782044

See http://code.google.com/p/google-caja/issues/detail?id=1571

[email protected]

http://code.google.com/p/google-caja/source/detail?r=5149

Added:
 /trunk/tests/com/google/caja/plugin/es53-test-taming-primitives.js
Modified:
 /trunk/src/com/google/caja/plugin/taming-membrane.js
 /trunk/tests/com/google/caja/plugin/GeneralBrowserTest.java
 /trunk/tests/com/google/caja/plugin/test-index.js

=======================================
--- /dev/null
+++ /trunk/tests/com/google/caja/plugin/es53-test-taming-primitives.js Fri Nov 9 14:57:44 2012
@@ -0,0 +1,63 @@
+// Copyright (C) 2010 Google Inc.
+//
+// Licensed under the Apache License, Version 2.0 (the "License");
+// you may not use this file except in compliance with the License.
+// You may obtain a copy of the License at
+//
+//      http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing, software
+// distributed under the License is distributed on an "AS IS" BASIS,
+// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+// See the License for the specific language governing permissions and
+// limitations under the License.
+
+/**
+ * @fileoverview ES53 tests of taming and untaming of language primitives.
+ *
+ * @author [email protected]
+ * @requires caja, jsunitRun, readyToTest
+ */
+
+(function() {
+
+  caja.initialize({
+    cajaServer: '/caja',
+    debug: true,
+    forceES5Mode: inES5Mode
+  });
+
+  // Set up basic stuff
+
+  var div = createDiv();
+  var uriPolicy = {
+    rewrite: function (uri, uriEffect, loaderType, hints) { return uri; }
+  };
+
+  caja.load(div, uriPolicy, function (frame) {
+
+    function assertPrimitives(f) {
+      assertEquals(null, f(null));
+      assertEquals(void 0, f(void 0));
+      assertEquals(37, f(37));
+      assertEquals(true, f(true));
+      assertEquals(false, f(false));
+      assertEquals('abc', f('abc'));
+    }
+
+    registerTest('testPrimitivesTaming',
+                   function testPrimitivesTaming() {
+      assertPrimitives(frame.tame);
+      jsunitPass('testPrimitivesTaming');
+    });
+
+    registerTest('testPrimitivesUntaming',
+                   function testPrimitivesUntaming() {
+      assertPrimitives(frame.untame);
+      jsunitPass('testPrimitivesUntaming');
+    });
+
+    readyToTest();
+    jsunitRun();
+  });
+})();
=======================================
--- /trunk/src/com/google/caja/plugin/taming-membrane.js Tue Oct 23 16:53:57 2012 +++ /trunk/src/com/google/caja/plugin/taming-membrane.js Fri Nov 9 14:57:44 2012
@@ -242,7 +242,10 @@
    * Returns a tame object representing f, or undefined on failure.
    */
   function tame(f) {
-    if (!f) { return f; }
+    if (f !== Object(f)) {
+      // Language primitive
+      return f;
+    }
     var ftype = typeof f;
     if (ftype !== 'function' && ftype !== 'object') {
       // Primitive value; tames to self
@@ -536,7 +539,10 @@
    * Returns a feral object representing t, or undefined on failure.
    */
   function untame(t) {
-    if (!t) { return t; }
+    if (t !== Object(t)) {
+      // language primitive
+      return t;
+    }
     var ttype = typeof t;
     if (ttype !== 'function' && ttype !== 'object') {
       // Primitive value; untames to self
=======================================
--- /trunk/tests/com/google/caja/plugin/GeneralBrowserTest.java Thu Nov 1 12:06:52 2012 +++ /trunk/tests/com/google/caja/plugin/GeneralBrowserTest.java Fri Nov 9 14:57:44 2012
@@ -183,6 +183,10 @@
   public final void testClientUriRewriting() throws Exception {
     runTestDriver("es53-test-client-uri-rewriting.js");
   }
+
+  public final void testTamingPrimitives() throws Exception {
+    runTestDriver("es53-test-taming-primitives.js", false);
+  }

   public final void testTamingTamed() throws Exception {
     runTestDriver("es53-test-taming-tamed.js");
=======================================
--- /trunk/tests/com/google/caja/plugin/test-index.js Tue Nov 6 16:59:57 2012 +++ /trunk/tests/com/google/caja/plugin/test-index.js Fri Nov 9 14:57:44 2012
@@ -48,6 +48,7 @@
     ['es53-test-relative-urls.js'],
     ['es53-test-taming-errors.js'],
     ['es53-test-taming-inout.js'],
+    ['es53-test-taming-primitives.js'],
     ['es53-test-taming-tamed.js'],
     ['es53-test-taming-untamed.js'],
     ['es53-test-unicode.js']

Reply via email to