From 69febf399bf93977894bdc98d725ba88b5c56941 Mon Sep 17 00:00:00 2001 From: Manolo Carrasco Date: Sat, 27 Aug 2011 21:04:49 +0000 Subject: [PATCH] implement has() method --- .../com/google/gwt/query/client/GQuery.java | 27 +++++++++++++++++-- .../google/gwt/query/client/LazyGQuery.java | 12 +++++++++ .../gwt/query/client/impl/SelectorEngine.java | 4 +++ .../gwt/query/client/GQueryCoreTest.java | 21 +++++++++++++++ 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 { * 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 { 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 @@ -1082,6 +1082,18 @@ public interface LazyGQuery extends LazyBase{ */ LazyGQuery gt(int pos); + /** + * Reduce the set of matched elements to those that have a descendant + * that matches the selector. + */ + LazyGQuery has(String selector); + + /** + * Reduce the set of matched elements to those that have a descendant + * that matches the Element. + */ + LazyGQuery 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("
    " + +"
  • list item 1
  • " + +"
  • list item 2" + +"
      " + +"
    • list item 2-a
    • " + +"
    • list item 2-b
    • " + +"
    " + +"
  • " + +"
  • list item 3 span" + +"
  • " + +"
  • list item 4
  • " + +"
"); + 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()); + } } -- 2.39.5