]> source.dussan.org Git - vaadin-framework.git/commitdiff
#7187 Allow HTML content in an MenuItem
authorLeif Åstrand <leif@vaadin.com>
Wed, 10 Aug 2011 07:04:37 +0000 (07:04 +0000)
committerLeif Åstrand <leif@vaadin.com>
Wed, 10 Aug 2011 07:04:37 +0000 (07:04 +0000)
svn changeset:20245/svn branch:6.7

WebContent/VAADIN/themes/base/menubar/menubar.css
WebContent/VAADIN/themes/reindeer/menubar/menubar.css
WebContent/VAADIN/themes/runo/menubar/menubar.css
src/com/vaadin/terminal/gwt/client/ui/VMenuBar.java
src/com/vaadin/ui/MenuBar.java
tests/src/com/vaadin/tests/components/menubar/MenuBarHtmlItems.html [new file with mode: 0644]
tests/src/com/vaadin/tests/components/menubar/MenuBarHtmlItems.java [new file with mode: 0644]
tests/src/com/vaadin/tests/components/optiongroup/HtmlOptionGroupItems.html

index 042ec15d1dd301e09d53d80cd59d2ebc47d4adbe..8b44085c2878f7632a21a6bea1b8762c4de1dc17 100644 (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;
 }
index e5e337725ef3fbeaf2f2a6c0f18ae3b4f5ae082f..165eacc840da93c922970cc60ddbf347b958cba9 100644 (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,
index 7c1a4036b33b30b6639ab535879bef643c589e38..91cf78189d3d3eb8100737b83687d641467e8e31 100644 (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;
 }
\ No newline at end of file
index 4d43b7c34602f103d78ec3ccea3f09a6f6460edd..f74ff4a1f50129924eb463750a8cdaaf754b2bc5 100644 (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();
index f8fbf917123fbfb0b11c9632088eb21f0c8939f0..4aae9b262114002f8888219cccfa0f41fd07dcef 100644 (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
diff --git a/tests/src/com/vaadin/tests/components/menubar/MenuBarHtmlItems.html b/tests/src/com/vaadin/tests/components/menubar/MenuBarHtmlItems.html
new file mode 100644 (file)
index 0000000..2878018
--- /dev/null
@@ -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>
diff --git a/tests/src/com/vaadin/tests/components/menubar/MenuBarHtmlItems.java b/tests/src/com/vaadin/tests/components/menubar/MenuBarHtmlItems.java
new file mode 100644 (file)
index 0000000..ba22bcb
--- /dev/null
@@ -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.";
+    }
+
+}
index af6f74b3df95c59d7b055abf1f2b0ca4311d9be2..d74354299dde07a5313d78cd5ec45ab3e3be2cbf 100644 (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>