From 34fb964358242f017c1c7ec9fc89d23f1510cd84 Mon Sep 17 00:00:00 2001 From: =?utf8?q?Olli=20Tiet=C3=A4v=C3=A4inen?= Date: Tue, 21 May 2019 12:41:59 +0300 Subject: [PATCH] pick Panel HTML caption changes from master (#11589) --- .../java/com/vaadin/client/ui/VPanel.java | 8 +++- .../client/ui/panel/PanelConnector.java | 22 +++++++---- .../components/panel/PanelChangeContents.java | 1 + .../components/panel/PanelHTMLCaption.java | 37 +++++++++++++++++++ .../panel/PanelChangeContentsTest.java | 2 + .../panel/PanelHTMLCaptionTest.java | 16 ++++++++ 6 files changed, 77 insertions(+), 9 deletions(-) create mode 100644 uitest/src/main/java/com/vaadin/tests/components/panel/PanelHTMLCaption.java create mode 100644 uitest/src/test/java/com/vaadin/tests/components/panel/PanelHTMLCaptionTest.java diff --git a/client/src/main/java/com/vaadin/client/ui/VPanel.java b/client/src/main/java/com/vaadin/client/ui/VPanel.java index f032fc5430..76870462ec 100644 --- a/client/src/main/java/com/vaadin/client/ui/VPanel.java +++ b/client/src/main/java/com/vaadin/client/ui/VPanel.java @@ -132,8 +132,12 @@ public class VPanel extends SimplePanel } /** For internal use only. May be removed or replaced in the future. */ - public void setCaption(String text) { - DOM.setInnerHTML(captionText, text); + public void setCaption(String text, boolean captionAsHtml) { + if (captionAsHtml) { + captionText.setInnerHTML(text); + } else { + captionText.setInnerText(text); + } } /** For internal use only. May be removed or replaced in the future. */ diff --git a/client/src/main/java/com/vaadin/client/ui/panel/PanelConnector.java b/client/src/main/java/com/vaadin/client/ui/panel/PanelConnector.java index 950c472ded..6e72bcacfb 100644 --- a/client/src/main/java/com/vaadin/client/ui/panel/PanelConnector.java +++ b/client/src/main/java/com/vaadin/client/ui/panel/PanelConnector.java @@ -91,18 +91,17 @@ public class PanelConnector extends AbstractSingleComponentContainerConnector // Affects size calculations // Restore default stylenames + getWidget().contentNode.setClassName(VPanel.CLASSNAME + "-content"); getWidget().bottomDecoration .setClassName(VPanel.CLASSNAME + "-deco"); getWidget().captionNode.setClassName(VPanel.CLASSNAME + "-caption"); - boolean hasCaption = false; - if (getState().caption != null && !"".equals(getState().caption)) { - getWidget().setCaption(getState().caption); - hasCaption = true; + boolean hasCaption = hasCaption(); + if (hasCaption) { + getWidget().setCaption(getState().caption,getState().captionAsHtml); } else { - getWidget().setCaption(""); - getWidget().captionNode - .setClassName(VPanel.CLASSNAME + "-nocaption"); + getWidget().setCaption("",false); + getWidget().captionNode.setClassName(VPanel.CLASSNAME + "-nocaption"); } // Add proper stylenames for all elements. This way we can prevent @@ -177,6 +176,15 @@ public class PanelConnector extends AbstractSingleComponentContainerConnector getWidget().contentNode.setTabIndex(getState().tabIndex); } + /** + * Detects if caption div should be visible. + * + * @return {@code true} if caption div should be shown + */ + protected boolean hasCaption() { + return getState().caption != null && !getState().caption.isEmpty(); + } + @Override public void updateCaption(ComponentConnector component) { // NOP: layouts caption, errors etc not rendered in Panel diff --git a/uitest/src/main/java/com/vaadin/tests/components/panel/PanelChangeContents.java b/uitest/src/main/java/com/vaadin/tests/components/panel/PanelChangeContents.java index a040647c12..ccd9a8a9fb 100644 --- a/uitest/src/main/java/com/vaadin/tests/components/panel/PanelChangeContents.java +++ b/uitest/src/main/java/com/vaadin/tests/components/panel/PanelChangeContents.java @@ -50,6 +50,7 @@ public class PanelChangeContents extends AbstractTestUI buttons.addComponent(companiesButton); buttons.addComponent(settingsButton); panel = new Panel(); + panel.setCaption("
Caption
"); panel.setSizeFull(); panel.setContent(stats); content.addComponent(buttons); diff --git a/uitest/src/main/java/com/vaadin/tests/components/panel/PanelHTMLCaption.java b/uitest/src/main/java/com/vaadin/tests/components/panel/PanelHTMLCaption.java new file mode 100644 index 0000000000..bbd5c57158 --- /dev/null +++ b/uitest/src/main/java/com/vaadin/tests/components/panel/PanelHTMLCaption.java @@ -0,0 +1,37 @@ +package com.vaadin.tests.components.panel; + +import com.vaadin.server.VaadinRequest; +import com.vaadin.tests.components.AbstractTestUI; +import com.vaadin.ui.Panel; +import com.vaadin.ui.Button; +import com.vaadin.ui.VerticalLayout; + +public class PanelHTMLCaption extends AbstractTestUI { + public static final String caption = "
BOLD CAPTION
"; + private Panel panel; + + @Override + protected void setup(VaadinRequest request) { + panel = new Panel(); + panel.setId("panelId"); + panel.setWidth("200px"); + panel.setHeight("200px"); + + panel.setCaption(caption); + panel.setCaptionAsHtml(false); + + panel.setContent(new VerticalLayout()); + + addComponent(panel); + Button changeCaptionFormat = new Button( + "Set Caption as HTML", new Button.ClickListener() { + @Override + public void buttonClick(Button.ClickEvent event) { + panel.setCaptionAsHtml(true); + } + }); + + changeCaptionFormat.setId("buttonId"); + addComponent(changeCaptionFormat); + } +} \ No newline at end of file diff --git a/uitest/src/test/java/com/vaadin/tests/components/panel/PanelChangeContentsTest.java b/uitest/src/test/java/com/vaadin/tests/components/panel/PanelChangeContentsTest.java index 35c716ab55..d2704d9357 100644 --- a/uitest/src/test/java/com/vaadin/tests/components/panel/PanelChangeContentsTest.java +++ b/uitest/src/test/java/com/vaadin/tests/components/panel/PanelChangeContentsTest.java @@ -3,6 +3,7 @@ package com.vaadin.tests.components.panel; import org.junit.Assert; import org.junit.Test; +import com.vaadin.testbench.By; import com.vaadin.tests.tb3.MultiBrowserTest; public class PanelChangeContentsTest extends MultiBrowserTest { @@ -31,5 +32,6 @@ public class PanelChangeContentsTest extends MultiBrowserTest { "/VVerticalLayout[0]/Slot[1]/VPanel[0]/VVerticalLayout[0]/Slot[0]/VLabel[0]") .getText()); + assertElementNotPresent(By.className("caption-with-html")); } } diff --git a/uitest/src/test/java/com/vaadin/tests/components/panel/PanelHTMLCaptionTest.java b/uitest/src/test/java/com/vaadin/tests/components/panel/PanelHTMLCaptionTest.java new file mode 100644 index 0000000000..e360bfa00a --- /dev/null +++ b/uitest/src/test/java/com/vaadin/tests/components/panel/PanelHTMLCaptionTest.java @@ -0,0 +1,16 @@ +package com.vaadin.tests.components.panel; + +import com.vaadin.tests.tb3.MultiBrowserTest; +import org.junit.Test; +import org.openqa.selenium.By; + + +public class PanelHTMLCaptionTest extends MultiBrowserTest { + @Test + public void testCaptionDisplayedAsText() { + openTestURL(); + assertElementNotPresent(By.id("divId")); + findElement(By.id("buttonId")).click(); + assertElementPresent(By.id("divId")); + } +} -- 2.39.5