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 From b363dcea923226179d067f987c462d89da3733fa Mon Sep 17 00:00:00 2001 From: Jean-Christophe Lariviere Date: Fri, 12 Dec 2014 11:01:00 -0500 Subject: Code review fixes --- .../java/com/google/gwt/query/client/GQuery.java | 35 ++++++++++++---------- 1 file changed, 20 insertions(+), 15 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 a589f3d5..261991c2 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 @@ -3377,35 +3377,40 @@ 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(); - for (Element e : elements) { - Node par = e.getParentNode(); - while (par != null && par != document) { - if (selector != null && $(par).is(selector)) { - break; - } - result.addNode(par); - par = par.getParentNode(); + public GQuery parentsUntil(final String selector) { + return parentsUntil(new Predicate(){ + @Override + public boolean f(Element e, int index) { + return $(e).is(selector); } - } - 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. + * including the node. */ - public GQuery parentsUntil(Node selector) { + public GQuery parentsUntil(final Node node) { + return parentsUntil(new Predicate() { + @Override + public boolean f(Element e, int index) { + return 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 == selector) { + if (!predicate.f(par, i)) { break; } result.addNode(par); par = par.getParentNode(); + i++; } } return new GQuery(unique(result)).setPreviousObject(this); -- cgit v1.2.3 From b22f696ac16fccd0953d6a9f018d46ccd8084aa7 Mon Sep 17 00:00:00 2001 From: Jean-Christophe Lariviere Date: Fri, 12 Dec 2014 12:09:07 -0500 Subject: Fix inverted condition --- gwtquery-core/src/main/java/com/google/gwt/query/client/GQuery.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) 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 261991c2..54b84b5e 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 @@ -3405,7 +3405,7 @@ public class GQuery implements Lazy { int i = 0; Node par = e.getParentNode(); while (par != null && par != document) { - if (!predicate.f(par, i)) { + if (predicate.f(par, i)) { break; } result.addNode(par); -- cgit v1.2.3 From 22179c8b50078e54d5629b5d99fdf6e34e0a5e5f Mon Sep 17 00:00:00 2001 From: Jean-Christophe Lariviere Date: Fri, 12 Dec 2014 12:10:21 -0500 Subject: Fix spacing --- .../src/test/java/com/google/gwt/query/client/GQueryCoreTestGwt.java | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) 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 176b5cb8..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 = "

child1

child2

"; $(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)); @@ -684,7 +684,7 @@ public class GQueryCoreTestGwt extends GWTTestCase { content = "

child1

child2

"; $(e).html(content); Element node = $("#mainDiv", e).get(0); - $("p",e).parentsUntil(node).css(CSS.COLOR.with(RGBColor.RED)); + $("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)); -- cgit v1.2.3 From 2a1b7c2425124f0975fe10412039f424475ea9aa Mon Sep 17 00:00:00 2001 From: Jean-Christophe Lariviere Date: Fri, 12 Dec 2014 13:40:50 -0500 Subject: Fix missing condition --- gwtquery-core/src/main/java/com/google/gwt/query/client/GQuery.java | 4 ++-- 1 file changed, 2 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 54b84b5e..aa82446d 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 @@ -3381,7 +3381,7 @@ public class GQuery implements Lazy { return parentsUntil(new Predicate(){ @Override public boolean f(Element e, int index) { - return $(e).is(selector); + return selector != null && $(e).is(selector); } }); } @@ -3394,7 +3394,7 @@ public class GQuery implements Lazy { return parentsUntil(new Predicate() { @Override public boolean f(Element e, int index) { - return e == node; + return node != null && e == node; } }); } -- cgit v1.2.3 From 6190d87ce7b8c242c306a12a82afef923c255609 Mon Sep 17 00:00:00 2001 From: Jean-Christophe Lariviere Date: Fri, 12 Dec 2014 14:03:01 -0500 Subject: Use right f() in predicate --- gwtquery-core/src/main/java/com/google/gwt/query/client/GQuery.java | 4 ++-- 1 file changed, 2 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 aa82446d..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 @@ -3380,7 +3380,7 @@ public class GQuery implements Lazy { public GQuery parentsUntil(final String selector) { return parentsUntil(new Predicate(){ @Override - public boolean f(Element e, int index) { + public boolean f(T e, int index) { return selector != null && $(e).is(selector); } }); @@ -3393,7 +3393,7 @@ public class GQuery implements Lazy { public GQuery parentsUntil(final Node node) { return parentsUntil(new Predicate() { @Override - public boolean f(Element e, int index) { + public boolean f(T e, int index) { return node != null && e == node; } }); -- cgit v1.2.3