]> source.dussan.org Git - gwtquery.git/commitdiff
allow document as a valid gquery context in order to handle correctly elements in...
authorManolo Carrasco <manolo@apache.org>
Fri, 16 Jul 2010 09:17:24 +0000 (09:17 +0000)
committerManolo Carrasco <manolo@apache.org>
Fri, 16 Jul 2010 09:17:24 +0000 (09:17 +0000)
gwtquery-core/src/main/java/com/google/gwt/query/client/GQuery.java

index b30a1ee6f0ddd29c2687fedbf48bd0691b4bb7bd..cab0ac3e0c201cb539273317b82454da00e5fb9b 100644 (file)
@@ -232,13 +232,21 @@ public class GQuery implements Lazy<GQuery, LazyGQuery> {
   /**\r
    * This function accepts a string containing a CSS selector which is then used\r
    * to match a set of elements, or it accepts raw HTML creating a GQuery\r
-   * element containing those elements. The second parameter is the context to\r
-   * use for the selector.\r
+   * element containing those elements.\r
+   * The second parameter is the context to use for the selector, or \r
+   * the document where the new elements will be created.\r
    */\r
-  public static GQuery $(String selector, Node context) {\r
-    return new GQuery(select(selector, context));\r
+  public static GQuery $(String selectorOrHtml, Node ctx) {\r
+    if (selectorOrHtml == null || selectorOrHtml.trim().length() == 0) {\r
+      return $();\r
+    }\r
+    if (selectorOrHtml.trim().charAt(0) == '<') {\r
+      Document doc = ctx instanceof Document ? (Document)ctx : document;\r
+      return $(clean(selectorOrHtml, doc));\r
+    }\r
+    return new GQuery(select(selectorOrHtml, ctx));\r
   }\r
-\r
+  \r
   /**\r
    * This function accepts a string containing a CSS selector which is then used\r
    * to match a set of elements, or it accepts raw HTML creating a GQuery\r
@@ -300,7 +308,7 @@ public class GQuery implements Lazy<GQuery, LazyGQuery> {
   }\r
 \r
   @SuppressWarnings("unchecked")\r
-  protected static GQuery clean(String elem) {\r
+  protected static GQuery clean(String elem, Document doc) {\r
     String tags = elem.trim().toLowerCase();\r
     String preWrap = "", postWrap = "";\r
     int wrapPos = 0;\r
@@ -331,7 +339,7 @@ public class GQuery implements Lazy<GQuery, LazyGQuery> {
     }\r
     // TODO: fix IE link tag serialization\r
     // TODO: fix IE <script> tag\r
-    Element div = document.createDivElement();\r
+    Element div = doc.createDivElement();\r
     div.setInnerHTML(preWrap + elem + postWrap);\r
     Node n = div;\r
     while (wrapPos-- != 0) {\r
@@ -387,7 +395,7 @@ public class GQuery implements Lazy<GQuery, LazyGQuery> {
   }\r
 \r
   private static GQuery innerHtml(String html) {\r
-    return $(clean(html));\r
+    return $(clean(html, document));\r
   }\r
 \r
   private static native String[] jsArrayToString0(JsArrayString array) /*-{\r
@@ -490,7 +498,7 @@ public class GQuery implements Lazy<GQuery, LazyGQuery> {
    * another if it's not in the page).\r
    */\r
   public GQuery after(String html) {\r
-    return domManip(html, FUNC_AFTER);\r
+    return domManip(html, document, FUNC_AFTER);\r
   }\r
 \r
   /**\r
@@ -526,7 +534,7 @@ public class GQuery implements Lazy<GQuery, LazyGQuery> {
    * into the document.\r
    */\r
   public GQuery append(String html) {\r
-    return domManip(html, FUNC_APPEND);\r
+    return domManip(html, document, FUNC_APPEND);\r
   }\r
 \r
   /**\r
@@ -650,7 +658,7 @@ public class GQuery implements Lazy<GQuery, LazyGQuery> {
    * another if it's not in the page).\r
    */\r
   public GQuery before(String html) {\r
-    return domManip(html, FUNC_BEFORE);\r
+    return domManip(html, document, FUNC_BEFORE);\r
   }\r
 \r
   /**\r
@@ -762,13 +770,12 @@ public class GQuery implements Lazy<GQuery, LazyGQuery> {
   public GQuery contents() {\r
     JSArray result = JSArray.create();\r
     for (Element e : elements()) {\r
-      NodeList<Node> children = e.getChildNodes();\r
-      for (int i = 0; i < children.getLength(); i++) {\r
-        Node n = children.getItem(i);\r
-        if (IFrameElement.is(n)) {\r
-          result.addNode(getContentDocument(n));\r
-        } else {\r
-          result.addNode(n);\r
+      if (IFrameElement.is(e)){\r
+        result.addNode(getContentDocument(e));\r
+      } else {\r
+        NodeList<Node> children = e.getChildNodes();\r
+        for (int i = 0; i < children.getLength(); i++) {\r
+          result.addNode(children.getItem(i));\r
         }\r
       }\r
     }\r
@@ -879,7 +886,7 @@ public class GQuery implements Lazy<GQuery, LazyGQuery> {
   /**\r
    * Stores the value in the named spot with desired return type.\r
    */\r
-  public Object data(String name, Object value) {\r
+  public GQuery data(String name, Object value) {\r
     for (Element e : elements()) {\r
       data(e, name, value);\r
     }\r
@@ -1304,6 +1311,13 @@ public class GQuery implements Lazy<GQuery, LazyGQuery> {
     return trigger(Event.ONKEYUP, key);\r
   }\r
   \r
+  /**\r
+   * Returns the computed left position of the first element matched.\r
+   */\r
+  public int left() {\r
+    return (int) GQUtils.cur(get(0), "left", true);\r
+  }  \r
+  \r
   /**\r
    * Returns the number of elements currently matched. The size function will\r
    * return the same value.\r
@@ -1573,7 +1587,7 @@ public class GQuery implements Lazy<GQuery, LazyGQuery> {
    * elements.\r
    */\r
   public GQuery prepend(String html) {\r
-    return domManip(html, FUNC_PREPEND);\r
+    return domManip(html, document, FUNC_PREPEND);\r
   }\r
   \r
   /**\r
@@ -1768,7 +1782,7 @@ public class GQuery implements Lazy<GQuery, LazyGQuery> {
       }\r
     }\r
   }\r
-\r
+  \r
   /**\r
    * Restore a set of previously saved Css properties in every matched element.\r
    */\r
@@ -2038,6 +2052,13 @@ public class GQuery implements Lazy<GQuery, LazyGQuery> {
     }\r
     return this;\r
   }\r
+  \r
+  /**\r
+   * Returns the computed left position of the first element matched.\r
+   */\r
+  public int top() {\r
+    return (int) GQUtils.cur(get(0), "top", true);\r
+  }  \r
 \r
   /**\r
    * Produces a string representation of the matched elements.\r
@@ -2052,7 +2073,7 @@ public class GQuery implements Lazy<GQuery, LazyGQuery> {
   public String toString(boolean pretty) {\r
     String r = "";\r
     for (Element e : elements()) {\r
-      if (window.equals(e) || document.equals(e)) {\r
+      if (window.equals(e)) {\r
         continue;\r
       }\r
       r += (pretty && r.length() > 0 ? "\n " : "") + e.getString();\r
@@ -2446,8 +2467,8 @@ public class GQuery implements Lazy<GQuery, LazyGQuery> {
     return this;\r
   }\r
 \r
-  private GQuery domManip(String html, int func) {\r
-    return domManip(clean(html), func);\r
+  private GQuery domManip(String html, Document doc, int func) {\r
+    return domManip(clean(html, doc), func);\r
   }\r
 \r
   private native Document getContentDocument(Node n) /*-{\r