diff options
3 files changed, 47 insertions, 6 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..2d5eb40b 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<GQuery, LazyGQuery> { * for the root element). */ public GQuery parents() { - return parentsUntil(null); + return parentsUntil((String) null); } /** @@ -3376,18 +3376,41 @@ public class GQuery implements Lazy<GQuery, LazyGQuery> { /** * 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) { + public GQuery parentsUntil(final String selector) { + return parentsUntil(new Predicate(){ + @Override + public <T> boolean f(T e, int index) { + return selector != null && $(e).is(selector); + } + }); + } + + /** + * Get the ancestors of each element in the current set of matched elements, up to but not + * including the node. + */ + public GQuery parentsUntil(final Node node) { + return parentsUntil(new Predicate() { + @Override + public <T> boolean f(T e, int index) { + return node != null && e == node; + } + }); + } + + private GQuery parentsUntil(Predicate predicate) { JsNodeArray result = JsNodeArray.create(); for (Element e : elements) { + int i = 0; Node par = e.getParentNode(); while (par != null && par != document) { - if (selector != null && $(par).is(selector)) { + if (predicate.f(par, i)) { break; } result.addNode(par); par = par.getParentNode(); + i++; } } return new GQuery(unique(result)).setPreviousObject(this); 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,11 +1728,16 @@ public interface LazyGQuery<T> extends LazyBase<T>{ /** * 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<T> 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<T> 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 * values for margins, borders and padding. This method only works with visible elements. 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..ca6e6a08 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 @@ -667,7 +667,7 @@ public class GQueryCoreTestGwt extends GWTTestCase { //parentsUntil() content = "<div id='mainDiv'><div id='subDiv1' class='subDiv'><div id='subSubDiv1'><p id='p1'>child1</p></div></div><div id='subDiv2' class='subDiv'><div id='subSubDiv2'><p id='p2'>child2</p></div></div></div>"; $(e).html(content); - $("p",e).parentsUntil("#mainDiv").css(CSS.COLOR.with(RGBColor.RED)); + $("p", e).parentsUntil("#mainDiv").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)); @@ -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 = "<div id='mainDiv'><div id='subDiv1' class='subDiv'><div id='subSubDiv1'><p id='p1'>child1</p></div></div><div id='subDiv2' class='subDiv'><div id='subSubDiv2'><p id='p2'>child2</p></div></div></div>"; + $(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 = "<form><input type=\"checkbox\"></form>"; $(e).html(content); |