Browse Source

#7187 Allow HTML content in an MenuItem

svn changeset:20245/svn branch:6.7
tags/6.7.0.beta1
Leif Åstrand 13 years ago
parent
commit
35887c66f8

+ 1
- 1
WebContent/VAADIN/themes/base/menubar/menubar.css View File

@@ -10,7 +10,7 @@
display: inline-block;
zoom: 1;
}
.v-menubar .v-menubar-menuitem-caption * {
.v-menubar .v-menubar-menuitem-caption .v-icon {
vertical-align: middle;
white-space: nowrap;
}

+ 1
- 1
WebContent/VAADIN/themes/reindeer/menubar/menubar.css View File

@@ -28,7 +28,7 @@
padding: 1px 26px 1px 10px;
line-height: 16px;
}
.v-menubar-submenu .v-menubar-menuitem-caption * {
.v-menubar-submenu .v-menubar-menuitem-caption .v-icon {
vertical-align: middle;
}
.v-menubar .v-menubar-menuitem-selected,

+ 1
- 1
WebContent/VAADIN/themes/runo/menubar/menubar.css View File

@@ -30,6 +30,6 @@
line-height: 16px;
padding-left: 10px;
}
.v-menubar-submenu .v-menubar-menuitem-caption * {
.v-menubar-submenu .v-menubar-menuitem-caption .v-icon {
vertical-align: middle;
}

+ 10
- 1
src/com/vaadin/terminal/gwt/client/ui/VMenuBar.java View File

@@ -69,6 +69,8 @@ public class VMenuBar extends SimpleFocusablePanel implements Paintable,

public static final String ATTRIBUTE_CHECKED = "checked";

public static final String HTML_CONTENT_ALLOWED = "usehtml";

/** Widget fields **/
protected boolean subMenu;
protected ArrayList<CustomMenuItem> items;
@@ -93,6 +95,8 @@ public class VMenuBar extends SimpleFocusablePanel implements Paintable,

private boolean openRootOnHover;

private boolean htmlContentAllowed;

public VMenuBar() {
// Create an empty horizontal menubar
this(false, null);
@@ -191,6 +195,8 @@ public class VMenuBar extends SimpleFocusablePanel implements Paintable,
return;
}

htmlContentAllowed = uidl.hasAttribute(HTML_CONTENT_ALLOWED);

openRootOnHover = uidl.getBooleanAttribute(OPEN_ROOT_MENU_ON_HOWER);

enabled = !uidl.getBooleanAttribute("disabled");
@@ -342,7 +348,10 @@ public class VMenuBar extends SimpleFocusablePanel implements Paintable,
+ Icon.CLASSNAME + "\" alt=\"\" />");
}
String itemText = item.getStringAttribute("text");
itemHTML.append(Util.escapeHTML(itemText));
if (!htmlContentAllowed) {
itemText = Util.escapeHTML(itemText);
}
itemHTML.append(itemText);
itemHTML.append("</span>");
}
return itemHTML.toString();

+ 32
- 0
src/com/vaadin/ui/MenuBar.java View File

