Repository: wicket
Updated Branches:
  refs/heads/5340-css-class-style-modifier [created] 526280230


WICKET-5340 CssAttributeModifier and StyleAttributeModifier


Project: http://git-wip-us.apache.org/repos/asf/wicket/repo
Commit: http://git-wip-us.apache.org/repos/asf/wicket/commit/52628023
Tree: http://git-wip-us.apache.org/repos/asf/wicket/tree/52628023
Diff: http://git-wip-us.apache.org/repos/asf/wicket/diff/52628023

Branch: refs/heads/5340-css-class-style-modifier
Commit: 526280230eb1ae69ec9dfbf801557c2bbc7b07e1
Parents: b6db8d9
Author: Martin Tzvetanov Grigorov <[email protected]>
Authored: Wed Feb 26 17:17:16 2014 +0200
Committer: Martin Tzvetanov Grigorov <[email protected]>
Committed: Wed Feb 26 17:17:16 2014 +0200

----------------------------------------------------------------------
 .../org/apache/wicket/AttributeModifier.java    |   6 +-
 .../apache/wicket/ClassAttributeModifier.java   |  80 ++++++++++++
 .../apache/wicket/AttributeModifierTest.java    |   2 +-
 .../wicket/ClassAttributeModifierTest.java      | 121 +++++++++++++++++++
 4 files changed, 207 insertions(+), 2 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/wicket/blob/52628023/wicket-core/src/main/java/org/apache/wicket/AttributeModifier.java
