/**\r
* Implementation class to modify attributes.\r
*/\r
- protected static AttributeImpl attributeImpl = GWT.create(AttributeImpl.class);\r
+ protected static AttributeImpl attributeImpl;\r
\r
/**\r
* The body element in the current page.\r
/**\r
* Implementation class used for style manipulations.\r
*/\r
- protected static DocumentStyleImpl styleImpl = GWT.create(DocumentStyleImpl.class);\r
+ private static DocumentStyleImpl styleImpl;\r
\r
private static JsRegexp tagNameRegex = new JsRegexp("<([\\w:]+)");\r
\r
\r
private static Element windowData = null;\r
\r
- private static final JsNamedArray<TagWrapper> wrapperMap;\r
-\r
- static {\r
- TagWrapper tableWrapper = new TagWrapper(1, "<table>", "</table>");\r
- TagWrapper selectWrapper = new TagWrapper(1, "<select multiple=\"multiple\">", "</select>");\r
- TagWrapper trWrapper = new TagWrapper(3, "<table><tbody><tr>", "</tr></tbody></table>");\r
- \r
- wrapperMap = JsNamedArray.create();\r
- wrapperMap.put("option", selectWrapper);\r
- wrapperMap.put("optgroup", selectWrapper);\r
- wrapperMap.put("legend", new TagWrapper(1, "<fieldset>", "</fieldset>") );\r
- wrapperMap.put("thead", tableWrapper);\r
- wrapperMap.put("tbody", tableWrapper);\r
- wrapperMap.put("tfoot", tableWrapper);\r
- wrapperMap.put("colgroup", tableWrapper);\r
- wrapperMap.put("caption", tableWrapper);\r
- wrapperMap.put("tr", new TagWrapper(2, "<table><tbody>", "</tbody></table>"));\r
- wrapperMap.put("td", trWrapper);\r
- wrapperMap.put("th", trWrapper); \r
- wrapperMap.put("col", new TagWrapper(2, "<table><tbody></tbody><colgroup>", "</colgroup></table>"));\r
- wrapperMap.put("area", new TagWrapper(1, "<map>", "</map>"));\r
- }\r
+ private static JsNamedArray<TagWrapper> wrapperMap;\r
\r
/**\r
* Create an empty GQuery object.\r
throw new RuntimeException("HTML snippet doesn't contain any tag");\r
}\r
\r
+ if (wrapperMap == null){\r
+ initWrapperMap();\r
+ }\r
+ \r
TagWrapper wrapper = wrapperMap.get(tag.toLowerCase());\r
\r
if (wrapper == null){\r
return name != null ? d.get(name) : id;\r
}\r
\r
+ protected static DocumentStyleImpl getStyleImpl(){\r
+ if (styleImpl == null){\r
+ styleImpl = GWT.create(DocumentStyleImpl.class);\r
+ }\r
+ return styleImpl;\r
+ }\r
+ \r
+ private static AttributeImpl getAttributeImpl(){\r
+ if (attributeImpl == null){\r
+ attributeImpl = GWT.create(AttributeImpl.class);\r
+ }\r
+ return attributeImpl;\r
+ }\r
+ \r
private static native void emptyDocument(Document d) /*-{\r
d.open();\r
d.write("<head/><body/>");\r
return $(cleanHtmlString(html, doc));\r
}\r
\r
+ private static void initWrapperMap(){\r
+ \r
+ TagWrapper tableWrapper = new TagWrapper(1, "<table>", "</table>");\r
+ TagWrapper selectWrapper = new TagWrapper(1, "<select multiple=\"multiple\">", "</select>");\r
+ TagWrapper trWrapper = new TagWrapper(3, "<table><tbody><tr>", "</tr></tbody></table>");\r
+ \r
+ wrapperMap = JsNamedArray.create();\r
+ wrapperMap.put("option", selectWrapper);\r
+ wrapperMap.put("optgroup", selectWrapper);\r
+ wrapperMap.put("legend", new TagWrapper(1, "<fieldset>", "</fieldset>") );\r
+ wrapperMap.put("thead", tableWrapper);\r
+ wrapperMap.put("tbody", tableWrapper);\r
+ wrapperMap.put("tfoot", tableWrapper);\r
+ wrapperMap.put("colgroup", tableWrapper);\r
+ wrapperMap.put("caption", tableWrapper);\r
+ wrapperMap.put("tr", new TagWrapper(2, "<table><tbody>", "</tbody></table>"));\r
+ wrapperMap.put("td", trWrapper);\r
+ wrapperMap.put("th", trWrapper); \r
+ wrapperMap.put("col", new TagWrapper(2, "<table><tbody></tbody><colgroup>", "</colgroup></table>"));\r
+ wrapperMap.put("area", new TagWrapper(1, "<map>", "</map>"));\r
+ \r
+ }\r
+ \r
protected static String[] jsArrayToString(JsArrayString array) {\r
if (GWT.isScript()) {\r
return jsArrayToString0(array);\r
if (n)\r
n.scrollIntoView()\r
}-*/;\r
- \r
- private static native void setElementAttribute(Element e, String key, String value) /*-{\r
- if (value == null)\r
- e.removeAttribute(key);\r
- else \r
- e.setAttribute(key, value);\r
- e[key] = value;\r
- }-*/;\r
- \r
- \r
+ \r
private static native void setElementValue(Element e, String value) /*-{\r
e.value = value;\r
}-*/;\r
*/\r
public GQuery attr(String key, Object value) {\r
assert key != null : "key cannot be null";\r
- attributeImpl.setAttribute(this, key, value);\r
+ getAttributeImpl().setAttribute(this, key, value);\r
return this;\r
}\r
\r
* parameter force=true.\r
*/\r
public String css(String name, boolean force) {\r
- return isEmpty() ? "" : styleImpl.curCSS(get(0), name, force);\r
+ return isEmpty() ? "" : getStyleImpl().curCSS(get(0), name, force);\r
}\r
\r
/**\r
*/\r
public GQuery css(String prop, String val) {\r
for (Element e : elements) {\r
- styleImpl.setStyleProperty(e, prop, val);\r
+ getStyleImpl().setStyleProperty(e, prop, val);\r
}\r
return this;\r
}\r
* When true returns the real computed value.\r
*/\r
public double cur(String prop, boolean force) {\r
- return isEmpty() ? 0 : styleImpl.cur(get(0), prop, force);\r
+ return isEmpty() ? 0 : getStyleImpl().cur(get(0), prop, force);\r
}\r
\r
/**\r
String currentDisplay = e.getStyle().getDisplay();\r
Object old = data(e, "oldDisplay", null);\r
if (old == null && !"none".equals(currentDisplay)) {\r
- data(e, "oldDisplay", styleImpl.curCSS(e, "display", false));\r
+ data(e, "oldDisplay", getStyleImpl().curCSS(e, "display", false));\r
}\r
}\r
\r
* Return true if the first element is visible.isVisible\r
*/\r
public boolean isVisible() {\r
- return isEmpty() ? false : styleImpl.isVisible(get(0));\r
+ return isEmpty() ? false : getStyleImpl().isVisible(get(0));\r
}\r
\r
/**\r
while (offParent != null\r
&& !"body".equalsIgnoreCase(offParent.getTagName())\r
&& !"html".equalsIgnoreCase(offParent.getTagName())\r
- && "static".equals(styleImpl.curCSS(offParent, "position", true))) {\r
+ && "static".equals(getStyleImpl().curCSS(offParent, "position", true))) {\r
offParent = offParent.getOffsetParent();\r
}\r
return new GQuery(offParent);\r
}\r
\r
// Subtract element margins\r
- int topMargin = (int) styleImpl.cur(element, "marginTop", true);\r
- // TODO: move this check to styleImpl\r
+ int topMargin = (int) getStyleImpl().cur(element, "marginTop", true);\r
+ // TODO: move this check to getStyleImpl()\r
// When margin-left = auto, Safari and chrome return a value while IE and\r
// Firefox return 0\r
// force the margin-left to 0 if margin-left = auto.\r
int leftMargin = 0;\r
if (!"auto".equals(element.getStyle().getMarginLeft())) {\r
- leftMargin = (int) styleImpl.cur(element, "marginLeft", true);\r
+ leftMargin = (int) getStyleImpl().cur(element, "marginLeft", true);\r
}\r
\r
offset = offset.add(-leftMargin, -topMargin);\r
\r
// Add offsetParent borders\r
- int parentOffsetBorderTop = (int) styleImpl.cur(offsetParent,\r
+ int parentOffsetBorderTop = (int) getStyleImpl().cur(offsetParent,\r
"borderTopWidth", true);\r
- int parentOffsetBorderLeft = (int) styleImpl.cur(offsetParent,\r
+ int parentOffsetBorderLeft = (int) getStyleImpl().cur(offsetParent,\r
"borderLeftWidth", true);\r
parentOffset = parentOffset.add(parentOffsetBorderLeft,\r
parentOffsetBorderTop);\r
* Remove the named attribute from every element in the matched set.\r
*/\r
public GQuery removeAttr(String key) {\r
- attributeImpl.removeAttribute(this, key);\r
+ getAttributeImpl().removeAttribute(this, key);\r
return this;\r
}\r
\r
public void restoreCssAttrs(String... cssProps) {\r
for (Element e : elements) {\r
for (String a : cssProps) {\r
- styleImpl.setStyleProperty(e, a, (String) data(e, OLD_DATA_PREFIX + a,\r
+ getStyleImpl().setStyleProperty(e, a, (String) data(e, OLD_DATA_PREFIX + a,\r
null));\r
}\r
}\r
public void saveCssAttrs(String... cssProps) {\r
for (Element e : elements) {\r
for (String a : cssProps) {\r
- data(OLD_DATA_PREFIX + a, styleImpl.curCSS(e, a, false));\r
+ data(OLD_DATA_PREFIX + a, getStyleImpl().curCSS(e, a, false));\r
}\r
}\r
}\r
\r
//reset the display\r
if (oldDisplay == null && "none".equals(currentDisplay)){\r
- styleImpl.setStyleProperty(e, "display", "");\r
+ getStyleImpl().setStyleProperty(e, "display", "");\r
currentDisplay = "";\r
}\r
\r
//check if the stylesheet impose display: none. If it is the case, determine \r
//the default display for the tag and store it at the element level\r
- if ("".equals(currentDisplay) && !styleImpl.isVisible(e)){\r
- data(e, "oldDisplay", styleImpl.defaultDisplay(e.getNodeName()));\r
+ if ("".equals(currentDisplay) && !getStyleImpl().isVisible(e)){\r
+ data(e, "oldDisplay", getStyleImpl().defaultDisplay(e.getNodeName()));\r
}\r
}\r
\r
for (Element e : elements) {\r
String currentDisplay = e.getStyle().getDisplay();\r
if ("".equals(currentDisplay) || "none".equals(currentDisplay)){\r
- styleImpl.setStyleProperty(e, "display", JsUtils.or((String) data(e,\r
+ getStyleImpl().setStyleProperty(e, "display", JsUtils.or((String) data(e,\r
"oldDisplay", null), ""));\r
}\r
}\r
*/\r
public GQuery toggle() {\r
for (Element e : elements) {\r
- if (styleImpl.isVisible(e)) {\r
+ if (getStyleImpl().isVisible(e)) {\r
$(e).hide();\r
} else {\r
$(e).show();\r