From 8b5e2353ed779ec1ebe56bd3587fc229271dd3a8 Mon Sep 17 00:00:00 2001 From: Manolo Carrasco Date: Sun, 2 May 2010 21:14:23 +0000 Subject: removed className manipulation which was copied from UIObject because it was moved into Element, so there is no reason to maintain this code any more --- .../java/com/google/gwt/query/client/GQuery.java | 81 +++++----------------- .../google/gwt/query/client/GwtQueryCoreTest.java | 14 +++- 2 files changed, 29 insertions(+), 66 deletions(-) (limited to 'gwtquery-core/src') diff --git a/gwtquery-core/src/main/java/com/google/gwt/query/client/GQuery.java b/gwtquery-core/src/main/java/com/google/gwt/query/client/GQuery.java index 90b72b35..6bb1e721 100644 --- a/gwtquery-core/src/main/java/com/google/gwt/query/client/GQuery.java +++ b/gwtquery-core/src/main/java/com/google/gwt/query/client/GQuery.java @@ -371,62 +371,6 @@ public class GQuery implements Lazy { } } - /** - * Copied from UIObject. - */ - protected static void setStyleName(Element elem, String style, boolean add) { - - style = style.trim(); - - // Get the current style string. - String oldStyle = elem.getClassName(); - int idx = oldStyle.indexOf(style); - - // Calculate matching index. - while (idx != -1) { - if (idx == 0 || oldStyle.charAt(idx - 1) == ' ') { - int last = idx + style.length(); - int lastPos = oldStyle.length(); - if ((last == lastPos) || ((last < lastPos) && (oldStyle.charAt(last) - == ' '))) { - break; - } - } - idx = oldStyle.indexOf(style, idx + 1); - } - - if (add) { - // Only add the style if it's not already present. - if (idx == -1) { - if (oldStyle.length() > 0) { - oldStyle += " "; - } - DOM.setElementProperty(elem.cast(), - "className", oldStyle + style); - } - } else { - // Don't try to remove the style if it's not there. - if (idx != -1) { - // Get the leading and trailing parts, without the removed name. - String begin = oldStyle.substring(0, idx).trim(); - String end = oldStyle.substring(idx + style.length()).trim(); - - // Some contortions to make sure we don't leave extra spaces. - String newClassName; - if (begin.length() == 0) { - newClassName = end; - } else if (end.length() == 0) { - newClassName = begin; - } else { - newClassName = begin + " " + end; - } - - DOM.setElementProperty(elem.cast(), - "className", newClassName); - } - } - } - protected static void setStyleProperty(String prop, String val, Element e) { // put in lower-case only if all letters are upper-case, to avoid modify already camelized properties if (prop.matches("^[A-Z]+$")) { @@ -531,7 +475,7 @@ public class GQuery implements Lazy { public GQuery addClass(String... classes) { for (Element e : elements()) { for (String clz : classes) { - setStyleName(e, clz, true); + e.addClassName(clz); } } return this; @@ -1800,7 +1744,7 @@ public class GQuery implements Lazy { public GQuery removeClass(String... classes) { for (Element e : elements()) { for (String clz : classes) { - setStyleName(e, clz, false); + e.removeClassName(clz); } } return this; @@ -2077,15 +2021,16 @@ public class GQuery implements Lazy { } /** - * Adds or removes the specified classes to each matched element. + * Adds or removes the specified classes to each matched element + * depending on the class's presence */ public GQuery toggleClass(String... classes) { for (Element e : elements()) { for (String clz : classes) { if (hasClass(e, clz)) { - setStyleName(e, clz, false); + e.removeClassName(clz); } else { - setStyleName(e, clz, true); + e.addClassName(clz); } } } @@ -2093,11 +2038,17 @@ public class GQuery implements Lazy { } /** - * Adds or removes the specified classes to each matched element. + * Adds or removes the specified classes to each matched element + * depending on the value of the switch argument. + * + * if addOrRemove is true, the class is added and in the case of + * false it is removed. */ - public GQuery toggleClass(String clz, boolean sw) { - for (Element e : elements()) { - setStyleName(e, clz, sw); + public GQuery toggleClass(String clz, boolean addOrRemove) { + if (addOrRemove) { + addClass(clz); + } else { + removeClass(clz); } return this; } 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 index 5dff29d7..ccd68d3d 100644 --- 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 @@ -93,7 +93,19 @@ public class GwtQueryCoreTest extends GWTTestCase { assertTrue(gq.hasClass("b2")); assertFalse(gq.hasClass("c1")); assertFalse(gq.hasClass("c2")); - + + // toggleClass() + gq.toggleClass("b2"); + assertTrue(gq.hasClass("b1")); + assertFalse(gq.hasClass("b2")); + gq.toggleClass("b2"); + assertTrue(gq.hasClass("b1")); + assertTrue(gq.hasClass("b2")); + gq.toggleClass("b2", true); + assertTrue(gq.hasClass("b2")); + gq.toggleClass("b2", false); + assertFalse(gq.hasClass("b2")); + // css() String content = "

Test Paragraph.

"; $(e).html(content); -- cgit v1.2.3