From e1df00dd9946d31f778f63b90748ecda52a82351 Mon Sep 17 00:00:00 2001 From: =?utf8?q?Manuel=20Carrasco=20Mo=C3=B1ino?= Date: Wed, 10 Apr 2013 10:50:45 +0200 Subject: [PATCH] Deprecate gwtquery JsRegexp implementation in favor of gwt RegExp which does exactly the same in client side, and works in jvm as well. Remove some imports --- .../google/gwt/query/client/Properties.java | 2 +- .../gwt/query/client/impl/SelectorEngine.java | 12 +- .../client/impl/SelectorEngineCssToXPath.java | 11 +- .../google/gwt/query/client/js/JsRegexp.java | 10 + .../gwt/query/client/plugins/MousePlugin.java | 2 - .../gwt/query/client/plugins/QueuePlugin.java | 2 + .../deferred/PromiseReqBuilderJSONP.java | 14 +- .../client/plugins/events/EventsListener.java | 1 - .../gwt/query/client/GQueryCoreTestGwt.java | 1 - .../query/client/GQueryEffectsTestGwt.java | 2 - .../query/client/deferred/DeferredTest.java | 208 ++++++++++++++++++ .../client/deferred/DeferredTestGwt.java | 29 +++ .../client/impl/SelectorEnginesTest.java | 1 - 13 files changed, 268 insertions(+), 27 deletions(-) create mode 100644 gwtquery-core/src/test/java/com/google/gwt/query/client/deferred/DeferredTest.java create mode 100644 gwtquery-core/src/test/java/com/google/gwt/query/client/deferred/DeferredTestGwt.java diff --git a/gwtquery-core/src/main/java/com/google/gwt/query/client/Properties.java b/gwtquery-core/src/main/java/com/google/gwt/query/client/Properties.java index d3dbda9b..b7e42c9a 100644 --- a/gwtquery-core/src/main/java/com/google/gwt/query/client/Properties.java +++ b/gwtquery-core/src/main/java/com/google/gwt/query/client/Properties.java @@ -17,7 +17,7 @@ package com.google.gwt.query.client; import com.google.gwt.core.client.JavaScriptObject; import com.google.gwt.core.client.JsArrayMixed; -import com.google.gwt.core.shared.GWT; +import com.google.gwt.core.client.GWT; import com.google.gwt.query.client.js.JsCache; import com.google.gwt.query.client.js.JsUtils; diff --git a/gwtquery-core/src/main/java/com/google/gwt/query/client/impl/SelectorEngine.java b/gwtquery-core/src/main/java/com/google/gwt/query/client/impl/SelectorEngine.java index 638cbf46..3ffff970 100644 --- a/gwtquery-core/src/main/java/com/google/gwt/query/client/impl/SelectorEngine.java +++ b/gwtquery-core/src/main/java/com/google/gwt/query/client/impl/SelectorEngine.java @@ -21,9 +21,9 @@ import com.google.gwt.dom.client.Element; import com.google.gwt.dom.client.Node; import com.google.gwt.dom.client.NodeList; import com.google.gwt.query.client.js.JsNodeArray; -import com.google.gwt.query.client.js.JsObjectArray; -import com.google.gwt.query.client.js.JsRegexp; import com.google.gwt.query.client.js.JsUtils; +import com.google.gwt.regexp.shared.MatchResult; +import com.google.gwt.regexp.shared.RegExp; /** * Core Selector engine functions, and native JS utility functions. @@ -122,21 +122,21 @@ public class SelectorEngine implements HasSelector { } // pseudo selectors which are computed by gquery - JsRegexp p = new JsRegexp("(.*):((visible|hidden)|((button|checkbox|file|hidden|image|password|radio|reset|submit|text)\\s*(,|$)))(.*)", "i"); + RegExp p = RegExp.compile("(.*):((visible|hidden)|((button|checkbox|file|hidden|image|password|radio|reset|submit|text)\\s*(,|$)))(.*)", "i"); public NodeList select(String selector, Node ctx) { if (p.test(selector)) { JsNodeArray res = JsNodeArray.create(); for (String s : selector.trim().split("\\s*,\\s*")) { NodeList nodes; - JsObjectArray a = p.match(s); - if (a.get(0) != null) { + MatchResult a = p.exec(s); + if (a != null) { if (s.endsWith(":visible")) { nodes = filterByVisibility(select(s.substring(0, s.length() - 8), ctx), true); } else if (s.endsWith(":hidden")) { nodes = filterByVisibility(select(s.substring(0, s.length() - 7), ctx), false); } else { - nodes = select((a.get(1) != null ? a.get(1) : "") + "[type=" + a.get(2) + "]", ctx); + nodes = select((a.getGroup(1) != null ? a.getGroup(1) : "") + "[type=" + a.getGroup(2) + "]", ctx); } } else { nodes = select(s, ctx); diff --git a/gwtquery-core/src/main/java/com/google/gwt/query/client/impl/SelectorEngineCssToXPath.java b/gwtquery-core/src/main/java/com/google/gwt/query/client/impl/SelectorEngineCssToXPath.java index d6218abf..e5ec73a6 100644 --- a/gwtquery-core/src/main/java/com/google/gwt/query/client/impl/SelectorEngineCssToXPath.java +++ b/gwtquery-core/src/main/java/com/google/gwt/query/client/impl/SelectorEngineCssToXPath.java @@ -24,9 +24,8 @@ import com.google.gwt.dom.client.Node; import com.google.gwt.dom.client.NodeList; import com.google.gwt.query.client.js.JsNamedArray; import com.google.gwt.query.client.js.JsNodeArray; -import com.google.gwt.query.client.js.JsObjectArray; -import com.google.gwt.query.client.js.JsRegexp; import com.google.gwt.query.client.js.JsUtils; +import com.google.gwt.regexp.shared.MatchResult; import com.google.gwt.regexp.shared.RegExp; /** @@ -183,14 +182,14 @@ public class SelectorEngineCssToXPath extends SelectorEngineImpl { // when using this engine in generators and tests for the JVM private Replacer replacer = new Replacer() { public String replaceAll(String s, String r, Object o) { - JsRegexp p = new JsRegexp(r); + RegExp p = RegExp.compile(r); if (o instanceof ReplaceCallback) { ReplaceCallback callback = (ReplaceCallback) o; while (p.test(s)) { - JsObjectArray a = p.match(s); + MatchResult a = p.exec(s); ArrayList args = new ArrayList(); - for (int i = 0; i < a.length(); i++) { - args.add(a.get(i)); + for (int i = 0; a != null && i < a.getGroupCount(); i++) { + args.add(a.getGroup(i)); } String f = callback.foundMatch(args); s = s.replaceFirst(r, f); diff --git a/gwtquery-core/src/main/java/com/google/gwt/query/client/js/JsRegexp.java b/gwtquery-core/src/main/java/com/google/gwt/query/client/js/JsRegexp.java index 7eeb0da5..b4018cf8 100644 --- a/gwtquery-core/src/main/java/com/google/gwt/query/client/js/JsRegexp.java +++ b/gwtquery-core/src/main/java/com/google/gwt/query/client/js/JsRegexp.java @@ -19,7 +19,9 @@ import com.google.gwt.core.client.JavaScriptObject; /** * A wrapper around Javascript Regexps. + * @deprecated use RegExp instead whose jsni implementation is the same */ +@Deprecated public class JsRegexp { public static native JavaScriptObject compile(String pat) /*-{ @@ -40,10 +42,18 @@ public class JsRegexp { private final JavaScriptObject regexp; + /** + * @deprecated use RegExp.compile(pattern) instead + */ + @Deprecated public JsRegexp(String pattern) { this.regexp = compile(pattern); } + /** + * @deprecated use RegExp.compile(pattern, flags) instead + */ + @Deprecated public JsRegexp(String pat, String flags) { this.regexp = compileFlags(pat, flags); } diff --git a/gwtquery-core/src/main/java/com/google/gwt/query/client/plugins/MousePlugin.java b/gwtquery-core/src/main/java/com/google/gwt/query/client/plugins/MousePlugin.java index 25a74334..aca8db05 100755 --- a/gwtquery-core/src/main/java/com/google/gwt/query/client/plugins/MousePlugin.java +++ b/gwtquery-core/src/main/java/com/google/gwt/query/client/plugins/MousePlugin.java @@ -20,8 +20,6 @@ import com.google.gwt.query.client.Function; import com.google.gwt.query.client.GQuery; import com.google.gwt.query.client.plugins.events.GqEvent; import com.google.gwt.user.client.Event; -import com.google.gwt.user.client.ui.Label; -import com.google.gwt.user.client.ui.RootPanel; /** * Base class for all plug-in that need to handle some mouse interactions. 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 0834a3d8..9227d699 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 @@ -352,6 +352,7 @@ public class QueuePlugin> extends GQuery { * in the queue. */ public void dequeueIfNotDoneYet(Element elem, String name, Object object) { + @SuppressWarnings("rawtypes") Queue queue = queue(elem, name, null); if (queue != null && object.equals(queue.peek())) { dequeueCurrentAndRunNext(elem, name); @@ -374,6 +375,7 @@ public class QueuePlugin> extends GQuery { } private void stop(Element elem, String name, boolean clear, boolean jumpToEnd) { + @SuppressWarnings("rawtypes") Queue q = queue(elem, name, null); if (q != null) { Object f = q.peek(); diff --git a/gwtquery-core/src/main/java/com/google/gwt/query/client/plugins/deferred/PromiseReqBuilderJSONP.java b/gwtquery-core/src/main/java/com/google/gwt/query/client/plugins/deferred/PromiseReqBuilderJSONP.java index 8ce184f3..9278bf7b 100644 --- a/gwtquery-core/src/main/java/com/google/gwt/query/client/plugins/deferred/PromiseReqBuilderJSONP.java +++ b/gwtquery-core/src/main/java/com/google/gwt/query/client/plugins/deferred/PromiseReqBuilderJSONP.java @@ -14,9 +14,9 @@ package com.google.gwt.query.client.plugins.deferred; import com.google.gwt.jsonp.client.JsonpRequestBuilder; -import com.google.gwt.query.client.js.JsObjectArray; -import com.google.gwt.query.client.js.JsRegexp; import com.google.gwt.query.client.plugins.deferred.Deferred.DeferredPromiseImpl; +import com.google.gwt.regexp.shared.MatchResult; +import com.google.gwt.regexp.shared.RegExp; import com.google.gwt.user.client.rpc.AsyncCallback; /** @@ -37,7 +37,7 @@ import com.google.gwt.user.client.rpc.AsyncCallback; */ public class PromiseReqBuilderJSONP extends DeferredPromiseImpl { - private static final JsRegexp callbackRegex = new JsRegexp("^(.+[\\?&])([^=]+)=\\?(.*)$"); + private static final RegExp callbackRegex = RegExp.compile("^(.+[\\?&])([^=]+)=\\?(.*)$"); public PromiseReqBuilderJSONP(String url) { this(url, null, 0); @@ -54,10 +54,10 @@ public class PromiseReqBuilderJSONP extends DeferredPromiseImpl { } // jQuery allows a parameter callback=? to figure out the callback parameter if (callbackParam == null) { - JsObjectArray tmp = callbackRegex.exec(url); - if (tmp.length() == 4) { - callbackParam = tmp.get(2); - url = tmp.get(1) + tmp.get(3); + MatchResult tmp = callbackRegex.exec(url); + if (tmp != null && tmp.getGroupCount() == 4) { + callbackParam = tmp.getGroup(2); + url = tmp.getGroup(1) + tmp.getGroup(3); } } if (callbackParam != null) { diff --git a/gwtquery-core/src/main/java/com/google/gwt/query/client/plugins/events/EventsListener.java b/gwtquery-core/src/main/java/com/google/gwt/query/client/plugins/events/EventsListener.java index 1f239fdc..5fbe5544 100644 --- a/gwtquery-core/src/main/java/com/google/gwt/query/client/plugins/events/EventsListener.java +++ b/gwtquery-core/src/main/java/com/google/gwt/query/client/plugins/events/EventsListener.java @@ -16,7 +16,6 @@ package com.google.gwt.query.client.plugins.events; import static com.google.gwt.query.client.GQuery.$; import com.google.gwt.core.client.Duration; -import com.google.gwt.core.client.JsArrayString; import com.google.gwt.dom.client.Element; import com.google.gwt.dom.client.EventTarget; import com.google.gwt.dom.client.NodeList; 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 d5d58793..c87a0025 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 @@ -49,7 +49,6 @@ import com.google.gwt.query.client.js.JsNodeArray; import com.google.gwt.query.client.js.JsUtils; import com.google.gwt.user.client.DOM; import com.google.gwt.user.client.Event; -import com.google.gwt.user.client.Window; import com.google.gwt.user.client.ui.Button; import com.google.gwt.user.client.ui.HTML; import com.google.gwt.user.client.ui.Label; diff --git a/gwtquery-core/src/test/java/com/google/gwt/query/client/GQueryEffectsTestGwt.java b/gwtquery-core/src/test/java/com/google/gwt/query/client/GQueryEffectsTestGwt.java index 531f58c3..b71b2300 100644 --- a/gwtquery-core/src/test/java/com/google/gwt/query/client/GQueryEffectsTestGwt.java +++ b/gwtquery-core/src/test/java/com/google/gwt/query/client/GQueryEffectsTestGwt.java @@ -19,8 +19,6 @@ import static com.google.gwt.query.client.GQuery.$; import static com.google.gwt.query.client.GQuery.$$; import com.google.gwt.dom.client.Element; -import com.google.gwt.junit.DoNotRunWith; -import com.google.gwt.junit.Platform; import com.google.gwt.junit.client.GWTTestCase; import com.google.gwt.query.client.GQuery.Offset; import com.google.gwt.query.client.plugins.Effects; diff --git a/gwtquery-core/src/test/java/com/google/gwt/query/client/deferred/DeferredTest.java b/gwtquery-core/src/test/java/com/google/gwt/query/client/deferred/DeferredTest.java new file mode 100644 index 00000000..b2ce198b --- /dev/null +++ b/gwtquery-core/src/test/java/com/google/gwt/query/client/deferred/DeferredTest.java @@ -0,0 +1,208 @@ +/* + * Copyright 2011, The gwtquery team. + * + * Licensed under the Apache License, Version 2.0 (the "License"); you may not + * use this file except in compliance with the License. You may obtain a copy of + * the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT + * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the + * License for the specific language governing permissions and limitations under + * the License. + */ +package com.google.gwt.query.client.deferred; + +import com.google.gwt.junit.client.GWTTestCase; +import com.google.gwt.query.client.Function; +import com.google.gwt.query.client.GQuery; +import com.google.gwt.query.client.plugins.deferred.Callbacks; +import com.google.gwt.query.client.plugins.deferred.Callbacks.Callback; +import com.google.gwt.query.client.plugins.deferred.PromiseFunction; + +/** + * Tests for Deferred which can run either in JVM and GWT + */ +public class DeferredTest extends GWTTestCase { + + public String getModuleName() { + return null; + } + + private String result = ""; + public void testCallbacks() { + Function fn1 = new Function() { + public Object f(Object...arguments) { + String s = " f1:"; + for (Object o: arguments){ + s += " " + o; + } + result += s; + return false; + } + }; + + Callback fn2 = new Callback() { + public boolean f(Object... objects) { + String s = " f2:"; + for (Object o: objects){ + s += " " + o; + } + result += s; + return false; + } + }; + + com.google.gwt.core.client.Callback fn3 = new com.google.gwt.core.client.Callback() { + public void onFailure(Object reason) { + result += " f3_fail: " + reason; + } + public void onSuccess(Object objects) { + String s = " f3_success:"; + for (Object o: (Object[])objects){ + s += " " + o; + } + result += s; + } + }; + + result = ""; + Callbacks callbacks = new Callbacks(); + callbacks.add( fn1 ); + callbacks.fire( "foo" ); + assertEquals(" f1: foo", result); + + result = ""; + callbacks.add( fn2 ); + callbacks.fire( "bar" ); + assertEquals(" f1: bar f2: bar", result); + + result = ""; + callbacks.remove( fn2 ); + callbacks.fire( "foobar" ); + assertEquals(" f1: foobar", result); + + result = ""; + callbacks.add( fn1 ); + callbacks.fire( "foo" ); + assertEquals(" f1: foo f1: foo", result); + + result = ""; + callbacks = new Callbacks("unique"); + callbacks.add( fn1 ); + callbacks.add( fn1 ); + callbacks.fire( "foo" ); + assertEquals(" f1: foo", result); + + result = ""; + callbacks.add( fn3 ); + callbacks.fire( "bar" ); + assertEquals(" f1: bar f3_success: bar", result); + + result = ""; + callbacks = new Callbacks("memory"); + callbacks.add( fn1 ); + callbacks.fire( "foo" ); + callbacks.add( fn2 ); + callbacks.fire( "bar" ); + callbacks.remove(fn2); + callbacks.fire( "foobar" ); + assertEquals(" f1: foo f2: foo f1: bar f2: bar f1: foobar", result); + + result = ""; + callbacks = new Callbacks("stopOnFalse"); + callbacks.add( fn1 ); + callbacks.add( fn2 ); + callbacks.fire( "bar" ); + assertEquals(" f1: bar", result); + + result = ""; + callbacks.disable(); + callbacks.fire( "bar" ); + assertEquals("", result); + + result = ""; + callbacks = new Callbacks("memory once unique"); + callbacks.add( fn1 ); + callbacks.add( fn1 ); + callbacks.fire( "bar" ); + assertEquals(" f1: bar", result); + callbacks.fire( "foo" ); + assertEquals(" f1: bar", result); + callbacks.add( fn2 ); + callbacks.add( fn2 ); + assertEquals(" f1: bar f2: bar f2: bar", result); + callbacks.remove( fn1 ); + callbacks.add( fn1 ); + assertEquals(" f1: bar f2: bar f2: bar f1: bar", result); + callbacks.remove( fn1 ); + callbacks.disable(); + callbacks.add( fn1 ); + assertEquals(" f1: bar f2: bar f2: bar f1: bar", result); + } + + public void testThen() { + new PromiseFunction() { + public void f(final Deferred dfd) { + dfd.resolve(5d); + } + }.done(new Function() { + public void f() { + assertEquals(5d, arguments(0)); + } + }).then(new Function() { + public Object f(Object... args) { + return (Double)args[0] * 2; + } + }).done(new Function() { + public void f() { + assertEquals(10d, arguments(0)); + } + }); + } + + public void testDeferredAjaxThenFail() { + delayTestFinish(5000); + + GQuery + .when(new PromiseFunction() { + public void f(Deferred dfd) { + dfd.resolve("message"); + } + }) + .then(new Function() { + public Object f(Object... args) { + return new PromiseFunction() { + public void f(Deferred dfd) { + dfd.resolve(arguments); + } + }; + } + }) + .then(new Function() { + public Object f(Object... args) { + return new PromiseFunction() { + public void f(Deferred dfd) { + dfd.reject(arguments); + } + }; + } + }) + .done(new Function() { + public void f() { + finishTest(); + fail(); + } + }) + .fail(new Function() { + public void f() { + assertEquals("message", arguments(0)); + finishTest(); + } + }); + } + + +} diff --git a/gwtquery-core/src/test/java/com/google/gwt/query/client/deferred/DeferredTestGwt.java b/gwtquery-core/src/test/java/com/google/gwt/query/client/deferred/DeferredTestGwt.java new file mode 100644 index 00000000..de6375db --- /dev/null +++ b/gwtquery-core/src/test/java/com/google/gwt/query/client/deferred/DeferredTestGwt.java @@ -0,0 +1,29 @@ +/* + * Copyright 2013, The gwtquery team. + * + * Licensed under the Apache License, Version 2.0 (the "License"); you may not + * use this file except in compliance with the License. You may obtain a copy of + * the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT + * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the + * License for the specific language governing permissions and limitations under + * the License. + */ +package com.google.gwt.query.client.deferred; + + +/** + * Test for deferred/callbacks shared code run in gwt + */ +public class DeferredTestGwt extends DeferredTest { + + @Override + public String getModuleName() { + return "com.google.gwt.query.Query"; + } + +} diff --git a/gwtquery-core/src/test/java/com/google/gwt/query/client/impl/SelectorEnginesTest.java b/gwtquery-core/src/test/java/com/google/gwt/query/client/impl/SelectorEnginesTest.java index 2f1171c1..7b8199ee 100644 --- a/gwtquery-core/src/test/java/com/google/gwt/query/client/impl/SelectorEnginesTest.java +++ b/gwtquery-core/src/test/java/com/google/gwt/query/client/impl/SelectorEnginesTest.java @@ -16,7 +16,6 @@ package com.google.gwt.query.client.impl; import com.google.gwt.junit.client.GWTTestCase; -import com.google.gwt.query.rebind.SelectorGeneratorCssToXPath; /** * Test for selector engine implementations -- 2.39.5