]> source.dussan.org Git - vaadin-framework.git/commitdiff
Add support for html in buttons (#8663)
authorLeif Åstrand <leif@vaadin.com>
Thu, 26 Apr 2012 08:52:41 +0000 (08:52 +0000)
committerLeif Åstrand <leif@vaadin.com>
Thu, 26 Apr 2012 08:52:41 +0000 (08:52 +0000)
svn changeset:23634/svn branch:6.8

src/com/vaadin/terminal/gwt/client/ui/VButton.java
src/com/vaadin/terminal/gwt/client/ui/VNativeButton.java
src/com/vaadin/ui/Button.java
tests/testbench/com/vaadin/tests/components/button/ButtonHtml.html [new file with mode: 0644]
tests/testbench/com/vaadin/tests/components/button/ButtonHtml.java [new file with mode: 0644]
tests/testbench/com/vaadin/tests/components/nativebutton/NativeButtonHtml.html [new file with mode: 0644]
tests/testbench/com/vaadin/tests/components/nativebutton/NativeButtonHtml.java [new file with mode: 0644]

index 9188f7406afe0bede505661280dcad7d1ba1bf07..98103dc41e5fef93ec68e25633cbec69460beb55 100644 (file)
@@ -93,6 +93,11 @@ public class VButton extends FocusWidget implements Paintable, ClickHandler,
 
     private int clickShortcut = 0;
 
+    /**
+     * If caption should be rendered in HTML
+     */
+    protected boolean htmlCaption = false;
+
     public VButton() {
         super(DOM.createDiv());
         setTabIndex(0);
@@ -130,6 +135,10 @@ public class VButton extends FocusWidget implements Paintable, ClickHandler,
         this.client = client;
         id = uidl.getId();
 
+        // Update HTML value before setting text
+        htmlCaption = uidl.hasAttribute("html-caption")
+                && uidl.getBooleanAttribute("html-caption");
+
         // Set text
         setText(uidl.getStringAttribute("caption"));
 
@@ -172,7 +181,11 @@ public class VButton extends FocusWidget implements Paintable, ClickHandler,
     }
 
     public void setText(String text) {
-        captionElement.setInnerText(text);
+        if (htmlCaption) {
+            captionElement.setInnerHTML(text);
+        } else {
+            captionElement.setInnerText(text);
+        }
     }
 
     @SuppressWarnings("deprecation")
index 98d3b505aec7351e9ff969269f56848a48248d12..039a0453f2908a818bcf38f7da80f7c7c0f4773b 100644 (file)
@@ -56,6 +56,11 @@ public class VNativeButton extends Button implements Paintable, ClickHandler,
 
     private boolean disableOnClick = false;
 
+    /**
+     * If caption should be rendered in HTML
+     */
+    protected boolean htmlCaption = false;
+
     public VNativeButton() {
         setStyleName(CLASSNAME);
 
@@ -88,6 +93,10 @@ public class VNativeButton extends Button implements Paintable, ClickHandler,
         this.client = client;
         id = uidl.getId();
 
+        // Update HTML value before setting text
+        htmlCaption = uidl.hasAttribute("html-caption")
+                && uidl.getBooleanAttribute("html-caption");
+
         // Set text
         setText(uidl.getStringAttribute("caption"));
 
@@ -141,7 +150,11 @@ public class VNativeButton extends Button implements Paintable, ClickHandler,
 
     @Override
     public void setText(String text) {
-        captionElement.setInnerText(text);
+        if (htmlCaption) {
+            captionElement.setInnerHTML(text);
+        } else {
+            captionElement.setInnerText(text);
+        }
     }
 
     @Override
index 3c9978459269d528ab0cf480f19b10b950286ce4..fdaef046e5233823c7d445e4157381b09d6ec2b7 100644 (file)
@@ -45,6 +45,11 @@ public class Button extends AbstractField implements FieldEvents.BlurNotifier,
 
     boolean disableOnClick = false;
 
+    /**
+     * If caption is rendered as HTML
+     */
+    private boolean htmlContentAllowed = false;
+
     /**
      * Creates a new push button. The value of the push button is false and it
      * is immediate by default.
@@ -156,6 +161,10 @@ public class Button extends AbstractField implements FieldEvents.BlurNotifier,
         if (clickShortcut != null) {
             target.addAttribute("keycode", clickShortcut.getKeyCode());
         }
+
+        if (isHtmlContentAllowed()) {
+            target.addAttribute("html-caption", true);
+        }
     }
 
     /**
@@ -694,4 +703,33 @@ public class Button extends AbstractField implements FieldEvents.BlurNotifier,
         requestRepaint();
     }
 
+    /**
+     * Set whether the caption text is rendered as HTML or not. You might need
+     * to retheme button to allow higher content than the original text style.
+     * 
+     * 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
+     *            <code>true</code> if caption is rendered as HTML,
+     *            <code>false</code> otherwise
+     */
+    public void setHtmlContentAllowed(boolean htmlContentAllowed) {
+        if (this.htmlContentAllowed != htmlContentAllowed) {
+            this.htmlContentAllowed = htmlContentAllowed;
+            requestRepaint();
+        }
+    }
+
+    /**
+     * Return HTML rendering setting
+     * 
+     * @return <code>true</code> if the caption text is to be rendered as HTML,
+     *         <code>false</code> otherwise
+     */
+    public boolean isHtmlContentAllowed() {
+        return htmlContentAllowed;
+    }
+
 }
diff --git a/tests/testbench/com/vaadin/tests/components/button/ButtonHtml.html b/tests/testbench/com/vaadin/tests/components/button/ButtonHtml.html
new file mode 100644 (file)
index 0000000..2d58d0e
--- /dev/null
@@ -0,0 +1,47 @@
+<?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>ButtonHtml</title>
+</head>
+<body>
+<table cellpadding="1" cellspacing="1" border="1">
+<thead>
+<tr><td rowspan="1" colspan="3">ButtonHtml</td></tr>
+</thead><tbody>
+<tr>
+       <td>open</td>
+       <td>/run/com.vaadin.tests.components.button.ButtonHtml?restartApplication</td>
+       <td></td>
+</tr>
+<tr>
+       <td>screenCapture</td>
+       <td></td>
+       <td>initial</td>
+</tr>
+<tr>
+       <td>click</td>
+       <td>vaadin=runcomvaadintestscomponentsbuttonButtonHtml::/VVerticalLayout[0]/ChildComponentContainer[1]/VVerticalLayout[0]/ChildComponentContainer[2]/VButton[0]/domChild[0]/domChild[0]</td>
+       <td></td>
+</tr>
+<tr>
+       <td>screenCapture</td>
+       <td></td>
+       <td>after_1_click</td>
+</tr>
+<tr>
+       <td>click</td>
+       <td>vaadin=runcomvaadintestscomponentsbuttonButtonHtml::/VVerticalLayout[0]/ChildComponentContainer[1]/VVerticalLayout[0]/ChildComponentContainer[2]/VButton[0]/domChild[0]/domChild[0]/domChild[0]</td>
+       <td></td>
+</tr>
+<tr>
+       <td>screenCapture</td>
+       <td></td>
+       <td>after_2_clicks</td>
+</tr>
+
+</tbody></table>
+</body>
+</html>
diff --git a/tests/testbench/com/vaadin/tests/components/button/ButtonHtml.java b/tests/testbench/com/vaadin/tests/components/button/ButtonHtml.java
new file mode 100644 (file)
index 0000000..253de5b
--- /dev/null
@@ -0,0 +1,39 @@
+package com.vaadin.tests.components.button;
+
+import com.vaadin.tests.components.TestBase;
+import com.vaadin.ui.Button;
+import com.vaadin.ui.Button.ClickEvent;
+
+public class ButtonHtml extends TestBase {
+
+    @Override
+    protected void setup() {
+        Button b = new Button("<b>Plain text button</b>");
+        addComponent(b);
+
+        b = new Button(
+                "<span style=\"color: red; font-weight: bold;\">HTML</span> button");
+        b.setHtmlContentAllowed(true);
+        addComponent(b);
+
+        final Button swapButton = new Button("<i>Swap button<i>");
+        swapButton.addListener(new Button.ClickListener() {
+
+            public void buttonClick(ClickEvent event) {
+                swapButton.setHtmlContentAllowed(!swapButton
+                        .isHtmlContentAllowed());
+            }
+        });
+        addComponent(swapButton);
+    }
+
+    @Override
+    protected String getDescription() {
+        return "Verify that Button HTML rendering works";
+    }
+
+    @Override
+    protected Integer getTicketNumber() {
+        return 8663;
+    }
+}
diff --git a/tests/testbench/com/vaadin/tests/components/nativebutton/NativeButtonHtml.html b/tests/testbench/com/vaadin/tests/components/nativebutton/NativeButtonHtml.html
new file mode 100644 (file)
index 0000000..8a1b21c
--- /dev/null
@@ -0,0 +1,47 @@
+<?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>NativeButtonHtml</title>
+</head>
+<body>
+<table cellpadding="1" cellspacing="1" border="1">
+<thead>
+<tr><td rowspan="1" colspan="3">NativeButtonHtml</td></tr>
+</thead><tbody>
+<tr>
+       <td>open</td>
+       <td>/run/com.vaadin.tests.components.nativebutton.NativeButtonHtml?restartApplication</td>
+       <td></td>
+</tr>
+<tr>
+       <td>screenCapture</td>
+       <td></td>
+       <td>initial</td>
+</tr>
+<tr>
+       <td>mouseClick</td>
+       <td>vaadin=runcomvaadintestscomponentsnativebuttonNativeButtonHtml::/VVerticalLayout[0]/ChildComponentContainer[1]/VVerticalLayout[0]/ChildComponentContainer[2]/VNativeButton[0]</td>
+       <td>116,9</td>
+</tr>
+<tr>
+       <td>screenCapture</td>
+       <td></td>
+       <td>after_1_click</td>
+</tr>
+<tr>
+       <td>mouseClick</td>
+       <td>vaadin=runcomvaadintestscomponentsnativebuttonNativeButtonHtml::/VVerticalLayout[0]/ChildComponentContainer[1]/VVerticalLayout[0]/ChildComponentContainer[2]/VNativeButton[0]</td>
+       <td>74,10</td>
+</tr>
+<tr>
+       <td>screenCapture</td>
+       <td></td>
+       <td>after_2_clicks</td>
+</tr>
+
+</tbody></table>
+</body>
+</html>
diff --git a/tests/testbench/com/vaadin/tests/components/nativebutton/NativeButtonHtml.java b/tests/testbench/com/vaadin/tests/components/nativebutton/NativeButtonHtml.java
new file mode 100644 (file)
index 0000000..011439f
--- /dev/null
@@ -0,0 +1,41 @@
+package com.vaadin.tests.components.nativebutton;
+
+import com.vaadin.tests.components.TestBase;
+import com.vaadin.ui.Button;
+import com.vaadin.ui.Button.ClickEvent;
+import com.vaadin.ui.NativeButton;
+
+public class NativeButtonHtml extends TestBase {
+
+    @Override
+    protected void setup() {
+        NativeButton b = new NativeButton("<b>Plain text button</b>");
+        addComponent(b);
+
+        b = new NativeButton(
+                "<span style=\"color: red; font-weight: bold;\">HTML</span> button");
+        b.setHtmlContentAllowed(true);
+        addComponent(b);
+
+        final NativeButton swapButton = new NativeButton("<i>Swap button<i>");
+        swapButton.addListener(new Button.ClickListener() {
+
+            public void buttonClick(ClickEvent event) {
+                swapButton.setHtmlContentAllowed(!swapButton
+                        .isHtmlContentAllowed());
+            }
+        });
+        addComponent(swapButton);
+    }
+
+    @Override
+    protected String getDescription() {
+        return "Verify that NativeButton HTML rendering works";
+    }
+
+    @Override
+    protected Integer getTicketNumber() {
+        // 8663 was for normal button (see ButtonHtml test)
+        return null;
+    }
+}