diff options
6 files changed, 179 insertions, 2 deletions
diff --git a/client/src/com/vaadin/client/ui/VTree.java b/client/src/com/vaadin/client/ui/VTree.java index 846b16d0cb..3e51374dc7 100644 --- a/client/src/com/vaadin/client/ui/VTree.java +++ b/client/src/com/vaadin/client/ui/VTree.java @@ -146,6 +146,9 @@ public class VTree extends FocusElementPanel implements VHasDropHandler, public boolean isNullSelectionAllowed = true; /** For internal use only. May be removed or replaced in the future. */ + public boolean isHtmlContentAllowed = false; + + /** For internal use only. May be removed or replaced in the future. */ public boolean disabled = false; /** For internal use only. May be removed or replaced in the future. */ @@ -983,6 +986,11 @@ public class VTree extends FocusElementPanel implements VHasDropHandler, DOM.setInnerText(nodeCaptionSpan, text); } + /** For internal use only. May be removed or replaced in the future. */ + public void setHtml(String html) { + nodeCaptionSpan.setInnerHTML(html); + } + public boolean isChildrenLoaded() { return childrenLoaded; } diff --git a/client/src/com/vaadin/client/ui/tree/TreeConnector.java b/client/src/com/vaadin/client/ui/tree/TreeConnector.java index 23091d0ad5..b2babe9d52 100644 --- a/client/src/com/vaadin/client/ui/tree/TreeConnector.java +++ b/client/src/com/vaadin/client/ui/tree/TreeConnector.java @@ -82,6 +82,8 @@ public class TreeConnector extends AbstractComponentConnector implements getWidget().isNullSelectionAllowed = uidl .getBooleanAttribute("nullselect"); + getWidget().isHtmlContentAllowed = uidl + .getBooleanAttribute(TreeConstants.ATTRIBUTE_HTML_ALLOWED); if (uidl.hasAttribute("alb")) { getWidget().bodyActionKeys = uidl.getStringArrayAttribute("alb"); @@ -245,8 +247,13 @@ public class TreeConnector extends AbstractComponentConnector implements level); String nodeKey = uidl.getStringAttribute("key"); - treeNode.setText(uidl - .getStringAttribute(TreeConstants.ATTRIBUTE_NODE_CAPTION)); + String caption = uidl + .getStringAttribute(TreeConstants.ATTRIBUTE_NODE_CAPTION); + if (getWidget().isHtmlContentAllowed) { + treeNode.setHtml(caption); + } else { + treeNode.setText(caption); + } treeNode.key = nodeKey; getWidget().registerNode(treeNode); diff --git a/server/src/com/vaadin/ui/Tree.java b/server/src/com/vaadin/ui/Tree.java index aac827e5b5..b41139275b 100644 --- a/server/src/com/vaadin/ui/Tree.java +++ b/server/src/com/vaadin/ui/Tree.java @@ -616,6 +616,10 @@ public class Tree extends AbstractSelect implements Container.Hierarchical, target.addAttribute("dragMode", dragMode.ordinal()); } + if (isHtmlContentAllowed()) { + target.addAttribute(TreeConstants.ATTRIBUTE_HTML_ALLOWED, true); + } + } // Initialize variables @@ -1310,6 +1314,8 @@ public class Tree extends AbstractSelect implements Container.Hierarchical, private DropHandler dropHandler; + private boolean htmlContentAllowed; + @Override public void addItemClickListener(ItemClickListener listener) { addListener(TreeConstants.ITEM_CLICK_EVENT_ID, ItemClickEvent.class, @@ -1896,4 +1902,34 @@ public class Tree extends AbstractSelect implements Container.Hierarchical, return element; } + + /** + * Sets whether html is allowed in the item captions. If set to + * <code>true</code>, the captions are passed to the browser as html and the + * developer is responsible for ensuring no harmful html is used. If set to + * <code>false</code>, the content is passed to the browser as plain text. + * The default setting is <code>false</code> + * + * @since + * @param htmlContentAllowed + * <code>true</code> if the captions are used as html, + * <code>false</code> if used as plain text + */ + public void setHtmlContentAllowed(boolean htmlContentAllowed) { + this.htmlContentAllowed = htmlContentAllowed; + markAsDirty(); + } + + /** + * Checks whether captions are interpreted as html or plain text. + * + * @since + * @return <code>true</code> if the captions are displayed as html, + * <code>false</code> if displayed as plain text + * @see #setHtmlContentAllowed(boolean) + */ + public boolean isHtmlContentAllowed() { + return htmlContentAllowed; + } + } diff --git a/shared/src/com/vaadin/shared/ui/tree/TreeConstants.java b/shared/src/com/vaadin/shared/ui/tree/TreeConstants.java index efeae9cb89..33560dec53 100644 --- a/shared/src/com/vaadin/shared/ui/tree/TreeConstants.java +++ b/shared/src/com/vaadin/shared/ui/tree/TreeConstants.java @@ -33,6 +33,8 @@ public class TreeConstants implements Serializable { public static final String ATTRIBUTE_ACTION_CAPTION = "caption"; @Deprecated public static final String ATTRIBUTE_ACTION_ICON = ATTRIBUTE_NODE_ICON; + @Deprecated + public static final String ATTRIBUTE_HTML_ALLOWED = "htmlAllowed"; public static final String ITEM_CLICK_EVENT_ID = "itemClick"; diff --git a/uitest/src/com/vaadin/tests/components/tree/TreeHtmlContentAllowed.java b/uitest/src/com/vaadin/tests/components/tree/TreeHtmlContentAllowed.java new file mode 100644 index 0000000000..b20b7c3aab --- /dev/null +++ b/uitest/src/com/vaadin/tests/components/tree/TreeHtmlContentAllowed.java @@ -0,0 +1,58 @@ +/* + * 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.tree; + +import com.vaadin.data.Property.ValueChangeEvent; +import com.vaadin.data.Property.ValueChangeListener; +import com.vaadin.server.VaadinRequest; +import com.vaadin.tests.components.AbstractTestUI; +import com.vaadin.ui.CheckBox; +import com.vaadin.ui.Tree; + +public class TreeHtmlContentAllowed extends AbstractTestUI { + + @Override + protected void setup(VaadinRequest request) { + String textParent = "Just text"; + String htmlParent = "Some <b>html</b>"; + String textChild = "Child text"; + String htmlChild = "Child <i>html</i>"; + + final Tree tree = new Tree("A tree"); + tree.addItem(textParent); + tree.addItem(htmlParent); + tree.addItem(textChild); + tree.addItem(htmlChild); + + tree.setParent(textChild, textParent); + tree.setParent(htmlChild, htmlParent); + + tree.setChildrenAllowed(textChild, false); + tree.setChildrenAllowed(htmlChild, false); + + final CheckBox toggle = new CheckBox("HTML content allowed", + tree.isHtmlContentAllowed()); + toggle.addValueChangeListener(new ValueChangeListener() { + @Override + public void valueChange(ValueChangeEvent event) { + tree.setHtmlContentAllowed(toggle.getValue().booleanValue()); + } + }); + + addComponents(tree, toggle); + } + +} diff --git a/uitest/src/com/vaadin/tests/components/tree/TreeHtmlContentAllowedTest.java b/uitest/src/com/vaadin/tests/components/tree/TreeHtmlContentAllowedTest.java new file mode 100644 index 0000000000..351eda6c90 --- /dev/null +++ b/uitest/src/com/vaadin/tests/components/tree/TreeHtmlContentAllowedTest.java @@ -0,0 +1,66 @@ +/* + * 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.tree; + +import java.util.List; + +import org.junit.Assert; +import org.junit.Test; +import org.openqa.selenium.By; +import org.openqa.selenium.WebElement; + +import com.vaadin.testbench.elements.CheckBoxElement; +import com.vaadin.testbench.elements.TreeElement; +import com.vaadin.tests.tb3.SingleBrowserTest; + +public class TreeHtmlContentAllowedTest extends SingleBrowserTest { + + @Test + public void testTreeHtmlContentAllowed() { + openTestURL(); + + CheckBoxElement toggle = $(CheckBoxElement.class).first(); + Assert.assertEquals("HTML content should be disabled by default", + "unchecked", toggle.getValue()); + + // Markup is seen as plain text + assertTreeCaptionTexts("Just text", "Some <b>html</b>"); + + toggle.click(); + assertTreeCaptionTexts("Just text", "Some html"); + + // Expand the HTML parent + findElements(By.className("v-tree-node")).get(1).click(); + + assertTreeCaptionTexts("Just text", "Some html", "Child html"); + + toggle.click(); + assertTreeCaptionTexts("Just text", "Some <b>html</b>", + "Child <i>html</i>"); + } + + private void assertTreeCaptionTexts(String... captions) { + TreeElement tree = $(TreeElement.class).first(); + List<WebElement> nodes = tree.findElements(By + .className("v-tree-node-caption")); + + Assert.assertEquals(captions.length, nodes.size()); + for (int i = 0; i < captions.length; i++) { + Assert.assertEquals(captions[i], nodes.get(i).getText()); + } + } + +} |