- Fixes a bug that was keeping IE from respecting rebasing the application via
the <base> tag.
- Makes the module's entry point fire as soon as the DOM is fully loaded, but
before window.onload() fires (so that images and iframes don't keep the app
from starting).

Patch by: jgw
Review by: scottb
Issue: 1141


git-svn-id: https://google-web-toolkit.googlecode.com/svn/trunk@1205 8db76d5a-ed1c-0410-87a9-c151d255dfc7
diff --git a/dev/core/src/com/google/gwt/dev/util/SelectionScriptTemplate-xs.js b/dev/core/src/com/google/gwt/dev/util/SelectionScriptTemplate-xs.js
index 35d8eea..9219e2d 100644
--- a/dev/core/src/com/google/gwt/dev/util/SelectionScriptTemplate-xs.js
+++ b/dev/core/src/com/google/gwt/dev/util/SelectionScriptTemplate-xs.js
@@ -56,16 +56,27 @@
 
   // --------------- WINDOW ONLOAD HOOK ---------------
 
-  var oldOnLoad = $wnd.onload;
-  $wnd.onload = function(evt) {
-    if (oldOnLoad) {
-      $wnd.onload = oldOnLoad;
-      $wnd.onload(evt);
+  var onBodyDone = function() {
+    if (!bodyDone) {
+      bodyDone = true;
+      maybeStartModule();
     }
-    bodyDone = true;
-    maybeStartModule();
   };
 
+  // For everyone that supports DOMContentLoaded.
+  if ($doc.addEventListener) {
+    $doc.addEventListener("DOMContentLoaded", onBodyDone, false);
+  }
+
+  // Fallback. If onBodyDone() gets fired twice, it's not a big deal.
+  var onBodyDoneTimerId = setInterval(function() {
+    if (/interactive|loaded|complete/.test($doc.readyState)) {
+      clearInterval(onBodyDoneTimerId);
+      onBodyDone();
+      onBodyDone = null;
+    }
+  }, 50);
+
   // --------------- INTERNAL FUNCTIONS ---------------
 
   function isHostedMode() {
@@ -112,11 +123,18 @@
       // Compute our base url
       base = getDirectoryOfFile(thisScript.src);
     }
-    
+
     // Make the base URL absolute
     if (base == '') {
-      // Trivial case; the base must be the same as the document location
-      base = getDirectoryOfFile($doc.location.href);
+      // If there's a base tag, use it.
+      var baseElements = $doc.getElementsByTagName('base');
+      if (baseElements.length > 0) {
+        // It's always the last parsed base tag that will apply to this script.
+        base = baseElements[baseElements.length - 1].href;
+      } else {
+        // No base tag; the base must be the same as the document location.
+        base = getDirectoryOfFile($doc.location.href);
+      }
     } else if ((base.match(/^\w+:\/\//))) {
       // If the URL is obviously absolute, do nothing.
     } else {
diff --git a/dev/core/src/com/google/gwt/dev/util/SelectionScriptTemplate.js b/dev/core/src/com/google/gwt/dev/util/SelectionScriptTemplate.js
index 77ceab2..b809e6a 100644
--- a/dev/core/src/com/google/gwt/dev/util/SelectionScriptTemplate.js
+++ b/dev/core/src/com/google/gwt/dev/util/SelectionScriptTemplate.js
@@ -56,16 +56,27 @@
 
   // --------------- WINDOW ONLOAD HOOK ---------------
 
-  var oldOnLoad = $wnd.onload;
-  $wnd.onload = function(evt) {
-    if (oldOnLoad) {
-      $wnd.onload = oldOnLoad;
-      $wnd.onload(evt);
+  var onBodyDone = function() {
+    if (!bodyDone) {
+      bodyDone = true;
+      maybeStartModule();
     }
-    bodyDone = true;
-    maybeStartModule();
   };
 
+  // For everyone that supports DOMContentLoaded.
+  if ($doc.addEventListener) {
+    $doc.addEventListener("DOMContentLoaded", onBodyDone, false);
+  }
+
+  // Fallback. If onBodyDone() gets fired twice, it's not a big deal.
+  var onBodyDoneTimerId = setInterval(function() {
+    if (/interactive|loaded|complete/.test($doc.readyState)) {
+      clearInterval(onBodyDoneTimerId);
+      onBodyDone();
+      onBodyDone = null;
+    }
+  }, 50);
+
   // --------------- INTERNAL FUNCTIONS ---------------
 
   function isHostedMode() {
@@ -129,11 +140,18 @@
       // Compute our base url
       base = getDirectoryOfFile(thisScript.src);
     }
-    
+
     // Make the base URL absolute
     if (base == '') {
-      // Trivial case; the base must be the same as the document location
-      base = getDirectoryOfFile($doc.location.href);
+      // If there's a base tag, use it.
+      var baseElements = $doc.getElementsByTagName('base');
+      if (baseElements.length > 0) {
+        // It's always the last parsed base tag that will apply to this script.
+        base = baseElements[baseElements.length - 1].href;
+      } else {
+        // No base tag; the base must be the same as the document location.
+        base = getDirectoryOfFile($doc.location.href);
+      }
     } else if ((base.match(/^\w+:\/\//))) {
       // If the URL is obviously absolute, do nothing.
     } else {