From 7a7a791b4dd6ce297d77fc33089e09a2e305045c Mon Sep 17 00:00:00 2001 From: Manolo Carrasco Date: Tue, 26 Apr 2011 08:10:36 +0000 Subject: Assure the order of Function.f() calls is correct and test it --- .../java/com/google/gwt/query/client/Function.java | 49 +++--- .../java/com/google/gwt/query/client/GQuery.java | 12 +- .../gwt/query/client/plugins/QueuePlugin.java | 5 +- .../google/gwt/query/client/plugins/UiPlugin.java | 2 +- .../google/gwt/query/client/GQueryCoreTest.java | 188 ++++++++++++++++++++- 5 files changed, 223 insertions(+), 33 deletions(-) (limited to 'gwtquery-core/src') diff --git a/gwtquery-core/src/main/java/com/google/gwt/query/client/Function.java b/gwtquery-core/src/main/java/com/google/gwt/query/client/Function.java index de5e8a09..f37793e5 100644 --- a/gwtquery-core/src/main/java/com/google/gwt/query/client/Function.java +++ b/gwtquery-core/src/main/java/com/google/gwt/query/client/Function.java @@ -15,7 +15,6 @@ */ package com.google.gwt.query.client; -import com.google.gwt.user.client.Element; import com.google.gwt.user.client.Event; import com.google.gwt.user.client.ui.Widget; @@ -27,21 +26,21 @@ public abstract class Function { /** * Override this for methods which invoke a cancel action. * - * @param e takes a com.google.gwt.user.client.Element. + * @param e takes a com.google.gwt.dom.client.Element. * */ - public void cancel(Element e) { + public void cancel(com.google.gwt.dom.client.Element e) { // This has to be the order of calls - cancel(e.cast()); + cancel(e.cast()); } /** * Override this for methods which invoke a cancel action. * - * @param e takes a com.google.gwt.dom.client.Element. + * @param e takes a com.google.gwt.user.client.Element. * */ - public void cancel(com.google.gwt.dom.client.Element e) { + public void cancel(com.google.gwt.user.client.Element e) { } /** @@ -56,27 +55,27 @@ public abstract class Function { * Override this for GQuery methods which loop over matched elements and * invoke a callback on each element. * - * @param e takes a com.google.gwt.user.client.Element. + * @param e takes a com.google.gwt.dom.client.Element. * */ - public Object f(Element e, int i) { + public Object f(com.google.gwt.dom.client.Element e, int i) { // This has to be the order of calls - return f(e.cast(), i); + return f(e.cast(), i); } /** * Override this for GQuery methods which loop over matched elements and * invoke a callback on each element. * - * @param e takes a com.google.gwt.dom.client.Element. + * @param e takes a com.google.gwt.user.client.Element. * */ - public Object f(com.google.gwt.dom.client.Element e, int i) { + public Object f(com.google.gwt.user.client.Element e, int i) { Widget w = GQuery.getAssociatedWidget(e); if (w != null){ f(w, i); } else { - f(e); + f(e.cast()); } return null; } @@ -90,7 +89,7 @@ public abstract class Function { * avoid a runtime exception. */ public Object f(Widget w, int i) { - f(w.getElement());//f(w) will be called later in f(Element) + f(w); return null; } @@ -106,7 +105,7 @@ public abstract class Function { * Override this method for bound event handlers. */ public boolean f(Event e) { - f((Element)e.getCurrentEventTarget().cast()); + f(e.getCurrentEventTarget().cast()); return true; } @@ -114,24 +113,26 @@ public abstract class Function { * Override this for GQuery methods which take a callback and do not expect a * return value. * - * @param e takes a com.google.gwt.user.client.Element + * @param e takes a com.google.gwt.dom.client.Element */ - public void f(Element e) { + public void f(com.google.gwt.dom.client.Element e) { // This has to be the order of calls - f(e.cast()); + f(e.cast()); } /** * Override this for GQuery methods which take a callback and do not expect a * return value. * - * @param e takes a com.google.gwt.dom.client.Element + * @param e takes a com.google.gwt.user.client.Element */ - public void f(com.google.gwt.dom.client.Element e) { + private boolean loop = false; + public void f(com.google.gwt.user.client.Element e) { Widget w = GQuery.getAssociatedWidget(e); if (w != null){ + loop = true; f(w); - }else{ + } else { f(); } } @@ -145,8 +146,12 @@ public abstract class Function { * avoid a runtime exception. */ public void f(Widget w){ - // Do not call f(e) here to avoid loop - f(); + if (loop) { + loop = false; + f(); + } else { + f(w.getElement().cast()); + } } } 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 1d0985bd..95940948 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 @@ -39,8 +39,6 @@ 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.event.logical.shared.ResizeEvent; -import com.google.gwt.event.logical.shared.ResizeHandler; import com.google.gwt.query.client.css.CSS; import com.google.gwt.query.client.css.HasCssValue; import com.google.gwt.query.client.css.TakesCssValue; @@ -863,7 +861,10 @@ public class GQuery implements Lazy { public GQuery attr(String key, Function closure) { for (int i = 0; i < elements.getLength(); i++) { Element e = elements.getItem(i); - e.setAttribute(key, String.valueOf(closure.f(e, i))); + Object val = closure.f(e.cast(), i); + if (val != null) { + e.setAttribute(key, String.valueOf(val)); + } } return this; } @@ -1680,7 +1681,7 @@ public class GQuery implements Lazy { if (f != null) { for (Function f1 : f) { for (int i = 0; i < elements.getLength(); i++) { - f1.f(elements.getItem(i), i); + f1.f(elements.getItem(i).cast(), i); } } } @@ -2372,7 +2373,7 @@ public class GQuery implements Lazy { ArrayList ret = new ArrayList(); for (int i = 0; i < elements().length; i++) { @SuppressWarnings("unchecked") - W o = (W)f.f(elements()[i], i); + W o = (W)f.f(elements()[i].cast(), i); if (o != null) { ret.add(o); } @@ -3046,7 +3047,6 @@ public class GQuery implements Lazy { $(el).remove(); } return this; - } /** diff --git a/gwtquery-core/src/main/java/com/google/gwt/query/client/plugins/QueuePlugin.java b/gwtquery-core/src/main/java/com/google/gwt/query/client/plugins/QueuePlugin.java index d78d5e99..cff22fdb 100644 --- a/gwtquery-core/src/main/java/com/google/gwt/query/client/plugins/QueuePlugin.java +++ b/gwtquery-core/src/main/java/com/google/gwt/query/client/plugins/QueuePlugin.java @@ -35,7 +35,6 @@ public abstract class QueuePlugin> extends GQuery { public void run() { dequeue(); } - } private int delay; @@ -146,7 +145,7 @@ public abstract class QueuePlugin> extends GQuery { Object f = q.peek(); if (f != null) { if (f instanceof Function) { - ((Function) f).f(elem); + ((Function) f).f(elem.cast()); } } } @@ -164,7 +163,7 @@ public abstract class QueuePlugin> extends GQuery { } if (q.size() == 1 && func != null) { if (func instanceof Function) { - ((Function) func).f(elem); + ((Function) func).f(elem.cast()); } } return q; diff --git a/gwtquery-core/src/main/java/com/google/gwt/query/client/plugins/UiPlugin.java b/gwtquery-core/src/main/java/com/google/gwt/query/client/plugins/UiPlugin.java index 76b78ab5..c75149e6 100755 --- a/gwtquery-core/src/main/java/com/google/gwt/query/client/plugins/UiPlugin.java +++ b/gwtquery-core/src/main/java/com/google/gwt/query/client/plugins/UiPlugin.java @@ -161,7 +161,7 @@ public class UiPlugin extends GQuery { handlerManager.fireEvent(e); } if (callback != null) { - callback.f(element); + callback.f(element.cast()); } } diff --git a/gwtquery-core/src/test/java/com/google/gwt/query/client/GQueryCoreTest.java b/gwtquery-core/src/test/java/com/google/gwt/query/client/GQueryCoreTest.java index 11b15310..be9a0188 100644 --- a/gwtquery-core/src/test/java/com/google/gwt/query/client/GQueryCoreTest.java +++ b/gwtquery-core/src/test/java/com/google/gwt/query/client/GQueryCoreTest.java @@ -47,6 +47,7 @@ import com.google.gwt.user.client.ui.HTML; import com.google.gwt.user.client.ui.Label; import com.google.gwt.user.client.ui.RootPanel; import com.google.gwt.user.client.ui.TextArea; +import com.google.gwt.user.client.ui.Widget; /** * Test class for testing gwtquery-core api. @@ -213,7 +214,7 @@ public class GQueryCoreTest extends GWTTestCase { }); assertHtmlEquals("

0

1

2

", $("p", e)); } - + public void testIFrameManipulation() { $(e).html("