From c3faf08ceb0572a362413e2028262e0f5ca81091 Mon Sep 17 00:00:00 2001 From: Julien Dramaix Date: Sat, 28 Jul 2012 22:02:16 +0000 Subject: [PATCH] implement other signatures of nextAll and nextUntil methods --- .../com/google/gwt/query/client/GQuery.java | 90 ++++++++++++++++--- .../gwt/query/client/GQueryCoreTestGwt.java | 32 ++++++- 2 files changed, 109 insertions(+), 13 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 9979d8e9..63bfe9f6 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 @@ -769,15 +769,17 @@ public class GQuery implements Lazy { } private void allNextSiblingElements(Element firstChildElement, - JsNodeArray result, Element elem, String untilSelector) { - while (firstChildElement != null) { + JsNodeArray result, Element elem, GQuery until, String filterSelector) { + + while (firstChildElement != null) { - if (untilSelector != null && $(firstChildElement).is(untilSelector)){ + if (until != null && until.index(firstChildElement) != -1){ return; } - if (firstChildElement != elem) { - result.addNode(firstChildElement); + if (firstChildElement != elem + && (filterSelector == null || $(firstChildElement).is(filterSelector))) { + result.addNode(firstChildElement); } firstChildElement = firstChildElement.getNextSiblingElement(); } @@ -1216,7 +1218,7 @@ public class GQuery implements Lazy { public GQuery children() { JsNodeArray result = JsNodeArray.create(); for (Element e : elements) { - allNextSiblingElements(e.getFirstChildElement(), result, null, null); + allNextSiblingElements(e.getFirstChildElement(), result, null, null, null); } return new GQuery(unique(result)); } @@ -2852,12 +2854,21 @@ public class GQuery implements Lazy { } /** - * Find all sibling elements after the current element. + * Get all following siblings of each element in the set of matched + * elements. */ public GQuery nextAll() { + return nextAll(null); + } + + /** + * Get all following siblings of each element in the set of matched + * elements, filtered by a selector. + */ + public GQuery nextAll(String filter) { JsNodeArray result = JsNodeArray.create(); for (Element e : elements) { - allNextSiblingElements(e.getNextSiblingElement(), result, null, null); + allNextSiblingElements(e.getNextSiblingElement(), result, null, null, filter); } return pushStack(unique(result), "nextAll", getSelector()); } @@ -2870,13 +2881,72 @@ public class GQuery implements Lazy { * @return */ public GQuery nextUntil(String selector) { + return nextUntil($(selector), null); + } + + /** + * Get all following siblings of each element up to but not including the + * element matched by the selector, filtered by a selector. + * + * @param selector + * @return + */ + public GQuery nextUntil(String selector, String filter) { + return nextUntil($(selector), filter); + } + + /** + * Get all following siblings of each element up to but not including the + * element matched by the DOM node. + * + * @param selector + * @return + */ + public GQuery nextUntil(Element until) { + return nextUntil($(until), null); + } + + + /** + * Get all following siblings of each element up to but not including the + * element matched by the DOM node, filtered by a selector. + * + * @param selector + * @return + */ + public GQuery nextUntil(Element until, String filter) { + return nextUntil($(until), filter); + } + + + /** + * Get all following siblings of each element up to but not including the + * element matched by the GQuery object. + * + * @param selector + * @return + */ + public GQuery nextUntil(GQuery until) { + return nextUntil(until, null); + } + + + /** + * Get all following siblings of each element up to but not including the + * element matched by the GQuery object, filtered by a selector + * + * @param selector + * @return + */ + public GQuery nextUntil(GQuery until, String filter) { JsNodeArray result = JsNodeArray.create(); for (Element e : elements) { - allNextSiblingElements(e.getNextSiblingElement(), result, null, selector); + allNextSiblingElements(e.getNextSiblingElement(), result, null, until, filter); } return pushStack(unique(result), "nextUntil", getSelector()); } + /** * Removes the specified Element from the set of matched elements. This method * is used to remove a single Element from a jQuery object. @@ -3889,7 +3959,7 @@ public class GQuery implements Lazy { JsNodeArray result = JsNodeArray.create(); for (Element e : elements) { allNextSiblingElements(e.getParentElement().getFirstChildElement(), - result, e, null); + result, e, null, null); } return new GQuery(unique(result)); } 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 b5e467cc..41b0849e 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 @@ -648,18 +648,44 @@ public class GQueryCoreTestGwt extends GWTTestCase { assertHtmlEquals(expected, $("p", e).next(".selected").get(0).getString()); // nextAll() - content = "
  • i1
  • i2
  • i3
  • i4
  • i5
"; - expected = "
  • i4
  • i5
  • "; + content = "
    • i1
    • i2
    • i3
    • i4
    • i5
    "; + expected = "
  • i4
  • i5
  • "; $(e).html(content); assertEquals(2, $("li.third-item", e).nextAll().size()); assertHtmlEquals(expected, $("li.third-item", e).nextAll()); + + expected = "
  • i5
  • "; + assertEquals(1, $("li.third-item", e).nextAll(".last-item").size()); + assertHtmlEquals(expected, $("li.third-item", e).nextAll(".last-item")); // nextUntil() - content = "
    • i1
    • i2
    • i3
    • i4
    • i5
    "; + content = "
    • i1
    • i2
    • i3
    • i4
    • i5
    "; expected = "
  • i4
  • "; $(e).html(content); assertEquals(1, $("li.third-item", e).nextUntil(".five-item").size()); assertHtmlEquals(expected, $("li.third-item", e).nextUntil(".five-item")); + + GQuery nextUntil = $("li.five-item"); + assertEquals(1, $("li.third-item", e).nextUntil(nextUntil).size()); + assertHtmlEquals(expected, $("li.third-item", e).nextUntil(nextUntil)); + + Element nextUntilElement = nextUntil.get(0); + assertEquals(1, $("li.third-item", e).nextUntil(nextUntilElement).size()); + assertHtmlEquals(expected, $("li.third-item", e).nextUntil(nextUntilElement)); + + + expected = "
  • i3
  • "; + $(e).html(content); + assertEquals(1, $("li.first-item", e).nextUntil(".five-item", "li.third-item").size()); + assertHtmlEquals(expected, $("li.first-item", e).nextUntil(".five-item", "li.third-item")); + + assertEquals(1, $("li.first-item", e).nextUntil(nextUntil, "li.third-item").size()); + assertHtmlEquals(expected, $("li.first-item", e).nextUntil(nextUntil, "li.third-item")); + + assertEquals(1, $("li.first-item", e).nextUntil(nextUntilElement, "li.third-item").size()); + assertHtmlEquals(expected, $("li.first-item", e).nextUntil(nextUntilElement, "li.third-item")); + + // andSelf() -- 2.39.5