Revision: 4164
Author: [email protected]
Date: Fri Jul  2 13:11:27 2010
Log: HostTools: Add support for specifying the base URL.
http://codereview.appspot.com/1686050

Add hostTools.setBaseURL method so that the base URL for module loading may be set to something other than this document's URL (in particular, the URL of a parent frame).

[email protected]

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

Modified:
 /trunk/src/com/google/caja/plugin/host-tools.js
 /trunk/tests/com/google/caja/plugin/host-tools-test.js

=======================================
--- /trunk/src/com/google/caja/plugin/host-tools.js     Fri Jun 25 11:21:10 2010
+++ /trunk/src/com/google/caja/plugin/host-tools.js     Fri Jul  2 13:11:27 2010
@@ -35,6 +35,9 @@

     // user-modifiable state
     var cajolerFinder = hasModuleLoader ? defaultCajolerFinder : null;
+    var baseURL = document.location.toString();
+ // TODO(kpreid): the above probably does the wrong thing in the case where
+    // the document has a <base>; fix.
     var load;

     // internal functions
@@ -44,7 +47,7 @@
       } else {
         // TODO(kpreid): allow subbing module id resolver
         // TODO(kpreid): Using XHR loader didn't work; why?
-        load = scriptModuleLoadMaker(document.location.toString(),
+        load = scriptModuleLoadMaker(baseURL,
                                      defaultModuleIdResolver,
                                      cajolerFinder);
       }
@@ -57,6 +60,11 @@
       cajolerFinder = new CajolingServiceFinder(url);
       updateLoad();
     }
+
+    function setBaseURL(url) {
+      baseURL = url;
+      updateLoad();
+    }

     function Sandbox() {
       // user-modifiable state
@@ -70,7 +78,7 @@
               "cannot dynamically load modules.");
         }

-        // TODO: review error handling
+ // TODO: review error handling -- are errors propagated and descriptive?
         return Q.when(load.async(mid), function (moduleFunc) {
           //console.log("got load Q callback");
           return moduleFunc(imports);
@@ -112,6 +120,7 @@
     }

     return cajita.freeze({
+      setBaseURL: setBaseURL,
       setCajolerService: setCajolerService,
       Sandbox: Sandbox,
     });
=======================================
--- /trunk/tests/com/google/caja/plugin/host-tools-test.js Thu Jul 1 18:19:14 2010 +++ /trunk/tests/com/google/caja/plugin/host-tools-test.js Fri Jul 2 13:11:27 2010
@@ -106,6 +106,23 @@
   }));
 });

+jsunitRegister("testSetBaseURL",
+               function testSetBaseURL() {
+  tools.setBaseURL(URI.resolve(URI.parse(document.location.toString()),
+                               URI.parse("../"))
+                      .toString());
+
+  var s = new tools.Sandbox();
+  s.imports.x = 1;
+  Q.when(s.run("c.js"), jsunitCallback(function (moduleResult) {
+    // We actually only care that the module ran.
+    assertIdentical("module result", 2, moduleResult);
+    jsunit.pass();
+  }), jsunitCallback(function (reason) {
+    throw reason;
+  }));
+});
+
 jsunitRegister("testRunGadget",
                function testRunGadget() {

Reply via email to