From: Manolo Carrasco Date: Wed, 2 Jun 2010 14:12:36 +0000 (+0000) Subject: native engine uses xpath selector for css selectors not supported by the native css... X-Git-Tag: release-1.3.2~704 X-Git-Url: https://source.dussan.org/?a=commitdiff_plain;h=ef30c9b034280665532efc2c1f5e8a1cc627f295;p=gwtquery.git native engine uses xpath selector for css selectors not supported by the native css selector --- diff --git a/gwtquery-core/src/main/java/com/google/gwt/query/client/SelectorEngine.java b/gwtquery-core/src/main/java/com/google/gwt/query/client/SelectorEngine.java index acf2437f..367cd887 100644 --- a/gwtquery-core/src/main/java/com/google/gwt/query/client/SelectorEngine.java +++ b/gwtquery-core/src/main/java/com/google/gwt/query/client/SelectorEngine.java @@ -49,10 +49,6 @@ public class SelectorEngine { return s1 || s2; }-*/; - public static native NodeList querySelectorAll(String selector) /*-{ - return $doc.querySelectorAll(selector); - }-*/; - public static native NodeList querySelectorAll(String selector, Node ctx) /*-{ return ctx.querySelectorAll(selector); diff --git a/gwtquery-core/src/main/java/com/google/gwt/query/client/impl/SelectorEngineNative.java b/gwtquery-core/src/main/java/com/google/gwt/query/client/impl/SelectorEngineNative.java index 236a6b33..477e4287 100644 --- a/gwtquery-core/src/main/java/com/google/gwt/query/client/impl/SelectorEngineNative.java +++ b/gwtquery-core/src/main/java/com/google/gwt/query/client/impl/SelectorEngineNative.java @@ -24,9 +24,15 @@ import com.google.gwt.query.client.SelectorEngine; * Runtime selector engine implementation for browsers with native * querySelectorAll support. */ -public class SelectorEngineNative extends SelectorEngineImpl { - +public class SelectorEngineNative extends SelectorEngineCssToXPath { + + public static String NATIVE_EXCEPTIONS_REGEXP = ".*(:contains|!=).*"; + public NodeList select(String selector, Node ctx) { - return SelectorEngine.querySelectorAll(selector, ctx); + if (selector.matches(NATIVE_EXCEPTIONS_REGEXP)) { + return super.select(selector, ctx); + } else { + return SelectorEngine.querySelectorAll(selector, ctx); + } } } diff --git a/gwtquery-core/src/main/java/com/google/gwt/query/rebind/SelectorGeneratorNative.java b/gwtquery-core/src/main/java/com/google/gwt/query/rebind/SelectorGeneratorNative.java index 4955cbb9..7efaee11 100644 --- a/gwtquery-core/src/main/java/com/google/gwt/query/rebind/SelectorGeneratorNative.java +++ b/gwtquery-core/src/main/java/com/google/gwt/query/rebind/SelectorGeneratorNative.java @@ -19,32 +19,34 @@ import com.google.gwt.core.ext.TreeLogger; import com.google.gwt.core.ext.UnableToCompleteException; import com.google.gwt.core.ext.typeinfo.JMethod; import com.google.gwt.query.client.Selector; +import com.google.gwt.query.client.impl.SelectorEngineNative; import com.google.gwt.user.rebind.SourceWriter; /** * Compile time selector generator which delegates to native browser methods. */ -public class SelectorGeneratorNative extends SelectorGeneratorBase { - - protected String getImplSuffix() { - return "Native" + super.getImplSuffix(); - } +public class SelectorGeneratorNative extends SelectorGeneratorCssToXPath { @Override - protected boolean hasGetElementsByClassName() { - return true; - } - protected void generateMethodBody(SourceWriter sw, JMethod method, TreeLogger treeLogger, boolean hasContext) throws UnableToCompleteException { String selector = method.getAnnotation(Selector.class).value(); - if (!hasContext) { - sw.println("return " - + wrap(method, "querySelectorAll(\"" + selector + "\")") + ";"); + if (selector.matches(SelectorEngineNative.NATIVE_EXCEPTIONS_REGEXP)) { + super.generateMethodBody(sw, method, treeLogger, hasContext); } else { sw.println("return " + wrap(method, "querySelectorAll(\"" + selector + "\", root)") + ";"); } } + + @Override + protected String getImplSuffix() { + return "Native" + super.getImplSuffix(); + } + + @Override + protected boolean hasGetElementsByClassName() { + return true; + } } \ No newline at end of file diff --git a/gwtquery-core/src/test/java/com/google/gwt/query/client/GQuerySelectorsTest.java b/gwtquery-core/src/test/java/com/google/gwt/query/client/GQuerySelectorsTest.java index d793780b..33bc316b 100644 --- a/gwtquery-core/src/test/java/com/google/gwt/query/client/GQuerySelectorsTest.java +++ b/gwtquery-core/src/test/java/com/google/gwt/query/client/GQuerySelectorsTest.java @@ -237,6 +237,13 @@ public class GQuerySelectorsTest extends GWTTestCase { executeSelectorEngineTests(selEng); } + public void testSelectorEngineNative() { + SelectorEngineImpl selEng = new SelectorEngineNative(); + if (hasNativeSelector()) { + executeSelectorEngineTests(selEng); + } + } + public void testSelectorEngineXpath() { SelectorEngineImpl selEng = new SelectorEngineXPath(); executeSelectorEngineTests(selEng); @@ -328,11 +335,12 @@ public class GQuerySelectorsTest extends GWTTestCase { SelectorEngineImpl selNative = new SelectorEngineNative(); assertArrayContains(selector, selSizz.select(selector, elem).getLength(), array); assertArrayContains(selector, selJS.select(selector, elem).getLength(), array); + if (hasNativeSelector()) { + assertArrayContains(selector, selNative.select(selector, elem).getLength(), array); + } assertArrayContains(selector, selXpath.select(selector, elem).getLength(), array); assertArrayContains(selector, selC2X.select(selector, elem).getLength(), array); - if (hasNativeSelector()) { - assertArrayContains(selector, selNative.select(selector, elem), array); - } + } private static native boolean hasNativeSelector() /*-{ @@ -352,7 +360,7 @@ public class GQuerySelectorsTest extends GWTTestCase { assertArrayContains(selEng.select("a[href][lang][class]", e).getLength(), 1); assertArrayContains(selEng.select("*:checked", e).getLength(), 1); assertArrayContains(selEng.select("div .example", e).getLength(), 43); - assertArrayContains(selEng.select("div > div", e).getLength(), 51); + assertArrayContains(selEng.select("div > div", e).getLength(), 51, 52); assertArrayContains(selEng.select("div:not(.example)", e).getLength(), 9, 10); assertArrayContains(selEng.select("div p", e).getLength(), 324); assertArrayContains(selEng.select("div p a", e).getLength(), 85, 84);