diff options
Diffstat (limited to 'gwtquery-core/src')
8 files changed, 120 insertions, 21 deletions
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 5e62abbd..41f9a685 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 @@ -108,4 +108,16 @@ public class SelectorEngine { return r;
}
}
+
+ protected Node root = Document.get();
+
+ public void setRoot(Node root) {
+ assert root != null;
+ this.root = root;
+ }
+
+ public Node getRoot() {
+ return root;
+ }
+
}
diff --git a/gwtquery-core/src/main/java/com/google/gwt/query/client/Selectors.java b/gwtquery-core/src/main/java/com/google/gwt/query/client/Selectors.java index 759c6dae..2118668a 100644 --- a/gwtquery-core/src/main/java/com/google/gwt/query/client/Selectors.java +++ b/gwtquery-core/src/main/java/com/google/gwt/query/client/Selectors.java @@ -15,10 +15,16 @@ */
package com.google.gwt.query.client;
+import com.google.gwt.dom.client.Node;
+
/**
* Tagging interface used to generate compile time selectors.
*/
public interface Selectors {
DeferredGQuery[] getAllSelectors();
+
+ public void setRoot(Node node);
+
+ public Node getRoot();
}
diff --git a/gwtquery-core/src/main/java/com/google/gwt/query/rebind/SelectorGeneratorBase.java b/gwtquery-core/src/main/java/com/google/gwt/query/rebind/SelectorGeneratorBase.java index dbe0d119..e69d41ff 100644 --- a/gwtquery-core/src/main/java/com/google/gwt/query/rebind/SelectorGeneratorBase.java +++ b/gwtquery-core/src/main/java/com/google/gwt/query/rebind/SelectorGeneratorBase.java @@ -88,23 +88,16 @@ public abstract class SelectorGeneratorBase extends Generator { sw.indent();
Selector sel = method.getAnnotation(Selector.class);
- // short circuit #foo
if (sel != null && sel.value().matches("^#\\w+$")) {
- sw.println("return " + wrap(method, "JSArray.create(((Document)"
- + (hasContext ? "root" : "(Node)Document.get()")
- + ").getElementById(\"" + sel.value().substring(1) + "\"))") + ";");
+ // short circuit #foo
+ sw.println("return " + wrap(method, "JSArray.create(((Document)root).getElementById(\"" + sel.value().substring(1) + "\"))") + ";");
} else if (sel != null && sel.value().matches("^\\w+$")) {
// short circuit FOO
- sw.println("return " + wrap(method, "JSArray.create(((Element)"
- + (hasContext ? "root" : "(Node)Document.get()")
- + ").getElementsByTagName(\"" + sel.value() + "\"))") + ";");
+ sw.println("return " + wrap(method, "JSArray.create(((Element)root).getElementsByTagName(\"" + sel.value() + "\"))") + ";");
} else if (sel != null && sel.value().matches("^\\.\\w+$")
&& hasGetElementsByClassName()) {
// short circuit .foo for browsers with native getElementsByClassName
- sw.println("return " + wrap(method,
- "JSArray.create(getElementsByClassName(\"" + sel.value().substring(1)
- + "\", " + (hasContext ? "root" : "(Node)Document.get()") + "))")
- + ";");
+ sw.println("return " + wrap(method, "JSArray.create(getElementsByClassName(\"" + sel.value().substring(1) + "\", root))") + ";");
} else {
generateMethodBody(sw, method, logger, hasContext);
}
diff --git a/gwtquery-core/src/main/java/com/google/gwt/query/rebind/SelectorGeneratorJS.java b/gwtquery-core/src/main/java/com/google/gwt/query/rebind/SelectorGeneratorJS.java index 8e0b9c5e..3cb7010e 100644 --- a/gwtquery-core/src/main/java/com/google/gwt/query/rebind/SelectorGeneratorJS.java +++ b/gwtquery-core/src/main/java/com/google/gwt/query/rebind/SelectorGeneratorJS.java @@ -36,9 +36,6 @@ public class SelectorGeneratorJS extends SelectorGeneratorBase { throws UnableToCompleteException {
String selector = method.getAnnotation(Selector.class).value();
- if (!hasContext) {
- sw.println("Node root = Document.get();");
- }
sw.println("return " + wrap(method,
"new SelectorEngine().select(\"" + selector + "\", root)") + ";");
diff --git a/gwtquery-core/src/main/java/com/google/gwt/query/rebind/SelectorGeneratorJSOptimal.java b/gwtquery-core/src/main/java/com/google/gwt/query/rebind/SelectorGeneratorJSOptimal.java index b8f88481..064f3712 100644 --- a/gwtquery-core/src/main/java/com/google/gwt/query/rebind/SelectorGeneratorJSOptimal.java +++ b/gwtquery-core/src/main/java/com/google/gwt/query/rebind/SelectorGeneratorJSOptimal.java @@ -72,9 +72,6 @@ public class SelectorGeneratorJSOptimal extends SelectorGeneratorBase { throws UnableToCompleteException {
String selector = method.getAnnotation(Selector.class).value();
- if (!hasContext) {
- sw.println("Node root = Document.get();");
- }
sw.println("return " + wrap(method,
"new SelectorEngine().select(\"" + selector + "\", root)") + ";");
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 b8cf6193..aabbfd44 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 @@ -39,6 +39,7 @@ public class SelectorGeneratorNative extends SelectorGeneratorBase { TreeLogger treeLogger, boolean hasContext)
throws UnableToCompleteException {
String selector = method.getAnnotation(Selector.class).value();
+ System.out.println("generateMethodBody " + method + " " + hasContext);
if (!hasContext) {
sw.println(
"return " + wrap(method, "querySelectorAll(\"" + selector + "\"")
diff --git a/gwtquery-core/src/main/java/com/google/gwt/query/rebind/SelectorGeneratorXPath.java b/gwtquery-core/src/main/java/com/google/gwt/query/rebind/SelectorGeneratorXPath.java index 266a2cbb..37c4c25b 100644 --- a/gwtquery-core/src/main/java/com/google/gwt/query/rebind/SelectorGeneratorXPath.java +++ b/gwtquery-core/src/main/java/com/google/gwt/query/rebind/SelectorGeneratorXPath.java @@ -165,9 +165,6 @@ public class SelectorGeneratorXPath extends SelectorGeneratorBase { }
}
- if (!hasContext) {
- sw.println("Node root = Document.get();");
- }
sw.println("return " + wrap(method,
"SelectorEngine.xpathEvaluate(\"" + xPathExpression + "\", root)")
+ ";");
@@ -331,4 +328,4 @@ public class SelectorGeneratorXPath extends SelectorGeneratorBase { }
return xpath;
}
-}
\ 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 new file mode 100644 index 00000000..dfaaf758 --- /dev/null +++ b/gwtquery-core/src/test/java/com/google/gwt/query/client/GQuerySelectorsTest.java @@ -0,0 +1,96 @@ +/* + * Copyright 2009 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; + +import static com.google.gwt.query.client.GQuery.$; + +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.junit.client.GWTTestCase; +import com.google.gwt.user.client.ui.HTML; +import com.google.gwt.user.client.ui.RootPanel; + +public class GQuerySelectorsTest extends GWTTestCase { + + public String getModuleName() { + return "com.google.gwt.query.Query"; + } + + static Element e = null; + static HTML testPanel = null; + + public void gwtSetUp() { + if (e == null) { + testPanel = new HTML(); + RootPanel.get().add(testPanel); + e = testPanel.getElement(); + e.setId("select-tst"); + } else { + e.setInnerHTML(""); + } + } + + public interface TestSelectors extends Selectors { + @Selector(".target") + public GQuery target(); + + @Selector(".branchA") + public GQuery branchA(); + + @Selector(".branchB") + public GQuery branchB(); + + @Selector(".target") + public GQuery target(Node n); + + @Selector(".branchA") + public GQuery branchA(Node n); + + @Selector(".branchB") + public GQuery branchB(Node n); + } + + public void testSelectorsWithContext() { + $(e).append("<div class='branchA'><div class='target'>branchA target</div></div>" + + "<div class='branchB'><div class='target'>branchB target</div></div>"); + + TestSelectors selectors = GWT.create(TestSelectors.class); + + assertEquals(2, selectors.target().length()); + Element branchA = selectors.branchA().get(0); + Element branchB = selectors.branchB().get(0); + assertNotNull(selectors.branchA().get(0)); + assertNotNull(selectors.branchB().get(0)); + + assertEquals(2, selectors.target(RootPanel.getBodyElement()).length()); + branchA = selectors.branchA(RootPanel.getBodyElement()).get(0); + branchB = selectors.branchB(RootPanel.getBodyElement()).get(0); + assertNotNull(branchA); + assertNotNull(branchB); + assertEquals("branchA target", selectors.target(branchA).text()); + assertEquals("branchB target", selectors.target(branchB).text()); + + selectors.setRoot(branchA); + assertEquals(1, selectors.target().length()); + assertEquals("branchA target", selectors.target().text()); + + selectors.setRoot(branchB); + assertEquals(1, selectors.target().length()); + assertEquals("branchB target", selectors.target().text()); + } + +} |