From be0e45c7fe64132f5155c8ae06431d768a4e7f0b Mon Sep 17 00:00:00 2001 From: Jean-Christophe Lariviere Date: Thu, 11 Dec 2014 16:37:06 -0500 Subject: Overload parentsUntil() to match a node --- .../java/com/google/gwt/query/client/GQuery.java | 22 ++++++++++++++++++++-- .../com/google/gwt/query/client/LazyGQuery.java | 7 ++++++- .../google/gwt/query/client/GQueryCoreTestGwt.java | 13 +++++++++++++ 3 files changed, 39 insertions(+), 3 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 b82a2a3c..a589f3d5 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 @@ -3361,7 +3361,7 @@ public class GQuery implements Lazy { * for the root element). */ public GQuery parents() { - return parentsUntil(null); + return parentsUntil((String) null); } /** @@ -3376,7 +3376,6 @@ public class GQuery implements Lazy { /** * Get the ancestors of each element in the current set of matched elements, up to but not * including the element matched by the selector. - * */ public GQuery parentsUntil(String selector) { JsNodeArray result = JsNodeArray.create(); @@ -3393,6 +3392,25 @@ public class GQuery implements Lazy { return new GQuery(unique(result)).setPreviousObject(this); } + /** + * Get the ancestors of each element in the current set of matched elements, up to but not + * including the element matched by the selector. + */ + public GQuery parentsUntil(Node selector) { + JsNodeArray result = JsNodeArray.create(); + for (Element e : elements) { + Node par = e.getParentNode(); + while (par != null && par != document) { + if (selector != null && par == selector) { + break; + } + result.addNode(par); + par = par.getParentNode(); + } + } + return new GQuery(unique(result)).setPreviousObject(this); + } + /** * Gets the top and left position of an element relative to its offset parent. The returned object * contains two Integer properties, top and left. For accurate calculations make sure to use pixel 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 7a4ab3ad..46d9d9ee 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 @@ -1728,10 +1728,15 @@ public interface LazyGQuery extends LazyBase{ /** * Get the ancestors of each element in the current set of matched elements, up to but not * including the element matched by the selector. - * */ LazyGQuery parentsUntil(String selector); + /** + * Get the ancestors of each element in the current set of matched elements, up to but not + * including the element matched by the selector. + */ + LazyGQuery parentsUntil(Node selector); + /** * Gets the top and left position of an element relative to its offset parent. The returned object * contains two Integer properties, top and left. For accurate calculations make sure to use pixel diff --git a/gwtquery-core/src/test/java/com/google/gwt/query/client/GQueryCoreTestGwt.java b/gwtquery-core/src/test/java/com/google/gwt/query/client/GQueryCoreTestGwt.java index 763655da..176b5cb8 100644 --- a/gwtquery-core/src/test/java/com/google/gwt/query/client/GQueryCoreTestGwt.java +++ b/gwtquery-core/src/test/java/com/google/gwt/query/client/GQueryCoreTestGwt.java @@ -680,6 +680,19 @@ public class GQueryCoreTestGwt extends GWTTestCase { assertEquals("red", $("#subDiv1", e).css(CSS.COLOR, false)); assertEquals("yellow", $("#subSubDiv1", e).css(CSS.COLOR, false)); + //parentsUntil() + content = "

child1

child2

"; + $(e).html(content); + Element node = $("#mainDiv", e).get(0); + $("p",e).parentsUntil(node).css(CSS.COLOR.with(RGBColor.RED)); + assertEquals("red", $("#subDiv1", e).css(CSS.COLOR, false)); + assertEquals("red", $("#subSubDiv1", e).css(CSS.COLOR, false)); + assertEquals("red", $("#subDiv2", e).css(CSS.COLOR, false)); + assertEquals("red", $("#subSubDiv2", e).css(CSS.COLOR, false)); + assertEquals("", $("#mainDiv", e).css(CSS.COLOR, false)); + assertEquals("", $("#p1", e).css(CSS.COLOR, false)); + assertEquals("", $("#p2", e).css(CSS.COLOR, false)); + // is() content = "
"; $(e).html(content); -- cgit v1.2.3