diff options
Diffstat (limited to 'gwtquery-core')
42 files changed, 2049 insertions, 695 deletions
diff --git a/gwtquery-core/pom.xml b/gwtquery-core/pom.xml index 5af705a8..9c4b6622 100644 --- a/gwtquery-core/pom.xml +++ b/gwtquery-core/pom.xml @@ -1,14 +1,14 @@ <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd"> <modelVersion>4.0.0</modelVersion> <parent> - <groupId>gwtquery</groupId> + <groupId>com.google.gwt</groupId> <artifactId>gwtquery-project</artifactId> <version>1.0-SNAPSHOT</version> </parent> <artifactId>gwtquery</artifactId> <packaging>jar</packaging> - <name>GwtQuery Core API</name> + <name>Gwt Query Core API</name> <url>http://gwtquery.com</url> <dependencies> <dependency> diff --git a/gwtquery-core/src/main/java/com/google/gwt/query/Query.gwt.xml b/gwtquery-core/src/main/java/com/google/gwt/query/Query.gwt.xml new file mode 100644 index 00000000..a09eeafc --- /dev/null +++ b/gwtquery-core/src/main/java/com/google/gwt/query/Query.gwt.xml @@ -0,0 +1,81 @@ +<module>
+ <inherits name='com.google.gwt.user.User'/>
+
+ <!--<define-property name="selectorCapability" values="native,xpath,js"/>-->
+ <!-- enable for native getClassByName shortcut acceleration -->
+ <define-property name="selectorCapability"
+ values="native,js"/>
+ <property-provider name="selectorCapability">
+ <![CDATA[
+ // useful for benchmarking tests when you want to force non-accelerated queries
+ //if(window.location.href.indexOf("_selector_force_js") != -1) return "js";
+ if(document.querySelectorAll && /native/.test(document.querySelectorAll.toString())) {
+ return "native";
+ }
+ return "js"
+ ]]>
+ </property-provider>
+
+ <generate-with class="com.google.gwt.query.rebind.SelectorGeneratorJS">
+ <when-type-assignable class="com.google.gwt.query.client.Selectors"/>
+ <any>
+ <when-property-is name="user.agent" value="gecko"/>
+ <when-property-is name="user.agent" value="ie6"/>
+ </any>
+
+ </generate-with>
+
+ <generate-with class="com.google.gwt.query.rebind.SelectorGeneratorXPath">
+ <when-type-assignable class="com.google.gwt.query.client.Selectors"/>
+ <any>
+ <when-property-is name="user.agent" value="gecko1_8"/>
+ <when-property-is name="user.agent" value="opera"/>
+ <all>
+ <when-property-is name="selectorCapability" value="js"/>
+ <when-property-is name="user.agent" value="safari"/>
+ </all>
+ </any>
+ </generate-with>
+
+
+ <replace-with class="com.google.gwt.query.client.impl.DocumentStyleImpl">
+ <when-type-assignable class="com.google.gwt.query.client.impl.DocumentStyleImpl"/>
+ </replace-with>
+
+ <replace-with class="com.google.gwt.query.client.impl.DocumentStyleImplIE">
+ <when-type-assignable class="com.google.gwt.query.client.impl.DocumentStyleImpl"/>
+ <when-property-is name="user.agent" value="ie6"/>
+ </replace-with>
+
+
+ <replace-with class="com.google.gwt.query.client.impl.SelectorEngineJS">
+ <when-type-assignable class="com.google.gwt.query.client.impl.SelectorEngineImpl"/>
+ <when-property-is name="user.agent" value="gecko"/>
+ </replace-with>
+
+ <replace-with class="com.google.gwt.query.client.impl.SelectorEngineJSIE">
+ <when-type-assignable class="com.google.gwt.query.client.impl.SelectorEngineImpl"/>
+ <when-property-is name="user.agent" value="ie6"/>
+ </replace-with>
+
+ <replace-with class="com.google.gwt.query.client.impl.SelectorEngineXPath">
+ <when-type-assignable class="com.google.gwt.query.client.impl.SelectorEngineImpl"/>
+ <any>
+ <when-property-is name="user.agent" value="gecko1_8"/>
+ <when-property-is name="user.agent" value="opera"/>
+ <all>
+ <when-property-is name="selectorCapability" value="js"/>
+ <when-property-is name="user.agent" value="safari"/>
+ </all>
+ </any>
+ </replace-with>
+
+ <replace-with class="com.google.gwt.query.client.impl.SelectorEngineNative">
+ <when-type-assignable class="com.google.gwt.query.client.impl.SelectorEngineImpl"/>
+ <all>
+ <when-property-is name="user.agent" value="safari"/>
+ <when-property-is name="selectorCapability" value="native"/>
+ </all>
+ </replace-with>
+
+</module>
\ No newline at end of file diff --git a/gwtquery-core/src/main/java/gwtquery/client/$.java b/gwtquery-core/src/main/java/com/google/gwt/query/client/$.java index 273c951c..16487834 100644 --- a/gwtquery-core/src/main/java/gwtquery/client/$.java +++ b/gwtquery-core/src/main/java/com/google/gwt/query/client/$.java @@ -1,11 +1,12 @@ -package gwtquery.client; +package com.google.gwt.query.client; import com.google.gwt.dom.client.Element; import com.google.gwt.dom.client.Node; import com.google.gwt.dom.client.NodeList; /** - * + * A facade class of forwarding functions which allow end users to refer to + * the GQuery class as '$' if they desire. */ public class $ { diff --git a/gwtquery-core/src/main/java/com/google/gwt/query/client/DeferredGQuery.java b/gwtquery-core/src/main/java/com/google/gwt/query/client/DeferredGQuery.java new file mode 100644 index 00000000..28bb99ec --- /dev/null +++ b/gwtquery-core/src/main/java/com/google/gwt/query/client/DeferredGQuery.java @@ -0,0 +1,33 @@ +package com.google.gwt.query.client;
+
+import com.google.gwt.dom.client.Node;
+import com.google.gwt.dom.client.Element;
+import com.google.gwt.dom.client.NodeList;
+
+/**
+ * A compiled selector that can be lazily turned into a GQuery.
+ */
+public interface DeferredGQuery {
+
+ /**
+ * The selector which was compiled.
+ * @return
+ */
+ String getSelector();
+
+ /**
+ * Evaluate the compiled selector with the given DOM node as a context.
+ * Returns the result as a GQuery object.
+ * @param ctx
+ * @return
+ */
+ GQuery eval(Node ctx);
+
+ /**
+ * Evaluate the compiled selector with the given DOM node as a context.
+ * Returns a NodeList as a result.
+ * @param ctx
+ * @return
+ */
+ NodeList<Element> array(Node ctx);
+}
diff --git a/gwtquery-core/src/main/java/com/google/gwt/query/client/Effects.java b/gwtquery-core/src/main/java/com/google/gwt/query/client/Effects.java new file mode 100644 index 00000000..ac69eb6e --- /dev/null +++ b/gwtquery-core/src/main/java/com/google/gwt/query/client/Effects.java @@ -0,0 +1,109 @@ +package com.google.gwt.query.client;
+
+import com.google.gwt.animation.client.Animation;
+import com.google.gwt.dom.client.Element;
+import com.google.gwt.dom.client.NodeList;
+
+public class Effects extends GQuery {
+
+ static {
+ GQuery.registerPlugin(Effects.class, new EffectsPlugin());
+ }
+
+ public static final Class<Effects> Effects = Effects.class;
+
+ public Effects(Element element) {
+ super(element);
+ }
+
+ public Effects(JSArray elements) {
+ super(elements);
+ }
+
+ public Effects(NodeList list) {
+ super(list);
+ }
+
+ public Effects hide() {
+ this.css("display", "none");
+ return this;
+ }
+
+ public Effects show() {
+ this.css("display", "");
+ return this;
+ }
+
+ public boolean visible() {
+ return !"none".equalsIgnoreCase(this.css("display"));
+ }
+
+ public Effects toggle() {
+ for (Element e : elements()) {
+ Effects ef = new Effects(e);
+ if (ef.visible()) {
+ ef.hide();
+ } else {
+ ef.show();
+ }
+ }
+ return this;
+ }
+
+ public Effects fadeOut() {
+ Animation a = new Animation() {
+
+ public void onCancel() {
+ }
+
+ public void onComplete() {
+ for (int i = 0; i < elements.getLength(); i++) {
+ elements.getItem(i).getStyle().setProperty("opacity", "0");
+ elements.getItem(i).getStyle().setProperty("display", "none");
+ }
+ }
+
+ public void onStart() {
+ }
+
+ public void onUpdate(double progress) {
+ for (int i = 0; i < elements.getLength(); i++) {
+ elements.getItem(i).getStyle()
+ .setProperty("opacity", String.valueOf(1.0 - progress));
+ }
+ }
+ };
+ a.run(1000);
+ return this;
+ }
+
+ public Effects fadeIn() {
+ Animation a = new Animation() {
+
+ public void onCancel() {
+ }
+
+ public void onComplete() {
+ }
+
+ public void onStart() {
+ }
+
+ public void onUpdate(double progress) {
+ for (int i = 0; i < elements.getLength(); i++) {
+ elements.getItem(i).getStyle()
+ .setProperty("opacity", String.valueOf(progress));
+ }
+ }
+ };
+ a.run(1000);
+ return this;
+ }
+
+ public static class EffectsPlugin implements Plugin<Effects> {
+
+ public Effects init(GQuery gq) {
+ return new Effects(gq.get());
+ }
+ }
+}
diff --git a/gwtquery-core/src/main/java/com/google/gwt/query/client/Events.java b/gwtquery-core/src/main/java/com/google/gwt/query/client/Events.java new file mode 100644 index 00000000..14025ea9 --- /dev/null +++ b/gwtquery-core/src/main/java/com/google/gwt/query/client/Events.java @@ -0,0 +1,172 @@ +package com.google.gwt.query.client; + +import com.google.gwt.dom.client.Element; +import com.google.gwt.dom.client.NodeList; +import com.google.gwt.user.client.Event; + +/** + * GQuery Plugin for handling and queuing browser events + */ +public class Events extends GQuery { + + static { + GQuery.registerPlugin(Events.class, new Plugin<Events>() { + public Events init(GQuery gq) { + return new Events(gq.get()); + } + }); + } + + public static final Class<Events> Events = Events.class; + + public Events(Element element) { + super(element); + } + + public Events(JSArray elements) { + super(elements); + } + + public Events(NodeList<Element> list) { + super(list); + } + + /** + * Binds a handler to a particular Event for each matched element. + * + * The event handler is passed as a Function that you can use to prevent + * default behaviour. To stop both default action and event bubbling, the + * function event handler has to return false. + * + * You can pass an additional Object data to your Function as the second + * parameter + */ + public GQuery bind(int eventbits, final Object data, final Function f) { + for (Element e : elements()) { + EventsListener.getInstance(e).bind(eventbits, data, f); + } + return this; + } + + /** + * Fires an event on each matched element. + * + * Example: fire(Event.ONCLICK) + */ + public GQuery fire(int eventbits, int... keys) { + for (Element e : elements()) { + EventsListener.getInstance(e).fire(eventbits, keys); + } + return this; + } + + /** + * Removes all handlers, that matches the events bits passed, from each + * element. + * + * Example: unbind(Event.ONCLICK | Event.ONMOUSEOVER) + */ + public GQuery unbind(int eventbits) { + for (Element e : elements()) { + EventsListener.getInstance(e).unbind(eventbits); + } + return this; + } +} + +/** + * Just a class with static methods for firing element events on demand + */ +class FireEvents { + + private native static Event createMouseEventImpl(String type) /*-{ + var event = $doc.createEvent('MouseEvents'); + event.initEvent(type, true, true); + return event; + }-*/; + + private native static Event createKeyEventImpl(String type, int keycode) /*-{ + var event; + if( $wnd.KeyEvent ) { + event = $doc.createEvent('KeyEvents'); + event.initKeyEvent( type, true, true, $wnd, false, false, false, false, keycode, 0 ); + } else { + event = $doc.createEvent('UIEvents'); + event.initUIEvent( type, true, true, $wnd, 1 ); + event.keyCode = keycode; + } + return event; + }-*/; + + private native static Event createHtmlEventImpl(String type) /*-{ + var event = $doc.createEvent('HTMLEvents'); + event.initEvent( type, true, true); + return event; + }-*/; + + private native static void dispatchEvent(Element elem, Event event) /*-{ + elem.dispatchEvent(event); + if (event.type == 'focus' && elem.focus) + elem.focus(); + else if (event.type == 'blur' && elem.focus) + elem.blur(); + }-*/; + + private static String getEventTypeStr(int type) { + switch (type) { + case Event.ONBLUR: + return "blur"; + case Event.ONCHANGE: + return "change"; + case Event.ONCLICK: + return "click"; + case Event.ONDBLCLICK: + return "dblclick"; + case Event.ONFOCUS: + return "focus"; + case Event.ONKEYDOWN: + return "keydown"; + case Event.ONKEYPRESS: + return "keypress"; + case Event.ONKEYUP: + return "keyup"; + case Event.ONLOSECAPTURE: + return "losecapture"; + case Event.ONMOUSEDOWN: + return "mousedown"; + case Event.ONMOUSEMOVE: + return "mousemove"; + case Event.ONMOUSEOUT: + return "mouseout"; + case Event.ONMOUSEOVER: + return "mouseover"; + case Event.ONMOUSEUP: + return "mouseup"; + case Event.ONSCROLL: + return "scroll"; + case Event.ONERROR: + return "error"; + case Event.ONMOUSEWHEEL: + return "mousewheel"; + default: + return ""; + } + } + + public static void fire(Element element, int eventbits, int... keys) { + Event event = null; + + String type = getEventTypeStr(eventbits); + + if ((eventbits & Event.MOUSEEVENTS) != 0 + || (eventbits | Event.ONCLICK) == Event.ONCLICK) { + event = createMouseEventImpl(type); + } else if ((eventbits & Event.KEYEVENTS) != 0) { + event = createKeyEventImpl(type, keys[0]); + } else if ((eventbits & Event.FOCUSEVENTS) != 0) { + event = createHtmlEventImpl(type); + } + + dispatchEvent(element, event); + } +}
\ No newline at end of file diff --git a/gwtquery-core/src/main/java/com/google/gwt/query/client/EventsListener.java b/gwtquery-core/src/main/java/com/google/gwt/query/client/EventsListener.java new file mode 100644 index 00000000..37adcf60 --- /dev/null +++ b/gwtquery-core/src/main/java/com/google/gwt/query/client/EventsListener.java @@ -0,0 +1,124 @@ +package com.google.gwt.query.client; + +import com.google.gwt.user.client.EventListener; +import com.google.gwt.user.client.Event; +import com.google.gwt.user.client.DOM; +import com.google.gwt.dom.client.Element; + +import java.util.List; +import java.util.ArrayList; + + +/** + * This class implements an event queue instance for one element. + * This queue instance is configured as the default event listener in GWT. + * + * The reference to this queue is stored as a uniq variable in the element's DOM + * + * The class takes care of calling the appropiate functions for each browser event + * and also calls sinkEvents methods. + * + */ +class EventsListener implements EventListener { + + private class BindFunction { + int type; + Function function; + Object data; + int times = -1; + + BindFunction(int t, Function f, Object d) { + type = t; + function = f; + data = d; + } + + BindFunction(int t, Function f, Object d, int times) { + this(t, f, d); + this.times = times; + } + + public boolean hasEventType(int etype) { + return (type | etype) == type; + } + + public boolean fire(Event event) { + if (times != 0) { + times--; + return function.f(event, data); + } + return true; + } + } + + private native static EventsListener getWidgetElementImpl( + Element elem) /*-{ + return elem.__gqueryevent; + }-*/; + + private native static void setWidgetElementImpl(Element elem, EventsListener gqevent) /*-{ + elem.__gqueryevent = gqevent; + }-*/; + + private native static void setFocusable(Element elem) /*-{ + elem.tabIndex = 0; + }-*/; + + + private List<EventsListener.BindFunction> elementEvents = new ArrayList<EventsListener.BindFunction>(); + private Element element; + + private EventsListener(Element e) { + element = e; + setWidgetElementImpl(element, this); + DOM.setEventListener((com.google.gwt.user.client.Element) e, this); + } + + public static EventsListener getInstance(Element e) { + EventsListener ret = getWidgetElementImpl(e); + return ret != null ? ret : new EventsListener(e); + } + + public void bind(int eventbits, final Object data, final Function function, int times) { + if (function == null) { + unbind(eventbits); + } else { + DOM.sinkEvents((com.google.gwt.user.client.Element) element, eventbits + | DOM.getEventsSunk((com.google.gwt.user.client.Element) element)); + + if ((eventbits | Event.FOCUSEVENTS) == Event.FOCUSEVENTS) + setFocusable(element); + + elementEvents.add(new EventsListener.BindFunction(eventbits, function, data, times)); + } + } + + public void bind(int eventbits, final Object data, final Function function) { + bind(eventbits, data, function, -1); + } + + public void fire(int eventbits, int... keys) { + FireEvents.fire(element, eventbits, keys); + } + + public void onBrowserEvent(Event event) { + int etype = DOM.eventGetType(event); + for (EventsListener.BindFunction listener : elementEvents) { + if (listener.hasEventType(etype)) { + if (!listener.fire(event)) { + event.cancelBubble(true); + event.preventDefault(); + } + } + } + } + + public void unbind(int eventbits) { + ArrayList<EventsListener.BindFunction> newList = new ArrayList<EventsListener.BindFunction>(); + for (EventsListener.BindFunction listener : elementEvents) + if (!listener.hasEventType(eventbits)) + newList.add(listener); + elementEvents = newList; + } + +} 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 new file mode 100644 index 00000000..d9c093e3 --- /dev/null +++ b/gwtquery-core/src/main/java/com/google/gwt/query/client/Function.java @@ -0,0 +1,50 @@ +package com.google.gwt.query.client; + +import com.google.gwt.dom.client.Element; +import com.google.gwt.user.client.Event; + +/** + * Extend this class to implement functions callbacks. + */ +public abstract class Function { + + /** + * Override this for GQuery methods which loop over matched elements and + * invoke a callback on each element. + * @param e + * @param i + * @return + */ + public String f(Element e, int i) { + return ""; + } + + /** + * Override this for GQuery methods which take a callback, but do not expect + * a return value, apply to a single element only. + * @param e + */ + public void f(Element e) { + } + + /** + * Override this method for bound event handlers if you wish to deal with + * per-handler user data. + * @param e + * @param data + * @return + */ + public boolean f(Event e, Object data) { + return f(e); + } + + /** + * Override this method for bound event handlers. + * @param e + * @return + */ + public boolean f(Event e) { + f(e.getCurrentTarget()); + return true; + } +} diff --git a/gwtquery-core/src/main/java/gwtquery/client/GQuery.java b/gwtquery-core/src/main/java/com/google/gwt/query/client/GQuery.java index 3354bba8..74cc5b85 100644 --- a/gwtquery-core/src/main/java/gwtquery/client/GQuery.java +++ b/gwtquery-core/src/main/java/com/google/gwt/query/client/GQuery.java @@ -1,4 +1,4 @@ -package gwtquery.client;
+package com.google.gwt.query.client;
import com.google.gwt.core.client.GWT;
import com.google.gwt.core.client.JavaScriptObject;
@@ -15,16 +15,18 @@ import com.google.gwt.dom.client.OptionElement; import com.google.gwt.dom.client.SelectElement;
import com.google.gwt.dom.client.Style;
import com.google.gwt.dom.client.TextAreaElement;
+import static com.google.gwt.query.client.Effects.Effects;
+import static com.google.gwt.query.client.Events.Events;
+import com.google.gwt.query.client.impl.DocumentStyleImpl;
import com.google.gwt.user.client.DOM;
import com.google.gwt.user.client.Event;
-import com.google.gwt.user.client.EventListener;
import com.google.gwt.user.client.Window;
+import java.util.ArrayList;
import java.util.HashMap;
+import java.util.List;
import java.util.Map;
-import gwtquery.client.impl.DocumentStyleImpl;
-
/**
*
*/
@@ -42,7 +44,7 @@ public class GQuery { }
}
- private static class DataCache extends JavaScriptObject {
+ private static final class DataCache extends JavaScriptObject {
protected DataCache() {
}
@@ -112,7 +114,7 @@ public class GQuery { }-*/;
}
- private static class FastSet extends JavaScriptObject {
+ private static final class FastSet extends JavaScriptObject {
public static FastSet create() {
return JavaScriptObject.createObject().cast();
@@ -138,7 +140,7 @@ public class GQuery { }-*/;
private native boolean contains0(int hc) /*-{
- return this[hc];
+ return this[hc] || false;
}-*/;
private native void remove0(int hc) /*-{
@@ -146,7 +148,7 @@ public class GQuery { }-*/;
}
- private static class Queue<T> extends JavaScriptObject {
+ private static final class Queue<T> extends JavaScriptObject {
public static Queue newInstance() {
return createArray().cast();
@@ -282,6 +284,10 @@ public class GQuery { }
}
+ public static native String camelize(String s)/*-{
+ return s.toLowerCase().replace(/-([a-z])/ig, function(a, c){return c.toUpperCase()} );
+ }-*/;
+
public static void registerPlugin(Class<? extends GQuery> plugin,
Plugin<? extends GQuery> pluginFactory) {
if (plugins == null) {
@@ -358,13 +364,13 @@ public class GQuery { }
private static void ensureStyleImpl() {
- if (styleImpl != null) {
+ if (styleImpl == null) {
styleImpl = GWT.create(DocumentStyleImpl.class);
}
}
private static boolean hasClass(Element e, String clz) {
- return e.getClassName().matches("\\s" + clz + "\\s");
+ return ((" " + e.getClassName() + " ").matches(".*\\s" + clz + "\\s.*"));
}
private static GQuery innerHtml(String html) {
@@ -403,11 +409,24 @@ public class GQuery { elements = JSArray.create(element);
}
+ /**
+ * Add elements to the set of matched elements if they are not included yet.
+ */
+
public GQuery add(String selector) {
return add($(selector));
}
/**
+ * Adds the specified classes to each matched element. Add elements to the set
+ * of matched elements if they are not included yet.
+ */
+ public GQuery add(GQuery previousObject) {
+ return pushStack(unique(merge(elements, previousObject.elements)), "add",
+ getSelector() + "," + previousObject.getSelector());
+ }
+
+ /**
* Adds the specified classes to each matched element.
*/
public GQuery addClass(String... classes) {
@@ -483,6 +502,16 @@ public class GQuery { }
/**
+ * Append all of the matched elements to another, specified, set of elements.
+ * This operation is, essentially, the reverse of doing a regular
+ * $(A).append(B), in that instead of appending B to A, you're appending A to
+ * B.
+ */
+ public GQuery appendTo(GQuery other) {
+ return other.append(this);
+ }
+
+ /**
* Convert to Plugin interface provided by Class literal.
*/
public <T extends GQuery> T as(Class<T> plugin) {
@@ -499,13 +528,14 @@ public class GQuery { * Attributes include title, alt, src, href, width, style, etc.
*/
public String attr(String name) {
- return elements.getItem(0).getAttribute(name);
+ return elements.getItem(0).getAttribute(fixAttributeName(name));
}
/**
* Set a single property to a value, on all matched elements.
*/
public GQuery attr(String key, String value) {
+ key = fixAttributeName(key);
for (Element e : elements()) {
e.setAttribute(key, value);
}
@@ -514,11 +544,14 @@ public class GQuery { /**
* Set a key/value object as properties to all matched elements.
+ *
+ * Example: $("img").attr(new Properties("src: 'test.jpg', alt: 'Test
+ * Image'"))
*/
public GQuery attr(Properties properties) {
for (Element e : elements()) {
for (String name : properties.keys()) {
- e.setAttribute(name, properties.get(name));
+ e.setAttribute(fixAttributeName(name), properties.get(name));
}
}
return this;
@@ -530,7 +563,7 @@ public class GQuery { public GQuery attr(String key, Function closure) {
for (int i = 0; i < elements.getLength(); i++) {
Element e = elements.getItem(i);
- e.setAttribute(key, closure.f(e, i));
+ e.setAttribute(fixAttributeName(key), closure.f(e, i));
}
return this;
}
@@ -541,7 +574,7 @@ public class GQuery { * another if it's not in the page).
*/
public GQuery before(Node n) {
- return domManip(JSArray.create(n), FUNC_AFTER);
+ return domManip(JSArray.create(n), FUNC_BEFORE);
}
/**
@@ -550,7 +583,7 @@ public class GQuery { * another if it's not in the page).
*/
public GQuery before(GQuery query) {
- return domManip(query.elements, FUNC_AFTER);
+ return domManip(query.elements, FUNC_BEFORE);
}
/**
@@ -559,46 +592,62 @@ public class GQuery { * another if it's not in the page).
*/
public GQuery before(String html) {
- return domManip(html, FUNC_AFTER);
+ return domManip(html, FUNC_BEFORE);
}
/**
- * Binds a handler to one or more events (like click) for each matched
+ * Binds a handler to a particular Event (like Event.ONCLICK) for each matched
* element.
+ *
+ * The event handler is passed as a Function that you can use to prevent
+ * default behaviour. To stop both default action and event bubbling, the
+ * function event handler has to return false.
+ *
+ * You can pass an additional Object data to your Function as the second
+ * parameter
*/
public GQuery bind(int eventbits, final Object data, final Function f) {
- EventListener listener = new EventListener() {
- public void onBrowserEvent(Event event) {
- if (!f.f(event, data)) {
- event.cancelBubble(true);
- event.preventDefault();
- }
- }
- };
- for (Element e : elements()) {
- DOM.sinkEvents((com.google.gwt.user.client.Element) e, eventbits);
- DOM.setEventListener((com.google.gwt.user.client.Element) e, listener);
- }
- return this;
+ return as(Events).bind(eventbits, data, f);
}
+ /**
+ * Bind a function to the blur event of each matched element.
+ */
public GQuery blur(Function f) {
return bind(Event.ONBLUR, null, f);
}
+ /**
+ * Trigger a blur event.
+ */
public GQuery blur() {
return trigger(Document.get().createBlurEvent(), null);
}
+ /**
+ * Bind a function to the change event of each matched element.
+ */
public GQuery change(Function f) {
return bind(Event.ONCHANGE, null, f);
}
+ /**
+ * Trigger a change event.
+ */
public GQuery change() {
return trigger(Document.get().createChangeEvent(), null);
}
/**
+ * Get a set of elements containing all of the unique children of each of the
+ * matched set of elements. This set is filtered with the expressions that
+ * will cause only elements matching any of the selectors to be collected.
+ */
+ public GQuery children(String... filters) {
+ return find(filters);
+ }
+
+ /**
* Get a set of elements containing all of the unique immediate children of
* each of the matched set of elements. Also note: while parents() will look
* at all ancestors, children() will only consider immediate child elements.
@@ -606,11 +655,14 @@ public class GQuery { public GQuery children() {
JSArray result = JSArray.create();
for (Element e : elements()) {
- allNextSiblingElements(e.getFirstChildElement(), result);
+ allNextSiblingElements(e.getFirstChildElement(), result, null);
}
return new GQuery(unique(result));
}
+ /**
+ * Trigger a click event.
+ */
public GQuery click() {
return trigger(
Document.get().createClickEvent(0, 0, 0, 0, 0, false, false, false,
@@ -638,6 +690,19 @@ public class GQuery { }
/**
+ * Filter the set of elements to those that contain the specified text.
+ */
+ public GQuery contains(String text) {
+ JSArray array = JSArray.create();
+ for (Element e : elements()) {
+ if ($(e).text().contains(text)) {
+ array.addNode(e);
+ }
+ }
+ return $(array);
+ }
+
+ /**
* Find all the child nodes inside the matched elements (including text
* nodes), or the content document, if the element is an iframe.
*/
@@ -665,14 +730,12 @@ public class GQuery { }
/**
- * Set a key/value object as style properties to all matched elements. This is
- * the best way to set several style properties on all matched elements. Be
- * aware, however, that when the key contains a hyphen, such as
- * "background-color," it must either be placed within quotation marks or be
- * written in camel case like so: backgroundColor. As "float" and "class" are
- * reserved words in JavaScript, it's recommended to always surround those
- * terms with quotes. gQuery normalizes the "opacity" property in Internet
- * Explorer.
+ * Set a key/value object as style properties to all matched elements. This
+ * serves as the best way to set a large number of style properties on all
+ * matched elements.
+ *
+ * Example: $(".item").css(Properties.create("color: 'red', background:
+ * 'blue'"))
*/
public GQuery css(Properties properties) {
for (String property : properties.keys()) {
@@ -682,12 +745,17 @@ public class GQuery { }
/**
- * Set a single style property to a value on all matched elements. If a number
- * is provided, it is automatically converted into a pixel value.
+ * Set a single style property to a value, on all matched elements.
*/
public GQuery css(String prop, String val) {
for (Element e : elements()) {
- e.getStyle().setProperty(prop, val);
+ String property = camelize(prop);
+ e.getStyle().setProperty(property, val);
+ if ("opacity".equals(property)) {
+ e.getStyle().setProperty("zoom", "1");
+ e.getStyle().setProperty("filter",
+ "alpha(opacity=" + (int) (Double.valueOf(val) * 100) + ")");
+ }
}
return this;
}
@@ -719,12 +787,18 @@ public class GQuery { }
}
+ /**
+ * Trigger a double click event.
+ */
public GQuery dblclick() {
return trigger(
Document.get().createDblClickEvent(0, 0, 0, 0, 0, false, false, false,
false), null);
}
+ /**
+ * Bind a function to the dblclick event of each matched element.
+ */
public GQuery dblclick(Function f) {
return bind(Event.ONDBLCLICK, null, f);
}
@@ -796,15 +870,51 @@ public class GQuery { return $(elements.getItem(pos));
}
+ /**
+ * Trigger an error event.
+ */
public GQuery error() {
return trigger(Document.get().createErrorEvent(), null);
}
+ /**
+ * Bind a function to the error event of each matched element.
+ */
public GQuery error(Function f) {
return bind(Event.ONERROR, null, f);
}
/**
+ * Fade in all matched elements by adjusting their opacity.
+ */
+ public GQuery fadeIn(int millisecs) {
+ return $(as(Effects).fadeIn(millisecs));
+ }
+
+ /**
+ * Fade in all matched elements by adjusting their opacity. The effect will
+ * take 1000 milliseconds to complete
+ */
+ public GQuery fadeIn() {
+ return $(as(Effects).fadeIn());
+ }
+
+ /**
+ * Fade out all matched elements by adjusting their opacity.
+ */
+ public GQuery fadeOut(int millisecs) {
+ return $(as(Effects).fadeOut(millisecs));
+ }
+
+ /**
+ * Fade out all matched elements by adjusting their opacity. The effect will
+ * take 1000 milliseconds to complete
+ */
+ public GQuery fadeOut() {
+ return $(as(Effects).fadeOut());
+ }
+
+ /**
* Removes all elements from the set of matched elements that do not match the
* specified function. The function is called with a context equal to the
* current element. If the function returns false, then the element is removed
@@ -821,10 +931,58 @@ public class GQuery { return pushStack(result, "filter", selector);
}
+ /**
+ * Removes all elements from the set of matched elements that do not pass the
+ * specified css expression. This method is used to narrow down the results of
+ * a search. Provide a comma-separated list of expressions to apply multiple
+ * filters at once.
+ */
+ public GQuery filter(String... filters) {
+ JSArray array = JSArray.create();
+ for (String f : filters) {
+ for (Element e : elements()) {
+ for (Element c : $(f, e.getParentNode()).elements()) {
+ if (c == e) {
+ array.addNode(c);
+ break;
+ }
+ }
+ }
+ }
+ return $(unique(array));
+ }
+
+ /**
+ * Searches for all elements that match the specified css expression. This
+ * method is a good way to find additional descendant elements with which to
+ * process.
+ *
+ * Provide a comma-separated list of expressions to apply multiple filters at
+ * once.
+ */
+ public GQuery find(String... filters) {
+ JSArray array = JSArray.create();
+ for (String selector : filters) {
+ for (Element e : elements()) {
+ for (Element c : $(selector, e).elements()) {
+ array.addNode(c);
+ }
+ }
+ }
+ return $(unique(array));
+ }
+
+ /**
+ * Trigger a focus event.
+ */
public GQuery focus() {
return trigger(Document.get().createFocusEvent(), null);
}
+ /**
+ * Bind a function to the focus event of each matched element.
+ */
+
public GQuery focus(Function f) {
return bind(Event.ONFOCUS, null, f);
}
@@ -844,16 +1002,33 @@ public class GQuery { return elements.getItem(i);
}
+ /**
+ * Return the previous set of matched elements prior to the last destructive
+ * operation (e.g. query)
+ */
public GQuery getPreviousObject() {
return previousObject;
}
+ /**
+ * Return the selector representing the current set of matched elements.
+ */
public String getSelector() {
return selector;
}
/**
* Returns true any of the specified classes are present on any of the matched
+ * Reduce the set of matched elements to all elements after a given position.
+ * The position of the element in the set of matched elements starts at 0 and
+ * goes to length - 1.
+ */
+ public GQuery gt(int pos) {
+ return $(slice(pos + 1, -1));
+ }
+
+ /**
+ * Returns true any of the specified classes are present on any of the matched
* elements.
*/
public boolean hasClass(String... classes) {
@@ -878,6 +1053,43 @@ public class GQuery { }
/**
+ * Set the height style property of every matched element. It's useful for
+ * using 'percent' or 'em' units Example: $(".a").width("100%")
+ */
+ public GQuery height(String height) {
+ return css("height", height);
+ }
+
+ /**
+ * Get the current computed, pixel, height of the first matched element.
+ */
+ public int height() {
+ return DOM
+ .getElementPropertyInt((com.google.gwt.user.client.Element) get(0),
+ "offsetHeight");
+ }
+
+ /**
+ * Make invisible all matched elements
+ */
+ public GQuery hide() {
+ return $(as(Effects).hide());
+ }
+
+ /**
+ * Bind a function to the mouseover event of each matched element. A method
+ * for simulating hovering (moving the mouse on, and off, an object). This is
+ * a custom method which provides an 'in' to a frequent task. Whenever the
+ * mouse cursor is moved over a matched element, the first specified function
+ * is fired. Whenever the mouse moves off of the element, the second specified
+ * function fires.
+ */
+ public GQuery hover(Function fover, Function fout) {
+ return bind(Event.ONMOUSEOVER, null, fover)
+ .bind(Event.ONMOUSEOUT, null, fout);
+ }
+
+ /**
* Get the innerHTML of the first matched element.
*/
public String html() {
@@ -969,56 +1181,118 @@ public class GQuery { return insertBefore($(selector));
}
+ /**
+ * Checks the current selection against an expression and returns true, if at
+ * least one element of the selection fits the given expression. Does return
+ * false, if no element fits or the expression is not valid.
+ */
+ public boolean is(String... filters) {
+ return filter(filters).size() > 0;
+ }
+
+ /**
+ * Trigger a keydown event.
+ */
public GQuery keydown() {
return trigger(
Document.get().createKeyDownEvent(false, false, false, false, 0, 0),
null);
}
+ /**
+ * Bind a function to the keydown event of each matched element.
+ */
public GQuery keydown(Function f) {
return bind(Event.ONKEYDOWN, null, f);
}
+ /**
+ * Trigger a keypress event.
+ */
public GQuery keypress() {
return trigger(
Document.get().createKeyPressEvent(false, false, false, false, 0, 0),
null);
}
+ /**
+ * Bind a function to the keypress event of each matched element.
+ */
public GQuery keypressed(Function f) {
return bind(Event.ONKEYPRESS, null, f);
}
+ /**
+ * Trigger a keyup event.
+ */
public GQuery keyup() {
return trigger(
Document.get().createKeyUpEvent(false, false, false, false, 0, 0),
null);
}
+ /**
+ * Bind a function to the keyup event of each matched element.
+ */
public GQuery keyup(Function f) {
return bind(Event.ONKEYUP, null, f);
}
+ /**
+ * Returns the number of elements currently matched. The size function will
+ * return the same value.
+ */
+ public int length() {
+ return size();
+ }
+
+ /**
+ * Bind a function to the load event of each matched element.
+ */
public GQuery load(Function f) {
return bind(Event.ONLOAD, null, f);
}
+ /**
+ * Reduce the set of matched elements to all elements before a given position.
+ * The position of the element in the set of matched elements starts at 0 and
+ * goes to length - 1.
+ */
+ public GQuery lt(int pos) {
+ return $(slice(0, pos));
+ }
+
+ /**
+ * Bind a function to the mousedown event of each matched element.
+ */
public GQuery mousedown(Function f) {
return bind(Event.ONMOUSEDOWN, null, f);
}
+ /**
+ * Bind a function to the mousemove event of each matched element.
+ */
public GQuery mousemove(Function f) {
return bind(Event.ONMOUSEMOVE, null, f);
}
+ /**
+ * Bind a function to the mouseout event of each matched element.
+ */
public GQuery mouseout(Function f) {
return bind(Event.ONMOUSEOUT, null, f);
}
+ /**
+ * Bind a function to the mouseover event of each matched element.
+ */
public GQuery mouseover(Function f) {
return bind(Event.ONMOUSEOVER, null, f);
}
+ /**
+ * Bind a function to the mouseup event of each matched element.
+ */
public GQuery mouseup(Function f) {
return bind(Event.ONMOUSEUP, null, f);
}
@@ -1040,16 +1314,71 @@ public class GQuery { }
/**
+ * Get a set of elements containing the unique next siblings of each of the
+ * given set of elements filtered by 1 or more selectors. next only returns
+ * the very next sibling for each element, not all next siblings see
+ * {#nextAll}.
+ */
+ public GQuery next(String... selectors) {
+ JSArray result = JSArray.create();
+ for (Element e : elements()) {
+ Element next = e.getNextSiblingElement();
+ if (next != null) {
+ result.addNode(next);
+ }
+ }
+ return new GQuery(unique(result)).filter(selectors);
+ }
+
+ /**
* Find all sibling elements after the current element.
*/
public GQuery nextAll() {
JSArray result = JSArray.create();
for (Element e : elements()) {
- allNextSiblingElements(e.getNextSiblingElement(), result);
+ allNextSiblingElements(e.getNextSiblingElement(), result, null);
}
return new GQuery(unique(result));
}
+ /**
+ * Removes the specified Element from the set of matched elements. This method
+ * is used to remove a single Element from a jQuery object.
+ */
+ public GQuery not(Element elem) {
+ JSArray array = JSArray.create();
+ for (Element e : elements()) {
+ if (e != elem) {
+ array.addNode(e);
+ }
+ }
+ return $(array);
+ }
+
+ /**
+ * Removes any elements inside the passed set of elements from the set of
+ * matched elements.
+ */
+ public GQuery not(GQuery gq) {
+ GQuery ret = this;
+ for (Element e : gq.elements()) {
+ ret = ret.not(e);
+ }
+ return ret;
+ }
+
+ /**
+ * Removes elements matching the specified expression from the set of matched
+ * elements.
+ */
+ public GQuery not(String... filters) {
+ GQuery ret = this;
+ for (String f : filters) {
+ ret = ret.not($(f));
+ }
+ return ret;
+ }
+
public Offset offset() {
return new Offset(get(0).getOffsetLeft(), get(0).getOffsetTop());
}
@@ -1071,6 +1400,24 @@ public class GQuery { }
/**
+ * Binds a handler to a particular Event (like Event.ONCLICK) for each matched
+ * element. The handler is executed only once for each element.
+ *
+ * The event handler is passed as a Function that you can use to prevent
+ * default behaviour. To stop both default action and event bubbling, the
+ * function event handler has to return false.
+ *
+ * You can pass an additional Object data to your Function as the second
+ * parameter
+ */
+ public GQuery one(int eventbits, final Object data, final Function f) {
+ for (Element e : elements()) {
+ EventsListener.getInstance(e).bind(eventbits, data, f, 1);
+ }
+ return this;
+ }
+
+ /**
* Get a set of elements containing the unique parents of the matched set of
* elements.
*/
@@ -1083,6 +1430,24 @@ public class GQuery { }
/**
+ * Get a set of elements containing the unique parents of the matched set of
+ * elements. You may use an optional expressions to filter the set of parent
+ * elements that will match one of them.
+ */
+ public GQuery parent(String... filters) {
+ return parent().filter(filters);
+ }
+
+ /**
+ * Get a set of elements containing the unique ancestors of the matched set of
+ * elements (except for the root element). The matched elements are filtered,
+ * returning those that match any of the filters.
+ */
+ public GQuery parents(String... filters) {
+ return parents().filter(filters);
+ }
+
+ /**
* Get a set of elements containing the unique ancestors of the matched set of
* elements (except for the root element).
*/
@@ -1151,6 +1516,16 @@ public class GQuery { }
/**
+ * Prepend all of the matched elements to another, specified, set of elements.
+ * This operation is, essentially, the reverse of doing a regular
+ * $(A).prepend(B), in that instead of prepending B to A, you're prepending A
+ * to B.
+ */
+ public GQuery prependTo(GQuery elms) {
+ return elms.prepend(this);
+ }
+
+ /**
* Get a set of elements containing the unique previous siblings of each of
* the matched set of elements. Only the immediately previous sibling is
* returned, not all previous siblings.
@@ -1167,6 +1542,22 @@ public class GQuery { }
/**
+ * Get a set of elements containing the unique previous siblings of each of
+ * the matched set of elements filtered by selector. Only the immediately
+ * previous sibling is returned, not all previous siblings.
+ */
+ public GQuery prev(String... selectors) {
+ JSArray result = JSArray.create();
+ for (Element e : elements()) {
+ Element next = getPreviousSiblingElement(e);
+ if (next != null) {
+ result.addNode(next);
+ }
+ }
+ return new GQuery(unique(result)).filter(selectors);
+ }
+
+ /**
* Find all sibling elements in front of the current element.
*/
public GQuery prevAll() {
@@ -1324,6 +1715,9 @@ public class GQuery { return replaceWith($(elem));
}
+ /**
+ * Bind a function to the scroll event of each matched element.
+ */
public GQuery scroll(Function f) {
return bind(Event.ONSCROLL, null, f);
}
@@ -1404,6 +1798,14 @@ public class GQuery { }
/**
+ * Return the number of elements in the matched set. Make visible all mached
+ * elements
+ */
+ public GQuery show() {
+ return $(as(Effects).show());
+ }
+
+ /**
* Get a set of elements containing all of the unique siblings of each of the
* matched set of elements.
*/
@@ -1411,12 +1813,20 @@ public class GQuery { JSArray result = JSArray.create();
for (Element e : elements()) {
allNextSiblingElements(e.getParentElement().getFirstChildElement(),
- result);
+ result, e);
}
return new GQuery(unique(result));
}
/**
+ * Get a set of elements containing all of the unique siblings of each of the
+ * matched set of elements filtered by the provided set of selectors.
+ */
+ public GQuery siblings(String... selectors) {
+ return siblings().filter(selectors);
+ }
+
+ /**
* Return the number of elements in the matched set.
*/
public int size() {
@@ -1431,7 +1841,7 @@ public class GQuery { if (end == -1 || end > elements.getLength()) {
end = elements.getLength();
}
- for (int i = start; i < elements.getLength(); i++) {
+ for (int i = start; i < end; i++) {
slice.addNode(elements.getItem(i));
}
return new GQuery(slice);
@@ -1446,7 +1856,11 @@ public class GQuery { * Return the text contained in the first matched element.
*/
public String text() {
- return elements.getItem(0).getInnerText();
+ String result="";
+ for(Element e : elements()) {
+ result += e.getInnerText();
+ }
+ return result;
}
/**
@@ -1460,6 +1874,20 @@ public class GQuery { }
/**
+ * Toggle among two or more function calls every other click.
+ */
+ public GQuery toggle(final Function... fn) {
+ return click(new Function() {
+ int click = 0;
+
+ @Override
+ public boolean f(Event e) {
+ return fn[(click++ % fn.length)].f(e);
+ }
+ });
+ }
+
+ /**
* Adds or removes the specified classes to each matched element.
*/
public GQuery toggleClass(String... classes) {
@@ -1486,6 +1914,38 @@ public class GQuery { }
/**
+ * Produces a string representation of the matched elements
+ */
+ public String toString() {
+ return toString(false);
+ }
+
+ /**
+ * Produces a string representation of the matched elements
+ */
+ public String toString(boolean pretty) {
+ String r = "";
+ for (Element e : elements()) {
+ r += (pretty && r.length() > 0 ? "\n " : "") + e.getString();
+ }
+ return r;
+ }
+
+ /**
+ * Trigger an event of type eventbits on every matched element.
+ */
+ public GQuery trigger(int eventbits, int... keys) {
+ return as(Events).fire(eventbits, keys);
+ }
+
+ /**
+ * Removes all events that match the eventbits
+ */
+ public GQuery unbind(int eventbits) {
+ return as(Events).unbind(eventbits);
+ }
+
+ /**
* Remove all duplicate elements from an array of elements. Note that this
* only works on arrays of DOM elements, not strings or numbers.
*/
@@ -1503,69 +1963,63 @@ public class GQuery { }
/**
- * Get the content of the value attribute of the first matched element,
- * returns more than one value if it is a multiple select.
+ * Gets the content of the value attribute of the first matched element,
+ * returns only the first value even if it is a multivalued element. To get an
+ * array of all values in multivalues elements use vals()
+ *
+ * When the first element is a radio-button and is not checked, then it looks
+ * for a the first checked radio-button that has the same name in the list of
+ * matched elements.
*/
- public String[] val() {
- if (size() > 0) {
- Element e = get(0);
- if (e.getNodeName().equals("select")) {
- SelectElement se = SelectElement.as(e);
- if (se.getMultiple() != null) {
- NodeList<OptionElement> oel = se.getOptions();
- int count = 0;
- for (OptionElement oe : asArray(oel)) {
- if (oe.isSelected()) {
- count++;
- }
- }
- String result[] = new String[count];
- count = 0;
- for (OptionElement oe : asArray(oel)) {
- if (oe.isSelected()) {
- result[count++] = oe.getValue();
- }
- }
-
- return result;
- } else {
- int index = se.getSelectedIndex();
- if (index != -1) {
- return new String[]{se.getOptions().getItem(index).getValue()};
- }
- }
- } else if (e.getNodeName().equals("input")) {
- InputElement ie = InputElement.as(e);
- return new String[]{ie.getValue()};
- }
- }
- return new String[0];
+ public String val() {
+ String[] v = vals();
+ return (v != null && v.length > 0) ? v[0] : "";
}
+ /**
+ * Sets the value attribute of every matched element In the case of multivalue
+ * elements, all values are setted for other elements, only the first value is
+ * considered.
+ */
public GQuery val(String... values) {
for (Element e : elements()) {
String name = e.getNodeName();
- if ("select".equals(name)) {
-
- } else if ("input".equals(name)) {
+ if ("select".equalsIgnoreCase(name)) {
+ SelectElement s = SelectElement.as(e);
+ if (values.length > 1 && s.isMultiple()) {
+ s.setSelectedIndex(-1);
+ for (String v : values) {
+ for (int i = 0; i < s.getOptions().getLength(); i++) {
+ if (v.equals(s.getOptions().getItem(i).getValue())) {
+ s.getOptions().getItem(i).setSelected(true);
+ }
+ }
+ }
+ } else {
+ s.setValue(values[0]);
+ }
+ } else if ("input".equalsIgnoreCase(name)) {
InputElement ie = InputElement.as(e);
String type = ie.getType();
- if ("radio".equals((type)) || "checkbox".equals(type)) {
- if ("checkbox".equals(type)) {
+ if ("radio".equalsIgnoreCase((type)) || "checkbox"
+ .equalsIgnoreCase(type)) {
+ if ("checkbox".equalsIgnoreCase(type)) {
for (String val : values) {
if (ie.getValue().equals(val)) {
ie.setChecked(true);
- } else if (ie.getValue().equals(val)) {
- ie.setChecked(true);
+ } else {
+ ie.setChecked(false);
}
}
+ } else if (values[0].equals(ie.getValue())) {
+ ie.setChecked(true);
}
} else {
ie.setValue(values[0]);
}
- } else if ("textarea".equals(name)) {
+ } else if ("textarea".equalsIgnoreCase(name)) {
TextAreaElement.as(e).setValue(values[0]);
- } else if ("button".equals(name)) {
+ } else if ("button".equalsIgnoreCase(name)) {
ButtonElement.as(e).setValue(values[0]);
}
}
@@ -1573,6 +2027,69 @@ public class GQuery { }
/**
+ * Gets the content of the value attribute of the first matched element,
+ * returns more than one value if it is a multiple select.
+ *
+ * When the first element is a radio-button and is not checked, then it looks
+ * for a the first checked radio-button that has the same name in the list of
+ * matched elements.
+ *
+ * This method always returns an array. If no valid value can be determined
+ * the array will be empty, otherwise it will contain one or more values.
+ */
+ public String[] vals() {
+ if (size() > 0) {
+ Element e = get(0);
+ if (e.getNodeName().equalsIgnoreCase("select")) {
+ SelectElement se = SelectElement.as(e);
+ if (se.isMultiple()) {
+ List<String> result = new ArrayList<String>();
+ for (OptionElement oe : asArray(se.getOptions())) {
+ if (oe.isSelected()) {
+ result.add(oe.getValue());
+ }
+ }
+ return result.toArray(new String[0]);
+ } else if (se.getSelectedIndex() >= 0) {
+ return new String[]{
+ se.getOptions().getItem(se.getSelectedIndex()).getValue()};
+ }
+ } else if (e.getNodeName().equalsIgnoreCase("input")) {
+ InputElement ie = InputElement.as(e);
+ if ("radio".equalsIgnoreCase(ie.getType())) {
+ for (Element e2 : elements()) {
+ if ("input".equalsIgnoreCase(e2.getNodeName())) {
+ InputElement ie2 = InputElement.as(e2);
+ if ("radio".equalsIgnoreCase(ie2.getType()) && ie2.isChecked()
+ && ie.getName().equals(ie2.getName())) {
+ return new String[]{ie2.getValue()};
+ }
+ }
+ }
+ } else if ("checkbox".equalsIgnoreCase(ie.getType())) {
+ if (ie.isChecked()) {
+ return new String[]{ie.getValue()};
+ }
+ } else {
+ return new String[]{ie.getValue()};
+ }
+ } else if (e.getNodeName().equalsIgnoreCase("textarea")) {
+ return new String[]{TextAreaElement.as(e).getValue()};
+ } else if (e.getNodeName().equalsIgnoreCase("button")) {
+ return new String[]{ButtonElement.as(e).getValue()};
+ }
+ }
+ return new String[0];
+ }
+
+ /**
+ * Return true if the first element is visible.
+ */
+ public boolean visible() {
+ return as(Effects).visible();
+ }
+
+ /**
* Set the width of every matched element.
*/
public GQuery width(int width) {
@@ -1583,6 +2100,15 @@ public class GQuery { }
/**
+ * Get the current computed, pixel, width of the first matched element.
+ */
+ public int width() {
+ return DOM
+ .getElementPropertyInt((com.google.gwt.user.client.Element) get(0),
+ "offsetWidth");
+ }
+
+ /**
* Wrap each matched element with the specified HTML content. This wrapping
* process is most useful for injecting additional structure into a document,
* without ruining the original semantic qualities of a document. This works
@@ -1732,15 +2258,12 @@ public class GQuery { return g;
}
- private GQuery add(GQuery previousObject) {
- return pushStack(unique(merge(elements, previousObject.elements)), "add",
- getSelector() + "," + previousObject.getSelector());
- }
-
- private void allNextSiblingElements(Element firstChildElement,
- JSArray result) {
+ private void allNextSiblingElements(Element firstChildElement, JSArray result,
+ Element elem) {
while (firstChildElement != null) {
- result.addNode(firstChildElement);
+ if (firstChildElement != elem) {
+ result.addNode(firstChildElement);
+ }
firstChildElement = firstChildElement.getNextSiblingElement();
}
}
@@ -1761,7 +2284,7 @@ public class GQuery { wrapPos = 1;
preWrap = "<select multiple=\"multiple\">";
postWrap = "</select>";
- } else if (!tags.contains("<leg")) {
+ } else if (tags.contains("<legend")) {
wrapPos = 1;
preWrap = "<fieldset>";
postWrap = "</fieldset>";
@@ -1790,7 +2313,7 @@ public class GQuery { n = n.getLastChild();
}
//TODO: add fixes for IE TBODY issue
- return JSArray.create(n);
+ return n.getChildNodes().cast();
}
private <S> Object data(Element item, String name, S value) {
@@ -1832,7 +2355,9 @@ public class GQuery { private GQuery domManip(NodeList nodes, int func) {
for (Element e : elements()) {
for (int i = 0; i < nodes.getLength(); i++) {
- Node n = nodes.getItem(i).cloneNode(true);
+ Node n = nodes.getItem(i);
+ if(size() > 1) n=n.cloneNode(true);
+
switch (func) {
case FUNC_PREPEND:
e.insertBefore(n, e.getFirstChild());
@@ -1852,6 +2377,10 @@ public class GQuery { return this;
}
+ private String fixAttributeName(String key) {
+ return key;
+ }
+
private native Document getContentDocument(Node n) /*-{
return n.contentDocument || n.contentWindow.document;
}-*/;
diff --git a/gwtquery-core/src/main/java/gwtquery/client/JSArray.java b/gwtquery-core/src/main/java/com/google/gwt/query/client/JSArray.java index a8049347..87242f56 100644 --- a/gwtquery-core/src/main/java/gwtquery/client/JSArray.java +++ b/gwtquery-core/src/main/java/com/google/gwt/query/client/JSArray.java @@ -1,4 +1,4 @@ -package gwtquery.client;
+package com.google.gwt.query.client;
import com.google.gwt.core.client.JavaScriptObject;
import com.google.gwt.dom.client.Element;
diff --git a/gwtquery-core/src/main/java/com/google/gwt/query/client/Plugin.java b/gwtquery-core/src/main/java/com/google/gwt/query/client/Plugin.java new file mode 100644 index 00000000..64b92af6 --- /dev/null +++ b/gwtquery-core/src/main/java/com/google/gwt/query/client/Plugin.java @@ -0,0 +1,16 @@ +package com.google.gwt.query.client;
+
+/**
+ * A GQuery plugin. All GQuery plugins must implement this interface.
+ */
+public interface Plugin<T extends GQuery> {
+
+ /**
+ * Called by the GQuery.as() method in order to pass the current matched
+ * set. Typically a plugin will want to call a super class copy constructor
+ * in order to copy the internal matched set of elements.
+ * @param gQuery
+ * @return
+ */
+ T init(GQuery gQuery);
+}
diff --git a/gwtquery-core/src/main/java/gwtquery/client/Predicate.java b/gwtquery-core/src/main/java/com/google/gwt/query/client/Predicate.java index feb7b644..89d78f28 100644 --- a/gwtquery-core/src/main/java/gwtquery/client/Predicate.java +++ b/gwtquery-core/src/main/java/com/google/gwt/query/client/Predicate.java @@ -1,4 +1,4 @@ -package gwtquery.client; +package com.google.gwt.query.client; import com.google.gwt.dom.client.Element; 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 new file mode 100644 index 00000000..c2954a49 --- /dev/null +++ b/gwtquery-core/src/main/java/com/google/gwt/query/client/Properties.java @@ -0,0 +1,51 @@ +package com.google.gwt.query.client;
+
+import com.google.gwt.core.client.JavaScriptObject;
+import com.google.gwt.core.client.JsArrayString;
+
+/**
+ * JSO for accessing Javascript objective literals used by GwtQuery functions.
+ */
+public class Properties extends JavaScriptObject {
+
+ protected Properties() {
+ }
+
+ public static Properties create(String properties) {
+ String s = properties.replaceFirst("^[({]*(.*)[})]*$", "({$1})");
+ return (Properties) createImpl(s);
+ }
+
+ public final native static JavaScriptObject createImpl(String properties) /*-{
+ return eval(properties);
+ }-*/;
+
+ public final native String get(String name) /*-{
+ return this[name];
+ }-*/;
+
+ public final native int getInt(String name) /*-{
+ return this[name];
+ }-*/;
+
+ public final native float getFloat(String name) /*-{
+ return this[name];
+ }-*/;
+
+ public final String[] keys() {
+ JsArrayString a = keysImpl();
+ String[] ret = new String[a.length()];
+ for (int i = 0; i < a.length(); i++) {
+ ret[i] = "" + a.get(i);
+ }
+ return ret;
+ }
+
+ public final native JsArrayString keysImpl() /*-{
+ var key, keys=[];
+ for(key in this) {
+ keys.push("" + key);
+ }
+ return keys;
+ }-*/;
+}
diff --git a/gwtquery-core/src/main/java/gwtquery/client/Regexp.java b/gwtquery-core/src/main/java/com/google/gwt/query/client/Regexp.java index 5eae1c1d..8a33ffd6 100644 --- a/gwtquery-core/src/main/java/gwtquery/client/Regexp.java +++ b/gwtquery-core/src/main/java/com/google/gwt/query/client/Regexp.java @@ -1,8 +1,9 @@ -package gwtquery.client;
+package com.google.gwt.query.client;
import com.google.gwt.core.client.JavaScriptObject;
/**
+ * A wrapper around Javascript Regexps.
*/
public class Regexp {
diff --git a/gwtquery-core/src/main/java/gwtquery/client/Selector.java b/gwtquery-core/src/main/java/com/google/gwt/query/client/Selector.java index 0ec58e5e..23eae98e 100644 --- a/gwtquery-core/src/main/java/gwtquery/client/Selector.java +++ b/gwtquery-core/src/main/java/com/google/gwt/query/client/Selector.java @@ -1,4 +1,4 @@ -package gwtquery.client;
+package com.google.gwt.query.client;
import java.lang.annotation.Target;
import java.lang.annotation.Retention;
diff --git a/gwtquery-core/src/main/java/gwtquery/client/SelectorEngine.java b/gwtquery-core/src/main/java/com/google/gwt/query/client/SelectorEngine.java index 167a6758..ebe36240 100644 --- a/gwtquery-core/src/main/java/gwtquery/client/SelectorEngine.java +++ b/gwtquery-core/src/main/java/com/google/gwt/query/client/SelectorEngine.java @@ -1,4 +1,4 @@ -package gwtquery.client;
+package com.google.gwt.query.client;
import com.google.gwt.core.client.GWT;
import com.google.gwt.core.client.JavaScriptObject;
@@ -6,11 +6,11 @@ import com.google.gwt.dom.client.Document; 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.impl.SelectorEngineImpl;
-import gwtquery.client.impl.SelectorEngineImpl;
/**
- *
+ * Core Selector engine functions, and native JS utility functions.
*/
public class SelectorEngine {
diff --git a/gwtquery-core/src/main/java/com/google/gwt/query/client/Selectors.java b/gwtquery-core/src/main/java/com/google/gwt/query/client/Selectors.java new file mode 100644 index 00000000..062df40e --- /dev/null +++ b/gwtquery-core/src/main/java/com/google/gwt/query/client/Selectors.java @@ -0,0 +1,8 @@ +package com.google.gwt.query.client;
+
+/**
+ * Tagging interface used to generate compile time selectors.
+ */
+public interface Selectors {
+ DeferredGQuery[] getAllSelectors();
+}
diff --git a/gwtquery-core/src/main/java/gwtquery/client/impl/DocumentStyleImpl.java b/gwtquery-core/src/main/java/com/google/gwt/query/client/impl/DocumentStyleImpl.java index c61a88b9..eb3aa74c 100644 --- a/gwtquery-core/src/main/java/gwtquery/client/impl/DocumentStyleImpl.java +++ b/gwtquery-core/src/main/java/com/google/gwt/query/client/impl/DocumentStyleImpl.java @@ -1,11 +1,11 @@ -package gwtquery.client.impl; +package com.google.gwt.query.client.impl; import com.google.gwt.dom.client.Element; - -import gwtquery.client.SelectorEngine; +import com.google.gwt.query.client.SelectorEngine; +import com.google.gwt.query.client.GQuery; /** - * + * A helper class to get computed CSS styles for elements. */ public class DocumentStyleImpl { @@ -17,7 +17,7 @@ public class DocumentStyleImpl { } else if ("for".equals(name)) { return "htmlFor"; } - return name; + return GQuery.camelize(name); } public String getCurrentStyle(Element elem, String name) { diff --git a/gwtquery-core/src/main/java/gwtquery/client/impl/DocumentStyleImplIE.java b/gwtquery-core/src/main/java/com/google/gwt/query/client/impl/DocumentStyleImplIE.java index b9d95bcb..14450247 100644 --- a/gwtquery-core/src/main/java/gwtquery/client/impl/DocumentStyleImplIE.java +++ b/gwtquery-core/src/main/java/com/google/gwt/query/client/impl/DocumentStyleImplIE.java @@ -1,11 +1,10 @@ -package gwtquery.client.impl; +package com.google.gwt.query.client.impl; import com.google.gwt.dom.client.Element; - -import gwtquery.client.SelectorEngine; +import com.google.gwt.query.client.SelectorEngine; /** - * + * A helper class to get computed CSS styles for elements on IE6. */ public class DocumentStyleImplIE extends DocumentStyleImpl { diff --git a/gwtquery-core/src/main/java/com/google/gwt/query/client/impl/SelectorEngineImpl.java b/gwtquery-core/src/main/java/com/google/gwt/query/client/impl/SelectorEngineImpl.java new file mode 100644 index 00000000..d42e7fa0 --- /dev/null +++ b/gwtquery-core/src/main/java/com/google/gwt/query/client/impl/SelectorEngineImpl.java @@ -0,0 +1,106 @@ +package com.google.gwt.query.client.impl;
+
+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.JSArray;
+import com.google.gwt.query.client.Regexp;
+import com.google.gwt.query.client.SelectorEngine;
+
+/**
+ * Base/Utility class for runtime selector engine implementations.
+ */
+public abstract class SelectorEngineImpl {
+
+ public abstract NodeList<Element> select(String selector, Node ctx);
+
+ protected static Sequence getSequence(String expression) {
+ int start = 0, add = 2, max = -1, modVal = -1;
+ Regexp expressionRegExp = new Regexp(
+ "^((odd|even)|([1-9]\\d*)|((([1-9]\\d*)?)n((\\+|\\-)(\\d+))?)|(\\-(([1-9]\\d*)?)n\\+(\\d+)))$");
+ JSArray pseudoValue = expressionRegExp.exec(expression);
+ if (!SelectorEngine.truth(pseudoValue)) {
+ return null;
+ } else {
+ if (SelectorEngine.truth(pseudoValue.getStr(2))) { // odd or even
+ start = (SelectorEngine.eq(pseudoValue.getStr(2), "odd")) ? 1 : 2;
+ modVal = (start == 1) ? 1 : 0;
+ } else if (SelectorEngine
+ .truth(pseudoValue.getStr(3))) { // single digit
+ start = Integer.parseInt(pseudoValue.getStr(3), 10);
+ add = 0;
+ max = start;
+ } else if (SelectorEngine.truth(pseudoValue.getStr(4))) { // an+b
+ add = SelectorEngine.truth(pseudoValue.getStr(6)) ? Integer
+ .parseInt(pseudoValue.getStr(6), 10) : 1;
+ start = SelectorEngine.truth(pseudoValue.getStr(7)) ? Integer.parseInt(
+ (pseudoValue.getStr(8).charAt(0) == '+' ? ""
+ : pseudoValue.getStr(8)) + pseudoValue.getStr(9), 10) : 0;
+ while (start < 1) {
+ start += add;
+ }
+ modVal = (start > add) ? (start - add) % add
+ : ((start == add) ? 0 : start);
+ } else if (SelectorEngine.truth(pseudoValue.getStr(10))) { // -an+b
+ add = SelectorEngine.truth(pseudoValue.getStr(12)) ? Integer
+ .parseInt(pseudoValue.getStr(12), 10) : 1;
+ start = max = Integer.parseInt(pseudoValue.getStr(13), 10);
+ while (start > add) {
+ start -= add;
+ }
+ modVal = (max > add) ? (max - add) % add : ((max == add) ? 0 : max);
+ }
+ }
+ Sequence s = new Sequence();
+ s.start = start;
+ s.add = add;
+ s.max = max;
+ s.modVal = modVal;
+ return s;
+ }
+
+ public static class Sequence {
+
+ public int start;
+
+ public int max;
+
+ public int add;
+
+ public int modVal;
+ }
+
+ public static class SplitRule {
+
+ public String tag;
+
+ public String id;
+
+ public String allClasses;
+
+ public String allAttr;
+
+ public String allPseudos;
+
+ public String tagRelation;
+
+ public SplitRule(String tag, String id, String allClasses, String allAttr,
+ String allPseudos) {
+ this.tag = tag;
+ this.id = id;
+ this.allClasses = allClasses;
+ this.allAttr = allAttr;
+ this.allPseudos = allPseudos;
+ }
+
+ public SplitRule(String tag, String id, String allClasses, String allAttr,
+ String allPseudos, String tagRelation) {
+ this.tag = tag;
+ this.id = id;
+ this.allClasses = allClasses;
+ this.allAttr = allAttr;
+ this.allPseudos = allPseudos;
+ this.tagRelation = tagRelation;
+ }
+ }
+}
diff --git a/gwtquery-core/src/main/java/gwtquery/client/impl/SelectorEngineJS.java b/gwtquery-core/src/main/java/com/google/gwt/query/client/impl/SelectorEngineJS.java index a65e1cdc..6c46e4d4 100644 --- a/gwtquery-core/src/main/java/gwtquery/client/impl/SelectorEngineJS.java +++ b/gwtquery-core/src/main/java/com/google/gwt/query/client/impl/SelectorEngineJS.java @@ -1,25 +1,16 @@ -package gwtquery.client.impl;
+package com.google.gwt.query.client.impl;
import com.google.gwt.dom.client.Document;
import com.google.gwt.dom.client.Element;
import com.google.gwt.dom.client.Node;
import com.google.gwt.dom.client.NodeList;
-
-import gwtquery.client.JSArray;
-import gwtquery.client.Regexp;
-import gwtquery.client.SelectorEngine;
+import com.google.gwt.query.client.SelectorEngine;
+import com.google.gwt.query.client.Regexp;
+import com.google.gwt.query.client.JSArray;
/**
- * Copyright 2007 Timepedia.org 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.
- *
- * @author Ray Cromwell <ray@timepedia.log>
+ * Runtime selector engine implementation with no-XPath/native support based
+ * on DOMAssistant.
*/
public class SelectorEngineJS extends SelectorEngineImpl {
private Regexp cssSelectorRegExp;
diff --git a/gwtquery-core/src/main/java/gwtquery/client/impl/SelectorEngineJSIE.java b/gwtquery-core/src/main/java/com/google/gwt/query/client/impl/SelectorEngineJSIE.java index e7aad623..e2a2cf16 100644 --- a/gwtquery-core/src/main/java/gwtquery/client/impl/SelectorEngineJSIE.java +++ b/gwtquery-core/src/main/java/com/google/gwt/query/client/impl/SelectorEngineJSIE.java @@ -1,9 +1,10 @@ -package gwtquery.client.impl;
+package com.google.gwt.query.client.impl;
import com.google.gwt.dom.client.Element;
/**
- * Fix some DOM ops for IE
+ * Runtime implementaton of non-XPath/native for IE that fixes some
+ * DOM operation incompatibilities.
*/
public class SelectorEngineJSIE extends SelectorEngineJS {
public native String getAttr(Element elm, String attr) /*-{
diff --git a/gwtquery-core/src/main/java/gwtquery/client/impl/SelectorEngineNative.java b/gwtquery-core/src/main/java/com/google/gwt/query/client/impl/SelectorEngineNative.java index b9e96bd8..130142b2 100644 --- a/gwtquery-core/src/main/java/gwtquery/client/impl/SelectorEngineNative.java +++ b/gwtquery-core/src/main/java/com/google/gwt/query/client/impl/SelectorEngineNative.java @@ -1,13 +1,14 @@ -package gwtquery.client.impl; +package com.google.gwt.query.client.impl; import com.google.gwt.dom.client.Element; import com.google.gwt.dom.client.NodeList; import com.google.gwt.dom.client.Node; -import gwtquery.client.SelectorEngine; +import com.google.gwt.query.client.SelectorEngine; /** - * + * Runtime selector engine implementation for browsers with native + * querySelectorAll support. */ public class SelectorEngineNative extends SelectorEngineImpl { public NodeList<Element> select(String selector, Node ctx) { diff --git a/gwtquery-core/src/main/java/gwtquery/client/impl/SelectorEngineXPath.java b/gwtquery-core/src/main/java/com/google/gwt/query/client/impl/SelectorEngineXPath.java index cbc2ca3b..4fa316fc 100644 --- a/gwtquery-core/src/main/java/gwtquery/client/impl/SelectorEngineXPath.java +++ b/gwtquery-core/src/main/java/com/google/gwt/query/client/impl/SelectorEngineXPath.java @@ -1,27 +1,19 @@ -package gwtquery.client.impl;
+package com.google.gwt.query.client.impl;
import com.google.gwt.core.client.GWT;
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.Regexp;
-import gwtquery.client.JSArray;
-import gwtquery.client.Regexp;
-import gwtquery.client.SelectorEngine;
-import static gwtquery.client.SelectorEngine.eq;
-import static gwtquery.client.SelectorEngine.truth;
+import com.google.gwt.query.client.JSArray;
+import com.google.gwt.query.client.SelectorEngine;
+import static com.google.gwt.query.client.SelectorEngine.eq;
+import static com.google.gwt.query.client.SelectorEngine.truth;
/**
- * Copyright 2007 Timepedia.org 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.
- *
- * @author Ray Cromwell <ray@timepedia.log>
+ * Runtime selector engine implementation which translates selectors to XPath
+ * and delegates to document.evaluate().
*/
public class SelectorEngineXPath extends SelectorEngineImpl {
private Regexp cssSelectorRegExp;
@@ -232,6 +224,6 @@ public class SelectorEngineXPath extends SelectorEngineImpl { private native String replaceAttr2(String allAttr) /*-{
if(!allAttr) return "";
- return allAttr.replace(/\[(\w+)(\^|\$|\*|\||~)?=?([\w\u00C0-\uFFFF\s\-_\.]+)?\]/g, @gwtquery.client.impl.SelectorEngineXPath::attrToXPath(Ljava/lang/String;Ljava/lang/String;Ljava/lang/String;Ljava/lang/String;));
+ return allAttr.replace(/\[(\w+)(\^|\$|\*|\||~)?=?([\w\u00C0-\uFFFF\s\-_\.]+)?\]/g, @com.google.gwt.query.client.impl.SelectorEngineXPath::attrToXPath(Ljava/lang/String;Ljava/lang/String;Ljava/lang/String;Ljava/lang/String;));
}-*/;
}
\ No newline at end of file diff --git a/gwtquery-core/src/main/java/gwtquery/rebind/SelectorGeneratorBase.java b/gwtquery-core/src/main/java/com/google/gwt/query/rebind/SelectorGeneratorBase.java index 3cf2c2e7..e19d9b4d 100644 --- a/gwtquery-core/src/main/java/gwtquery/rebind/SelectorGeneratorBase.java +++ b/gwtquery-core/src/main/java/com/google/gwt/query/rebind/SelectorGeneratorBase.java @@ -1,4 +1,4 @@ -package gwtquery.rebind;
+package com.google.gwt.query.rebind;
import com.google.gwt.core.ext.Generator;
import com.google.gwt.core.ext.GeneratorContext;
@@ -10,13 +10,12 @@ import com.google.gwt.core.ext.typeinfo.JParameter; import com.google.gwt.core.ext.typeinfo.TypeOracle;
import com.google.gwt.user.rebind.ClassSourceFileComposerFactory;
import com.google.gwt.user.rebind.SourceWriter;
+import com.google.gwt.query.client.Selector;
import java.io.PrintWriter;
-import gwtquery.client.Selector;
-
/**
- *
+ * Base class for compile time selector generators.
*/
public abstract class SelectorGeneratorBase extends Generator {
@@ -154,10 +153,10 @@ public abstract class SelectorGeneratorBase extends Generator { }
ClassSourceFileComposerFactory composerFactory
= new ClassSourceFileComposerFactory(packageName, className);
- composerFactory.setSuperclass("gwtquery.client.SelectorEngine");
+ composerFactory.setSuperclass("com.google.gwt.query.client.SelectorEngine");
composerFactory.addImport("com.google.gwt.core.client.GWT");
composerFactory.addImport("gwtquery.client.*");
-// composerFactory.addImport("gwtquery.client.JSArray");
+// composerFactory.addImport("com.google.gwt.query.client.JSArray");
composerFactory.addImport("com.google.gwt.dom.client.*");
for (String interfaceName : interfaceNames) {
diff --git a/gwtquery-core/src/main/java/com/google/gwt/query/rebind/SelectorGeneratorJS.java b/gwtquery-core/src/main/java/com/google/gwt/query/rebind/SelectorGeneratorJS.java new file mode 100644 index 00000000..67162e62 --- /dev/null +++ b/gwtquery-core/src/main/java/com/google/gwt/query/rebind/SelectorGeneratorJS.java @@ -0,0 +1,31 @@ +package com.google.gwt.query.rebind;
+
+import com.google.gwt.core.ext.TreeLogger;
+import com.google.gwt.core.ext.UnableToCompleteException;
+import com.google.gwt.core.ext.typeinfo.JMethod;
+import com.google.gwt.query.client.Selector;
+import com.google.gwt.user.rebind.SourceWriter;
+
+/**
+ * An implementaton of pure-JS compile time selectors. This implementation
+ * simply defers to the runtime selector engine.
+ */
+public class SelectorGeneratorJS extends SelectorGeneratorBase {
+
+ protected String getImplSuffix() {
+ return "JS" + super.getImplSuffix();
+ }
+
+ protected void generateMethodBody(SourceWriter sw, JMethod method,
+ TreeLogger treeLogger, boolean hasContext)
+ throws UnableToCompleteException {
+
+ String selector = method.getAnnotation(Selector.class).value();
+ if (!hasContext) {
+ sw.println("Node root = Document.get();");
+ }
+
+ sw.println("return " + wrap(method,
+ "new SelectorEngine().select(\"" + selector + "\", root)") + ";");
+ }
+}
diff --git a/gwtquery-core/src/main/java/gwtquery/rebind/SelectorGeneratorJS.java b/gwtquery-core/src/main/java/com/google/gwt/query/rebind/SelectorGeneratorJSOptimal.java index 8917f4c9..782b2d61 100644 --- a/gwtquery-core/src/main/java/gwtquery/rebind/SelectorGeneratorJS.java +++ b/gwtquery-core/src/main/java/com/google/gwt/query/rebind/SelectorGeneratorJSOptimal.java @@ -1,4 +1,4 @@ -package gwtquery.rebind;
+package com.google.gwt.query.rebind;
import com.google.gwt.core.ext.TreeLogger;
import com.google.gwt.core.ext.UnableToCompleteException;
@@ -7,12 +7,12 @@ import com.google.gwt.user.rebind.SourceWriter; import java.util.regex.Pattern;
-import gwtquery.client.Selector;
+import com.google.gwt.query.client.Selector;
/**
*
*/
-public class SelectorGeneratorJS extends SelectorGeneratorBase {
+public class SelectorGeneratorJSOptimal extends SelectorGeneratorBase {
protected static Pattern nonSpace = Pattern.compile("\\S/");
@@ -171,4 +171,4 @@ public class SelectorGeneratorJS extends SelectorGeneratorBase { this.fnTemplate = fnT;
}
}
-}
+}
\ No newline at end of file diff --git a/gwtquery-core/src/main/java/gwtquery/rebind/SelectorGeneratorNative.java b/gwtquery-core/src/main/java/com/google/gwt/query/rebind/SelectorGeneratorNative.java index e7db5da7..56880851 100644 --- a/gwtquery-core/src/main/java/gwtquery/rebind/SelectorGeneratorNative.java +++ b/gwtquery-core/src/main/java/com/google/gwt/query/rebind/SelectorGeneratorNative.java @@ -1,14 +1,14 @@ -package gwtquery.rebind;
+package com.google.gwt.query.rebind;
import com.google.gwt.core.ext.TreeLogger;
import com.google.gwt.core.ext.UnableToCompleteException;
import com.google.gwt.core.ext.typeinfo.JMethod;
import com.google.gwt.user.rebind.SourceWriter;
-
-import gwtquery.client.Selector;
+import com.google.gwt.query.client.Selector;
/**
- *
+ * Compile time selector generator which delegates to native browser
+ * methods.
*/
public class SelectorGeneratorNative extends SelectorGeneratorBase {
diff --git a/gwtquery-core/src/main/java/gwtquery/rebind/SelectorGeneratorXPath.java b/gwtquery-core/src/main/java/com/google/gwt/query/rebind/SelectorGeneratorXPath.java index d43cfd3e..e38b6df1 100644 --- a/gwtquery-core/src/main/java/gwtquery/rebind/SelectorGeneratorXPath.java +++ b/gwtquery-core/src/main/java/com/google/gwt/query/rebind/SelectorGeneratorXPath.java @@ -1,18 +1,18 @@ -package gwtquery.rebind;
+package com.google.gwt.query.rebind;
import com.google.gwt.core.ext.TreeLogger;
import com.google.gwt.core.ext.UnableToCompleteException;
import com.google.gwt.core.ext.typeinfo.JMethod;
+import com.google.gwt.query.client.Selector;
import com.google.gwt.user.rebind.SourceWriter;
import java.util.ArrayList;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
-import gwtquery.client.Selector;
-
/**
- *
+ * Compile time selector generator which translates selector into XPath at
+ * compile time.
*/
public class SelectorGeneratorXPath extends SelectorGeneratorBase {
@@ -40,7 +40,7 @@ public class SelectorGeneratorXPath extends SelectorGeneratorBase { }
protected String getImplSuffix() {
- return "XPath"+super.getImplSuffix();
+ return "XPath" + super.getImplSuffix();
}
protected void generateMethodBody(SourceWriter sw, JMethod method,
@@ -108,9 +108,9 @@ public class SelectorGeneratorXPath extends SelectorGeneratorBase { + "']";
}
if (notNull(splitRule.allClasses)) {
- xPathExpression += splitRule.allClasses.replaceAll(
- "\\.([a-zA-Z_0-9\u00C0 -\uFFFF\\-_]+)",
- "[contains(concat(' ', @class, ' '), ' $1 ')]");
+ xPathExpression += splitRule.allClasses
+ .replaceAll("\\.([a-zA-Z_0-9\u00C0 -\uFFFF\\-_]+)",
+ "[contains(concat(' ', @class, ' '), ' $1 ')]");
}
if (notNull(splitRule.allAttr)) {
xPathExpression += attrToXPath(splitRule.allAttr,
@@ -146,8 +146,9 @@ public class SelectorGeneratorXPath extends SelectorGeneratorBase { if (!hasContext) {
sw.println("Node root = Document.get();");
}
- sw.println("return "+wrap(method, "SelectorEngine.xpathEvaluate(\""
- + xPathExpression + "\", root)")+";");
+ sw.println("return " + wrap(method,
+ "SelectorEngine.xpathEvaluate(\"" + xPathExpression + "\", root)")
+ + ";");
}
static class Sequence {
@@ -175,18 +176,18 @@ public class SelectorGeneratorXPath extends SelectorGeneratorBase { + ")";
} else if ("nth".equals(pseudo[0])) {
if (!pseudoValue.matches("^n$")) {
- String position = (("last".equals(pseudo[1]))
- ? "(count(following-sibling::" : "(count(preceding-sibling::") + tag
- + ") + 1)";
+ String position =
+ (("last".equals(pseudo[1])) ? "(count(following-sibling::"
+ : "(count(preceding-sibling::") + tag + ") + 1)";
Sequence sequence = getSequence(pseudoValue);
if (sequence != null) {
if (sequence.start == sequence.max) {
xpath = position + " = " + sequence.start;
} else {
xpath = position + " mod " + sequence.add + " = " + sequence.modVal
- + ((sequence.start > 1) ? " and " + position + " >= " + sequence
- .start : "") + ((sequence.max > 0) ? " and " + position + " <= "
- + sequence.max : "");
+ + ((sequence.start > 1) ? " and " + position + " >= "
+ + sequence.start : "") + ((sequence.max > 0) ? " and "
+ + position + " <= " + sequence.max : "");
}
}
}
@@ -204,13 +205,14 @@ public class SelectorGeneratorXPath extends SelectorGeneratorBase { if (pseudoValue.matches("^(:a-zA-Z_0-9+[a-zA-Z_0-9\\-]*)$")) {
xpath = "not(" + pseudoToXPath(tag, pseudoValue.substring(1), "") + ")";
} else {
- pseudoValue = pseudoValue.replaceAll(
- "^\\[#([a-zA-Z_0-9\u00C0-\uFFFF\\-\\_]+)\\]$", "[id=$1]");
+ pseudoValue = pseudoValue
+ .replaceAll("^\\[#([a-zA-Z_0-9\u00C0-\uFFFF\\-\\_]+)\\]$",
+ "[id=$1]");
String notSelector = pseudoValue
.replaceFirst("^(a-zA-Z_0-9+)", "self::$1");
- notSelector = notSelector.replaceAll(
- "^\\.([a-zA-Z_0-9\u00C0-\uFFFF\\-_]+)",
- "contains(concat(' ', @class, ' '), ' $1 ')");
+ notSelector = notSelector
+ .replaceAll("^\\.([a-zA-Z_0-9\u00C0-\uFFFF\\-_]+)",
+ "contains(concat(' ', @class, ' '), ' $1 ')");
notSelector = attrToXPath(notSelector,
"\\[(a-zA-Z_0-9+)(\\^|\\$|\\*|\\||~)?=?([a-zA-Z_0-9\u00C0-\uFFFF\\s\\-_\\.]+)?\\]");
xpath = "not(" + notSelector + ")";
diff --git a/gwtquery-core/src/main/java/gwtquery/GwtQuery.gwt.xml b/gwtquery-core/src/main/java/gwtquery/GwtQuery.gwt.xml deleted file mode 100644 index 8cfabdb0..00000000 --- a/gwtquery-core/src/main/java/gwtquery/GwtQuery.gwt.xml +++ /dev/null @@ -1,105 +0,0 @@ -<module>
- <inherits name='com.google.gwt.user.User'/>
-
- <!--<define-property name="selectorCapability" values="native,xpath,js"/>-->
- <!-- enable for native getClassByName shortcut acceleration -->
- <define-property name="selectorCapability"
- values="native,js"/>
- <property-provider name="selectorCapability">
- <![CDATA[
- // useful for benchmarking tests when you want to force non-accelerated queries
- //if(window.location.href.indexOf("_selector_force_js") != -1) return "js";
- if(document.querySelectorAll && /native/.test(document.querySelectorAll.toString())) {
- return "native";
- }
- return "js"
- ]]>
- </property-provider>
-
- <generate-with class="gwtquery.rebind.SelectorGeneratorJS">
- <when-type-assignable class="gwtquery.client.Selectors"/>
- <any>
- <when-property-is name="user.agent" value="gecko"/>
- <when-property-is name="user.agent" value="ie6"/>
- </any>
-
- </generate-with>
-
- <generate-with class="gwtquery.rebind.SelectorGeneratorXPath">
- <when-type-assignable class="gwtquery.client.Selectors"/>
- <any>
- <when-property-is name="user.agent" value="gecko1_8"/>
- <when-property-is name="user.agent" value="opera"/>
- <all>
- <when-property-is name="selectorCapability" value="js"/>
- <when-property-is name="user.agent" value="safari"/>
- </all>
- </any>
- </generate-with>
-
- <generate-with class="gwtquery.rebind.gebcn.SelectorGeneratorNativeGEBCN">
- <when-type-assignable class="gwtquery.client.Selectors"/>
- <all>
- <when-property-is name="selectorCapability" value="native"/>
- <when-property-is name="user.agent" value="safari"/>
- </all>
- </generate-with>
-
- <!--versions which handle native getElementsByClassName -->
- <!--<generate-with class="gwtquery.rebind.gebcn.SelectorGeneratorJSGEBCN">-->
- <!--<when-type-assignable class="gwtquery.client.Selectors"/>-->
- <!--<when-property-is name="selectorCapability" value="js_gebcn"/>-->
- <!--</generate-with>-->
-
- <!--<generate-with class="gwtquery.rebind.gebcn.SelectorGeneratorXPathGEBCN">-->
- <!--<when-type-assignable class="gwtquery.client.Selectors"/>-->
- <!--<when-property-is name="selectorCapability" value="xpath_gebcn"/>-->
- <!--</generate-with>-->
-
- <!--<generate-with class="gwtquery.rebind.gebcn.SelectorGeneratorNativeGEBCN">-->
- <!--<when-type-assignable class="gwtquery.client.Selectors"/>-->
- <!--<when-property-is name="selectorCapability" value="native_gebcn"/>-->
- <!--</generate-with>-->
-
-
- <replace-with class="gwtquery.client.impl.DocumentStyleImpl">
- <when-type-assignable class="gwtquery.client.impl.DocumentStyleImpl"/>
- </replace-with>
-
- <replace-with class="gwtquery.client.impl.DocumentStyleImplIE">
- <when-type-assignable class="gwtquery.client.impl.DocumentStyleImpl"/>
- <when-property-is name="user.agent" value="ie6"/>
- </replace-with>
-
-
- <replace-with class="gwtquery.client.impl.SelectorEngineJS">
- <when-type-assignable class="gwtquery.client.impl.SelectorEngineImpl"/>
- <when-property-is name="user.agent" value="gecko"/>
- </replace-with>
-
- <replace-with class="gwtquery.client.impl.SelectorEngineJSIE">
- <when-type-assignable class="gwtquery.client.impl.SelectorEngineImpl"/>
- <when-property-is name="user.agent" value="ie6"/>
- </replace-with>
-
- <replace-with class="gwtquery.client.impl.SelectorEngineXPath">
- <when-type-assignable class="gwtquery.client.impl.SelectorEngineImpl"/>
- <any>
- <when-property-is name="user.agent" value="gecko1_8"/>
- <when-property-is name="user.agent" value="opera"/>
- <all>
- <when-property-is name="selectorCapability" value="js"/>
- <when-property-is name="user.agent" value="safari"/>
- </all>
- </any>
- </replace-with>
-
- <replace-with class="gwtquery.client.impl.SelectorEngineNative">
- <when-type-assignable class="gwtquery.client.impl.SelectorEngineImpl"/>
- <all>
- <when-property-is name="user.agent" value="safari"/>
- <when-property-is name="selectorCapability" value="native"/>
- </all>
- </replace-with>
-
-</module>
\ No newline at end of file diff --git a/gwtquery-core/src/main/java/gwtquery/client/DeferredGQuery.java b/gwtquery-core/src/main/java/gwtquery/client/DeferredGQuery.java deleted file mode 100644 index fe1165ae..00000000 --- a/gwtquery-core/src/main/java/gwtquery/client/DeferredGQuery.java +++ /dev/null @@ -1,14 +0,0 @@ -package gwtquery.client;
-
-import com.google.gwt.dom.client.Node;
-import com.google.gwt.dom.client.Element;
-import com.google.gwt.dom.client.NodeList;
-
-/**
- * A compiled selector that can be lazily turned into a GQuery
- */
-public interface DeferredGQuery {
- String getSelector();
- GQuery eval(Node ctx);
- NodeList<Element> array(Node ctx);
-}
diff --git a/gwtquery-core/src/main/java/gwtquery/client/Effects.java b/gwtquery-core/src/main/java/gwtquery/client/Effects.java deleted file mode 100644 index 21fabb86..00000000 --- a/gwtquery-core/src/main/java/gwtquery/client/Effects.java +++ /dev/null @@ -1,152 +0,0 @@ -package gwtquery.client;
-
-import com.google.gwt.dom.client.Element;
-import com.google.gwt.dom.client.NodeList;
-import com.google.gwt.animation.client.Animation;
-
-public class Effects extends GQuery {
-
- static {
- GQuery.registerPlugin(Effects.class, new EffectsPlugin());
- }
-
- public static final Class<Effects> Effects = Effects.class;
-
- public Effects(Element element) {
- super(element);
- }
-
- public Effects(JSArray elements) {
- super(elements);
- }
-
- public Effects(NodeList list) {
- super(list);
- }
-
- /**
- * function( prop, speed, easing, callback ) {
- var optall = jQuery.speed(speed, easing, callback);
-
- return this[ optall.queue === false ? "each" : "queue" ](function(){
-
- var opt = jQuery.extend({}, optall), p,
- hidden = this.nodeType == 1 && jQuery(this).is(":hidden"),
- self = this;
-
- for ( p in prop ) {
- if ( prop[p] == "hide" && hidden || prop[p] == "show" && !hidden )
- return opt.complete.call(this);
-
- if ( ( p == "height" || p == "width" ) && this.style ) {
- // Store display property
- opt.display = jQuery.css(this, "display");
-
- // Make sure that nothing sneaks out
- opt.overflow = this.style.overflow;
- }
- }
-
- if ( opt.overflow != null )
- this.style.overflow = "hidden";
-
- opt.curAnim = jQuery.extend({}, prop);
-
- jQuery.each( prop, function(name, val){
- var e = new jQuery.fx( self, opt, name );
-
- if ( /toggle|show|hide/.test(val) )
- e[ val == "toggle" ? hidden ? "show" : "hide" : val ]( prop );
- else {
- var parts = val.toString().match(/^([+-]=)?([\d+-.]+)(.*)$/),
- start = e.cur(true) || 0;
-
- if ( parts ) {
- var end = parseFloat(parts[2]),
- unit = parts[3] || "px";
-
- // We need to compute starting value
- if ( unit != "px" ) {
- self.style[ name ] = (end || 1) + unit;
- start = ((end || 1) / e.cur(true)) * start;
- self.style[ name ] = start + unit;
- }
-
- // If a +=/-= token was provided, we're doing a relative animation
- if ( parts[1] )
- end = ((parts[1] == "-=" ? -1 : 1) * end) + start;
-
- e.custom( start, end, unit );
- } else
- e.custom( start, val, "" );
- }
- });
-
- // For JS strict compliance
- return true;
- });
- },
- * @return
- */
- public Effects animate(Properties props, String speed, String easing,
- Function callback) {
- return this;
- }
-
- public Effects fadeOut() {
- Animation a = new Animation() {
-
- public void onCancel() {
- }
-
- public void onComplete() {
- for (int i = 0; i < elements.getLength(); i++) {
- elements.getItem(i).getStyle().setProperty("opacity", "0");
- elements.getItem(i).getStyle().setProperty("display", "none");
- }
- }
-
- public void onStart() {
- }
-
- public void onUpdate(double progress) {
- for (int i = 0; i < elements.getLength(); i++) {
- elements.getItem(i).getStyle()
- .setProperty("opacity", String.valueOf(1.0 - progress));
- }
- }
- };
- a.run(1000);
- return this;
- }
-
- public Effects fadeIn() {
- Animation a = new Animation() {
-
- public void onCancel() {
- }
-
- public void onComplete() {
- }
-
- public void onStart() {
- }
-
- public void onUpdate(double progress) {
- for (int i = 0; i < elements.getLength(); i++) {
- elements.getItem(i).getStyle()
- .setProperty("opacity", String.valueOf(progress));
- }
- }
- };
- a.run(1000);
- return this;
- }
-
- public static class EffectsPlugin implements Plugin<Effects> {
-
- public Effects init(GQuery gq) {
- return new Effects(gq.get());
- }
- }
-}
diff --git a/gwtquery-core/src/main/java/gwtquery/client/Function.java b/gwtquery-core/src/main/java/gwtquery/client/Function.java deleted file mode 100644 index f9d70a4f..00000000 --- a/gwtquery-core/src/main/java/gwtquery/client/Function.java +++ /dev/null @@ -1,26 +0,0 @@ -package gwtquery.client; - -import com.google.gwt.dom.client.Element; -import com.google.gwt.user.client.Event; - -/** - * Extend this class to implement functions. - */ -public abstract class Function { - - public String f(Element e, int i) { - return ""; - } - - public void f(Element e) { - } - - public boolean f(Event e, Object data) { - return f(e); - } - - public boolean f(Event e) { - f(e.getCurrentTarget()); - return true; - } -} diff --git a/gwtquery-core/src/main/java/gwtquery/client/Plugin.java b/gwtquery-core/src/main/java/gwtquery/client/Plugin.java deleted file mode 100644 index 9f7864d2..00000000 --- a/gwtquery-core/src/main/java/gwtquery/client/Plugin.java +++ /dev/null @@ -1,8 +0,0 @@ -package gwtquery.client;
-
-/**
- * A GQuery plugin
- */
-public interface Plugin<T extends GQuery> {
- T init(GQuery gq);
-}
diff --git a/gwtquery-core/src/main/java/gwtquery/client/Properties.java b/gwtquery-core/src/main/java/gwtquery/client/Properties.java deleted file mode 100644 index 20030926..00000000 --- a/gwtquery-core/src/main/java/gwtquery/client/Properties.java +++ /dev/null @@ -1,35 +0,0 @@ -package gwtquery.client;
-
-import com.google.gwt.core.client.JavaScriptObject;
-
-/**
- *
- */
-public class Properties extends JavaScriptObject {
-
- protected Properties() { }
- public native static Properties create(String properties) /*-{
- return eval(properties);
- }-*/;
-
- public final native String get(String name) /*-{
- return this[name];
- }-*/;
-
- public final native int getInt(String name) /*-{
- return this[name];
- }-*/;
-
- public final native float getFloat(String name) /*-{
- return this[name];
- }-*/;
-
- public final native String[] keys() /*-{
- var key, keys=[];
-
- for(key in this) {
- keys.push(key);
- }
- return keys;
- }-*/;
-}
diff --git a/gwtquery-core/src/main/java/gwtquery/client/Selectors.java b/gwtquery-core/src/main/java/gwtquery/client/Selectors.java deleted file mode 100644 index 6ebd5018..00000000 --- a/gwtquery-core/src/main/java/gwtquery/client/Selectors.java +++ /dev/null @@ -1,7 +0,0 @@ -package gwtquery.client;
-
-/**
- */
-public interface Selectors {
- DeferredGQuery[] getAllSelectors();
-}
diff --git a/gwtquery-core/src/main/java/gwtquery/client/impl/SelectorEngineImpl.java b/gwtquery-core/src/main/java/gwtquery/client/impl/SelectorEngineImpl.java deleted file mode 100644 index 18774d0c..00000000 --- a/gwtquery-core/src/main/java/gwtquery/client/impl/SelectorEngineImpl.java +++ /dev/null @@ -1,104 +0,0 @@ -package gwtquery.client.impl;
-
-import gwtquery.client.Regexp;
-import gwtquery.client.JSArray;
-import gwtquery.client.SelectorEngine;
-import com.google.gwt.dom.client.*;
-
-/**
- * Copyright 2007 Timepedia.org
- * 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.
- *
- * @author Ray Cromwell <ray@timepedia.log>
- */
-public abstract class SelectorEngineImpl {
-
- public abstract NodeList<Element> select(String selector, Node ctx);
-
- protected static Sequence getSequence(String expression) {
- int start = 0, add = 2, max = -1, modVal = -1;
- Regexp expressionRegExp = new Regexp(
- "^((odd|even)|([1-9]\\d*)|((([1-9]\\d*)?)n((\\+|\\-)(\\d+))?)|(\\-(([1-9]\\d*)?)n\\+(\\d+)))$");
- JSArray pseudoValue = expressionRegExp.exec(expression);
- if (!SelectorEngine.truth(pseudoValue)) {
- return null;
- } else {
- if (SelectorEngine.truth(pseudoValue.getStr(2))) { // odd or even
- start = (SelectorEngine.eq(pseudoValue.getStr(2), "odd")) ? 1 : 2;
- modVal = (start == 1) ? 1 : 0;
- } else if (SelectorEngine.truth(pseudoValue.getStr(3))) { // single digit
- start = Integer.parseInt(pseudoValue.getStr(3), 10);
- add = 0;
- max = start;
- } else if (SelectorEngine.truth(pseudoValue.getStr(4))) { // an+b
- add = SelectorEngine.truth(pseudoValue.getStr(6)) ? Integer
- .parseInt(pseudoValue.getStr(6), 10) : 1;
- start = SelectorEngine.truth(pseudoValue.getStr(7)) ? Integer
- .parseInt((pseudoValue.getStr(8).charAt(0) == '+' ? "" : pseudoValue.getStr(8)) + pseudoValue.getStr(9), 10) : 0;
- while (start < 1) {
- start += add;
- }
- modVal = (start > add) ? (start - add) % add
- : ((start == add) ? 0 : start);
- } else if (SelectorEngine.truth(pseudoValue.getStr(10))) { // -an+b
- add = SelectorEngine.truth(pseudoValue.getStr(12)) ? Integer
- .parseInt(pseudoValue.getStr(12), 10) : 1;
- start = max = Integer.parseInt(pseudoValue.getStr(13), 10);
- while (start > add) {
- start -= add;
- }
- modVal = (max > add) ? (max - add) % add : ((max == add) ? 0 : max);
- }
- }
- Sequence s = new Sequence();
- s.start = start;
- s.add = add;
- s.max = max;
- s.modVal = modVal;
- return s;
- }
-
- public static class Sequence {
- public int start;
- public int max;
- public int add;
- public int modVal;
- }
-
- public static class SplitRule {
-
- public String tag;
- public String id;
- public String allClasses;
- public String allAttr;
- public String allPseudos;
- public String tagRelation;
-
- public SplitRule(String tag, String id, String allClasses, String allAttr,
- String allPseudos) {
- this.tag = tag;
- this.id = id;
- this.allClasses = allClasses;
- this.allAttr = allAttr;
- this.allPseudos = allPseudos;
- }
-
- public SplitRule(String tag, String id, String allClasses, String allAttr,
- String allPseudos, String tagRelation) {
- this.tag = tag;
- this.id = id;
- this.allClasses = allClasses;
- this.allAttr = allAttr;
- this.allPseudos = allPseudos;
- this.tagRelation = tagRelation;
- }
- }
-}
diff --git a/gwtquery-core/src/main/java/gwtquery/rebind/gebcn/SelectorGeneratorJSGEBCN.java b/gwtquery-core/src/main/java/gwtquery/rebind/gebcn/SelectorGeneratorJSGEBCN.java deleted file mode 100644 index a287303a..00000000 --- a/gwtquery-core/src/main/java/gwtquery/rebind/gebcn/SelectorGeneratorJSGEBCN.java +++ /dev/null @@ -1,12 +0,0 @@ -package gwtquery.rebind.gebcn; - -import gwtquery.rebind.SelectorGeneratorJS; - -/** - */ -public class SelectorGeneratorJSGEBCN extends SelectorGeneratorJS { - - protected boolean hasGetElementsByClassName() { - return true; - } -} diff --git a/gwtquery-core/src/main/java/gwtquery/rebind/gebcn/SelectorGeneratorNativeGEBCN.java b/gwtquery-core/src/main/java/gwtquery/rebind/gebcn/SelectorGeneratorNativeGEBCN.java deleted file mode 100644 index 04226dbd..00000000 --- a/gwtquery-core/src/main/java/gwtquery/rebind/gebcn/SelectorGeneratorNativeGEBCN.java +++ /dev/null @@ -1,13 +0,0 @@ -package gwtquery.rebind.gebcn; - -import gwtquery.rebind.SelectorGeneratorNative; - -/** - * - */ -public class SelectorGeneratorNativeGEBCN extends SelectorGeneratorNative { - - protected boolean hasGetElementsByClassName() { - return true; - } -} diff --git a/gwtquery-core/src/main/java/gwtquery/rebind/gebcn/SelectorGeneratorXPathGEBCN.java b/gwtquery-core/src/main/java/gwtquery/rebind/gebcn/SelectorGeneratorXPathGEBCN.java deleted file mode 100644 index 9bcce9ce..00000000 --- a/gwtquery-core/src/main/java/gwtquery/rebind/gebcn/SelectorGeneratorXPathGEBCN.java +++ /dev/null @@ -1,12 +0,0 @@ -package gwtquery.rebind.gebcn; - -import gwtquery.rebind.SelectorGeneratorXPath; - -/** - */ -public class SelectorGeneratorXPathGEBCN extends SelectorGeneratorXPath { - - protected boolean hasGetElementsByClassName() { - return true; - } -} diff --git a/gwtquery-core/src/test/java/com/google/gwt/query/client/GwtQueryCoreTest.java b/gwtquery-core/src/test/java/com/google/gwt/query/client/GwtQueryCoreTest.java new file mode 100644 index 00000000..13509b77 --- /dev/null +++ b/gwtquery-core/src/test/java/com/google/gwt/query/client/GwtQueryCoreTest.java @@ -0,0 +1,558 @@ +package com.google.gwt.query.client; + +import static com.google.gwt.query.client.GQuery.$; +import com.google.gwt.query.client.GQuery; +import com.google.gwt.query.client.Properties; +import com.google.gwt.query.client.Function; + +import com.google.gwt.dom.client.Element; +import com.google.gwt.dom.client.InputElement; +import com.google.gwt.junit.client.GWTTestCase; +import com.google.gwt.user.client.Event; +import com.google.gwt.user.client.Timer; +import com.google.gwt.user.client.ui.HTML; +import com.google.gwt.user.client.ui.RootPanel; + +/** + * Test class for testing gwtquery-core api. + */ +public class GwtQueryCoreTest extends GWTTestCase { + + public String getModuleName() { + return "com.google.gwt.query.Query"; + } + + static HTML testPanel = null; + static Element e = null; + + public void gwtSetUp() { + if (e == null) { + testPanel = new HTML(); + RootPanel.get().add(testPanel); + e = testPanel.getElement(); + e.setId("tst"); + } else { + e.setInnerHTML(""); + } + } + + public void testBrowserStartUp() { + // just a test for seeing in eclipse that GWTTestCase internal browser is starting + assertTrue(true); + } + + public void testInnerMethods() { + String txt = "<p>I would like to say: </p>"; + + // Check that setHTML and getHTML works as GQuery html() + testPanel.setHTML(txt); + assertEquals(txt, testPanel.getHTML()); + assertEquals(txt, $(e).html()); + assertEquals(txt, $("#tst").html()); + $(e).html(""); + assertEquals("", $(e).html()); + $(e).html(txt); + assertEquals(txt, $(e).html()); + + // toString() + assertEquals(txt, $("p", e).toString()); + + // remove() + $("p", e).remove(); + assertEquals("", $(e).html()); + + // text() + String expected = "I would like to say: I would like to say: "; + $(e).html(txt + txt); + assertEquals(expected, $("p", e).text()); + + // empty() + expected = "<p></p><p></p>"; + $("p", e).empty(); + assertEquals(expected, $(e).html()); + } + + public void testAttributeMethods() { + + $(e).html("<p class=\"a1\">Content</p>"); + GQuery gq = $("p", e); + + // attr() + assertEquals("a1", gq.attr("class")); + gq.attr("class", "b1 b2"); + + // hasClass() + assertTrue(gq.hasClass("b1")); + assertTrue(gq.hasClass("b2")); + + // addClass() + gq.addClass("c1", "c2"); + assertTrue(gq.hasClass("b1")); + assertTrue(gq.hasClass("b2")); + assertTrue(gq.hasClass("c1")); + assertTrue(gq.hasClass("c2")); + + // removeClass() + gq.removeClass("c2", "c1"); + assertTrue(gq.hasClass("b1")); + assertTrue(gq.hasClass("b2")); + assertFalse(gq.hasClass("c1")); + assertFalse(gq.hasClass("c2")); + + // css() + String content = "<p style='color:red;'>Test Paragraph.</p>"; + $(e).html(content); + assertEquals("red", $("p", e).css("color")); + $("p", e).css("font-weight", "bold"); + assertEquals("bold", $("p", e).css("font-weight")); + + // css() properties + $(e).html("<p>Test Paragraph.</p>"); + $("p", e).css(Properties.create("color: 'red', 'font-weight': 'bold', background: 'blue'")); + assertEquals("red", $("p", e).css("color")); + assertEquals("bold", $("p", e).css("font-weight")); + assertEquals("blue", $("p", e).css("background-color")); + + // css() camelize and uppercase + $(e).html("<p>Test Paragraph.</p>"); + $("p", e).css(Properties.create("COLOR: 'red', 'FONT-WEIGHT': 'bold'")); + assertEquals("red", $("p", e).css("color")); + assertEquals("", $("p", e).css("background")); + + } + + public void testSliceMethods() { + String content = "<p>This is just a test.</p><p>So is this</p>"; + $(e).html(content); + + String expected = "<p>So is this</p>"; + assertEquals(1, $("p", e).eq(1).size()); + assertEquals(expected, $("p", e).eq(1).toString()); + + expected = "<p>This is just a test.</p>"; + assertEquals(1, $("p", e).lt(1).size()); + assertEquals(expected, $("p", e).lt(1).toString()); + + expected = "<p>So is this</p>"; + assertEquals(1, $("p", e).gt(0).size()); + assertEquals(expected, $("p", e).gt(0).toString()); + + assertEquals(2, $("p", e).slice(0, 2).size()); + assertEquals(2, $("p", e).slice(0, -1).size()); + assertEquals(0, $("p", e).slice(3, 2).size()); + } + + public void testRelativeMethods() { + String content = "<p><span>Hello</span>, how are you?</p>"; + String expected = "<span>Hello</span>"; + + // find() + $(e).html(content); + assertEquals(expected, $("p", e).find("span").toString()); + + // filter() + content = "<p>First</p><p class=\"selected\">Hello</p><p>How are you?</p>"; + $(e).html(content); + expected = "<p class=\"selected\">Hello</p>"; + assertEquals(expected, $("p", e).filter(".selected").toString()); + + // filter() + // Commented because GQuery doesn't support this syntax yet + // expected = "<p class=\"selected\">Hello</p>"; + // assertEquals(expected, $("p", e).filter(".selected, :first").toString()); + + // not() + expected = "<p>First</p><p>How are you?</p>"; + assertEquals(2, $("p", e).not(".selected").size()); + assertEquals(expected, $("p", e).not(".selected").toString()); + assertEquals(2, $("p", e).not($(".selected")).size()); + assertEquals(expected, $("p", e).not($(".selected")).toString()); + assertEquals(2, $("p", e).not($(".selected").get(0)).size()); + assertEquals(expected, $("p", e).not($(".selected").get(0)).toString()); + + // add() + String added = "<p>Last</p>"; + expected = content + added; + assertEquals(4, $("p", e).add(added).size()); + assertEquals(expected, $("p", e).add(added).toString()); + + // parent() + expected = content = "<div><p>Hello</p><p>Hello</p></div>"; + $(e).html(content); + assertEquals(expected, $("p", e).parent().toString()); + + // parent() + content = "<div><p>Hello</p></div><div class=\"selected\"><p>Hello Again</p></div>"; + expected = "<div class=\"selected\"><p>Hello Again</p></div>"; + $(e).html(content); + assertEquals(expected, $("p", e).parent(".selected").toString()); + + // parents() + content = "<div><p><span>Hello</span></p><span>Hello Again</span></div>"; + $(e).html(content); + assertEquals(2, $("span", e).size()); + assertTrue(3 < $("span", e).parents().size()); + assertEquals(1, $("span", e).parents().filter("body").size()); + $("span", e).parents().filter("body").toString().contains(content); + + // is() + content = "<form><input type=\"checkbox\"></form>"; + $(e).html(content); + assertEquals(true, $("input[type=\"checkbox\"]", e).parent().is("form")); + + // is() + content = "<form><p><input type=\"checkbox\"></p></form>"; + $(e).html(content); + assertEquals(false, $("input[type='checkbox']", e).parent().is("form")); + + // next() + content = "<p>Hello</p><p>Hello Again</p><div><span>And Again</span></div>"; + String next1 = "<p>Hello Again</p>"; + String next2 = "<div><span>And Again</span></div>"; + $(e).html(content); + assertEquals(2, $("p", e).next().size()); + assertEquals(next1, $("p", e).next().get(0).getString()); + assertEquals(next2, $("p", e).next().get(1).getString()); + + // next() + content = "<p>Hello</p><p class=\"selected\">Hello Again</p><div><span>And Again</span></div>"; + expected = "<p class=\"selected\">Hello Again</p>"; + $(e).html(content); + assertEquals(1, $("p", e).next(".selected").size()); + assertEquals(expected, $("p", e).next(".selected").get(0).getString()); + + // prev() + content = "<p>Hello</p><div><span>Hello Again</span></div><p>And Again</p>"; + expected = "<div><span>Hello Again</span></div>"; + $(e).html(content); + assertEquals(1, $("p", e).prev().size()); + assertEquals(expected, $("p", e).prev().get(0).getString()); + + // prev() + content = "<div><span>Hello</span></div><p class=\"selected\">Hello Again</p><p>And Again</p>"; + expected = "<p class=\"selected\">Hello Again</p>"; + $(e).html(content); + assertEquals(2, $("p", e).prev().size()); + assertEquals(1, $("p", e).prev(".selected").size()); + assertEquals(expected, $("p", e).prev(".selected").get(0).getString()); + + // siblings() + content = "<p>Hello</p><div><span>Hello Again</span></div><p>And Again</p>"; + next1 = "<p>Hello</p>"; + next2 = "<p>And Again</p>"; + $(e).html(content); + assertEquals(2, $("div", e).siblings().size()); + assertEquals(next1, $("div", e).siblings().get(0).getString()); + assertEquals(next2, $("div", e).siblings().get(1).getString()); + + // siblings() + content = "<div><span>Hello</span></div><p class=\"selected\">Hello Again</p><p>And Again</p>"; + expected = "<p class=\"selected\">Hello Again</p>"; + $(e).html(content); + assertEquals(1, $("p", e).siblings(".selected").size()); + assertEquals(expected, $("p", e).siblings(".selected").get(0).getString()); + + // children() + content = "<p>Hello</p><div><span>Hello Again</span></div><p>And Again</p>"; + expected = "<span>Hello Again</span>"; + $(e).html(content); + assertEquals(expected, $("div", e).children().toString()); + + // children() + content = "<div><span>Hello</span><p class=\"selected\">Hello Again</p><p>And Again</p></div>"; + expected = "<p class=\"selected\">Hello Again</p>"; + $(e).html(content); + assertEquals(expected, $("div", e).children(".selected").toString()); + + // contains() + content = "<p>This is just a test.</p><p>So is this</p>"; + expected = "<p>This is just a test.</p>"; + $(e).html(content); + assertEquals(expected, $("p", e).contains("test").toString()); + } + + public void testModifyMethods() { + String p_txt = "<p>I would like to say: </p>"; + String b_txt = "<b>Hello</b>"; + + // append() + String expected = "<p>I would like to say: <b>Hello</b></p>"; + $(e).html(p_txt); + $("p", e).append(b_txt); + assertEquals(expected, $(e).html()); + + // appendTo() + expected = "<p>I would like to say: <b>Hello</b></p>"; + $(e).html(b_txt + p_txt); + $("b", e).appendTo($("p", e)); + assertEquals(expected, $(e).html()); + + // prepend() + expected = "<p><b>Hello</b>I would like to say: </p>"; + $(e).html(p_txt); + $("p", e).prepend(b_txt); + assertEquals(expected, $(e).html()); + + // prependTo() + expected = "<p><b>Hello</b>I would like to say: </p>"; + $(e).html(b_txt + p_txt); + $("b", e).prependTo($("p", e)); + assertEquals(expected, $(e).html()); + + // prependTo() + expected = "<b>Hello</b><p><b>Hello</b>I would like to say: </p>"; + $(e).html(b_txt + p_txt); + $("b", e).clone().prependTo($("p", e)); + assertEquals(expected, $(e).html()); + + // before() + expected = "<b>Hello</b><p>I would like to say: </p>"; + $(e).html(p_txt); + $("p", e).before(b_txt); + assertEquals(expected, $(e).html()); + + // before() + expected = "<b>Hello</b><p>I would like to say: </p>"; + $(e).html(p_txt + b_txt); + $("p", e).before($("b", e)); + assertEquals(expected, $(e).html()); + + // before() + expected = "<b>Hello</b><p>I would like to say: </p><b>Hello</b>"; + $(e).html(p_txt + b_txt); + $("p", e).before($("b", e).clone()); + assertEquals(expected, $(e).html()); + + // insertBefore() + expected = "<b>Hello</b><p>I would like to say: </p>"; + $(e).html(p_txt + b_txt); + $("b", e).insertBefore($("p", e)); + assertEquals(expected, $(e).html()); + + // insertBefore() + expected = "<b>Hello</b><p>I would like to say: </p><b>Hello</b>"; + $(e).html(p_txt + b_txt); + $("b", e).clone().insertBefore($("p", e)); + assertEquals(expected, $(e).html()); + + // after() + expected = "<p>I would like to say: </p><b>Hello</b>"; + $(e).html(p_txt); + $("p", e).after(b_txt); + assertEquals(expected, testPanel.getHTML()); + + // after() + expected = "<p>I would like to say: </p><b>Hello</b>"; + $(e).html(b_txt + p_txt); + $("p", e).after($("b", e)); + assertEquals(expected, $(e).html()); + + // after() + expected = "<b>Hello</b><p>I would like to say: </p><b>Hello</b>"; + $(e).html(b_txt + p_txt); + $("p", e).after($("b", e).clone()); + assertEquals(expected, $(e).html()); + } + + public void testWrapMethod() { + String content = "<p>Test Paragraph.</p><div id=\"content\">Content</div>"; + + String expected = "<div id=\"content\">Content<p>Test Paragraph.</p></div>"; + $(e).html(content); + + $("p", e).wrap($("div", e).get(0)); + assertEquals(expected, $(e).html()); + + expected = "<b><p>Test Paragraph.</p></b><b><div id=\"content\">Content</div></b>"; + $(e).html(content); + $("*", e).wrap("<b></b>"); + assertEquals(expected, $(e).html()); + } + + public void testInputValueMethods() { + // imput text + $(e).html("<input type='text'/>"); + GQuery gq = $("input", e); + assertEquals("", gq.val()); + gq.val("some value"); + assertEquals("some value", gq.val()); + + // select + $(e).html("<select name='n'><option value='v1'>1</option><option value='v2' selected='selected'>2</option></select>"); + gq = $("select", e); + assertEquals("v2", gq.val()); + gq.val("v1"); + assertEquals("v1", gq.val()); + + // select multiple + $(e).html("<select name='n' multiple='multiple'><option value='v1'>1</option><option value='v2' selected='selected'>2</option><option value='v3'>3</option></select>"); + gq = $("select", e); + gq.val("v1","v3","invalid"); + assertEquals(2, gq.vals().length); + assertEquals("v1", gq.vals()[0]); + assertEquals("v3", gq.vals()[1]); + gq.val("v1"); + assertEquals(1, gq.vals().length); + assertEquals("v1", gq.val()); + + // input radio + $(e).html("<input type='radio' name='n' value='v1'>1</input><input type='radio' name='n' value='v2' checked='checked'>2</input>"); + gq = $("input", e); + assertEquals("v2", gq.val()); + gq.val("v1"); + assertEquals("v1", gq.val()); + gq.val("v2"); + assertEquals("v2", gq.val()); + + // input checkbox + $(e).html("<input type='checkbox' name='n1' value='v1'>1</input><input type='checkbox' name='n2' value='v2' checked='checked'>2</input>"); + gq = $("input", e); + assertEquals("", gq.val()); + gq.val("v1"); + assertEquals("v1", gq.val()); + } + + public void testEventsPlugin() { + $(e).html("<p>Content</p>"); + + // click + $("p", e).click(new Function() { + public void f(Element elem) { + $(elem).css("color", "red"); + } + }); + $("p", e).trigger(Event.ONCLICK); + assertEquals("red", $("p", e).css("color")); + + // unbind + $("p", e).css("color", ""); + $("p", e).unbind(Event.ONCLICK); + $("p", e).trigger(Event.ONCLICK); + assertEquals("rgb(0, 0, 0)", $("p", e).css("color")); + + // one + $("p", e).one(Event.ONCLICK, null, new Function() { + public void f(Element elem) { + $(elem).css("color", "red"); + } + }); + $("p", e).trigger(Event.ONCLICK); + assertEquals("red", $("p", e).css("color")); + $("p", e).css("color", ""); + $("p", e).trigger(Event.ONCLICK); + assertEquals("rgb(0, 0, 0)", $("p", e).css("color")); + + // hover (mouseover, mouseout) + $("p", e).hover(new Function() { + public void f(Element elem) { + $(elem).css("background-color", "yellow"); + } + }, new Function() { + public void f(Element elem) { + $(elem).css("background-color", ""); + } + }); + $("p", e).trigger(Event.ONMOUSEOVER); + assertEquals("yellow", $("p", e).css("background-color")); + $("p", e).trigger(Event.ONMOUSEOUT); + assertEquals("rgba(0, 0, 0, 0)", $("p", e).css("background-color")); + + // focus + $("p", e).focus(new Function() { + public void f(Element elem) { + $(elem).css("border", "1px dotted black"); + } + }); + $("p", e).trigger(Event.ONFOCUS); + assertEquals("1px dotted black", $("p", e).css("border")); + + // blur + $("p", e).blur(new Function() { + public void f(Element elem) { + $(elem).css("border", ""); + } + }); + $("p", e).trigger(Event.ONBLUR); + assertEquals("", $("p", e).css("border")); + + // keypressed + $(e).html("<input type='text'/>"); + $("input", e).keypressed(new Function() { + public boolean f(Event evnt) { + Element elem = evnt.getCurrentTarget(); + InputElement input = InputElement.as(elem); + input.setValue(input.getValue() + Character.toString((char) evnt.getKeyCode())); + return false; + } + }); + $("input", e).trigger(Event.ONFOCUS); + $("input", e).trigger(Event.ONKEYPRESS, 'a'); + assertEquals("a", InputElement.as($("input", e).get(0)).getValue()); + + } + + public void testEffectsPlugin() { + $(e).html("<p id='id1'>Content 1</p><p id='id2'>Content 2</p><p id='id3'>Content 3</p>"); + + final GQuery sect_a = $("#id1"); + final GQuery sect_b = $("#id2"); + final GQuery sect_c = $("#id3"); + + // hide() + sect_a.hide(); + assertEquals("none", sect_a.css("display")); + + // show() + sect_b.show(); + assertEquals("", sect_b.css("display")); + + // toggle() + assertEquals("", sect_c.css("display")); + sect_c.toggle(); + assertEquals("none", sect_c.css("display")); + sect_c.toggle(); + assertEquals("", sect_c.css("display")); + + // fadeIn() & fadeOut() are tested with delayed assertions + sect_a.fadeIn(2000); + sect_b.fadeOut(2000); + + // Configure the max duration for this test + // If the test exceeds the timeout without calling finishTest() it will fail + delayTestFinish(2500); + + // Delayed assertions at different intervals + Timer timer_shortTime = new Timer() { + public void run() { + double o = Double.valueOf(sect_a.css("opacity")); + assertTrue(o > 0 && o < 0.5); + o = Double.valueOf(sect_b.css("opacity")); + assertTrue(o > 0.5 && o < 1); + } + }; + Timer timer_midTime = new Timer() { + public void run() { + assertEquals("", sect_a.css("display")); + assertEquals("", sect_b.css("display")); + double o = Double.valueOf(sect_a.css("opacity")); + assertTrue(o > 0.5 && o < 1); + o = Double.valueOf(sect_b.css("opacity")); + assertTrue(o > 0 && o < 0.5); + } + }; + Timer timer_longTime = new Timer() { + public void run() { + assertEquals("", sect_a.css("display")); + assertEquals("none", sect_b.css("display")); + // Last delayed assertion has to stop the test to avoid a timeout failure + finishTest(); + } + }; + // schedule the delayed assertions + timer_shortTime.schedule(200); + timer_midTime.schedule(1200); + timer_longTime.schedule(2200); + + } + +} diff --git a/gwtquery-core/src/test/java/gwtquery/client/GwtQueryCoreTest.java b/gwtquery-core/src/test/java/gwtquery/client/GwtQueryCoreTest.java deleted file mode 100644 index c65724b2..00000000 --- a/gwtquery-core/src/test/java/gwtquery/client/GwtQueryCoreTest.java +++ /dev/null @@ -1,13 +0,0 @@ -package gwtquery.client; - -import com.google.gwt.junit.client.GWTTestCase; - -/** - * - */ -public class GwtQueryCoreTest extends GWTTestCase { - - public String getModuleName() { - return "gwtquery.GwtQuery"; - } -} |