Repository: wicket
Updated Branches:
  refs/heads/5340-css-class-style-modifier 526280230 -> a20cdb51c


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/a20cdb51
Tree: http://git-wip-us.apache.org/repos/asf/wicket/tree/a20cdb51
Diff: http://git-wip-us.apache.org/repos/asf/wicket/diff/a20cdb51

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

----------------------------------------------------------------------
 .../apache/wicket/ClassAttributeModifier.java   |   2 +-
 .../apache/wicket/StyleAttributeModifier.java   |  84 +++++++++++++
 .../wicket/ClassAttributeModifierTest.java      |   4 +-
 .../wicket/StyleAttributeModifierTest.java      | 120 +++++++++++++++++++
 4 files changed, 207 insertions(+), 3 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/wicket/blob/a20cdb51/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
index 6f3bf8a..b67ff5b 100644
--- a/wicket-core/src/main/java/org/apache/wicket/ClassAttributeModifier.java
+++ b/wicket-core/src/main/java/org/apache/wicket/ClassAttributeModifier.java
@@ -50,7 +50,7 @@ public abstract class ClassAttributeModifier extends 
AttributeAppender
                }
                else
                {
-                       classes = SPLITTER.split(currentValue);
+                       classes = SPLITTER.split(currentValue.trim());
                }
                Set<String> oldClasses = new TreeSet<>();
                Collections.addAll(oldClasses, classes);

http://git-wip-us.apache.org/repos/asf/wicket/blob/a20cdb51/wicket-core/src/main/java/org/apache/wicket/StyleAttributeModifier.java
----------------------------------------------------------------------
diff --git 
a/wicket-core/src/main/java/org/apache/wicket/StyleAttributeModifier.java 
b/wicket-core/src/main/java/org/apache/wicket/StyleAttributeModifier.java
new file mode 100644
index 0000000..64cf077
--- /dev/null
+++ b/wicket-core/src/main/java/org/apache/wicket/StyleAttributeModifier.java
@@ -0,0 +1,84 @@
+/*
+ * 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.LinkedHashMap;
+import java.util.Map;
+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 style</em>
+ * attribute
+ */
+public abstract class StyleAttributeModifier extends AttributeAppender
+{
+       private static final Pattern STYLES_SPLITTER = 
Pattern.compile("\\s*;\\s*");
+       private static final Pattern KEY_VALUE_SPLITTER = 
Pattern.compile("\\s*:\\s*");
+
+       /**
+        * Constructor.
+        */
+       public StyleAttributeModifier()
+       {
+               super("style", null, ";");
+       }
+
+       @Override
+       protected final String newValue(String currentValue, String appendValue)
+       {
+               String[] styles;
+               if (Strings.isEmpty(currentValue))
+               {
+                       styles = new String[0];
+               }
+               else
+               {
+                       styles = STYLES_SPLITTER.split(currentValue.trim());
+               }
+               Map<String, String> oldStyles = new LinkedHashMap<>();
+               for (String style : styles)
+               {
+                       String[] keyValue = KEY_VALUE_SPLITTER.split(style, 2);
+                       oldStyles.put(keyValue[0], keyValue[1]);
+               }
+
+               Map<String, String> newStyles = update(oldStyles);
+
+               StringBuilder result = new StringBuilder();
+               for (Map.Entry<String, String> entry : newStyles.entrySet())
+               {
+                       result
+                               .append(entry.getKey())
+                               .append(':')
+                               .append(entry.getValue())
+                               .append(getSeparator());
+               }
+               return result.length() > 0 ? result.toString() : 
VALUELESS_ATTRIBUTE_REMOVE;
+       }
+
+       /**
+        * Callback to update the CSS class values for a tag.
+        *
+        * @param oldStyles
+        *          A map with the old style key/values
+        * @return A map with the new style key/values
+        */
+       protected abstract Map<String, String> update(Map<String, String> 
oldStyles);
+}

http://git-wip-us.apache.org/repos/asf/wicket/blob/a20cdb51/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
index 9b425ad..7cde80c 100644
--- 
a/wicket-core/src/test/java/org/apache/wicket/ClassAttributeModifierTest.java
+++ 
b/wicket-core/src/test/java/org/apache/wicket/ClassAttributeModifierTest.java
@@ -75,7 +75,7 @@ public class ClassAttributeModifierTest extends Assert
                ComponentTag tag = createTag();
 
                Map<String, Object> attributes = tag.getAttributes();
