Adding support for custom html elements.tags/gwtquery-project-1.4.3
<profile> | <profile> | ||||
<id>test-browser</id> | <id>test-browser</id> | ||||
<properties> | <properties> | ||||
<gwt.args>-draftCompile -style PRETTY -prod -runStyle Manual:1</gwt.args> | |||||
<gwt.args>-draftCompile -style PRETTY -optimize 9 -prod -runStyle Manual:1 -XjsInteropMode JS</gwt.args> | |||||
</properties> | </properties> | ||||
</profile> | </profile> | ||||
</profiles> | </profiles> |
if (o instanceof Function) { | if (o instanceof Function) { | ||||
return $((Function)o); | return $((Function)o); | ||||
} | } | ||||
if (JsUtils.isElement(o)) { | |||||
return $(JsUtils.<Element>cast(o)); | |||||
} | |||||
if (o instanceof JavaScriptObject) { | if (o instanceof JavaScriptObject) { | ||||
return $((JavaScriptObject)o); | return $((JavaScriptObject)o); | ||||
} | } | ||||
if (o instanceof IsWidget) { | if (o instanceof IsWidget) { | ||||
return $(Arrays.asList(o)); | return $(Arrays.asList(o)); | ||||
} | } | ||||
if (!GWT.isProdMode()) { | |||||
System.err.println("GQuery.$(Object o) could not wrap the type : " + o.getClass()); | |||||
} | |||||
console.log("Error: GQuery.$(Object o) could not wrap the type : ", o.getClass().getName(), o); | |||||
} | } | ||||
return $(); | return $(); | ||||
} | } | ||||
protected static String[] jsArrayToString(JsArrayString array) { | protected static String[] jsArrayToString(JsArrayString array) { | ||||
if (GWT.isScript()) { | if (GWT.isScript()) { | ||||
return jsArrayToString0(array); | |||||
return JsUtils.castArrayString(array); | |||||
} else { | } else { | ||||
String result[] = new String[array.length()]; | String result[] = new String[array.length()]; | ||||
for (int i = 0, l = result.length; i < l; i++) { | for (int i = 0, l = result.length; i < l; i++) { | ||||
} | } | ||||
} | } | ||||
private static native String[] jsArrayToString0(JsArrayString array) /*-{ | |||||
return array; | |||||
}-*/; | |||||
/** | /** | ||||
* Return a lazy version of the GQuery interface. Lazy function calls are simply queued up and not | * Return a lazy version of the GQuery interface. Lazy function calls are simply queued up and not |
import com.google.gwt.core.client.JavaScriptObject; | import com.google.gwt.core.client.JavaScriptObject; | ||||
import com.google.gwt.core.client.JsArray; | import com.google.gwt.core.client.JsArray; | ||||
import com.google.gwt.core.client.JsArrayMixed; | import com.google.gwt.core.client.JsArrayMixed; | ||||
import com.google.gwt.core.client.JsArrayString; | |||||
import com.google.gwt.dom.client.Document; | import com.google.gwt.dom.client.Document; | ||||
import com.google.gwt.dom.client.Element; | import com.google.gwt.dom.client.Element; | ||||
import com.google.gwt.dom.client.Node; | import com.google.gwt.dom.client.Node; | ||||
return Object.prototype.toString.call(o) == '[object Array]' | return Object.prototype.toString.call(o) == '[object Array]' | ||||
|| typeof o.length == 'number'; | || typeof o.length == 'number'; | ||||
}-*/; | }-*/; | ||||
/** | /** | ||||
* Check is a javascript object is a FormData | * Check is a javascript object is a FormData | ||||
*/ | */ | ||||
/** | /** | ||||
* Check is a javascript object can be cast to an Element | * Check is a javascript object can be cast to an Element | ||||
*/ | */ | ||||
public static boolean isElement(JavaScriptObject o) { | |||||
return hasProperty(o, "nodeType") && hasProperty(o, "nodeName"); | |||||
} | |||||
public static native boolean isElement(Object o) /*-{ | |||||
return o && o.nodeType && o.nodeName ? true : false; | |||||
}-*/; | |||||
/** | /** | ||||
* Check is a javascript object can be cast to an Event | * Check is a javascript object can be cast to an Event | ||||
return utilsImpl.text(e); | return utilsImpl.text(e); | ||||
} | } | ||||
/** | |||||
* Utility method to cast objects in production. | |||||
* Useful for casting native implementations to interfaces like JsInterop | |||||
*/ | |||||
public static native <T> T cast(Object o) /*-{ | |||||
return o; | |||||
}-*/; | |||||
/** | |||||
* Utility method to cast objects to array of string in production. | |||||
*/ | |||||
public static native String[] castArrayString(Object a)/*-{ | |||||
return a | |||||
}-*/; | |||||
/** | /** | ||||
* Call via jsni any arbitrary function present in a Javascript object. | * Call via jsni any arbitrary function present in a Javascript object. | ||||
* | * |
import static com.google.gwt.query.client.GQuery.$; | import static com.google.gwt.query.client.GQuery.$; | ||||
import static com.google.gwt.query.client.GQuery.$$; | import static com.google.gwt.query.client.GQuery.$$; | ||||
import static com.google.gwt.query.client.GQuery.console; | |||||
import static com.google.gwt.query.client.GQuery.document; | import static com.google.gwt.query.client.GQuery.document; | ||||
import static com.google.gwt.query.client.GQuery.window; | import static com.google.gwt.query.client.GQuery.window; | ||||
import com.google.gwt.core.client.js.JsType; | |||||
import com.google.gwt.dom.client.Document; | import com.google.gwt.dom.client.Document; | ||||
import com.google.gwt.dom.client.Element; | import com.google.gwt.dom.client.Element; | ||||
import com.google.gwt.dom.client.InputElement; | import com.google.gwt.dom.client.InputElement; | ||||
import com.google.gwt.user.client.ui.RootPanel; | import com.google.gwt.user.client.ui.RootPanel; | ||||
import com.google.gwt.user.client.ui.TextArea; | import com.google.gwt.user.client.ui.TextArea; | ||||
import com.google.gwt.user.client.ui.Widget; | import com.google.gwt.user.client.ui.Widget; | ||||
import junit.framework.Assert; | import junit.framework.Assert; | ||||
import java.util.ArrayList; | import java.util.ArrayList; | ||||
assertEquals(122, g.outerWidth()); | assertEquals(122, g.outerWidth()); | ||||
assertEquals(142, g.outerHeight(true)); | assertEquals(142, g.outerHeight(true)); | ||||
assertEquals(142, g.outerWidth(true)); | assertEquals(142, g.outerWidth(true)); | ||||
// When hiding the element we should get the same sizes | |||||
$(e).hide(); | |||||
assertEquals(100, g.width()); | |||||
assertEquals(100, g.height()); | |||||
assertEquals(100d, g.cur("width", false)); | |||||
assertEquals(100d, g.cur("height", false)); | |||||
assertEquals(100d, g.cur("width", true)); | |||||
assertEquals(100d, g.cur("height", true)); | |||||
assertEquals("100px", g.css("width")); | |||||
assertEquals("100px", g.css("height")); | |||||
assertEquals("100px", g.get(0).getStyle().getProperty("width")); | |||||
assertEquals("100px", g.get(0).getStyle().getProperty("height")); | |||||
// Modern browsers report 0 size when element is hidden | |||||
assertTrue(g.innerWidth() == 120 || g.innerWidth() == 0); | |||||
assertTrue(g.innerHeight() == 120 || g.innerHeight() == 0); | |||||
assertTrue(g.outerHeight() == 122 || g.outerHeight() == 0); | |||||
assertTrue(g.outerWidth() == 122 || g.outerWidth() == 0); | |||||
assertTrue(g.outerHeight() == 142 || g.outerHeight() == 0); | |||||
assertTrue(g.outerWidth() == 142 || g.outerWidth() == 0); | |||||
} | } | ||||
public void testWidthHeightInlineElement() { | public void testWidthHeightInlineElement() { | ||||
Integer value = $("#target", e).data("intValue", Integer.class); | Integer value = $("#target", e).data("intValue", Integer.class); | ||||
assertEquals(1, value.intValue()); | assertEquals(1, value.intValue()); | ||||
} | } | ||||
} | } |
} | } | ||||
// In chrome it returns 14.39999px instead of 15px | |||||
@DoNotRunWith(Platform.Prod) | |||||
public void testBorderSpacingProperty() { | public void testBorderSpacingProperty() { | ||||
$(e).html("<table id='test'><tr><td>Content<td></tr></table>"); | $(e).html("<table id='test'><tr><td>Content<td></tr></table>"); |
import com.google.gwt.core.client.Scheduler; | import com.google.gwt.core.client.Scheduler; | ||||
import com.google.gwt.core.client.Scheduler.RepeatingCommand; | import com.google.gwt.core.client.Scheduler.RepeatingCommand; | ||||
import com.google.gwt.dom.client.Element; | 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.junit.client.GWTTestCase; | ||||
import com.google.gwt.query.client.GQuery.Offset; | import com.google.gwt.query.client.GQuery.Offset; | ||||
import com.google.gwt.query.client.plugins.effects.Fx; | import com.google.gwt.query.client.plugins.effects.Fx; | ||||
assertEquals(1, back.size()); | assertEquals(1, back.size()); | ||||
} | } | ||||
// FIXME: timer 3 fails in real browsers (chrome) | |||||
@DoNotRunWith(Platform.Prod) | |||||
public void testEffectsShouldBeQueued() { | public void testEffectsShouldBeQueued() { | ||||
$(e).html("<p id='idtest'>Content 1</p></p>"); | $(e).html("<p id='idtest'>Content 1</p></p>"); | ||||
} | } | ||||
public void testComputeFxPropTransitions() { | public void testComputeFxPropTransitions() { | ||||
$(e).html("<table border=1 id=idtest><tr><td width=200px>A</td><td width=100px>B</td></tr></table>"); | |||||
$("#idtest").css("position", "absolute").find("td"); | |||||
final GQuery g = $("#idtest td"); | |||||
$(e).html("<div id='idtest' style='width: 200px; height 200px; border: solid 1px; position: absolute' ></div>"); | |||||
final GQuery g = $("#idtest", e); | |||||
TransitFx fx = (TransitFx)TransitionsAnimation.computeFxProp(g.get(0), "width", "+=100", false); | TransitFx fx = (TransitFx)TransitionsAnimation.computeFxProp(g.get(0), "width", "+=100", false); | ||||
assertEquals("10", fx.transitStart.replace(".0","")); | |||||
assertEquals("110", fx.transitEnd.replace(".0","")); | |||||
assertEquals("200", fx.transitStart.replace(".0","")); | |||||
assertEquals("300", fx.transitEnd.replace(".0","")); | |||||
assertEquals("px", fx.unit); | assertEquals("px", fx.unit); | ||||
} | } | ||||
public static Test suite() { | public static Test suite() { | ||||
GWTTestSuite suite = new GWTTestSuite( "GQuery Suite" ); | GWTTestSuite suite = new GWTTestSuite( "GQuery Suite" ); | ||||
suite.addTestSuite(AjaxTestJre.class); | suite.addTestSuite(AjaxTestJre.class); | ||||
suite.addTestSuite(GQueryJsInteropTestGwt.class); | |||||
suite.addTestSuite(DataBindingTestJre.class); | suite.addTestSuite(DataBindingTestJre.class); | ||||
suite.addTestSuite(DataBindingTestGwt.class); | suite.addTestSuite(DataBindingTestGwt.class); | ||||
suite.addTestSuite(GQueryAjaxTestGwt.class); | suite.addTestSuite(GQueryAjaxTestGwt.class); |
/* | |||||
* 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; | |||||
import static com.google.gwt.query.client.GQuery.$; | |||||
import com.google.gwt.core.client.JavaScriptObject; | |||||
import com.google.gwt.core.client.ScriptInjector; | |||||
import com.google.gwt.core.client.js.JsProperty; | |||||
import com.google.gwt.core.client.js.JsType; | |||||
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.js.JsUtils; | |||||
import com.google.gwt.user.client.DOM; | |||||
import com.google.gwt.user.client.ui.HTML; | |||||
import com.google.gwt.user.client.ui.RootPanel; | |||||
/** | |||||
* Test class for testing jsinterop | |||||
*/ | |||||
public class GQueryJsInteropTestGwt extends GWTTestCase { | |||||
static Element e = null; | |||||
static HTML testPanel = null; | |||||
public String getModuleName() { | |||||
return "com.google.gwt.query.QueryTest"; | |||||
} | |||||
public void gwtTearDown() { | |||||
$(e).remove(); | |||||
e = null; | |||||
} | |||||
public void gwtSetUp() { | |||||
if (e == null || DOM.getElementById("core-tst") == null) { | |||||
testPanel = new HTML(); | |||||
RootPanel.get().add(testPanel); | |||||
e = testPanel.getElement(); | |||||
e.setId("core-tst"); | |||||
} else { | |||||
e.setInnerHTML(""); | |||||
} | |||||
} | |||||
@JsType(prototype = "Window", isNative = true) | |||||
public interface HTMLWindow { | |||||
@JsProperty String getName(); | |||||
@JsProperty void setName(String name); | |||||
} | |||||
@JsType(prototype = "Window", isNative = true) | |||||
public interface HWindow { | |||||
@JsProperty HDocument document(); | |||||
} | |||||
@JsType(prototype = "HTMLDocument", isNative = false) | |||||
public interface HDocument { | |||||
HElement createElement(String tag); | |||||
} | |||||
@JsType(prototype = "HTMLElement", isNative = false) | |||||
public interface HElement { | |||||
@JsProperty void id(String s); | |||||
} | |||||
// jsInterop only works in prod mode. | |||||
@DoNotRunWith({Platform.HtmlUnitUnknown, Platform.Devel}) | |||||
public void testJsType() { | |||||
JavaScriptObject jsw = ScriptInjector.TOP_WINDOW; | |||||
// FIXME: this might not be necessary when GWT issue #9059 is fixed | |||||
HWindow w = JsUtils.cast(jsw); | |||||
assertNotNull(w); | |||||
HDocument d = w.document(); | |||||
assertNotNull(d); | |||||
HElement h = d.createElement("div"); | |||||
h.id("foo"); | |||||
// GQuery must support native elements created through jsInterop interfaces | |||||
$(h).appendTo(e); | |||||
GQuery f = $("#foo", e); | |||||
assertEquals(1, f.size()); | |||||
} | |||||
} |