diff options
author | Manolo Carrasco <manolo@apache.org> | 2011-04-26 08:10:36 +0000 |
---|---|---|
committer | Manolo Carrasco <manolo@apache.org> | 2011-04-26 08:10:36 +0000 |
commit | 7a7a791b4dd6ce297d77fc33089e09a2e305045c (patch) | |
tree | f7bc95abcbbf820afe42538ac298e0b2c76d9b5a /gwtquery-core/src | |
parent | af9dd9b94801c552dfbcce5023e007ccd3ea79f2 (diff) | |
download | gwtquery-7a7a791b4dd6ce297d77fc33089e09a2e305045c.tar.gz gwtquery-7a7a791b4dd6ce297d77fc33089e09a2e305045c.zip |
Assure the order of Function.f() calls is correct and test it
Diffstat (limited to 'gwtquery-core/src')
5 files changed, 223 insertions, 33 deletions
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.<com.google.gwt.dom.client.Element>cast()); + cancel(e.<com.google.gwt.user.client.Element>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.<com.google.gwt.dom.client.Element>cast(), i); + return f(e.<com.google.gwt.user.client.Element>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.<com.google.gwt.dom.client.Element>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().<com.google.gwt.dom.client.Element>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.<com.google.gwt.dom.client.Element>cast()); + f(e.<com.google.gwt.user.client.Element>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().<com.google.gwt.dom.client.Element>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<GQuery, LazyGQuery> { 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.<com.google.gwt.dom.client.Element>cast(), i);
+ if (val != null) {
+ e.setAttribute(key, String.valueOf(val));
+ }
}
return this;
}
@@ -1680,7 +1681,7 @@ public class GQuery implements Lazy<GQuery, LazyGQuery> { 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).<com.google.gwt.dom.client.Element>cast(), i);
}
}
}
@@ -2372,7 +2373,7 @@ public class GQuery implements Lazy<GQuery, LazyGQuery> { ArrayList<W> ret = new ArrayList<W>();
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].<com.google.gwt.dom.client.Element>cast(), i);
if (o != null) {
ret.add(o);
}
@@ -3046,7 +3047,6 @@ public class GQuery implements Lazy<GQuery, LazyGQuery> { $(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<T extends QueuePlugin<?>> extends GQuery { public void run() { dequeue(); } - } private int delay; @@ -146,7 +145,7 @@ public abstract class QueuePlugin<T extends QueuePlugin<?>> extends GQuery { Object f = q.peek(); if (f != null) { if (f instanceof Function) { - ((Function) f).f(elem); + ((Function) f).f(elem.<com.google.gwt.dom.client.Element>cast()); } } } @@ -164,7 +163,7 @@ public abstract class QueuePlugin<T extends QueuePlugin<?>> extends GQuery { } if (q.size() == 1 && func != null) { if (func instanceof Function) { - ((Function) func).f(elem); + ((Function) func).f(elem.<com.google.gwt.dom.client.Element>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.<com.google.gwt.dom.client.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("<p>0</p><p>1</p><p>2</p>", $("p", e)); } - + public void testIFrameManipulation() { $(e).html("<iframe name='miframe' id='miframe' src=\"javascript:''\">"); // FF has to call empty to open and close the document before @@ -1316,4 +1317,189 @@ public class GQueryCoreTest extends GWTTestCase { assertTrue(w.width() > 0); assertTrue(w.height() > 0); } + + public void testFunction() { + $(e).html("<div id=fid>0</div>"); + GQuery g = $("#fid"); + assertEquals("0", g.text()); + + // EACH + g.each(new Function() { + @Override + public void f(com.google.gwt.user.client.Element e) { + $(e).text("U"); + } + }); + assertEquals("U", g.text()); + g.each(new Function() { + @Override + public void f(com.google.gwt.dom.client.Element e) { + $(e).text("D"); + } + }); + assertEquals("D", g.text()); + g.each(new Function() { + @Override + public Object f(com.google.gwt.user.client.Element e, int idx) { + $(e).text("U" + idx); + return ""; + } + }); + assertEquals("U0", g.text()); + g.each(new Function() { + @Override + public Object f(com.google.gwt.user.client.Element e, int idx) { + $(e).text("D" + idx); + return ""; + } + }); + assertEquals("D0", g.text()); + + // EVENTS + g.unbind(Event.ONCLICK).click(new Function(){ + @Override + public void f(com.google.gwt.user.client.Element e) { + $(e).text("U"); + } + }).click(); + assertEquals("U", g.text()); + g.unbind(Event.ONCLICK).click(new Function(){ + @Override + public void f(com.google.gwt.dom.client.Element e) { + $(e).text("D"); + } + }).click(); + assertEquals("D", g.text()); + g.unbind(Event.ONCLICK).click(new Function(){ + @Override + public boolean f(Event e) { + $(e).text("E"); + return false; + } + }).click(); + assertEquals("E", g.text()); + g.unbind(Event.ONCLICK).bind(Event.ONCLICK, "D", new Function(){ + @Override + public boolean f(Event e, Object o) { + $(e).text("E" + o); + return false; + } + }).click(); + assertEquals("ED", g.text()); + + // ELEMENTS AND WIDGETS + Label label = new Label("1"); + RootPanel.get().add(label); + g = g.add($(label)); + assertEquals(2, g.size()); + + g.each(new Function() { + @Override + public void f(com.google.gwt.user.client.Element e) { + $(e).text("U"); + } + }); + assertEquals("UU", g.text()); + g.each(new Function() { + @Override + public void f(com.google.gwt.dom.client.Element e) { + $(e).text("D"); + } + }); + assertEquals("DD", g.text()); + + g.each(new Function() { + @Override + public void f(com.google.gwt.user.client.Element e) { + $(e).text("U"); + } + @Override + public void f(Widget w) { + $(w).text("W"); + } + }); + assertEquals("UW", g.text()); + g.each(new Function() { + @Override + public void f(com.google.gwt.dom.client.Element e) { + $(e).text("D"); + } + @Override + public void f(Widget w) { + $(w).text("W"); + } + }); + assertEquals("DW", g.text()); + + g.each(new Function() { + @Override + public Object f(com.google.gwt.user.client.Element e, int idx) { + $(e).text("U" + idx); + return ""; + } + }); + assertEquals("U0U1", g.text()); + g.each(new Function() { + @Override + public Object f(com.google.gwt.user.client.Element e, int idx) { + $(e).text("D" + idx); + return ""; + } + }); + assertEquals("D0D1", g.text()); + + g.each(new Function() { + @Override + public Object f(com.google.gwt.user.client.Element e, int idx) { + $(e).text("U" + idx); + return ""; + } + @Override + public Object f(Widget w, int idx) { + $(w).text("W" + idx); + return ""; + } + }); + assertEquals("U0U1", g.text()); + g.each(new Function() { + @Override + public Object f(com.google.gwt.dom.client.Element e, int idx) { + $(e).text("D" + idx); + return ""; + } + @Override + public Object f(Widget w, int idx) { + $(w).text("W" + idx); + return ""; + } + }); + assertEquals("D0D1", g.text()); + + g.each(new Function() { + @Override + public void f(com.google.gwt.user.client.Element e) { + $(e).text("U"); + } + @Override + public Object f(Widget w, int idx) { + $(w).text("W" + idx); + return ""; + } + }); + assertEquals("UW1", g.text()); + g.each(new Function() { + @Override + public void f(com.google.gwt.dom.client.Element e) { + $(e).text("D"); + } + @Override + public Object f(Widget w, int idx) { + $(w).text("W" + idx); + return ""; + } + }); + assertEquals("DW1", g.text()); + + label.removeFromParent(); + } } |