-               attributes.put(cam.getAttribute(), "one two");
+               attributes.put(cam.getAttribute(), "one two    ");
 
                cam.replaceAttributeValue(null, tag);
 
@@ -102,7 +102,7 @@ public class ClassAttributeModifierTest extends Assert
                ComponentTag tag = createTag();
 
                Map<String, Object> attributes = tag.getAttributes();
-               attributes.put(cam.getAttribute(), "two one");
+               attributes.put(cam.getAttribute(), "two    one");
 
                cam.replaceAttributeValue(null, tag);
 

http://git-wip-us.apache.org/repos/asf/wicket/blob/a20cdb51/wicket-core/src/test/java/org/apache/wicket/StyleAttributeModifierTest.java
----------------------------------------------------------------------
diff --git 
a/wicket-core/src/test/java/org/apache/wicket/StyleAttributeModifierTest.java 
b/wicket-core/src/test/java/org/apache/wicket/StyleAttributeModifierTest.java
new file mode 100644
index 0000000..b471166
--- /dev/null
+++ 
b/wicket-core/src/test/java/org/apache/wicket/StyleAttributeModifierTest.java
@@ -0,0 +1,120 @@
+/*
+ * 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 org.apache.wicket.markup.ComponentTag;
+import org.apache.wicket.markup.parser.XmlTag;
+import org.junit.Assert;
+import org.junit.Test;
+
+/**
+ * Tests for StyleAttributeModifier
+ */
+public class StyleAttributeModifierTest extends Assert
+{
+       /**
+        * Adds two style properties
+        */
+       @Test
+       public void addCssStyles()
+       {
+               StyleAttributeModifier cam = new StyleAttributeModifier()
+               {
+                       @Override
+                       protected Map<String, String> update(Map<String, 
String> oldStyles)
+                       {
+                               oldStyles.put("color", "white");
+                               oldStyles.put("font-size", "9px");
+                               return oldStyles;
+                       }
+               };
+               ComponentTag tag = createTag();
+
+               Map<String, Object> attributes = tag.getAttributes();
+
+               cam.replaceAttributeValue(null, tag);
+
+               String styles = (String) attributes.get(cam.getAttribute());
+               assertEquals("color:white;font-size:9px;", styles);
+       }
+
+       /**
+        * Modifies one style, removes another and adds a new style
+        */
+       @Test
+       public void addRemoveCssStyles()
+       {
+               StyleAttributeModifier cam = new StyleAttributeModifier()
+               {
+                       @Override
+                       protected Map<String, String> update(Map<String, 
String> oldStyles)
+                       {
+                               oldStyles.put("color", "black");           // 
modify the value
+                               oldStyles.remove("font-size");             // 
remove
+                               oldStyles.put("background-color", "red");  // 
add
+                               return oldStyles;
+                       }
+               };
+               ComponentTag tag = createTag();
+
+               Map<String, Object> attributes = tag.getAttributes();
+               attributes.put(cam.getAttribute(), 
"color:white;font-size:9px;");
+
+               cam.replaceAttributeValue(null, tag);
+
+               String classes = (String) attributes.get(cam.getAttribute());
+               assertEquals("color:black;background-color:red;", classes);
+       }
+
+       /**
+        * Removes all CSS style values and the attribute itself
+        */
+       @Test
+       public void removeAllCssStyles()
+       {
+               StyleAttributeModifier cam = new StyleAttributeModifier()
+               {
+                       @Override
+                       protected Map<String, String> update(Map<String, 
String> oldStyles)
+                       {
+                               oldStyles.remove("color");
+                               oldStyles.remove("font-size");
+                               return oldStyles;
+                       }
+               };
+               ComponentTag tag = createTag();
+
+               Map<String, Object> attributes = tag.getAttributes();
+               attributes.put(cam.getAttribute(), "color:white ;   
font-size:99999px; ");
+
+               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("StyleAttributeModifier");
+               tag.setName("test");
+               return tag;
+       }
+}

Reply via email to