]> source.dussan.org Git - gwtquery.git/commitdiff
first attemp to save the number of permutations. Optimized Sizzle selector for IE
authorManolo Carrasco <manolo@apache.org>
Mon, 25 Oct 2010 07:57:57 +0000 (07:57 +0000)
committerManolo Carrasco <manolo@apache.org>
Mon, 25 Oct 2010 07:57:57 +0000 (07:57 +0000)
15 files changed:
devtest/src/main/java/com/google/gwt/query/client/DevTestRunner.java
gwtquery-core/src/main/java/com/google/gwt/query/Query.gwt.xml
gwtquery-core/src/main/java/com/google/gwt/query/client/Function.java
gwtquery-core/src/main/java/com/google/gwt/query/client/GQUtils.java
gwtquery-core/src/main/java/com/google/gwt/query/client/GQuery.java
gwtquery-core/src/main/java/com/google/gwt/query/client/SelectorEngine.java
gwtquery-core/src/main/java/com/google/gwt/query/client/css/CSS.java
gwtquery-core/src/main/java/com/google/gwt/query/client/impl/HasSelector.java [new file with mode: 0644]
gwtquery-core/src/main/java/com/google/gwt/query/client/impl/SelectorEngineImpl.java
gwtquery-core/src/main/java/com/google/gwt/query/client/impl/SelectorEngineNative.java
gwtquery-core/src/main/java/com/google/gwt/query/client/impl/SelectorEngineNativeIE8.java
gwtquery-core/src/main/java/com/google/gwt/query/client/impl/SelectorEngineSizzle.java
gwtquery-core/src/main/java/com/google/gwt/query/client/impl/SelectorEngineSizzleIE.java [new file with mode: 0644]
gwtquery-core/src/main/java/com/google/gwt/query/rebind/SelectorGeneratorNativeIE8.java
gwtquery-core/src/test/java/com/google/gwt/query/client/GQueryCoreTest.java

index da78946d0e34f92c6042b37d61ef17a04d083e0e..193635fc0e6951712033ac2b10335f842d87accd 100644 (file)
@@ -26,6 +26,8 @@ import com.google.gwt.event.dom.client.FocusHandler;
 import com.google.gwt.event.shared.GwtEvent;\r
 import com.google.gwt.event.shared.HandlerRegistration;\r
 import com.google.gwt.query.client.impl.SelectorEngineImpl;\r
