From 3f7c75415bc73ab750f809bde0c5da02f5d8e7c2 Mon Sep 17 00:00:00 2001 From: Artur Signell Date: Sat, 13 Dec 2014 12:12:12 +0200 Subject: Support plain-text attribute for Label (#7749) Change-Id: I4ef210d131f1e616b501468cb94ea518945a3b30 --- server/src/com/vaadin/ui/AbstractComponent.java | 2 + server/src/com/vaadin/ui/Button.java | 6 +-- server/src/com/vaadin/ui/Label.java | 10 +++++ .../component/button/TestSynchronizeToDesign.java | 13 +++++++ .../component/label/TestSynchronizeToDesign.java | 43 ++++++++++++++++++++-- 5 files changed, 68 insertions(+), 6 deletions(-) (limited to 'server') diff --git a/server/src/com/vaadin/ui/AbstractComponent.java b/server/src/com/vaadin/ui/AbstractComponent.java index 1cda253b01..808b70386f 100644 --- a/server/src/com/vaadin/ui/AbstractComponent.java +++ b/server/src/com/vaadin/ui/AbstractComponent.java @@ -111,6 +111,8 @@ public abstract class AbstractComponent extends AbstractClientConnector private Boolean explicitImmediateValue; + protected static final String DESIGN_ATTR_PLAIN_TEXT = "plain-text"; + /* Constructor */ /** diff --git a/server/src/com/vaadin/ui/Button.java b/server/src/com/vaadin/ui/Button.java index 82666613bb..9a754c24d9 100644 --- a/server/src/com/vaadin/ui/Button.java +++ b/server/src/com/vaadin/ui/Button.java @@ -686,7 +686,7 @@ public class Button extends AbstractComponent implements def.getTabIndex(), Integer.class)); // plain-text (default is html) setHtmlContentAllowed(!DesignAttributeHandler.readAttribute( - "plain-text", attr, false, Boolean.class)); + DESIGN_ATTR_PLAIN_TEXT, attr, false, Boolean.class)); setIconAlternateText(DesignAttributeHandler.readAttribute("icon-alt", attr, def.getIconAlternateText(), String.class)); // click-shortcut @@ -707,7 +707,7 @@ public class Button extends AbstractComponent implements protected Collection getCustomAttributes() { Collection result = super.getCustomAttributes(); result.add("tabindex"); - result.add("plain-text"); + result.add(DESIGN_ATTR_PLAIN_TEXT); result.add("caption"); result.add("icon-alt"); result.add("click-shortcut"); @@ -736,7 +736,7 @@ public class Button extends AbstractComponent implements def.getTabIndex(), Integer.class); // plain-text (default is html) if (!isHtmlContentAllowed()) { - design.attr("plain-text", ""); + design.attr(DESIGN_ATTR_PLAIN_TEXT, ""); } // icon-alt DesignAttributeHandler.writeAttribute("icon-alt", attr, diff --git a/server/src/com/vaadin/ui/Label.java b/server/src/com/vaadin/ui/Label.java index a4d9da752d..f882940fe6 100644 --- a/server/src/com/vaadin/ui/Label.java +++ b/server/src/com/vaadin/ui/Label.java @@ -590,6 +590,11 @@ public class Label extends AbstractComponent implements Property, if (innerHtml != null && !"".equals(innerHtml)) { setValue(innerHtml); } + if (design.hasAttr(DESIGN_ATTR_PLAIN_TEXT)) { + setContentMode(ContentMode.TEXT); + } else { + setContentMode(ContentMode.HTML); + } } /* @@ -601,6 +606,7 @@ public class Label extends AbstractComponent implements Property, protected Collection getCustomAttributes() { Collection result = super.getCustomAttributes(); result.add("value"); + result.add("content-mode"); return result; } @@ -618,5 +624,9 @@ public class Label extends AbstractComponent implements Property, if (content != null) { design.html(getValue()); } + // plain-text (default is html) + if (getContentMode() == ContentMode.TEXT) { + design.attr(DESIGN_ATTR_PLAIN_TEXT, ""); + } } } diff --git a/server/tests/src/com/vaadin/tests/server/component/button/TestSynchronizeToDesign.java b/server/tests/src/com/vaadin/tests/server/component/button/TestSynchronizeToDesign.java index 7e9d673f35..c96fd36d5d 100644 --- a/server/tests/src/com/vaadin/tests/server/component/button/TestSynchronizeToDesign.java +++ b/server/tests/src/com/vaadin/tests/server/component/button/TestSynchronizeToDesign.java @@ -66,6 +66,19 @@ public class TestSynchronizeToDesign extends TestCase { assertEquals("ctrl-shift-o", e.attr("click-shortcut")); } + @Test + public void testUpdateContentMode() { + Button button = new Button("OK"); + Element e = new Element(Tag.valueOf("v-button"), "", new Attributes()); + button.synchronizeToDesign(e, ctx); + assertTrue("Button is plain text by default", e.hasAttr("plain-text")); + + button.setHtmlContentAllowed(true); + button.synchronizeToDesign(e, ctx); + assertTrue("Button is updated to HTML", !e.hasAttr("plain-text")); + + } + private void createAndTestButtons(String content) { Button b1 = new Button(content); // we need to set this on, since the plain-text attribute will appear diff --git a/server/tests/src/com/vaadin/tests/server/component/label/TestSynchronizeToDesign.java b/server/tests/src/com/vaadin/tests/server/component/label/TestSynchronizeToDesign.java index 462e6a087b..869546ac3d 100644 --- a/server/tests/src/com/vaadin/tests/server/component/label/TestSynchronizeToDesign.java +++ b/server/tests/src/com/vaadin/tests/server/component/label/TestSynchronizeToDesign.java @@ -17,9 +17,12 @@ package com.vaadin.tests.server.component.label; import junit.framework.TestCase; +import org.jsoup.nodes.Attributes; import org.jsoup.nodes.Element; +import org.jsoup.parser.Tag; import org.junit.Test; +import com.vaadin.shared.ui.label.ContentMode; import com.vaadin.ui.Label; import com.vaadin.ui.declarative.DesignContext; @@ -56,6 +59,43 @@ public class TestSynchronizeToDesign extends TestCase { createAndTestLabel("A label", "Label caption"); } + @Test + public void testContentModeText() { + Label l = new Label("plain text label"); + Element e = new Element(Tag.valueOf("v-label"), "", new Attributes()); + l.synchronizeToDesign(e, ctx); + assertTrue("Label should be marked as plain text", + e.hasAttr("plain-text")); + } + + @Test + public void testContentModeHtml() { + Label l = new Label("html label"); + l.setContentMode(ContentMode.HTML); + + Element e = new Element(Tag.valueOf("v-label"), "", new Attributes()); + l.synchronizeToDesign(e, ctx); + assertFalse("Label should not be marked as plain text", + e.hasAttr("plain-text")); + } + + @Test + public void testChangeContentMode() { + Label l = new Label("html label"); + l.setContentMode(ContentMode.HTML); + + Element e = new Element(Tag.valueOf("v-label"), "", new Attributes()); + l.synchronizeToDesign(e, ctx); + + assertFalse("Label should not be marked as plain text", + e.hasAttr("plain-text")); + l.setContentMode(ContentMode.TEXT); + l.synchronizeToDesign(e, ctx); + + assertTrue("Label should be marked as plain text", + e.hasAttr("plain-text")); + } + @Test public void testWithoutContentAndCaption() { createAndTestLabel(null, null); @@ -75,9 +115,6 @@ public class TestSynchronizeToDesign extends TestCase { assertTrue("Unexpected content in the v-label element.", e.html() == null || "".equals(e.html())); } - int numAttributes = (caption == null ? 0 : 1); - assertEquals("Wrong number of attributes in v-label.", numAttributes, e - .attributes().size()); if (caption != null) { assertEquals("Wrong caption in the v-label element.", caption, e.attr("caption")); -- cgit v1.2.3