* Attributes include title, alt, src, href, width, style, etc.\r
*/\r
public String attr(String name) {\r
- return elements.getItem(0).getAttribute(name);\r
+ return size() == 0 ? "" : get(0).getAttribute(name);\r
}\r
\r
/**\r
* value).\r
*/\r
public Object data(String name) {\r
- return data(elements.getItem(0), name, null);\r
+ return size() == 0 ? null : data(get(0), name, null);\r
}\r
\r
/**\r
*/\r
@SuppressWarnings("unchecked")\r
public <T> T data(String name, Class<T> clz) {\r
- return size() == 0 ? null: (T)data(elements.getItem(0), name, null);\r
+ return size() == 0 ? null: (T)data(get(0), name, null);\r
}\r
\r
/**\r
* left. The method works only with visible elements.\r
*/\r
public com.google.gwt.query.client.GQuery.Offset offset() {\r
- return size() == 0 ? new Offset(0, 0) : new Offset(get(0).getAbsoluteLeft(), get(0).getAbsoluteTop());\r
+ Element e = get(0);\r
+ return e == null ? new Offset(0, 0) : new Offset(e.getAbsoluteLeft(), e.getAbsoluteTop());\r
}\r
\r
/**\r
* relative or absolute). This method only works with visible elements.\r
*/\r
public GQuery offsetParent() {\r
+ if (size() == 0) {\r
+ return $();\r
+ }\r
Element offParent = JsUtils.or(elements.getItem(0).getOffsetParent(), body);\r
while (offParent != null\r
&& !"body".equalsIgnoreCase(offParent.getTagName())\r
if (size() == 0) {\r
return 0;\r
}\r
- int outerHeight = get(0).getOffsetHeight(); // height including padding and\r
- // border\r
+ // height including padding and border\r
+ int outerHeight = get(0).getOffsetHeight(); \r
if (includeMargin) {\r
outerHeight += cur("marginTop", true) + cur("marginBottom", true);\r
}\r
if (size() == 0) {\r
return 0;\r
}\r
- int outerWidth = get(0).getOffsetWidth(); // width including padding and\r
- // border\r
+ // width including padding and border\r
+ int outerWidth = get(0).getOffsetWidth(); \r
if (includeMargin) {\r
outerWidth += cur("marginRight", true) + cur("marginLeft", true);\r
}\r
* and padding. This method only works with visible elements.\r
*/\r
public com.google.gwt.query.client.GQuery.Offset position() {\r
- Element element = get(0);\r
- if (element == null) {\r
- return null;\r
+ if (size() == 0) {\r
+ return new Offset(0, 0);\r
}\r
+ Element element = get(0);\r
// Get *real* offsetParent\r
- Element offsetParent = element.getOffsetParent();\r
+ Element offsetParent = get(0).getOffsetParent();\r
// Get correct offsets\r
Offset offset = offset();\r
Offset parentOffset = null;\r
* Scrolls the first matched element into view.\r
*/\r
public GQuery scrollIntoView() {\r
- scrollIntoViewImpl(get(0));\r
+ if (size() > 0) scrollIntoViewImpl(get(0));\r
return this;\r
}\r
\r
* everything else.\r
*/\r
public GQuery wrapAll(GQuery query) {\r
- GQuery wrap = query.clone();\r
- if (elements.getItem(0).getParentNode() != null) {\r
- wrap.insertBefore(elements.getItem(0));\r
- }\r
- for (Element e : wrap.elements()) {\r
- Node n = e;\r
- while (n.getFirstChild() != null\r
- && n.getFirstChild().getNodeType() == Node.ELEMENT_NODE) {\r
- n = n.getFirstChild();\r
+ if (size() > 0) {\r
+ GQuery wrap = query.clone();\r
+ if (elements.getItem(0).getParentNode() != null) {\r
+ wrap.insertBefore(elements.getItem(0));\r
+ }\r
+ for (Element e : wrap.elements()) {\r
+ Node n = e;\r
+ while (n.getFirstChild() != null\r
+ && n.getFirstChild().getNodeType() == Node.ELEMENT_NODE) {\r
+ n = n.getFirstChild();\r
+ }\r
+ $((Element) n).append(this);\r
}\r
- $((Element) n).append(this);\r
}\r
return this;\r
}\r