summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorArtur Signell <artur@vaadin.com>2014-09-04 22:12:59 +0300
committerVaadin Code Review <review@vaadin.com>2014-12-18 21:47:26 +0000
commit42ef01bfdb2c93e03b8b8e54b7422cbc9b1eea32 (patch)
tree6959d960cf291550d97de6a681bc1588bb532be8
parent304cd3f48a0e7729053491d6f2afc51b3947a240 (diff)
downloadvaadin-framework-42ef01bfdb2c93e03b8b8e54b7422cbc9b1eea32.tar.gz
vaadin-framework-42ef01bfdb2c93e03b8b8e54b7422cbc9b1eea32.zip
Allow TabSheet and Accordion tab captions to contain HTML (#14609)
Change-Id: If15db442fdbdcc80918e52f8c87e0808f76eb336
-rw-r--r--client/src/com/vaadin/client/ui/VAccordion.java1
-rw-r--r--client/src/com/vaadin/client/ui/VTabsheet.java1
-rw-r--r--client/src/com/vaadin/client/ui/VTabsheetBase.java30
-rw-r--r--server/src/com/vaadin/ui/TabSheet.java30
-rw-r--r--shared/src/com/vaadin/shared/ui/tabsheet/TabsheetState.java4
-rw-r--r--uitest/src/com/vaadin/tests/components/tabsheet/HtmlInTabCaption.java72
-rw-r--r--uitest/src/com/vaadin/tests/components/tabsheet/HtmlInTabCaptionTest.java73
7 files changed, 211 insertions, 0 deletions
diff --git a/client/src/com/vaadin/client/ui/VAccordion.java b/client/src/com/vaadin/client/ui/VAccordion.java
index ff77a8cb91..422f195af9 100644
--- a/client/src/com/vaadin/client/ui/VAccordion.java
+++ b/client/src/com/vaadin/client/ui/VAccordion.java
@@ -344,6 +344,7 @@ public class VAccordion extends VTabsheetBase {
public void updateCaption(TabState tabState) {
// TODO need to call this because the caption does not have an owner
+ caption.setCaptionAsHtml(isTabCaptionsAsHtml());
caption.updateCaptionWithoutOwner(
tabState.caption,
!tabState.enabled,
diff --git a/client/src/com/vaadin/client/ui/VTabsheet.java b/client/src/com/vaadin/client/ui/VTabsheet.java
index 10c9a332e0..2d34897986 100644
--- a/client/src/com/vaadin/client/ui/VTabsheet.java
+++ b/client/src/com/vaadin/client/ui/VTabsheet.java
@@ -235,6 +235,7 @@ public class VTabsheet extends VTabsheetBase implements Focusable, SubPartAware
}
private void updateFromState(TabState tabState) {
+ tabCaption.setCaptionAsHtml(getTabsheet().isTabCaptionsAsHtml());
tabCaption.update(tabState);
// Apply the styleName set for the tab
String newStyleName = tabState.styleName;
diff --git a/client/src/com/vaadin/client/ui/VTabsheetBase.java b/client/src/com/vaadin/client/ui/VTabsheetBase.java
index d3c9bf9e10..e96aa035ed 100644
--- a/client/src/com/vaadin/client/ui/VTabsheetBase.java
+++ b/client/src/com/vaadin/client/ui/VTabsheetBase.java
@@ -49,6 +49,8 @@ public abstract class VTabsheetBase extends ComplexPanel implements HasEnabled {
/** For internal use only. May be removed or replaced in the future. */
protected AbstractComponentConnector connector;
+ private boolean tabCaptionsAsHtml = false;
+
public VTabsheetBase(String classname) {
setElement(DOM.createDiv());
setStyleName(classname);
@@ -168,4 +170,32 @@ public abstract class VTabsheetBase extends ComplexPanel implements HasEnabled {
public boolean isEnabled() {
return !disabled;
}
+
+ /**
+ * Sets whether the caption is rendered as HTML.
+ * <p>
+ * The default is false, i.e. render tab captions as plain text
+ *
+ * @since 7.4
+ * @param captionAsHtml
+ * true if the captions are rendered as HTML, false if rendered
+ * as plain text
+ */
+ public void setTabCaptionsAsHtml(boolean tabCaptionsAsHtml) {
+ this.tabCaptionsAsHtml = tabCaptionsAsHtml;
+ }
+
+ /**
+ * Checks whether captions are rendered as HTML
+ *
+ * The default is false, i.e. render tab captions as plain text
+ *
+ * @since 7.4
+ * @return true if the captions are rendered as HTML, false if rendered as
+ * plain text
+ */
+ public boolean isTabCaptionsAsHtml() {
+ return tabCaptionsAsHtml;
+ }
+
}
diff --git a/server/src/com/vaadin/ui/TabSheet.java b/server/src/com/vaadin/ui/TabSheet.java
index 266c93e81f..67dfdd4258 100644
--- a/server/src/com/vaadin/ui/TabSheet.java
+++ b/server/src/com/vaadin/ui/TabSheet.java
@@ -1621,4 +1621,34 @@ public class TabSheet extends AbstractComponentContainer implements Focusable,
}
}
+ /**
+ * Sets whether HTML is allowed in the tab captions.
+ * <p>
+ * If set to true, the captions are rendered in the browser as HTML and the
+ * developer is responsible for ensuring no harmful HTML is used. If set to
+ * false, the content is rendered in the browser as plain text.
+ * <p>
+ * The default is false, i.e. render tab captions as plain text
+ *
+ * @param tabCaptionsAsHtml
+ * true if the tab captions are rendered as HTML, false if
+ * rendered as plain text
+ * @since 7.4
+ */
+ public void setTabCaptionsAsHtml(boolean tabCaptionsAsHtml) {
+ getState().tabCaptionsAsHtml = tabCaptionsAsHtml;
+ }
+
+ /**
+ * Checks whether HTML is allowed in the tab captions.
+ * <p>
+ * The default is false, i.e. render tab captions as plain text
+ *
+ * @return true if the tab captions are rendered as HTML, false if rendered
+ * as plain text
+ * @since 7.4
+ */
+ public boolean isTabCaptionsAsHtml() {
+ return getState(false).tabCaptionsAsHtml;
+ }
}
diff --git a/shared/src/com/vaadin/shared/ui/tabsheet/TabsheetState.java b/shared/src/com/vaadin/shared/ui/tabsheet/TabsheetState.java
index 98a1d2b87f..f17f214626 100644
--- a/shared/src/com/vaadin/shared/ui/tabsheet/TabsheetState.java
+++ b/shared/src/com/vaadin/shared/ui/tabsheet/TabsheetState.java
@@ -19,6 +19,7 @@ import java.util.ArrayList;
import java.util.List;
import com.vaadin.shared.AbstractComponentState;
+import com.vaadin.shared.annotations.DelegateToWidget;
public class TabsheetState extends AbstractComponentState {
public static final String PRIMARY_STYLE_NAME = "v-tabsheet";
@@ -41,4 +42,7 @@ public class TabsheetState extends AbstractComponentState {
/** the key of the currently selected tab */
public String selected;
+ @DelegateToWidget
+ public boolean tabCaptionsAsHtml = false;
+
}
diff --git a/uitest/src/com/vaadin/tests/components/tabsheet/HtmlInTabCaption.java b/uitest/src/com/vaadin/tests/components/tabsheet/HtmlInTabCaption.java
new file mode 100644
index 0000000000..66a27a10b5
--- /dev/null
+++ b/uitest/src/com/vaadin/tests/components/tabsheet/HtmlInTabCaption.java
@@ -0,0 +1,72 @@
+/*
+ * Copyright 2000-2014 Vaadin Ltd.
+ *
+ * Licensed 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 com.vaadin.tests.components.tabsheet;
+
+import com.vaadin.server.VaadinRequest;
+import com.vaadin.tests.components.AbstractTestUI;
+import com.vaadin.ui.Accordion;
+import com.vaadin.ui.Label;
+import com.vaadin.ui.TabSheet;
+
+public class HtmlInTabCaption extends AbstractTestUI {
+
+ /*
+ * (non-Javadoc)
+ *
+ * @see com.vaadin.tests.components.AbstractTestUI#setup(com.vaadin.server.
+ * VaadinRequest)
+ */
+ @Override
+ protected void setup(VaadinRequest request) {
+ getLayout().setSpacing(true);
+ TabSheet ts = new TabSheet();
+ ts.setCaption("TabSheet - no <u>html</u> in tab captions");
+ ts.setCaptionAsHtml(true);
+ ts.addTab(new Label(), "<font color='red'>red</font>");
+ ts.addTab(new Label(), "<font color='blue'>blue</font>");
+ addComponent(ts);
+
+ ts = new TabSheet();
+ ts.setCaption("TabSheet - <b>html</b> in tab captions");
+ ts.setCaptionAsHtml(false);
+ ts.setTabCaptionsAsHtml(true);
+ ts.addTab(new Label(), "<font color='red'>red</font>");
+ ts.addTab(new Label(), "<font color='blue'>blue</font>");
+ addComponent(ts);
+
+ Accordion acc = new Accordion();
+ acc.setCaption("Accordion - no <u>html</u> in tab captions");
+ acc.setCaptionAsHtml(true);
+ acc.addTab(new Label(), "<font color='red'>red</font>");
+ acc.addTab(new Label(), "<font color='blue'>blue</font>");
+ addComponent(acc);
+
+ acc = new Accordion();
+ acc.setCaption("Accordion - <b>html</b> in tab captions");
+ acc.setCaptionAsHtml(false);
+ acc.setTabCaptionsAsHtml(true);
+ acc.addTab(new Label(), "<font color='red'>red</font>");
+ acc.addTab(new Label(), "<font color='blue'>blue</font>");
+ addComponent(acc);
+
+ }
+
+ @Override
+ protected Integer getTicketNumber() {
+ return 14609;
+ }
+
+}
diff --git a/uitest/src/com/vaadin/tests/components/tabsheet/HtmlInTabCaptionTest.java b/uitest/src/com/vaadin/tests/components/tabsheet/HtmlInTabCaptionTest.java
new file mode 100644
index 0000000000..41b7037b09
--- /dev/null
+++ b/uitest/src/com/vaadin/tests/components/tabsheet/HtmlInTabCaptionTest.java
@@ -0,0 +1,73 @@
+/*
+ * Copyright 2000-2014 Vaadin Ltd.
+ *
+ * Licensed 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 com.vaadin.tests.components.tabsheet;
+
+import org.junit.Assert;
+import org.junit.Test;
+import org.openqa.selenium.WebElement;
+
+import com.vaadin.testbench.By;
+import com.vaadin.testbench.elements.AccordionElement;
+import com.vaadin.testbench.elements.TabSheetElement;
+import com.vaadin.tests.tb3.SingleBrowserTest;
+
+public class HtmlInTabCaptionTest extends SingleBrowserTest {
+ static final String PLAIN_TEXT_RED = "<font color='red'>red</font>";
+ static final String HTML_TEXT_RED = "red";
+ static final String PLAIN_TEXT_BLUE = "<font color='blue'>blue</font>";
+ static final String HTML_TEXT_BLUE = "blue";
+
+ @Test
+ public void tabsheetWithoutHtmlCaptions() {
+ openTestURL();
+ TabSheetElement ts = $(TabSheetElement.class).get(0);
+ Assert.assertEquals(PLAIN_TEXT_RED, getTab(ts, 0).getText());
+ Assert.assertEquals(PLAIN_TEXT_BLUE, getTab(ts, 1).getText());
+ }
+
+ private WebElement getTab(TabSheetElement tabSheetElement, int i) {
+ String className = "v-tabsheet-tabitem";
+ if (tabSheetElement instanceof AccordionElement) {
+ className = "v-accordion-item";
+ }
+ return tabSheetElement.findElements(By.className(className)).get(i);
+ }
+
+ @Test
+ public void tabsheetWithHtmlCaptions() {
+ openTestURL();
+ TabSheetElement ts = $(TabSheetElement.class).get(1);
+ Assert.assertEquals(HTML_TEXT_RED, getTab(ts, 0).getText());
+ Assert.assertEquals(HTML_TEXT_BLUE, getTab(ts, 1).getText());
+ }
+
+ @Test
+ public void accordionWithoutHtmlCaptions() {
+ openTestURL();
+ AccordionElement acc = $(AccordionElement.class).get(0);
+ Assert.assertEquals(PLAIN_TEXT_RED, getTab(acc, 0).getText());
+ Assert.assertEquals(PLAIN_TEXT_BLUE, getTab(acc, 1).getText());
+
+ }
+
+ @Test
+ public void accordionWithHtmlCaptions() {
+ openTestURL();
+ AccordionElement acc = $(AccordionElement.class).get(1);
+ Assert.assertEquals(HTML_TEXT_RED, getTab(acc, 0).getText());
+ Assert.assertEquals(HTML_TEXT_BLUE, getTab(acc, 1).getText());
+ }
+}