@@ -51,6 +51,8 @@ public class MenuBar extends AbstractComponent {

private boolean openRootOnHover;

private boolean htmlContentAllowed;

/** Paint (serialise) the component for the client. */
@Override
public void paintContent(PaintTarget target) throws PaintException {
@@ -60,6 +62,10 @@ public class MenuBar extends AbstractComponent {

target.addAttribute(VMenuBar.OPEN_ROOT_MENU_ON_HOWER, openRootOnHover);

if (isHtmlContentAllowed()) {
target.addAttribute(VMenuBar.HTML_CONTENT_ALLOWED, true);
}

target.startTag("options");

if (submenuIcon != null) {
@@ -410,6 +416,32 @@ public class MenuBar extends AbstractComponent {
return openRootOnHover;
}

/**
* Sets whether html is allowed in the item captions. If set to true, the
* captions are passed to the browser as html and the developer is
* responsible for ensuring no harmful html is used. If set to false, the
* content is passed to the browser as plain text.
*
* @param htmlContentAllowed
* true if the captions are used as html, false if used as plain
* text
*/
public void setHtmlContentAllowed(boolean htmlContentAllowed) {
this.htmlContentAllowed = htmlContentAllowed;
requestRepaint();
}

/**
* Checks whether item captions are interpreted as html or plain text.
*
* @return true if the captions are used as html, false if used as plain
* text
* @see #setHtmlContentAllowed(boolean)
*/
public boolean isHtmlContentAllowed() {
return htmlContentAllowed;
}

/**
* This interface contains the layer for menu commands of the
* {@link com.vaadin.ui.MenuBar} class. It's method will fire when the user

+ 72
- 0
tests/src/com/vaadin/tests/components/menubar/MenuBarHtmlItems.html View File

@@ -0,0 +1,72 @@
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head profile="http://selenium-ide.openqa.org/profiles/test-case">
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<link rel="selenium.base" href="" />
<title>New Test</title>
</head>
<body>
<table cellpadding="1" cellspacing="1" border="1">
<thead>
<tr><td rowspan="1" colspan="3">MenuBarHtmlItems</td></tr>
</thead><tbody>
<tr>
<td>open</td>
<td>/run/com.vaadin.tests.components.menubar.MenuBarHtmlItems?restartApplication</td>
<td></td>
</tr>
<tr>
<td>mouseClick</td>
<td>vaadin=runcomvaadintestscomponentsmenubarMenuBarHtmlItems::PID_Scheckboxaction-Html content allowed/domChild[0]</td>
<td>44,1</td>
</tr>
<tr>
<td>mouseClick</td>
<td>vaadin=runcomvaadintestscomponentsmenubarMenuBarHtmlItems::/VVerticalLayout[0]/ChildComponentContainer[1]/VVerticalLayout[0]/ChildComponentContainer[1]/VMenuBar[0]#item0</td>
<td>7,6</td>
</tr>
<tr>
<td>pressSpecialKey</td>
<td>vaadin=runcomvaadintestscomponentsmenubarMenuBarHtmlItems::/VVerticalLayout[0]/ChildComponentContainer[1]/VVerticalLayout[0]/ChildComponentContainer[1]/VMenuBar[0]</td>
<td>down</td>
</tr>
<tr>
<td>pressSpecialKey</td>
<td>vaadin=runcomvaadintestscomponentsmenubarMenuBarHtmlItems::/VVerticalLayout[0]/ChildComponentContainer[1]/VVerticalLayout[0]/ChildComponentContainer[1]/VMenuBar[0]</td>
<td>right</td>
</tr>
<tr>
<td>screenCapture</td>
<td></td>
<td>html</td>
</tr>
<tr>
<td>mouseClick</td>
<td>vaadin=runcomvaadintestscomponentsmenubarMenuBarHtmlItems::PID_Scheckboxaction-Html content allowed/domChild[0]</td>
<td>37,1</td>
</tr>
<tr>
<td>mouseClick</td>
<td>vaadin=runcomvaadintestscomponentsmenubarMenuBarHtmlItems::/VVerticalLayout[0]/ChildComponentContainer[1]/VVerticalLayout[0]/ChildComponentContainer[1]/VMenuBar[0]#item0</td>
<td>13,6</td>
</tr>
<tr>
<td>pressSpecialKey</td>
<td>vaadin=runcomvaadintestscomponentsmenubarMenuBarHtmlItems::/VVerticalLayout[0]/ChildComponentContainer[1]/VVerticalLayout[0]/ChildComponentContainer[1]/VMenuBar[0]</td>
<td>down</td>
</tr>
<tr>
<td>pressSpecialKey</td>
<td>vaadin=runcomvaadintestscomponentsmenubarMenuBarHtmlItems::/VVerticalLayout[0]/ChildComponentContainer[1]/VVerticalLayout[0]/ChildComponentContainer[1]/VMenuBar[0]</td>
<td>right</td>
</tr>
<tr>
<td>screenCapture</td>
<td></td>
<td>plain</td>
</tr>

</tbody></table>
</body>
</html>

+ 63
- 0
tests/src/com/vaadin/tests/components/menubar/MenuBarHtmlItems.java View File

@@ -0,0 +1,63 @@
package com.vaadin.tests.components.menubar;

import java.util.Arrays;
import java.util.List;

import com.vaadin.terminal.Resource;
import com.vaadin.terminal.ThemeResource;
import com.vaadin.tests.components.ComponentTestCase;
import com.vaadin.ui.Component;
import com.vaadin.ui.MenuBar;
import com.vaadin.ui.MenuBar.MenuItem;

public class MenuBarHtmlItems extends ComponentTestCase<MenuBar> {

@Override
protected Class<MenuBar> getTestClass() {
return MenuBar.class;
}

@Override
protected void initializeComponents() {
MenuBar m = new MenuBar();
MenuItem submenu = m.addItem("Item <u>1</u>", getIcon(), null);
MenuItem subsubmenu = submenu.addItem("<b>Bold</b> item", null);
subsubmenu.addItem("<i><u>I</u>talic</i> item", getIcon(), null);
submenu.addItem(
"<span style='font-size: 30px'>Big</span> <span style='font-size: 8px'>disabled</span> item",
null).setEnabled(false);

m.addItem("<span style='font-size: 30px'>Big</span> item", null);

addTestComponent(m);
}

private Resource getIcon() {
return new ThemeResource("../runo/icons/16/user.png");
}

@Override
protected List<Component> createActions() {
return Arrays.asList(createSwitchHtmlAction());
}

private Component createSwitchHtmlAction() {
return createBooleanAction("Html content allowed", false,
new Command<MenuBar, Boolean>() {
public void execute(MenuBar c, Boolean value, Object data) {
c.setHtmlContentAllowed(value.booleanValue());
}
});
}

@Override
protected Integer getTicketNumber() {
return 7187;
}

@Override
protected String getDescription() {
return "A menu containing items with embedded html. Items should chould either render the html or show it as plain text depending on the setting.";
}

}

+ 1
- 1
tests/src/com/vaadin/tests/components/optiongroup/HtmlOptionGroupItems.html View File

@@ -9,7 +9,7 @@
<body>
<table cellpadding="1" cellspacing="1" border="1">
<thead>
<tr><td rowspan="1" colspan="3">New Test</td></tr>
<tr><td rowspan="1" colspan="3">HtmlOptionGroupItems</td></tr>
</thead><tbody>
<tr>
<td>open</td>

Loading…
Cancel
Save