+import com.google.gwt.query.client.plugins.Events;\r
+import com.google.gwt.user.client.Event;\r
 import com.google.gwt.user.client.Timer;\r
 \r
 /**\r
@@ -48,13 +50,77 @@ public class DevTestRunner extends MyTestCase implements EntryPoint {
   public void onModuleLoad() {\r
     try {\r
       gwtSetUp();\r
-      testCompiledSelectors();\r
+      testSiblings();\r
     } catch (Exception ex) {\r
       ex.printStackTrace();\r
       $(e).html("").after("<div>ERROR: " + ex.getMessage() + "</div>");\r
     }\r
   }\r
   \r
+  public void testSiblings() {\r
+    String content = "<table><tr ><td class='r1'>1</td><td>2</td></tr><tr><td>1</td><td>2</td></tr></table>";\r
+    $(e).html(content);\r
+    $(".r1").css("background", "red");\r
+    \r
+    $(".r1").hover(new Function() {\r
+      public void f(Element e) {\r
+        $(e).add($(e).siblings()).text("A");\r
+      }\r
+    },new Function() {\r
+      public void f(Element e) {\r
+        $(e).add($(e).siblings()).text("B");\r
+      }\r
+    });\r
+  }\r
+  \r
+  \r
+  public void testNamedBinding() {\r
+    $(e).html("<p>Content</p>");\r
+\r
+    $("p", e, Events.Events).bind("click.first.namespace", null, new Function() {; \r
+      public void f(Element elem) {\r
+        $(elem).css("color", "red");\r
+      }\r
+    });\r
+    $("p", e, Events.Events).bind("click.second.namespace", null, new Function() {; \r
+      public void f(Element elem) {\r
+        $(elem).css("background", "green");\r
+      }\r
+    });\r
+    $("p", e, Events.Events).bind("click", null, new Function() {; \r
+      public void f(Element elem) {\r
+        $(elem).css("fontSize", "24px");\r
+      }\r
+    });\r
+    $("p", e, Events.Events).trigger(Event.ONCLICK);\r
+    assertEquals("red", $("p", e).css("color"));\r
+    assertEquals("green", $("p", e).css("background-color"));\r
+    assertEquals(24.0d, GQUtils.cur($("p", e).get(0), "fontSize", true));\r
+    \r
+    $("p", e).css("color","").css("background-color","").css("fontSize", "12px");\r
+    assertFalse("red".equalsIgnoreCase($("p", e).css("color")));\r
+    assertFalse("green".equalsIgnoreCase($("p", e).css("background-color")));\r
+    assertEquals(12.0d, GQUtils.cur($("p", e).get(0), "fontSize", true));\r
+\r
+    $("p", e, Events.Events).unbind("click.first.namespace");\r
+    $("p", e, Events.Events).trigger(Event.ONCLICK);\r
+    assertFalse("red".equalsIgnoreCase($("p", e).css("color")));\r
+    assertEquals("green", $("p", e).css("background-color"));\r
+    assertEquals(24.0d, GQUtils.cur($("p", e).get(0), "fontSize", true));\r
+    \r
+    \r
+    $("p", e).css("color","").css("background-color","").css("fontSize", "12px");\r
+    assertFalse("red".equalsIgnoreCase($("p", e).css("color")));\r
+    assertFalse("green".equalsIgnoreCase($("p", e).css("background-color")));\r
+    assertEquals(12.0d, GQUtils.cur($("p", e).get(0), "fontSize", true));\r
+\r
+    $("p", e, Events.Events).unbind("click");\r
+    $("p", e, Events.Events).trigger(Event.ONCLICK);\r
+    assertFalse("red".equalsIgnoreCase($("p", e).css("color")));\r
+    assertFalse("green".equalsIgnoreCase($("p", e).css("background-color")));\r
+    assertEquals(12.0d, GQUtils.cur($("p", e).get(0), "fontSize", true));\r
+  }\r
+  \r
   public void testSelectorEngineNative() {\r
     SelectorEngineImpl selEng = GWT.create(SelectorEngineImpl.class);\r
     executeSelectorEngineTests(selEng);\r
index 05cf506dad0ac566f92b855468a7ae06c7647215..6a3cd06cee1eeaa9ca4347fef48eb90ddd8c0726 100644 (file)
@@ -3,58 +3,48 @@
 \r
     <!--<define-property name="selectorCapability" values="native,xpath,js"/>-->\r
       <!-- enable for native getClassByName shortcut acceleration -->\r
-    <define-property name="selectorCapability"\r
-                     values="native,js"/>\r
-    <property-provider name="selectorCapability">\r
-        <![CDATA[\r
-         // Select javascript permutation when the user wants to\r
-         // force non-accelerated queries (for benchmarking tests)\r
-         if ($doc.location.href.indexOf("_selector_force_js") != -1) \r
-           return "js"; \r
-           \r
-         // Select the native permutation if querySelectorAll is available\r
-         if ($doc.querySelectorAll && /native/.test(String($doc.querySelectorAll))) \r
-           return "native";\r
-           \r
-         return "js";\r
-        ]]>\r
-    </property-provider>\r
+<!--    <define-property name="selectorCapability"-->\r
+<!--                     values="native,js"/>-->\r
+<!--    <property-provider name="selectorCapability">-->\r
+<!--        <![CDATA[-->\r
+<!--         // Select javascript permutation when the user wants to-->\r
+<!--         // force non-accelerated queries (for benchmarking tests)-->\r
+<!--         if ($doc.location.href.indexOf("_selector_force_js") != -1) -->\r
+<!--           return "js"; -->\r
+<!--           -->\r
+<!--         // Select the native permutation if querySelectorAll is available-->\r
+<!--         if ($doc.querySelectorAll && /native/.test(String($doc.querySelectorAll))) -->\r
+<!--           return "native";-->\r
+<!--           -->\r
+<!--         return "js";-->\r
+<!--        ]]>-->\r
+<!--    </property-provider>-->\r
 \r
     <generate-with class="com.google.gwt.query.rebind.LazyGenerator">\r
         <when-type-assignable class="com.google.gwt.query.client.Lazy"/>\r
     </generate-with>\r
-\r
+    \r
+    <!-- Selector Generators -->\r
     <generate-with class="com.google.gwt.query.rebind.SelectorGeneratorJS">\r
         <when-type-assignable class="com.google.gwt.query.client.Selectors"/>\r
     </generate-with>\r
-\r
-    <generate-with class="com.google.gwt.query.rebind.SelectorGeneratorCssToXPath">\r
+    <generate-with class="com.google.gwt.query.rebind.SelectorGeneratorNative">\r
         <when-type-assignable class="com.google.gwt.query.client.Selectors"/>\r
         <any>\r
             <when-property-is name="user.agent" value="gecko1_8"/>\r
             <when-property-is name="user.agent" value="opera"/>\r
-            <all>\r
-                <when-property-is name="selectorCapability" value="js"/>\r
-                <when-property-is name="user.agent" value="safari"/>\r
-            </all>\r
+            <when-property-is name="user.agent" value="safari"/>\r
         </any>\r
     </generate-with>\r
-\r
-    <generate-with class="com.google.gwt.query.rebind.SelectorGeneratorNative">\r
-        <when-type-assignable class="com.google.gwt.query.client.Selectors"/>\r
-        <when-property-is name="selectorCapability" value="native"/>\r
-    </generate-with>\r
-
     <generate-with class="com.google.gwt.query.rebind.SelectorGeneratorNativeIE8">\r
         <when-type-assignable class="com.google.gwt.query.client.Selectors"/>\r
-        <when-property-is name="selectorCapability" value="native"/>
         <when-property-is name="user.agent" value="ie8"/>\r
     </generate-with>
 \r
+    <!--  Document Style -->\r
     <replace-with class="com.google.gwt.query.client.impl.DocumentStyleImpl">\r
         <when-type-assignable class="com.google.gwt.query.client.impl.DocumentStyleImpl"/>\r
     </replace-with>\r
-\r
     <replace-with class="com.google.gwt.query.client.impl.DocumentStyleImplIE">\r
         <when-type-assignable class="com.google.gwt.query.client.impl.DocumentStyleImpl"/>\r
         <any>\r
         </any>\r
     </replace-with>\r
 \r
+    <!-- Selector Engines -->\r
     <replace-with class="com.google.gwt.query.client.impl.SelectorEngineSizzle">\r
-        <when-type-assignable class="com.google.gwt.query.client.impl.SelectorEngineImpl"/>\r
+        <when-type-assignable class="com.google.gwt.query.client.impl.HasSelector"/>\r
     </replace-with>\r
-\r
-    <replace-with class="com.google.gwt.query.client.impl.SelectorEngineCssToXPath">\r
-        <when-type-assignable\r
-                class="com.google.gwt.query.client.impl.SelectorEngineImpl"/>\r
+    <replace-with class="com.google.gwt.query.client.impl.SelectorEngineSizzleIE">\r
+        <when-type-assignable class="com.google.gwt.query.client.impl.HasSelector"/>\r
         <any>\r
-            <when-property-is name="user.agent" value="gecko1_8"/>\r
-            <when-property-is name="user.agent" value="opera"/>\r
-            <all>\r
-                <when-property-is name="selectorCapability" value="js"/>\r
-                <when-property-is name="user.agent" value="safari"/>\r
-            </all>\r
+            <when-property-is name="user.agent" value="ie8"/>\r
+            <when-property-is name="user.agent" value="ie6"/>\r
         </any>\r
     </replace-with>\r
-\r
+    <replace-with class="com.google.gwt.query.client.impl.SelectorEngineSizzle">\r
+        <when-type-assignable class="com.google.gwt.query.client.impl.SelectorEngineImpl"/>\r
+    </replace-with>\r
+    <replace-with class="com.google.gwt.query.client.impl.SelectorEngineSizzleIE">\r
+        <when-type-assignable class="com.google.gwt.query.client.impl.SelectorEngineImpl"/>\r
+        <when-property-is name="user.agent" value="ie6"/>\r
+    </replace-with>\r
     <replace-with class="com.google.gwt.query.client.impl.SelectorEngineNative">\r
         <when-type-assignable class="com.google.gwt.query.client.impl.SelectorEngineImpl"/>\r
-        <when-property-is name="selectorCapability" value="native"/>\r
+        <any>\r
+            <when-property-is name="user.agent" value="gecko1_8"/>\r
+            <when-property-is name="user.agent" value="opera"/>\r
+            <when-property-is name="user.agent" value="safari"/>\r
+        </any>\r
     </replace-with>\r
-    \r
     <replace-with class="com.google.gwt.query.client.impl.SelectorEngineNativeIE8">\r
         <when-type-assignable class="com.google.gwt.query.client.impl.SelectorEngineImpl"/>\r
-        <when-property-is name="selectorCapability" value="native"/>\r
         <when-property-is name="user.agent" value="ie8"/>\r
     </replace-with>\r
-    \r
+\r
+    <!-- IE8 needs the iframe with the app set to standard to use native selector -->    \r
     <define-linker name="stddoctype" class="com.google.gwt.query.linker.IFrameWithDocTypeLinker"/>\r
     <add-linker name="stddoctype"/>\r
-    
-    <entry-point class='com.google.gwt.query.client.css.CSS'/>\r
-\r
+    \r
 </module>\r
index b15fba2fd10952ef712ea205a154155f95115996..97cfb7b9020a93c541c97f00d357566922ec07ec 100644 (file)
@@ -29,6 +29,13 @@ public abstract class Function {
   public void cancel(Element e) {
   }
 
+  /**
+   * Override this to define a function which does not need any parameter
+   */
+  public void f() {
+    throw new RuntimeException("You have to override the adequate method to handle this action.");
+  }
+
   /**
    * Override this for GQuery methods which loop over matched elements and
    * invoke a callback on each element.
@@ -43,7 +50,7 @@ public abstract class Function {
    * return value, apply to a single element only.
    */
   public void f(Element e) {
-    throw new RuntimeException("You have to override the adequate method to handle this action.");
+    f();
   }
 
   /**
index fd08305758a968ea3735a72f91f1044277597f01..f9cd8e581822eac65d1d05da2664a86cb6de377d 100644 (file)
@@ -94,7 +94,7 @@ public class GQUtils {
   public static native double or(double s1, double s2) /*-{
     return s1 || s2;
   }-*/;
-  
+
   /**
    * Return the element which is truth in the javascript scope.
    */
