Sfoglia il codice sorgente

Allow TabSheet and Accordion tab captions to contain HTML (#14609)

Change-Id: If15db442fdbdcc80918e52f8c87e0808f76eb336
tags/7.4.0.beta1
Artur Signell 9 anni fa
parent
commit
42ef01bfdb

+ 1
- 0
client/src/com/vaadin/client/ui/VAccordion.java Vedi File

@@ -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,

+ 1
- 0
client/src/com/vaadin/client/ui/VTabsheet.java Vedi File

@@ -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;

+ 30
- 0
client/src/com/vaadin/client/ui/VTabsheetBase.java Vedi File

@@ -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;
}

}

+ 30
- 0
server/src/com/vaadin/ui/TabSheet.java Vedi File

@@ -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;
}
}

+ 4
- 0
shared/src/com/vaadin/shared/ui/tabsheet/TabsheetState.java Vedi File

@@ -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;

}

+ 72
- 0
uitest/src/com/vaadin/tests/components/tabsheet/HtmlInTabCaption.java Vedi File

@@ -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;
}

}

+ 73
- 0
uitest/src/com/vaadin/tests/components/tabsheet/HtmlInTabCaptionTest.java Vedi File

@@ -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());
}
}

Loading…
Annulla
Salva