aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorJean-Christophe Lariviere <jclariviere@arcbees.com>2014-12-11 16:37:06 -0500
committerJean-Christophe Lariviere <jclariviere@arcbees.com>2014-12-11 16:37:06 -0500
commitbe0e45c7fe64132f5155c8ae06431d768a4e7f0b (patch)
treea2a590c737fc6267cc3ba13b0bc8e617417fec73
parent09516bca8bb56a7393ad6cc4aa776ee275d298e1 (diff)
downloadgwtquery-be0e45c7fe64132f5155c8ae06431d768a4e7f0b.tar.gz
gwtquery-be0e45c7fe64132f5155c8ae06431d768a4e7f0b.zip
Overload parentsUntil() to match a node
-rw-r--r--gwtquery-core/src/main/java/com/google/gwt/query/client/GQuery.java22
-rw-r--r--gwtquery-core/src/main/java/com/google/gwt/query/client/LazyGQuery.java7
-rw-r--r--gwtquery-core/src/test/java/com/google/gwt/query/client/GQueryCoreTestGwt.java13
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<GQuery, LazyGQuery> {
* for the root element).
*/
public GQuery parents() {
- return parentsUntil(null);
+ return parentsUntil((String) null);
}
/**
@@ -3376,7 +3376,6 @@ 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) {
JsNodeArray result = JsNodeArray.create();
@@ -3394,6 +3393,25 @@ 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(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
* values for margins, borders and padding. This method only works with visible elements.
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..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 = "<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);