----------------------------------------------------------------------
diff --git a/wicket-core/src/main/java/org/apache/wicket/AttributeModifier.java 
b/wicket-core/src/main/java/org/apache/wicket/AttributeModifier.java
index 3a9b3f7..826939b 100644
--- a/wicket-core/src/main/java/org/apache/wicket/AttributeModifier.java
+++ b/wicket-core/src/main/java/org/apache/wicket/AttributeModifier.java
@@ -173,7 +173,11 @@ public class AttributeModifier extends Behavior implements 
IClusterable
                        {
                                final String value = 
toStringOrNull(attributes.get(attribute));
                                final String newValue = newValue(value, 
toStringOrNull(replacementValue));
-                               if (newValue != null)
+                               if (newValue == VALUELESS_ATTRIBUTE_REMOVE)
+                               {
+                                       attributes.remove(attribute);
+                               }
+                               else if (newValue != null)
                                {
                                        attributes.put(attribute, newValue);
                                }

http://git-wip-us.apache.org/repos/asf/wicket/blob/52628023/wicket-core/src/main/java/org/apache/wicket/ClassAttributeModifier.java
----------------------------------------------------------------------
diff --git 
a/wicket-core/src/main/java/org/apache/wicket/ClassAttributeModifier.java 
b/wicket-core/src/main/java/org/apache/wicket/ClassAttributeModifier.java
new file mode 100644
index 0000000..6f3bf8a
--- /dev/null
+++ b/wicket-core/src/main/java/org/apache/wicket/ClassAttributeModifier.java
@@ -0,0 +1,80 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.wicket;
+
+import java.util.Collections;
+import java.util.Set;
+import java.util.TreeSet;
+import java.util.regex.Pattern;
+
+import org.apache.wicket.behavior.AttributeAppender;
+import org.apache.wicket.util.string.Strings;
+
+/**
+ * An AttributeModifier specialized in managing the <em>CSS class</em>
+ * attribute
+ */
+public abstract class ClassAttributeModifier extends AttributeAppender
+{
+       private static final Pattern SPLITTER = Pattern.compile("\\s+");
+
+       /**
+        * Constructor.
+        */
+       public ClassAttributeModifier()
+       {
+               super("class", null, " ");
+       }
+
+       @Override
+       protected final String newValue(String currentValue, String appendValue)
+       {
+               String[] classes;
+               if (Strings.isEmpty(currentValue))
+               {
+                       classes = new String[0];
+               }
+               else
+               {
+                       classes = SPLITTER.split(currentValue);
+               }
+               Set<String> oldClasses = new TreeSet<>();
+               Collections.addAll(oldClasses, classes);
+
+               Set<String> newClasses = update(oldClasses);
+
+               StringBuilder result = new StringBuilder();
+               for (String cls : newClasses)
+               {
+                       if (result.length() > 0)
+                       {
+                               result.append(getSeparator());
+                       }
+                       result.append(cls);
+               }
+               return result.length() > 0 ? result.toString() : 
VALUELESS_ATTRIBUTE_REMOVE;
+       }
+
+       /**
+        * Callback to update the CSS class values for a tag.
+        *
+        * @param oldClasses
+        *          A set with the old class values
+        * @return A set with the new class values
+        */
+       protected abstract Set<String> update(Set<String> oldClasses);
+}

http://git-wip-us.apache.org/repos/asf/wicket/blob/52628023/wicket-core/src/test/java/org/apache/wicket/AttributeModifierTest.java
----------------------------------------------------------------------
diff --git 
a/wicket-core/src/test/java/org/apache/wicket/AttributeModifierTest.java 
b/wicket-core/src/test/java/org/apache/wicket/AttributeModifierTest.java
index edc8cdc..bd71b4f 100644
--- a/wicket-core/src/test/java/org/apache/wicket/AttributeModifierTest.java
+++ b/wicket-core/src/test/java/org/apache/wicket/AttributeModifierTest.java
@@ -38,7 +38,7 @@ public class AttributeModifierTest extends Assert
        @Test(expected = IllegalArgumentException.class)
        public void nullAttributeFailsConstruction()
        {
-               new AttributeModifier(null, new Model<String>("model"));
+               new AttributeModifier(null, new Model<>("model"));
        }
 
        /**

http://git-wip-us.apache.org/repos/asf/wicket/blob/52628023/wicket-core/src/test/java/org/apache/wicket/ClassAttributeModifierTest.java
----------------------------------------------------------------------
diff --git 
a/wicket-core/src/test/java/org/apache/wicket/ClassAttributeModifierTest.java 
b/wicket-core/src/test/java/org/apache/wicket/ClassAttributeModifierTest.java
new file mode 100644
index 0000000..9b425ad
--- /dev/null
+++ 
b/wicket-core/src/test/java/org/apache/wicket/ClassAttributeModifierTest.java
@@ -0,0 +1,121 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.wicket;
+
+import java.util.Map;
+import java.util.Set;
+
+import org.apache.wicket.markup.ComponentTag;
+import org.apache.wicket.markup.parser.XmlTag;
+import org.junit.Assert;
+import org.junit.Test;
+
+/**
+ * Tests for ClassAttributeModifier
+ */
+public class ClassAttributeModifierTest extends Assert
+{
+       /**
+        * Adds two values
+        */
+       @Test
+       public void addCssClasses()
+       {
+               ClassAttributeModifier cam = new ClassAttributeModifier()
+               {
+                       @Override
+                       protected Set<String> update(Set<String> oldClasses)
+                       {
+                               oldClasses.add("one");
+                               oldClasses.add("two");
+                               return oldClasses;
+                       }
+               };
+               ComponentTag tag = createTag();
+
+               Map<String, Object> attributes = tag.getAttributes();
+
+               cam.replaceAttributeValue(null, tag);
+
+               String classes = (String) attributes.get(cam.getAttribute());
+               assertEquals("one two", classes);
+       }
+
+       /**
+        * Adds 'three' and removes 'two'
+        */
+       @Test
+       public void addRemoveCssClasses()
+       {
+               ClassAttributeModifier cam = new ClassAttributeModifier()
+               {
+                       @Override
+                       protected Set<String> update(Set<String> oldClasses)
+                       {
+                               oldClasses.add("one");
+                               oldClasses.remove("two");
+                               oldClasses.add("three");
+                               return oldClasses;
+                       }
+               };
+               ComponentTag tag = createTag();
+
+               Map<String, Object> attributes = tag.getAttributes();
+               attributes.put(cam.getAttribute(), "one two");
+
+               cam.replaceAttributeValue(null, tag);
+
+               String classes = (String) attributes.get(cam.getAttribute());
+               assertEquals("one three", classes);
+       }
+
+       /**
+        * Removes all CSS class values
+        */
+       @Test
+       public void removeAllCssClasses()
+       {
+               ClassAttributeModifier cam = new ClassAttributeModifier()
+               {
+                       @Override
+                       protected Set<String> update(Set<String> oldClasses)
+                       {
+                               oldClasses.remove("one");
+                               oldClasses.remove("two");
+                               return oldClasses;
+                       }
+               };
+               ComponentTag tag = createTag();
+
+               Map<String, Object> attributes = tag.getAttributes();
+               attributes.put(cam.getAttribute(), "two one");
+
+               cam.replaceAttributeValue(null, tag);
+
+               String classes = (String) attributes.get(cam.getAttribute());
+               assertNull(classes);
+       }
+
+       private ComponentTag createTag()
+       {
+               XmlTag xmlTag = new XmlTag();
+               ComponentTag tag = new ComponentTag(xmlTag);
+               tag.setId("ClassAttributeModifier");
+               tag.setName("test");
+               return tag;
+       }
+}

Reply via email to