/**\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
}\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
}\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
}\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
* 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
* 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
* 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
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
/**\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
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
* 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
}\r
}\r
}\r
-\r
+ \r
/**\r
* Restore a set of previously saved Css properties in every matched element.\r
*/\r
}\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
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
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