Maintenance on selection scripts to propagate changes from the main IFrame selection script.

Review by: bobv (desk)


git-svn-id: https://google-web-toolkit.googlecode.com/svn/releases/1.6@4400 8db76d5a-ed1c-0410-87a9-c151d255dfc7
diff --git a/dev/core/src/com/google/gwt/core/linker/SingleScriptLinker.java b/dev/core/src/com/google/gwt/core/linker/SingleScriptLinker.java
index 50aa425..c4b3186 100644
--- a/dev/core/src/com/google/gwt/core/linker/SingleScriptLinker.java
+++ b/dev/core/src/com/google/gwt/core/linker/SingleScriptLinker.java
@@ -26,7 +26,6 @@
 import com.google.gwt.core.ext.linker.impl.SelectionScriptLinker;
 import com.google.gwt.dev.About;
 import com.google.gwt.dev.util.DefaultTextOutput;
-import com.google.gwt.dev.util.Util;
 
 import java.util.Set;
 
@@ -58,16 +57,13 @@
 
     DefaultTextOutput out = new DefaultTextOutput(true);
 
-    // Emit the selection script in a function closure.
-    out.print("(function () {");
-    out.newlineOpt();
+    // Emit the selection script.
     String bootstrap = generateSelectionScript(logger, context, artifacts);
     bootstrap = context.optimizeJavaScript(logger, bootstrap);
     out.print(bootstrap);
-    out.print("})();");
     out.newlineOpt();
 
-    // Emit the module's JS in another closure
+    // Emit the module's JS a closure.
     out.print("(function () {");
     out.newlineOpt();
     out.print("var $gwt_version = \"" + About.GWT_VERSION_NUM + "\";");
@@ -94,22 +90,15 @@
 
     out.print(result.getJavaScript());
 
-    // Add a callback to the selection script
+    // Generate the call to tell the bootstrap code that we're ready to go.
     out.newlineOpt();
-    out.print("if (" + context.getModuleFunctionName() + ") {");
-    out.newlineOpt();
-    out.print("  var __gwt_initHandlers = " + context.getModuleFunctionName()
-        + ".__gwt_initHandlers;");
-    out.print("  " + context.getModuleFunctionName()
-        + ".onScriptLoad(gwtOnLoad);");
-    out.newlineOpt();
-    out.print("}");
+    out.print("if (" + context.getModuleFunctionName() + ") "
+        + context.getModuleFunctionName() + ".onScriptLoad();");
     out.newlineOpt();
     out.print("})();");
     out.newlineOpt();
 
-    byte[] selectionScriptBytes = Util.getBytes(out.toString());
-    return emitBytes(logger, selectionScriptBytes, context.getModuleName()
+    return emitString(logger, out.toString(), context.getModuleName()
         + ".nocache.js");
   }
 
diff --git a/dev/core/src/com/google/gwt/core/linker/SingleScriptTemplate.js b/dev/core/src/com/google/gwt/core/linker/SingleScriptTemplate.js
index d7c6c3d..3668e91 100644
--- a/dev/core/src/com/google/gwt/core/linker/SingleScriptTemplate.js
+++ b/dev/core/src/com/google/gwt/core/linker/SingleScriptTemplate.js
@@ -1,5 +1,5 @@
 /*
- * Copyright 2007 Google Inc.
+ * Copyright 2008 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
@@ -14,7 +14,7 @@
  * the License.
  */
 
-window.__MODULE_FUNC__ = function() {
+function __MODULE_FUNC__() {
   // ---------------- INTERNAL GLOBALS ----------------
   
   // Cache symbols locally for good obfuscation
@@ -77,17 +77,23 @@
   }
 
   // Determine our own script's URL via magic :)
+  // This function produces one side-effect, it sets base to the module's
+  // base url.
   //
   function computeScriptBase() {
-    // see if gwt.js left a marker for us
-    var thisScript, markerScript;
+    var thisScript
+    ,markerId = "__gwt_marker___MODULE_NAME__"
+    ,markerScript;
 
-    // try writing a marker
-    $doc.write('<script id="__gwt_marker___MODULE_NAME__"></script>');
-    markerScript = $doc.getElementById("__gwt_marker___MODULE_NAME__");
-    if (markerScript) {
-      // this script should be the previous element
-      thisScript = markerScript.previousSibling;
+    $doc.write('<script id="' + markerId + '"></script>');
+    markerScript = $doc.getElementById(markerId);
+
+    // Our script element is assumed to be the closest previous script element
+    // to the marker, so start at the marker and walk backwards until we find
+    // a script.
+    thisScript = markerScript && markerScript.previousSibling;
+    while (thisScript && thisScript.tagName != 'SCRIPT') {
+      thisScript = thisScript.previousSibling;
     }
 
     // Gets the part of a url up to and including the 'path' portion.
@@ -217,6 +223,12 @@
 
   // --------------- STRAIGHT-LINE CODE ---------------
 
+  if (isHostedMode()) {
+    alert("Single-script hosted mode not yet implemented. See issue " +
+      "http://code.google.com/p/google-web-toolkit/issues/detail?id=2079");
+    return;
+  }
+
   // do it early for compile/browse rebasing
   computeScriptBase();
   processMetas();
@@ -243,7 +255,9 @@
 
   // For everyone that supports DOMContentLoaded.
   if ($doc.addEventListener) {
-    $doc.addEventListener("DOMContentLoaded", onBodyDone, false);
+    $doc.addEventListener("DOMContentLoaded", function() {
+      onBodyDone();
+    }, false);
   }
 
   // Fallback. If onBodyDone() gets fired twice, it's not a big deal.
diff --git a/dev/core/src/com/google/gwt/core/linker/XSLinker.java b/dev/core/src/com/google/gwt/core/linker/XSLinker.java
index ed544c2..b53fa8c 100644
--- a/dev/core/src/com/google/gwt/core/linker/XSLinker.java
+++ b/dev/core/src/com/google/gwt/core/linker/XSLinker.java
@@ -79,14 +79,8 @@
 
     // Generate the call to tell the bootstrap code that we're ready to go.
     out.newlineOpt();
-    out.print("if (" + context.getModuleFunctionName() + ") {");
-    out.newlineOpt();
-    out.print("  var __gwt_initHandlers = " + context.getModuleFunctionName()
-        + ".__gwt_initHandlers;");
-    out.print("  " + context.getModuleFunctionName()
-        + ".onScriptLoad(gwtOnLoad);");
-    out.newlineOpt();
-    out.print("}");
+    out.print("if (" + context.getModuleFunctionName() + ") "
+        + context.getModuleFunctionName() + ".onScriptLoad();");
     out.newlineOpt();
     out.print("})();");
     out.newlineOpt();