]> source.dussan.org Git - vaadin-framework.git/commitdiff
Fix NPE in Label.writeDesign with NULL values #19434
authorJohn Ahlroos <john@vaadin.com>
Mon, 4 Jan 2016 12:42:05 +0000 (14:42 +0200)
committerVaadin Code Review <review@vaadin.com>
Tue, 5 Jan 2016 11:48:19 +0000 (11:48 +0000)
Change-Id: If3628bc655d6cd58ab51b7400af808bdbca3dc91

server/src/com/vaadin/ui/Label.java
server/tests/src/com/vaadin/tests/server/component/label/LabelDeclarativeTest.java

index 2b96d51fa59d9716d566e43d830d2a7d4e5a3f4e..1498246ec56ef1c7a6cc14880574f121e176f55d 100644 (file)
@@ -625,12 +625,31 @@ public class Label extends AbstractComponent implements Property<String>,
         super.writeDesign(design, designContext);
         String content = getValue();
         if (content != null) {
-            design.html(getValue());
-        }
-        // plain-text (default is html)
-        if (getContentMode() == ContentMode.TEXT) {
-            design.attr(DESIGN_ATTR_PLAIN_TEXT, true);
-            design.html(DesignFormatter.encodeForTextNode(getValue()));
+            switch (getContentMode()) {
+            case TEXT:
+            case PREFORMATTED:
+            case XML:
+            case RAW: {
+                // FIXME This attribute is not enough to be able to restore the
+                // content mode in readDesign. The content mode should instead
+                // be written directly in the attribute and restored in
+                // readDesign. See ticket #19435
+                design.attr(DESIGN_ATTR_PLAIN_TEXT, true);
+                String encodeForTextNode = DesignFormatter
+                        .encodeForTextNode(content);
+                if (encodeForTextNode != null) {
+                    design.html(encodeForTextNode);
+                }
+            }
+                break;
+            case HTML:
+                design.html(content);
+                break;
+            default:
+                throw new IllegalStateException(
+                        "ContentMode " + getContentMode()
+                                + " is not supported by writeDesign");
+            }
         }
     }
 }
index f911aab77903184be3db75c90d8f9853defe38e2..7a7f4bd84465887998a9ab096079c97b9a0b622b 100644 (file)
@@ -18,6 +18,7 @@ package com.vaadin.tests.server.component.label;
 import org.jsoup.nodes.Element;
 import org.jsoup.parser.Tag;
 import org.junit.Assert;
+import org.junit.Ignore;
 import org.junit.Test;
 
 import com.vaadin.shared.ui.label.ContentMode;
@@ -112,6 +113,38 @@ public class LabelDeclarativeTest extends DeclarativeTestBase<Label> {
         Assert.assertEquals("&amp; Test", root.html());
     }
 
+    @Test
+    public void testNullValue() {
+        Label label = new Label();
+        label.setValue(null);
+
+        label.setContentMode(ContentMode.TEXT);
+        Element root = new Element(Tag.valueOf("vaadin-label"), "");
+        label.writeDesign(root, new DesignContext());
+        Assert.assertEquals("", root.html());
+
+        label.setContentMode(ContentMode.HTML);
+        root = new Element(Tag.valueOf("vaadin-label"), "");
+        label.writeDesign(root, new DesignContext());
+        Assert.assertEquals("", root.html());
+    }
+
+    /**
+     * FIXME Using another content mode than TEXT OR HTML is currently not
+     * supported and will cause the content mode to fallback without the users
+     * knowledge to HTML. This test can be enabled when
+     * https://dev.vaadin.com/ticket/19435 is fixed.
+     */
+    @Test
+    @Ignore("Test ignored due to https://dev.vaadin.com/ticket/19435")
+    public void testContentModes() {
+        String design = "<vaadin-label caption='This\n is a label' />";
+        Label l = createLabel(null, "This\n is a label", true);
+        l.setContentMode(ContentMode.PREFORMATTED);
+        testRead(design, l);
+        testWrite(design, l);
+    }
+
     private Label createLabel(String content, String caption, boolean html) {
         Label label = new Label();
         label.setContentMode(html ? ContentMode.HTML : ContentMode.TEXT);