Fixes an issue in StyleInjector where it could try to inject more than 30 style sheets in IE if a style sheet already existed.  We now look at the total number of style sheets instead of just the number that were injected by StyleInjector.

Patch by: mathewar
Review by: jlabanca



git-svn-id: https://google-web-toolkit.googlecode.com/svn/trunk@6094 8db76d5a-ed1c-0410-87a9-c151d255dfc7
diff --git a/user/src/com/google/gwt/dom/client/StyleInjector.java b/user/src/com/google/gwt/dom/client/StyleInjector.java
index d566d65..1ec5e7c 100644
--- a/user/src/com/google/gwt/dom/client/StyleInjector.java
+++ b/user/src/com/google/gwt/dom/client/StyleInjector.java
@@ -95,7 +95,7 @@
     @Override
     public StyleElement injectStyleSheet(String contents) {
       int idx = STYLE_ELEMENTS.length();
-      if (idx < MAX_STYLE_SHEETS) {
+      if (getDocumentStyleCount() < MAX_STYLE_SHEETS) {
         // Just create a new style element and add it to the list
         StyleElement style = createElement();
         setContents(style, contents);
@@ -115,6 +115,14 @@
             shortestIdx = i;
           }
         }
+
+        /**
+         * This assertion can fail if the max number of style elements exist
+         * before this module can inject any style elements, so STYLE_ELEMENTS
+         * will be empty. However, the fix would degrade performance for the
+         * general case.
+         * TODO(jlabanca): Can we handle this scenario efficiently?
+         */
         assert shortestIdx != -1;
 
         StyleElement style = STYLE_ELEMENTS.get(shortestIdx);
@@ -165,6 +173,10 @@
     private native StyleElement createElement() /*-{
       return $doc.createStyleSheet();
     }-*/;
+
+    private native int getDocumentStyleCount() /*-{
+      return $doc.styleSheets.length;
+    }-*/;
   }
 
   /**