From 11cdb557957ee6e9eed30105341ed7dc2c3a6377 Mon Sep 17 00:00:00 2001 From: =?utf8?q?Leif=20=C3=85strand?= Date: Thu, 26 Apr 2012 08:52:41 +0000 Subject: [PATCH] Add support for html in buttons (#8663) svn changeset:23634/svn branch:6.8 --- .../terminal/gwt/client/ui/VButton.java | 15 +++++- .../terminal/gwt/client/ui/VNativeButton.java | 15 +++++- src/com/vaadin/ui/Button.java | 38 +++++++++++++++ .../tests/components/button/ButtonHtml.html | 47 +++++++++++++++++++ .../tests/components/button/ButtonHtml.java | 39 +++++++++++++++ .../nativebutton/NativeButtonHtml.html | 47 +++++++++++++++++++ .../nativebutton/NativeButtonHtml.java | 41 ++++++++++++++++ 7 files changed, 240 insertions(+), 2 deletions(-) create mode 100644 tests/testbench/com/vaadin/tests/components/button/ButtonHtml.html create mode 100644 tests/testbench/com/vaadin/tests/components/button/ButtonHtml.java create mode 100644 tests/testbench/com/vaadin/tests/components/nativebutton/NativeButtonHtml.html create mode 100644 tests/testbench/com/vaadin/tests/components/nativebutton/NativeButtonHtml.java diff --git a/src/com/vaadin/terminal/gwt/client/ui/VButton.java b/src/com/vaadin/terminal/gwt/client/ui/VButton.java index 9188f7406a..98103dc41e 100644 --- a/src/com/vaadin/terminal/gwt/client/ui/VButton.java +++ b/src/com/vaadin/terminal/gwt/client/ui/VButton.java @@ -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") diff --git a/src/com/vaadin/terminal/gwt/client/ui/VNativeButton.java b/src/com/vaadin/terminal/gwt/client/ui/VNativeButton.java index 98d3b505ae..039a0453f2 100644 --- a/src/com/vaadin/terminal/gwt/client/ui/VNativeButton.java +++ b/src/com/vaadin/terminal/gwt/client/ui/VNativeButton.java @@ -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 diff --git a/src/com/vaadin/ui/Button.java b/src/com/vaadin/ui/Button.java index 3c99784592..fdaef046e5 100644 --- a/src/com/vaadin/ui/Button.java +++ b/src/com/vaadin/ui/Button.java @@ -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 + * true if caption is rendered as HTML, + * false otherwise + */ + public void setHtmlContentAllowed(boolean htmlContentAllowed) { + if (this.htmlContentAllowed != htmlContentAllowed) { + this.htmlContentAllowed = htmlContentAllowed; + requestRepaint(); + } + } + + /** + * Return HTML rendering setting + * + * @return true if the caption text is to be rendered as HTML, + * false 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 index 0000000000..2d58d0e2e1 --- /dev/null +++ b/tests/testbench/com/vaadin/tests/components/button/ButtonHtml.html @@ -0,0 +1,47 @@ + + + + + + +ButtonHtml + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
ButtonHtml
open/run/com.vaadin.tests.components.button.ButtonHtml?restartApplication
screenCaptureinitial
clickvaadin=runcomvaadintestscomponentsbuttonButtonHtml::/VVerticalLayout[0]/ChildComponentContainer[1]/VVerticalLayout[0]/ChildComponentContainer[2]/VButton[0]/domChild[0]/domChild[0]
screenCaptureafter_1_click
clickvaadin=runcomvaadintestscomponentsbuttonButtonHtml::/VVerticalLayout[0]/ChildComponentContainer[1]/VVerticalLayout[0]/ChildComponentContainer[2]/VButton[0]/domChild[0]/domChild[0]/domChild[0]
screenCaptureafter_2_clicks
+ + 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 index 0000000000..253de5b43c --- /dev/null +++ b/tests/testbench/com/vaadin/tests/components/button/ButtonHtml.java @@ -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("Plain text button"); + addComponent(b); + + b = new Button( + "HTML button"); + b.setHtmlContentAllowed(true); + addComponent(b); + + final Button swapButton = new Button("Swap button"); + 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 index 0000000000..8a1b21c35d --- /dev/null +++ b/tests/testbench/com/vaadin/tests/components/nativebutton/NativeButtonHtml.html @@ -0,0 +1,47 @@ + + + + + + +NativeButtonHtml + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
NativeButtonHtml
open/run/com.vaadin.tests.components.nativebutton.NativeButtonHtml?restartApplication
screenCaptureinitial
mouseClickvaadin=runcomvaadintestscomponentsnativebuttonNativeButtonHtml::/VVerticalLayout[0]/ChildComponentContainer[1]/VVerticalLayout[0]/ChildComponentContainer[2]/VNativeButton[0]116,9
screenCaptureafter_1_click
mouseClickvaadin=runcomvaadintestscomponentsnativebuttonNativeButtonHtml::/VVerticalLayout[0]/ChildComponentContainer[1]/VVerticalLayout[0]/ChildComponentContainer[2]/VNativeButton[0]74,10
screenCaptureafter_2_clicks
+ + 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 index 0000000000..011439f810 --- /dev/null +++ b/tests/testbench/com/vaadin/tests/components/nativebutton/NativeButtonHtml.java @@ -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("Plain text button"); + addComponent(b); + + b = new NativeButton( + "HTML button"); + b.setHtmlContentAllowed(true); + addComponent(b); + + final NativeButton swapButton = new NativeButton("Swap button"); + 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; + } +} -- 2.39.5