@@ -115,7 +115,10 @@ public class GQUtils {
   public static native boolean truth(Object a) /*-{
     return a ? true : false;
   }-*/;
-
+  
+  /**
+   * Remove duplicates from an elements array
+   */
   public static JsArray<Element> unique(JsArray<Element> a) {
     JsArray<Element> ret = JavaScriptObject.createArray().cast();
     HashSet<Integer> f = new HashSet<Integer>();
index ba08268a28c6e24a1c98eec65e8870bea3fd2bb9..2d5613718abfec3a1edbfa31abf77dc3d8bdc711 100644 (file)
@@ -595,9 +595,12 @@ public class GQuery implements Lazy<GQuery, LazyGQuery> {
     if (plugin == GQUERY) {\r
       return (T) $(this);\r
     } else if (plugins != null) {\r
-      return (T) plugins.get(plugin).init(this);\r
+      Plugin p = plugins.get(plugin);\r
+      if (p != null) {\r
+        return (T) p.init(this);\r
+      }\r
     }\r
-    throw new RuntimeException("No plugin registered for class " + plugin);\r
+    throw new RuntimeException("No plugin registered for class " + plugin.getName());\r
   }\r
 \r
   /**\r
@@ -2181,8 +2184,10 @@ public class GQuery implements Lazy<GQuery, LazyGQuery> {
                 ie.setChecked(false);\r
               }\r
             }\r
-          } else if (values[0].equals(ie.getValue())) {\r
+          } else if (ie.getValue().equals(values[0])) {\r
             ie.setChecked(true);\r
+          } else {\r
+            ie.setChecked(false);\r
           }\r
         } else {\r
           ie.setValue(values[0]);\r
@@ -2456,10 +2461,12 @@ public class GQuery implements Lazy<GQuery, LazyGQuery> {
     }\r
   }\r
 \r
-  private GQuery domManip(GQuery g, int func) {\r
+  private GQuery domManip(GQuery g, int func, Element...elms) {\r
     JSArray newNodes = JSArray.create();\r
-    for (int i = 0; i < elements().length; i++) {\r
-      Element e = elements()[i];\r
+    if (elms.length == 0) {\r
+      elms = elements();\r
+    }\r
+    for (Element e: elms) {\r
       e.getOwnerDocument();\r
       if (e.getNodeType() == Node.DOCUMENT_NODE) {\r
         e = e.<Document>cast().getBody();\r
@@ -2485,14 +2492,13 @@ public class GQuery implements Lazy<GQuery, LazyGQuery> {
         }\r
       }\r
     }\r
-    if (newNodes.size() > 0) {\r
+    if (newNodes.size() > g.size()) {\r
       g.setArray(newNodes);\r
     }\r
     return this;\r
   }\r
-\r
+  \r
   private GQuery domManip(String htmlString, int func) {\r
-    GQuery ret = $();\r
     HashMap<Document, GQuery> cache = new HashMap<Document, GQuery>();\r
     for (Element e: elements()) {\r
       Document d = e.getNodeType() == Node.DOCUMENT_NODE ? e.<Document>cast() : e.getOwnerDocument();\r
@@ -2501,9 +2507,9 @@ public class GQuery implements Lazy<GQuery, LazyGQuery> {
         g = cleanHtmlString(htmlString, d);\r
         cache.put(d, g);\r
       }\r
-      ret.add(domManip(g, func));\r
+      domManip(g.clone(), func, e);\r
     }\r
-    return ret;\r
+    return this;\r
   }\r
 \r
   private native Element getPreviousSiblingElement(Element elem)  /*-{\r
index 45646d7609c71fc9f08cfb5fe1b0b8782cf8bc95..6c0b7763807c1f88ceec775969229f77712f66db 100644 (file)
@@ -30,32 +30,28 @@ public class SelectorEngine {
   public static native NodeList<Element> getElementsByClassName(String clazz,\r
       Node ctx) /*-{\r
         return ctx.getElementsByClassName(clazz);\r
-    }-*/;\r
+  }-*/;\r
 \r
   public static native Node getNextSibling(Node n) /*-{\r
        return n.nextSibling || null; \r
-    }-*/;\r
+  }-*/;\r
 \r
   public static native Node getPreviousSibling(Node n) /*-{\r
        return n.previousSibling || null; \r
-    }-*/;\r
-  \r
-  private static boolean degradate = false;\r
+  }-*/;\r
   \r
