aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorManolo Carrasco <manolo@apache.org>2011-08-27 21:04:49 +0000
committerManolo Carrasco <manolo@apache.org>2011-08-27 21:04:49 +0000
commit69febf399bf93977894bdc98d725ba88b5c56941 (patch)
tree65408daf871b030c9421e70d30ccfa56602c3c70
parentc80ab43e0358773cc5c70ec2190dc999c6234cc9 (diff)
downloadgwtquery-69febf399bf93977894bdc98d725ba88b5c56941.tar.gz
gwtquery-69febf399bf93977894bdc98d725ba88b5c56941.zip
implement has() method
-rw-r--r--gwtquery-core/src/main/java/com/google/gwt/query/client/GQuery.java27
-rw-r--r--gwtquery-core/src/main/java/com/google/gwt/query/client/LazyGQuery.java12
-rw-r--r--gwtquery-core/src/main/java/com/google/gwt/query/client/impl/SelectorEngine.java4
-rw-r--r--gwtquery-core/src/test/java/com/google/gwt/query/client/GQueryCoreTest.java21
4 files changed, 62 insertions, 2 deletions
diff --git a/gwtquery-core/src/main/java/com/google/gwt/query/client/GQuery.java b/gwtquery-core/src/main/java/com/google/gwt/query/client/GQuery.java
index d9a05ef0..96473dbd 100644
--- a/gwtquery-core/src/main/java/com/google/gwt/query/client/GQuery.java
+++ b/gwtquery-core/src/main/java/com/google/gwt/query/client/GQuery.java
@@ -1864,9 +1864,7 @@ public class GQuery implements Lazy<GQuery, LazyGQuery> {
* filters at once.
*/
public GQuery filter(String... filters) {
-
JsNodeArray array = JsNodeArray.create();
-
for (String f : filters) {
for (Element e : elements) {
boolean ghostParent = false;
@@ -1993,6 +1991,31 @@ public class GQuery implements Lazy<GQuery, LazyGQuery> {
public GQuery gt(int pos) {
return $(slice(pos + 1, -1));
}
+
+
+ /**
+ * Reduce the set of matched elements to those that have a descendant
+ * that matches the selector.
+ */
+ public GQuery has(final String selector) {
+ return filter(new Predicate(){
+ public boolean f(Element e, int index) {
+ return !$(selector, e).isEmpty();
+ }
+ });
+ }
+
+ /**
+ * Reduce the set of matched elements to those that have a descendant
+ * that matches the Element.
+ */
+ public GQuery has(final Element elem) {
+ return filter(new Predicate(){
+ public boolean f(Element e, int index) {
+ return engine.contains(e, elem);
+ }
+ });
+ }
/**
* Returns true any of the specified classes are present on any of the matched
diff --git a/gwtquery-core/src/main/java/com/google/gwt/query/client/LazyGQuery.java b/gwtquery-core/src/main/java/com/google/gwt/query/client/LazyGQuery.java
index dfa090fa..b2d1d37e 100644
--- a/gwtquery-core/src/main/java/com/google/gwt/query/client/LazyGQuery.java
+++ b/gwtquery-core/src/main/java/com/google/gwt/query/client/LazyGQuery.java
@@ -1083,6 +1083,18 @@ public interface LazyGQuery<T> extends LazyBase<T>{
LazyGQuery<T> gt(int pos);
/**
+ * Reduce the set of matched elements to those that have a descendant
+ * that matches the selector.
+ */
+ LazyGQuery<T> has(String selector);
+
+ /**
+ * Reduce the set of matched elements to those that have a descendant
+ * that matches the Element.
+ */
+ LazyGQuery<T> has(Element elem);
+
+ /**
* Returns true any of the specified classes are present on any of the matched
* elements.
*/
diff --git a/gwtquery-core/src/main/java/com/google/gwt/query/client/impl/SelectorEngine.java b/gwtquery-core/src/main/java/com/google/gwt/query/client/impl/SelectorEngine.java
index c013a226..46de0000 100644
--- a/gwtquery-core/src/main/java/com/google/gwt/query/client/impl/SelectorEngine.java
+++ b/gwtquery-core/src/main/java/com/google/gwt/query/client/impl/SelectorEngine.java
@@ -139,6 +139,10 @@ public class SelectorEngine implements HasSelector {
}
}
+ public native boolean contains(Element a, Element b) /*-{
+ return a.contains ? a != b && a.contains(b) : !!(a.compareDocumentPosition(b) & 16)
+ }-*/;
+
public void setRoot(Node root) {
assert root != null;
this.root = root;
diff --git a/gwtquery-core/src/test/java/com/google/gwt/query/client/GQueryCoreTest.java b/gwtquery-core/src/test/java/com/google/gwt/query/client/GQueryCoreTest.java
index b7632fd3..b7cd195c 100644
--- a/gwtquery-core/src/test/java/com/google/gwt/query/client/GQueryCoreTest.java
+++ b/gwtquery-core/src/test/java/com/google/gwt/query/client/GQueryCoreTest.java
@@ -1744,5 +1744,26 @@ public class GQueryCoreTest extends GWTTestCase {
test.addClass("test");
test.removeClass("test");
}
+
+ public void testHas() {
+ $(e).html("<ul>"
+ +"<li>list item 1</li>"
+ +"<li id='l2'>list item 2"
+ +" <ul>"
+ +" <li>list item 2-a</li>"
+ +" <li>list item 2-b</li>"
+ +" </ul>"
+ +"</li>"
+ +"<li id='l3'>list item 3 <span>span</span>"
+ +"</li>"
+ +"<li>list item 4</li>"
+ +"</ul>");
+ assertEquals("", $("#l2").css("background-color"));
+ $("li", e).has("ul").css("background-color", "red");
+ assertEquals("red", $("#l2").css("background-color"));
+
+ Element span = $("span", e).get(0);
+ assertEquals("l3", $("li", e).has(span).id());
+ }
}