From a85924bf117cc5c53e3f6e83723b620f581ffbed Mon Sep 17 00:00:00 2001 From: Julien Dramaix Date: Wed, 25 Jul 2012 21:43:52 +0000 Subject: [PATCH] implement other signatures of prevAll and prevUntil methods --- .../com/google/gwt/query/client/GQuery.java | 125 ++++++++++++++---- .../gwt/query/client/GQueryCoreTestGwt.java | 32 ++++- 2 files changed, 131 insertions(+), 26 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 0570ba13..9979d8e9 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 @@ -15,9 +15,12 @@ */ package com.google.gwt.query.client; +import static com.google.gwt.query.client.plugins.QueuePlugin.Queue; + import java.util.ArrayList; import java.util.Arrays; import java.util.List; +import java.util.Map; import com.google.gwt.core.client.GWT; import com.google.gwt.core.client.JavaScriptObject; @@ -36,6 +39,7 @@ import com.google.gwt.dom.client.SelectElement; import com.google.gwt.dom.client.Style.Display; import com.google.gwt.dom.client.Style.HasCssName; import com.google.gwt.dom.client.TextAreaElement; +import com.google.gwt.query.client.css.CSS; import com.google.gwt.query.client.css.HasCssValue; import com.google.gwt.query.client.css.TakesCssValue; import com.google.gwt.query.client.css.TakesCssValue.CssSetter; @@ -63,7 +67,6 @@ import com.google.gwt.user.client.EventListener; import com.google.gwt.user.client.Window; import com.google.gwt.user.client.ui.GqUi; import com.google.gwt.user.client.ui.Widget; -import static com.google.gwt.query.client.plugins.QueuePlugin.Queue; /** * GwtQuery is a GWT clone of the popular jQuery library. @@ -781,12 +784,16 @@ public class GQuery implements Lazy { } private void allPreviousSiblingElements(Element firstChildElement, - JsNodeArray result, String untilSelector) { + JsNodeArray result, GQuery until, String filterSelector) { while (firstChildElement != null) { - if (untilSelector != null && $(firstChildElement).is(untilSelector)){ + if (until != null && until.index(firstChildElement) != -1){ return; } - result.addNode(firstChildElement); + + if(filterSelector == null || $(firstChildElement).is(filterSelector)){ + result.addNode(firstChildElement); + } + firstChildElement = getPreviousSiblingElement(firstChildElement); } } @@ -3207,27 +3214,97 @@ public class GQuery implements Lazy { } return new GQuery(unique(result)).filter(selectors); } - /** - * Find all sibling elements in front of the current element. - */ - public GQuery prevAll() { - JsNodeArray result = JsNodeArray.create(); - for (Element e : elements) { - allPreviousSiblingElements(getPreviousSiblingElement(e), result, null); - } - return pushStack(unique(result), "prevAll", getSelector()); - } + + /** + * Get all preceding siblings of each element in the set of matched + * elements. + */ + public GQuery prevAll() { + return prevAll(null); + } - /** - * Find all sibling elements in front of the current element. - */ - public GQuery prevUntil(String selector) { - JsNodeArray result = JsNodeArray.create(); - for (Element e : elements) { - allPreviousSiblingElements(getPreviousSiblingElement(e), result, selector); - } - return pushStack(unique(result), "prevUntil", getSelector()); - } + /** + * Get all preceding siblings of each element in the set of matched elements + * filtered by a selector. + */ + public GQuery prevAll(String selector) { + JsNodeArray result = JsNodeArray.create(); + for (Element e : elements) { + allPreviousSiblingElements(getPreviousSiblingElement(e), result, + null, selector); + } + return pushStack(unique(result), "prevAll", getSelector()); + } + + /** + * Get all preceding siblings of each element up to but not including the + * element matched by the selector. + * + * The elements are returned in order from the closest sibling to the + * farthest. + */ + public GQuery prevUntil(String selector) { + return prevUntil($(selector), null); + } + + /** + * Get all preceding siblings of each element up to but not including the + * until element. + * + * The elements are returned in order from the closest sibling to the + * farthest. + */ + public GQuery prevUntil(Element until) { + return prevUntil($(until), null); + } + + /** + * Get all preceding siblings of each element up to but not including the + * until element. + * + * The elements are returned in order from the closest sibling to the + * farthest. + */ + public GQuery prevUntil(GQuery until) { + return prevUntil(until, null); + } + + /** + * Get all preceding siblings of each element matching the + * filter up to but not including the element matched by the + * selector. + * + * The elements are returned in order from the closest sibling to the + * farthest. + */ + public GQuery prevUntil(String selector, String filter) { + return prevUntil($(selector), filter); + } + + /** + * Get all preceding siblings of each element matching the + * filter up to but not including the until + * element. + * + */ + public GQuery prevUntil(Element until, String filter) { + return prevUntil($(until), filter); + } + + /** + * Get all preceding siblings of each element matching the + * filter up to but not including the element matched by the + * until element. + * + */ + public GQuery prevUntil(GQuery until, String filter) { + JsNodeArray result = JsNodeArray.create(); + for (Element e : elements) { + allPreviousSiblingElements(getPreviousSiblingElement(e), result, + until, filter); + } + return pushStack(unique(result), "prevUntil", getSelector()); + } /** * Accesses a boolean property on the first matched element. 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 a2ec3a62..b5e467cc 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 @@ -690,15 +690,43 @@ public class GQueryCoreTestGwt extends GWTTestCase { $(e).html(content); assertEquals(4, $("li.five-item", e).prevAll().size()); assertHtmlEquals(expected, $("li.five-item", e).prevAll()); + + expected = "
  • i3
  • "; + assertEquals(1, $("li.five-item", e).prevAll(".third-item").size()); + assertHtmlEquals(expected, $("li.five-item", e).prevAll(".third-item")); + // prevUntil() - content = "
    • i1
    • i2
    • i3
    • i4
    • i5
    "; - expected = "
  • i4
  • "; + content = "
    • i1
    • i2
    • i3
    • i4
    • i5
    "; + expected = "
  • i4
  • "; $(e).html(content); assertEquals(1, $("li.five-item", e).prevUntil(".third-item").size()); assertHtmlEquals(expected, $("li.five-item", e).prevUntil(".third-item")); + assertEquals(1, $("li.five-item", e).prevUntil($(".third-item")).size()); + assertHtmlEquals(expected, $("li.five-item", e).prevUntil($(".third-item"))); + + Element until = $(".third-item").get(0); + assertEquals(1, $("li.five-item", e).prevUntil(until).size()); + assertHtmlEquals(expected, $("li.five-item", e).prevUntil(until)); + + + assertEquals(0, $("li.five-item", e).prevUntil(".third-item", ".fake-class").size()); + assertEquals(1, $("li.five-item", e).prevUntil(".second-item", ".item").size()); + assertHtmlEquals(expected, $("li.five-item", e).prevUntil(".second-item", ".item")); + + assertEquals(0, $("li.five-item", e).prevUntil($(".third-item"), ".fake-class").size()); + assertEquals(1, $("li.five-item", e).prevUntil($(".second-item"), ".item").size()); + assertHtmlEquals(expected, $("li.five-item", e).prevUntil($(".second-item"), ".item")); + + assertEquals(0, $("li.five-item", e).prevUntil(until, ".fake-class").size()); + + until = $(".second-item").get(0); + assertEquals(1, $("li.five-item", e).prevUntil(until, ".item").size()); + assertHtmlEquals(expected, $("li.five-item", e).prevUntil(until, ".item")); + + // siblings() content = "

    Hello

    Hello Again

    And Again

    "; next1 = "

    Hello

    "; -- 2.39.5