-  public NodeList<Element> querySelectorAllIE8(String selector, Node ctx) {\r
-    if (degradate) {\r
+  public NodeList<Element> querySelectorAll(String selector, Node ctx) {\r
+    if (!hasQuerySelector) {\r
       return impl.select(selector, ctx);\r
-    } \r
+    }\r
     try {\r
-      return querySelectorAll(selector, ctx);\r
+      return querySelectorAllImpl(selector, ctx);\r
     } catch (Exception e) {\r
-      System.out.println("IE8 Degradating to dynamic implementation, it seems IE8 is not running in standars mode");\r
-      degradate = true;\r
-      return querySelectorAll(selector, ctx);\r
+      return impl.select(selector, ctx);\r
     }\r
   }\r
 \r
-  public static native NodeList<Element> querySelectorAll(String selector,\r
+  public static native NodeList<Element> querySelectorAllImpl(String selector,\r
       Node ctx) /*-{\r
       return ctx.querySelectorAll(selector);\r
   }-*/;\r
@@ -79,6 +75,8 @@ public class SelectorEngine {
   protected SelectorEngineImpl impl;\r
 \r
   protected Node root = Document.get();\r
+  \r
+  public static final boolean hasQuerySelector = hasQuerySelectorAll();\r
 \r
   public SelectorEngine() {\r
     impl = (SelectorEngineImpl) GWT.create(SelectorEngineImpl.class);\r
@@ -113,7 +111,16 @@ public class SelectorEngine {
   }\r
   \r
   public boolean isDegradated() {\r
-    return degradate;\r
+    return !hasQuerySelector;\r
   }\r
   \r
+  /**\r
+   * Check if the browser has native support for css selectors\r
+   */\r
+  public static native boolean hasQuerySelectorAll() /*-{\r
+    return $doc.location.href.indexOf("_force_no_native") < 0 &&\r
+           $doc.querySelectorAll && \r
+           /native/.test(String($doc.querySelectorAll)); \r
+  }-*/;\r
+  \r
 }\r
index 593f638e0b5036689eb1204bd1fed4bd7da170b2..9e28010f8d0fce4b9fc86b516a7a9707c99092d5 100644 (file)
@@ -1,12 +1,11 @@
 package com.google.gwt.query.client.css;
 
-import com.google.gwt.core.client.EntryPoint;
 
 /**
  * Created by IntelliJ IDEA. User: ray Date: May 2, 2009 Time: 12:56:09 AM To
  * change this template use File | Settings | File Templates.
  */
-public class CSS implements EntryPoint {
+public class CSS {
 
   /**
    * This property affects the vertical positioning inside a line box of the
@@ -447,7 +446,7 @@ public class CSS implements EntryPoint {
    */
   public static Height HEIGHT;
 
-  public static void init() {
+  static {
     INHERIT = Inherit.create();
     BackgroundAttachment.init();
     BackgroundColor.init();
@@ -463,7 +462,4 @@ public class CSS implements EntryPoint {
     Width.init();
   }
 
-  public void onModuleLoad() {
-    CSS.init();
-  }
 }
diff --git a/gwtquery-core/src/main/java/com/google/gwt/query/client/impl/HasSelector.java b/gwtquery-core/src/main/java/com/google/gwt/query/client/impl/HasSelector.java
new file mode 100644 (file)
index 0000000..fd5932e
--- /dev/null
@@ -0,0 +1,18 @@
+package com.google.gwt.query.client.impl;
+
+import com.google.gwt.dom.client.Element;
+import com.google.gwt.dom.client.Node;
+import com.google.gwt.dom.client.NodeList;
+
+public interface HasSelector {
+  
+  /**
+   * Parse and execute a given selector expression given a context.
+   *
+   * @param selector the CSS selector expression
+   * @param ctx      the DOM node to use as a context
+   * @return a list of matched nodes
+   */
+  NodeList<Element> select(String selector, Node ctx);
+
+}
index 65a2442c5c805148e143bb03f1c3902f82b8a1cc..7e1f8ad80079d7d7a115db8a8b567a52df6a1c92 100644 (file)
@@ -17,10 +17,6 @@ package com.google.gwt.query.client.impl;
 import static com.google.gwt.query.client.GQUtils.eq;\r
 import static com.google.gwt.query.client.GQUtils.truth;\r
 \r
-\r
-import com.google.gwt.dom.client.Element;\r
-import com.google.gwt.dom.client.Node;\r
-import com.google.gwt.dom.client.NodeList;\r
 import com.google.gwt.query.client.GQUtils;\r
 import com.google.gwt.query.client.JSArray;\r
 import com.google.gwt.query.client.Regexp;\r
@@ -28,7 +24,7 @@ import com.google.gwt.query.client.Regexp;
 /**\r
  * Base/Utility class for runtime selector engine implementations.\r
  */\r
-public abstract class SelectorEngineImpl {\r
+public abstract class SelectorEngineImpl implements HasSelector {\r
 \r
   /**\r
    * Internal class.\r
@@ -126,12 +122,4 @@ public abstract class SelectorEngineImpl {
     return s;\r
   }\r
 \r
-  /**\r
-   * Parse and execute a given selector expression given a context.\r
-   *\r
-   * @param selector the CSS selector expression\r
-   * @param ctx      the DOM node to use as a context\r
-   * @return a list of matched nodes\r
-   */\r
-  public abstract NodeList<Element> select(String selector, Node ctx);\r
 }\r
index 477e42870146624df9dca3ce20530349e0daed63..f7dc9223bb93d1dfc1435adb59e9cf44580c09cd 100644 (file)
@@ -15,6 +15,7 @@
  */
 package com.google.gwt.query.client.impl;
 
+import com.google.gwt.core.client.GWT;
 import com.google.gwt.dom.client.Element;
 import com.google.gwt.dom.client.Node;
 import com.google.gwt.dom.client.NodeList;
@@ -24,15 +25,28 @@ import com.google.gwt.query.client.SelectorEngine;
  * Runtime selector engine implementation for browsers with native
  * querySelectorAll support.
  */
-public class SelectorEngineNative extends SelectorEngineCssToXPath {
+public class SelectorEngineNative extends SelectorEngineImpl {
   
   public static String NATIVE_EXCEPTIONS_REGEXP = ".*(:contains|!=).*";
   
+  private static HasSelector impl;
+  
+  public SelectorEngineNative() {
+    if (impl == null) {
+      impl = GWT.create(HasSelector.class);
+    }
+  }
+  
   public NodeList<Element> select(String selector, Node ctx) {
-    if (selector.matches(NATIVE_EXCEPTIONS_REGEXP)) {
-      return super.select(selector, ctx); 
+    if (!SelectorEngine.hasQuerySelector || selector.matches(NATIVE_EXCEPTIONS_REGEXP)) {
+      return impl.select(selector, ctx); 
     } else {
-      return SelectorEngine.querySelectorAll(selector, ctx);
+      try {
+        return SelectorEngine.querySelectorAllImpl(selector, ctx);
+      } catch (Exception e) {
+        return impl.select(selector, ctx); 
+      }
     }
   }
+  
 }
index cf911e2ba93e347d8325f2a170644d7b423cfbe7..c880a3ec88fa6676298d80f3b2bbd49e4e9f925f 100644 (file)
@@ -24,21 +24,17 @@ import com.google.gwt.query.client.SelectorEngine;
  * Runtime selector engine implementation for browsers with native
  * querySelectorAll support.
  */
-public class SelectorEngineNativeIE8 extends SelectorEngineSizzle {
+public class SelectorEngineNativeIE8 extends SelectorEngineSizzleIE {
   
   public static String NATIVE_EXCEPTIONS_REGEXP = ".*(:contains|!=|:checked|:not|:nth-|:last-|:only-).*";
-
-  private static boolean degradate = false;
   
   public NodeList<Element> select(String selector, Node ctx) {
-    if (degradate || selector.matches(NATIVE_EXCEPTIONS_REGEXP)) {
+    if (!SelectorEngine.hasQuerySelector || selector.matches(NATIVE_EXCEPTIONS_REGEXP)) {
       return super.select(selector, ctx); 
     } else {
       try {
-        return SelectorEngine.querySelectorAll(selector, ctx);
+        return SelectorEngine.querySelectorAllImpl(selector, ctx);
       } catch (Exception e) {
-        System.out.println("IE8 Degradating to dynamic implementation, it seems IE8 is not running in standars mode");
-        degradate = true;
         return super.select(selector, ctx); 
       }
     }
index d983481aefa008821cdc171b14dc11c4f9a35110..1d8d254f517ab5f0fd80f320daf375cddab88d57 100644 (file)
@@ -924,39 +924,39 @@ GQS.getText = function( elems ) {
 
   div = null; // release memory in IE
 })();
-
-if ( document.querySelectorAll ) {
-  (function(){
-    var oldGQS = GQS, div = document.createElement("div");
-    div.innerHTML = "<p class='TEST'></p>";
-
-    // Safari can't handle uppercase or unicode characters when
-    // in quirks mode.
-    if ( div.querySelectorAll && div.querySelectorAll(".TEST").length === 0 ) {
-      return;
-    }
-  
-    GQS = function(query, context, extra, seed){
-      context = context || document;
-
-      // Only use querySelectorAll on non-XML documents
-      // (ID selectors don't work in non-HTML documents)
-      if ( !seed && context.nodeType === 9 && !GQS.isXML(context) ) {
-        try {
-          return makeArray( context.querySelectorAll(query), extra );
-        } catch(e){}
-      }
-    
-      return oldGQS(query, context, extra, seed);
-    };
-
-    for ( var prop in oldGQS ) {
-      GQS[ prop ] = oldGQS[ prop ];
-    }
-
-    div = null; // release memory in IE
-  })();
-}
+alert("");
+//if ( document.querySelectorAll ) {
+//  (function(){
+//    var oldGQS = GQS, div = document.createElement("div");
+//    div.innerHTML = "<p class='TEST'></p>";
+//
+//    // Safari can't handle uppercase or unicode characters when
+//    // in quirks mode.
+//    if ( div.querySelectorAll && div.querySelectorAll(".TEST").length === 0 ) {
+//      return;
+//    }
+//  
+//    GQS = function(query, context, extra, seed){
+//      context = context || document;
+//
+//      // Only use querySelectorAll on non-XML documents
+//      // (ID selectors don't work in non-HTML documents)
+//      if ( !seed && context.nodeType === 9 && !GQS.isXML(context) ) {
+//        try {
+//          return makeArray( context.querySelectorAll(query), extra );
+//        } catch(e){}
+//      }
+//    
+//      return oldGQS(query, context, extra, seed);
+//    };
+//
+//    for ( var prop in oldGQS ) {
+//      GQS[ prop ] = oldGQS[ prop ];
+//    }
+//
+//    div = null; // release memory in IE
+//  })();
+//}
 
 (function(){
   var div = document.createElement("div");
diff --git a/gwtquery-core/src/main/java/com/google/gwt/query/client/impl/SelectorEngineSizzleIE.java b/gwtquery-core/src/main/java/com/google/gwt/query/client/impl/SelectorEngineSizzleIE.java
new file mode 100644 (file)
index 0000000..2c147d3
--- /dev/null
@@ -0,0 +1,956 @@
+/*
+ * Copyright 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.
+ */
+package com.google.gwt.query.client.impl;
+
+
+import com.google.gwt.core.client.JavaScriptObject;
+import com.google.gwt.core.client.JsArray;
+import com.google.gwt.dom.client.Element;
+import com.google.gwt.dom.client.Node;
+import com.google.gwt.dom.client.NodeList;
+
+/**
+ * Original Sizzle CSS Selector Engine v1.0 inserted in a JSNI method.
+ * There are a couple of differences with the original:
+ * - It uses the window.IES instead of window.Sizzle to avoid interfering.
+ * - All the stuff related with non IE browsers has been removed making this
+ *   implementation a bit faster and smaller.
+ * 
+ * There are some reasons why we are using sizzle with IE instead of any other 
+ * implementation.
+ *  - Sizzle is the faster selector with IE we have tested.
+ *  - Only IE8 under some conditions and with a limited set of selectors
+ *    has a native and fast method for selecting dom elements. So we need
+ *    a complete implementation in javascript.
+ *  - We don't want to make the effort of writing a complete js implementation (DRY).
+ *  - It is supposed this class will be here for a while, because new versions of
+ *    IE are coming.
+ */
+public class SelectorEngineSizzleIE extends SelectorEngineImpl {
+  
+  
+  public static native void initialize() /*-{
+(function(){
+
+var chunker = /((?:\((?:\([^()]+\)|[^()]+)+\)|\[(?:\[[^\[\]]*\]|['"][^'"]*['"]|[^\[\]'"]+)+\]|\\.|[^ >+~,(\[\\]+)+|[>+~])(\s*,\s*)?((?:.|\r|\n)*)/g,
+  done = 0,
+  toString = Object.prototype.toString,
+  hasDuplicate = false;
+
+var IES = function(selector, context, results, seed) {
+  results = results || [];
+  context = context || document;
+
+  var origContext = context;
+
+  if ( context.nodeType !== 1 && context.nodeType !== 9 ) {
+    return [];
+  }
+  
+  if ( !selector || typeof selector !== "string" ) {
+    return results;
+  }
+
+  var parts = [], m, set, checkSet, extra, prune = true, contextXML = IES.isXML(context),
+    soFar = selector, ret, cur, pop, i;
+  
+  // Reset the position of the chunker regexp (start from head)
+  do {
+    chunker.exec("");
+    m = chunker.exec(soFar);
+
+    if ( m ) {
+      soFar = m[3];
+    
+      parts.push( m[1] );
+    
+      if ( m[2] ) {
+        extra = m[3];
+        break;
+      }
+    }
+  } while ( m );
+
+  if ( parts.length > 1 && origPOS.exec( selector ) ) {
+    if ( parts.length === 2 && Expr.relative[ parts[0] ] ) {
+      set = posProcess( parts[0] + parts[1], context );
+    } else {
+      set = Expr.relative[ parts[0] ] ?
+        [ context ] :
+        IES( parts.shift(), context );
+
+      while ( parts.length ) {
+        selector = parts.shift();
+
+        if ( Expr.relative[ selector ] ) {
+          selector += parts.shift();
+        }
+        
+        set = posProcess( selector, set );
+      }
+    }
+  } else {
+    // Take a shortcut and set the context if the root selector is an ID
+    // (but not if it'll be faster if the inner selector is an ID)
+    if ( !seed && parts.length > 1 && context.nodeType === 9 && !contextXML &&
+        Expr.match.ID.test(parts[0]) && !Expr.match.ID.test(parts[parts.length - 1]) ) {
+      ret = IES.find( parts.shift(), context, contextXML );
+      context = ret.expr ? IES.filter( ret.expr, ret.set )[0] : ret.set[0];
+    }
+
+    if ( context ) {
+      ret = seed ?
+        { expr: parts.pop(), set: makeArray(seed) } :
+        IES.find( parts.pop(), parts.length === 1 && (parts[0] === "~" || parts[0] === "+") && context.parentNode ? context.parentNode : context, contextXML );
+      set = ret.expr ? IES.filter( ret.expr, ret.set ) : ret.set;
+
+      if ( parts.length > 0 ) {
+        checkSet = makeArray(set);
+      } else {
+        prune = false;
+      }
+
+      while ( parts.length ) {
+        cur = parts.pop();
+        pop = cur;
+
+        if ( !Expr.relative[ cur ] ) {
+          cur = "";
+        } else {
+          pop = parts.pop();
+        }
+
+        if ( pop == null ) {
+          pop = context;
+        }
+
+        Expr.relative[ cur ]( checkSet, pop, contextXML );
+      }
+    } else {
+      checkSet = parts = [];
+    }
+  }
+
+  if ( !checkSet ) {
+    checkSet = set;
+  }
+
+  if ( !checkSet ) {
+    IES.error( cur || selector );
+  }
+
+  if ( toString.call(checkSet) === "[object Array]" ) {
+    if ( !prune ) {
+      results.push.apply( results, checkSet );
+    } else if ( context && context.nodeType === 1 ) {
+      for ( i = 0; checkSet[i] != null; i++ ) {
+        if ( checkSet[i] && (checkSet[i] === true || checkSet[i].nodeType === 1 && IES.contains(context, checkSet[i])) ) {
+          results.push( set[i] );
+        }
+      }
+    } else {
+      for ( i = 0; checkSet[i] != null; i++ ) {
+        if ( checkSet[i] && checkSet[i].nodeType === 1 ) {
+          results.push( set[i] );
+        }
+      }
+    }
+  } else {
+    makeArray( checkSet, results );
+  }
+
+  if ( extra ) {
+    IES( extra, origContext, results, seed );
+    IES.uniqueSort( results );
+  }
+
+  return results;
+};
+
+IES.uniqueSort = function(results){
+  if ( sortOrder ) {
+    hasDuplicate = false;
+    results.sort(sortOrder);
+
+    if ( hasDuplicate ) {
+      for ( var i = 1; i < results.length; i++ ) {
+        if ( results[i] === results[i-1] ) {
+          results.splice(i--, 1);
+        }
+      }
+    }
+  }
+
+  return results;
+};
+
+IES.matches = function(expr, set){
+  return IES(expr, null, null, set);
+};
+
+IES.find = function(expr, context, isXML){
+  var set;
+
+  if ( !expr ) {
+    return [];
+  }
+
+  for ( var i = 0, l = Expr.order.length; i < l; i++ ) {
+    var type = Expr.order[i], match;
+    
+    if ( (match = Expr.leftMatch[ type ].exec( expr )) ) {
+      var left = match[1];
+      match.splice(1,1);
+
+      if ( left.substr( left.length - 1 ) !== "\\" ) {
+        match[1] = (match[1] || "").replace(/\\/g, "");
+        set = Expr.find[ type ]( match, context, isXML );
+        if ( set != null ) {
+          expr = expr.replace( Expr.match[ type ], "" );
+          break;
+        }
+      }
+    }
+  }
+
+  if ( !set ) {
+    set = context.getElementsByTagName("*");
+  }
+
+  return {set: set, expr: expr};
+};
+
+IES.filter = function(expr, set, inplace, not){
+  var old = expr, result = [], curLoop = set, match, anyFound,
+    isXMLFilter = set && set[0] && IES.isXML(set[0]);
+
+  while ( expr && set.length ) {
+    for ( var type in Expr.filter ) {
+      if ( (match = Expr.leftMatch[ type ].exec( expr )) != null && match[2] ) {
+        var filter = Expr.filter[ type ], found, item, left = match[1];
+        anyFound = false;
+
+        match.splice(1,1);
+
+        if ( left.substr( left.length - 1 ) === "\\" ) {
+          continue;
+        }
+
+        if ( curLoop === result ) {
+          result = [];
+        }
+
+        if ( Expr.preFilter[ type ] ) {
+          match = Expr.preFilter[ type ]( match, curLoop, inplace, result, not, isXMLFilter );
+
+          if ( !match ) {
+            anyFound = found = true;
+          } else if ( match === true ) {
+            continue;
+          }
+        }
+
+        if ( match ) {
+          for ( var i = 0; (item = curLoop[i]) != null; i++ ) {
+            if ( item ) {
+              found = filter( item, match, i, curLoop );
+              var pass = not ^ !!found;
+
+              if ( inplace && found != null ) {
+                if ( pass ) {
+                  anyFound = true;
+                } else {
+                  curLoop[i] = false;
+                }
+              } else if ( pass ) {
+                result.push( item );
+                anyFound = true;
+              }
+            }
+          }
+        }
+
+        if ( found !== undefined ) {
+          if ( !inplace ) {
+            curLoop = result;
+          }
+
+          expr = expr.replace( Expr.match[ type ], "" );
+
+          if ( !anyFound ) {
+            return [];
+          }
+
+          break;
+        }
+      }
+    }
+
+    // Improper expression
+    if ( expr === old ) {
+      if ( anyFound == null ) {
+        IES.error( expr );
+      } else {
+        break;
+      }
+    }
+
+    old = expr;
+  }
+
+  return curLoop;
+};
+
+IES.error = function( msg ) {
+  throw "Syntax error, unrecognized expression: " + msg;
+};
+
+var Expr = IES.selectors = {
+  order: [ "ID", "NAME", "TAG" ],
+  match: {
+    ID: /#((?:[\w\u00c0-\uFFFF\-]|\\.)+)/,
+    CLASS: /\.((?:[\w\u00c0-\uFFFF\-]|\\.)+)/,
+    NAME: /\[name=['"]*((?:[\w\u00c0-\uFFFF\-]|\\.)+)['"]*\]/,
+    ATTR: /\[\s*((?:[\w\u00c0-\uFFFF\-]|\\.)+)\s*(?:(\S?=)\s*(['"]*)(.*?)\3|)\s*\]/,
+    TAG: /^((?:[\w\u00c0-\uFFFF\*\-]|\\.)+)/,
+    CHILD: /:(only|nth|last|first)-child(?:\((even|odd|[\dn+\-]*)\))?/,
+    POS: /:(nth|eq|gt|lt|first|last|even|odd)(?:\((\d*)\))?(?=[^\-]|$)/,
+    PSEUDO: /:((?:[\w\u00c0-\uFFFF\-]|\\.)+)(?:\((['"]?)((?:\([^\)]+\)|[^\(\)]*)+)\2\))?/
+  },
+  leftMatch: {},
+  attrMap: {
+    "class": "className",
+    "for": "htmlFor"
+  },
+  attrHandle: {
+    href: function(elem){
+      return elem.getAttribute("href", 2);
+    }
+  },
+  relative: {
+    "+": function(checkSet, part){
+      var isPartStr = typeof part === "string",
+        isTag = isPartStr && !/\W/.test(part),
+        isPartStrNotTag = isPartStr && !isTag;
+
+      if ( isTag ) {
+        part = part.toLowerCase();
+      }
+
+      for ( var i = 0, l = checkSet.length, elem; i < l; i++ ) {
+        if ( (elem = checkSet[i]) ) {
+          while ( (elem = elem.previousSibling) && elem.nodeType !== 1 ) {}
+
+          checkSet[i] = isPartStrNotTag || elem && elem.nodeName.toLowerCase() === part ?
+            elem || false :
+            elem === part;
+        }
+      }
+
+      if ( isPartStrNotTag ) {
+        IES.filter( part, checkSet, true );
+      }
+    },
+    ">": function(checkSet, part){
+      var isPartStr = typeof part === "string",
+        elem, i = 0, l = checkSet.length;
+
+      if ( isPartStr && !/\W/.test(part) ) {
+        part = part.toLowerCase();
+
+        for ( ; i < l; i++ ) {
+          elem = checkSet[i];
+          if ( elem ) {
+            var parent = elem.parentNode;
+            checkSet[i] = parent.nodeName.toLowerCase() === part ? parent : false;
+          }
+        }
+      } else {
+        for ( ; i < l; i++ ) {
+          elem = checkSet[i];
+          if ( elem ) {
+            checkSet[i] = isPartStr ?
+              elem.parentNode :
+              elem.parentNode === part;
+          }
+        }
+
+        if ( isPartStr ) {
+          IES.filter( part, checkSet, true );
+        }
+      }
+    },
+    "": function(checkSet, part, isXML){
+      var doneName = done++, checkFn = dirCheck, nodeCheck;
+
+      if ( typeof part === "string" && !/\W/.test(part) ) {
+        part = part.toLowerCase();
+        nodeCheck = part;
+        checkFn = dirNodeCheck;
+      }
+
+      checkFn("parentNode", part, doneName, checkSet, nodeCheck, isXML);
+    },
+    "~": function(checkSet, part, isXML){
+      var doneName = done++, checkFn = dirCheck, nodeCheck;
+
+      if ( typeof part === "string" && !/\W/.test(part) ) {
+        part = part.toLowerCase();
+        nodeCheck = part;
+        checkFn = dirNodeCheck;
+      }
+
+      checkFn("previousSibling", part, doneName, checkSet, nodeCheck, isXML);
+    }
+  },
+  find: {
+    ID: function(match, context, isXML){
+      if ( typeof context.getElementById !== "undefined" && !isXML ) {
+        var m = context.getElementById(match[1]);
+        return m ? m.id === match[1] || typeof m.getAttributeNode !== "undefined" && m.getAttributeNode("id").nodeValue === match[1] ? [m] : undefined : [];
+      }
+    },
+    NAME: function(match, context){
+      if ( typeof context.getElementsByName !== "undefined" ) {
+        var ret = [], results = context.getElementsByName(match[1]);
+
+        for ( var i = 0, l = results.length; i < l; i++ ) {
+          if ( results[i].getAttribute("name") === match[1] ) {
+            ret.push( results[i] );
+          }
+        }
+
+        return ret.length === 0 ? null : ret;
+      }
+    },
+    // IE espedific
+    TAG: function(match, context){
+      var results = context.getElementsByTagName(match[1]);
+      // Filter out possible comments
+      if ( match[1] === "*" ) {
+        var tmp = [];
+        for ( var i = 0; results[i]; i++ ) {
+          if ( results[i].nodeType === 1 ) {
+            tmp.push( results[i] );
+          }
+        }
+        results = tmp;
+      }
+      return results;
+    }
+  },
+  preFilter: {
+    CLASS: function(match, curLoop, inplace, result, not, isXML){
+      match = " " + match[1].replace(/\\/g, "") + " ";
+
+      if ( isXML ) {
+        return match;
+      }
+
+      for ( var i = 0, elem; (elem = curLoop[i]) != null; i++ ) {
+        if ( elem ) {
+          if ( not ^ (elem.className && (" " + elem.className + " ").replace(/[\t\n]/g, " ").indexOf(match) >= 0) ) {
+            if ( !inplace ) {
+              result.push( elem );
+            }
+          } else if ( inplace ) {
+            curLoop[i] = false;
+          }
+        }
+      }
+
+      return false;
+    },
+    ID: function(match){
+      return match[1].replace(/\\/g, "");
+    },
+    TAG: function(match, curLoop){
+      return match[1].toLowerCase();
+    },
+    CHILD: function(match){
+      if ( match[1] === "nth" ) {
+        // parse equations like 'even', 'odd', '5', '2n', '3n+2', '4n-1', '-n+6'
+        var test = /(-?)(\d*)n((?:\+|-)?\d*)/.exec(
+          match[2] === "even" && "2n" || match[2] === "odd" && "2n+1" ||
+          !/\D/.test( match[2] ) && "0n+" + match[2] || match[2]);
+
+        // calculate the numbers (first)n+(last) including if they are negative
+        match[2] = (test[1] + (test[2] || 1)) - 0;
+        match[3] = test[3] - 0;
+      }
+
+      // TODO: Move to normal caching system
+      match[0] = done++;
+
+      return match;
+    },
+    ATTR: function(match, curLoop, inplace, result, not, isXML){
+      var name = match[1].replace(/\\/g, "");
+      
+      if ( !isXML && Expr.attrMap[name] ) {
+        match[1] = Expr.attrMap[name];
+      }
+
+      if ( match[2] === "~=" ) {
+        match[4] = " " + match[4] + " ";
+      }
+
+      return match;
+    },
+    PSEUDO: function(match, curLoop, inplace, result, not){
+      if ( match[1] === "not" ) {
+        // If we're dealing with a complex expression, or a simple one
+        if ( ( chunker.exec(match[3]) || "" ).length > 1 || /^\w/.test(match[3]) ) {
+          match[3] = IES(match[3], null, null, curLoop);
+        } else {
+          var ret = IES.filter(match[3], curLoop, inplace, true ^ not);
+          if ( !inplace ) {
+            result.push.apply( result, ret );
+          }
+          return false;
+        }
+      } else if ( Expr.match.POS.test( match[0] ) || Expr.match.CHILD.test( match[0] ) ) {
+        return true;
+      }
+      
+      return match;
+    },
+    POS: function(match){
+      match.unshift( true );
+      return match;
+    }
+  },
+  filters: {
+    enabled: function(elem){
+      return elem.disabled === false && elem.type !== "hidden";
+    },
+    disabled: function(elem){
+      return elem.disabled === true;
+    },
+    checked: function(elem){
+      return elem.checked === true;
+    },
+    selected: function(elem){
+      // Accessing this property makes selected-by-default
+      // options in Safari work properly
+      elem.parentNode.selectedIndex;
+      return elem.selected === true;
+    },
+    parent: function(elem){
+      return !!elem.firstChild;
+    },
+    empty: function(elem){
+      return !elem.firstChild;
+    },
+    has: function(elem, i, match){
+      return !!IES( match[3], elem ).length;
+    },
+    header: function(elem){
+      return (/h\d/i).test( elem.nodeName );
+    },
+    text: function(elem){
+      return "text" === elem.type;
+    },
+    radio: function(elem){
+      return "radio" === elem.type;
+    },
+    checkbox: function(elem){
+      return "checkbox" === elem.type;
+    },
+    file: function(elem){
+      return "file" === elem.type;
+    },
+    password: function(elem){
+      return "password" === elem.type;
+    },
+    submit: function(elem){
+      return "submit" === elem.type;
+    },
+    image: function(elem){
+      return "image" === elem.type;
+    },
+    reset: function(elem){
+      return "reset" === elem.type;
+    },
+    button: function(elem){
+      return "button" === elem.type || elem.nodeName.toLowerCase() === "button";
+    },
+    input: function(elem){
+      return (/input|select|textarea|button/i).test(elem.nodeName);
+    }
+  },
+  setFilters: {
+    first: function(elem, i){
+      return i === 0;
+    },
+    last: function(elem, i, match, array){
+      return i === array.length - 1;
+    },
+    even: function(elem, i){
+      return i % 2 === 0;
+    },
+    odd: function(elem, i){
+      return i % 2 === 1;
+    },
+    lt: function(elem, i, match){
+      return i < match[3] - 0;
+    },
+    gt: function(elem, i, match){
+      return i > match[3] - 0;
+    },
+    nth: function(elem, i, match){
+      return match[3] - 0 === i;
+    },
+    eq: function(elem, i, match){
+      return match[3] - 0 === i;
+    }
+  },
+  filter: {
+    PSEUDO: function(elem, match, i, array){
+      var name = match[1], filter = Expr.filters[ name ];
+
+      if ( filter ) {
+        return filter( elem, i, match, array );
+      } else if ( name === "contains" ) {
+        return (elem.textContent || elem.innerText || IES.getText([ elem ]) || "").indexOf(match[3]) >= 0;
+      } else if ( name === "not" ) {
+        var not = match[3];
+
+        for ( var j = 0, l = not.length; j < l; j++ ) {
+          if ( not[j] === elem ) {
+            return false;
+          }
+        }
+
+        return true;
+      } else {
+        IES.error( "Syntax error, unrecognized expression: " + name );
+      }
+    },
+    CHILD: function(elem, match){
+      var type = match[1], node = elem;
+      switch (type) {
+        case 'only':
+        case 'first':
+          while ( (node = node.previousSibling) )  {
+            if ( node.nodeType === 1 ) { 
+              return false; 
+            }
+          }
+          if ( type === "first" ) { 
+            return true; 
+          }
+          node = elem;
+        case 'last':
+          while ( (node = node.nextSibling) )  {
+            if ( node.nodeType === 1 ) { 
+              return false; 
+            }
+          }
+          return true;
+        case 'nth':
+          var first = match[2], last = match[3];
+
+          if ( first === 1 && last === 0 ) {
+            return true;
+          }
+          
+          var doneName = match[0],
+            parent = elem.parentNode;
+  
+          if ( parent && (parent.sizcache !== doneName || !elem.nodeIndex) ) {
+            var count = 0;
+            for ( node = parent.firstChild; node; node = node.nextSibling ) {
+              if ( node.nodeType === 1 ) {
+                node.nodeIndex = ++count;
+              }
+            } 
+            parent.sizcache = doneName;
+          }
+          
+          var diff = elem.nodeIndex - last;
+          if ( first === 0 ) {
+            return diff === 0;
+          } else {
+            return ( diff % first === 0 && diff / first >= 0 );
+          }
+      }
+    },
+    ID: function(elem, match){
+      var node = typeof elem.getAttributeNode !== "undefined" && elem.getAttributeNode("id");
+      return elem.nodeType === 1 && node && node.nodeValue === match;
+    },
+    TAG: function(elem, match){
+      return (match === "*" && elem.nodeType === 1) || elem.nodeName.toLowerCase() === match;
+    },
+    CLASS: function(elem, match){
+      return (" " + (elem.className || elem.getAttribute("class")) + " ")
+        .indexOf( match ) > -1;
+    },
+    ATTR: function(elem, match){
+      var name = match[1],
+        result = Expr.attrHandle[ name ] ?
+          Expr.attrHandle[ name ]( elem ) :
+          elem[ name ] != null ?
+            elem[ name ] :
+            elem.getAttribute( name ),
+        value = result + "",
+        type = match[2],
+        check = match[4];
+
+      return result == null ?
+        type === "!=" :
+        type === "=" ?
+        value === check :
+        type === "*=" ?
+        value.indexOf(check) >= 0 :
+        type === "~=" ?
+        (" " + value + " ").indexOf(check) >= 0 :
+        !check ?
+        value && result !== false :
+        type === "!=" ?
+        value !== check :
+        type === "^=" ?
+        value.indexOf(check) === 0 :
+        type === "$=" ?
+        value.substr(value.length - check.length) === check :
+        type === "|=" ?
+        value === check || value.substr(0, check.length + 1) === check + "-" :
+        false;
+    },
+    POS: function(elem, match, i, array){
+      var name = match[2], filter = Expr.setFilters[ name ];
+
+      if ( filter ) {
+        return filter( elem, i, match, array );
+      }
+    }
+  }
+};
+
+var origPOS = Expr.match.POS,
+  fescape = function(all, num){
+    return "\\" + (num - 0 + 1);
+  };
+
+for ( var type in Expr.match ) {
+  Expr.match[ type ] = new RegExp( Expr.match[ type ].source + (/(?![^\[]*\])(?![^\(]*\))/.source) );
+  Expr.leftMatch[ type ] = new RegExp( /(^(?:.|\r|\n)*?)/.source + Expr.match[ type ].source.replace(/\\(\d+)/g, fescape) );
+}
+
+var makeArray = function(array, results) {
+  array = Array.prototype.slice.call( array, 0 );
+
+  if ( results ) {
+    results.push.apply( results, array );
+    return results;
+  }
+  
+  return array;
+};
+
+// Perform a simple check to determine if the browser is capable of
+// converting a NodeList to an array using builtin methods.
+// Also verifies that the returned array holds DOM nodes
+// (which is not the case in the Blackberry browser)
+try {
+  Array.prototype.slice.call( document.documentElement.childNodes, 0 )[0].nodeType;
+
+// Provide a fallback method if it does not work
+} catch(e){
+  makeArray = function(array, results) {
+    var ret = results || [], i = 0;
+
+    if ( toString.call(array) === "[object Array]" ) {
+      Array.prototype.push.apply( ret, array );
+    } else {
+      if ( typeof array.length === "number" ) {
+        for ( var l = array.length; i < l; i++ ) {
+          ret.push( array[i] );
+        }
+      } else {
+        for ( ; array[i]; i++ ) {
+          ret.push( array[i] );
+        }
+      }
+    }
+
+    return ret;
+  };
+}
+
+var sortOrder = function( a, b ) {
+    if ( !a.sourceIndex || !b.sourceIndex ) {
+      if ( a == b ) {
+        hasDuplicate = true;
+      }
+      return a.sourceIndex ? -1 : 1;
+    }
+
+    var ret = a.sourceIndex - b.sourceIndex;
+    if ( ret === 0 ) {
+      hasDuplicate = true;
+    }
+    return ret;
+};
+
+// Utility function for retreiving the text value of an array of DOM nodes
+IES.getText = function( elems ) {
+  var ret = "", elem;
+
+  for ( var i = 0; elems[i]; i++ ) {
+    elem = elems[i];
+
+    // Get the text from text nodes and CDATA nodes
+    if ( elem.nodeType === 3 || elem.nodeType === 4 ) {
+      ret += elem.nodeValue;
+
+    // Traverse everything else, except comment nodes
+    } else if ( elem.nodeType !== 8 ) {
+      ret += IES.getText( elem.childNodes );
+    }
+  }
+
+  return ret;
+};
+
+function dirNodeCheck( dir, cur, doneName, checkSet, nodeCheck, isXML ) {
+  for ( var i = 0, l = checkSet.length; i < l; i++ ) {
+    var elem = checkSet[i];
+    if ( elem ) {
+      elem = elem[dir];
+      var match = false;
+
+      while ( elem ) {
+        if ( elem.sizcache === doneName ) {
+          match = checkSet[elem.sizset];
+          break;
+        }
+
+        if ( elem.nodeType === 1 && !isXML ){
+          elem.sizcache = doneName;
+          elem.sizset = i;
+        }
+
+        if ( elem.nodeName.toLowerCase() === cur ) {
+          match = elem;
+          break;
+        }
+
+        elem = elem[dir];
+      }
+
+      checkSet[i] = match;
+    }
+  }
+}
+
+function dirCheck( dir, cur, doneName, checkSet, nodeCheck, isXML ) {
+  for ( var i = 0, l = checkSet.length; i < l; i++ ) {
+    var elem = checkSet[i];
+    if ( elem ) {
+      elem = elem[dir];
+      var match = false;
+
+      while ( elem ) {
+        if ( elem.sizcache === doneName ) {
+          match = checkSet[elem.sizset];
+          break;
+        }
+
+        if ( elem.nodeType === 1 ) {
+          if ( !isXML ) {
+            elem.sizcache = doneName;
+            elem.sizset = i;
+          }
+          if ( typeof cur !== "string" ) {
+            if ( elem === cur ) {
+              match = true;
+              break;
+            }
+
+          } else if ( IES.filter( cur, [elem] ).length > 0 ) {
+            match = elem;
+            break;
+          }
+        }
+
+        elem = elem[dir];
+      }
+
+      checkSet[i] = match;
+    }
+  }
+}
+
+IES.contains = function(a, b) {
+  return a !== b && (a.contains ? a.contains(b) : true);
+};
+
+IES.isXML = function(elem){
+  // documentElement is verified for cases where it doesn't yet exist
+  // (such as loading iframes in IE - #4833) 
+  var documentElement = (elem ? elem.ownerDocument || elem : 0).documentElement;
+  return documentElement ? documentElement.nodeName !== "HTML" : false;
+};
+
+var posProcess = function(selector, context){
+  var tmpSet = [], later = "", match,
+    root = context.nodeType ? [context] : context;
+
+  // Position selectors must be done after the filter
+  // And so must :not(positional) so we move all PSEUDOs to the end
+  while ( (match = Expr.match.PSEUDO.exec( selector )) ) {
+    later += match[0];
+    selector = selector.replace( Expr.match.PSEUDO, "" );
+  }
+
+  selector = Expr.relative[selector] ? selector + "*" : selector;
+
+  for ( var i = 0, l = root.length; i < l; i++ ) {
+    IES( selector, root[i], tmpSet );
+  }
+
+  return IES.filter( later, tmpSet );
+};
+
+// EXPOSE
+
+window.IES = IES;
+$wnd.IES = IES;
+
+})();
+    
+  }-*/;
+  
+
+  private static native JsArray<Element> select(String selector, Node context, JsArray<Element> results, JsArray<Element> seed) /*-{
+    return $wnd.IES(selector, context, results, seed);
+  }-*/;
+  
+  static boolean initialized = false;
+  
+  public SelectorEngineSizzleIE() {
+    if (!initialized) {
+      initialize();
+    }
+  }
+  
+  public NodeList<Element> select(String selector, Node context) {
+    JsArray<Element> results = JavaScriptObject.createArray().cast();
+    return select(selector, context, results, null).cast();
+  }
+}
index 3051374824bf00d38752e3fde6f70513f9102a57..6549ac5ee11515a0720c5a58dc6e345edc0064d4 100644 (file)
@@ -36,7 +36,7 @@ public class SelectorGeneratorNativeIE8 extends SelectorGeneratorJS {
       super.generateMethodBody(sw, method, treeLogger, hasContext);\r
     } else {\r
       sw.println("return "\r
-          + wrap(method, "querySelectorAllIE8(\"" + selector + "\", root)") + ";");\r
+          + wrap(method, "querySelectorAll(\"" + selector + "\", root)") + ";");\r
     }\r
   }\r
 \r
index 5c7d1ac8a8763d48ad9c0618d12db4bb40d59aaf..2cf892638dd950b86a4acd699ba04d129a2edc11 100644 (file)
@@ -172,6 +172,10 @@ public class GQueryCoreTest extends GWTTestCase {
     assertEquals(4, $("span", e).size());
     assertEquals(2, $("span.target", e).size());
     assertHtmlEquals(content, $(e).html());
+    
+    $(e).html("<span>a</span><span>b</span>");
+    $("span").append("<div>c</div>");
+    assertHtmlEquals("<span>a<div>c</div></span><span>b<div>c</div></span>", $(e).html());
   }
   
   public void testEach() {