]> source.dussan.org Git - vaadin-framework.git/commitdiff
#6097 Window.showNotification should support plain text in addition to Html
authorLeif Åstrand <leif@vaadin.com>
Wed, 10 Aug 2011 11:09:20 +0000 (11:09 +0000)
committerLeif Åstrand <leif@vaadin.com>
Wed, 10 Aug 2011 11:09:20 +0000 (11:09 +0000)
svn changeset:20263/svn branch:6.7

src/com/vaadin/terminal/gwt/client/ui/VView.java
src/com/vaadin/ui/Window.java
tests/src/com/vaadin/tests/components/notification/NotificationsHtmlAllowed.html [new file with mode: 0644]
tests/src/com/vaadin/tests/components/notification/NotificationsHtmlAllowed.java [new file with mode: 0644]

index 9f1acb52fe564befa63d33888ce0ffa31a680500..eba6036deb6147263d8515b17231b7b1eebec48c 100644 (file)
@@ -47,6 +47,8 @@ public class VView extends SimplePanel implements Container, ResizeHandler,
 
     private static final String CLASSNAME = "v-view";
 
+    public static final String NOTIFICATION_HTML_CONTENT_ALLOWED = "usehtml";
+
     private String theme;
 
     private Paintable layout;
@@ -320,6 +322,8 @@ public class VView extends SimplePanel implements Container, ResizeHandler,
                 for (final Iterator<?> it = childUidl.getChildIterator(); it
                         .hasNext();) {
                     final UIDL notification = (UIDL) it.next();
+                    boolean htmlContentAllowed = notification
+                            .hasAttribute(NOTIFICATION_HTML_CONTENT_ALLOWED);
                     String html = "";
                     if (notification.hasAttribute("icon")) {
                         final String parsedUri = client
@@ -328,14 +332,22 @@ public class VView extends SimplePanel implements Container, ResizeHandler,
                         html += "<img src=\"" + parsedUri + "\" />";
                     }
                     if (notification.hasAttribute("caption")) {
-                        html += "<h1>"
-                                + notification.getStringAttribute("caption")
-                                + "</h1>";
+                        String caption = notification
+                                .getStringAttribute("caption");
+                        if (!htmlContentAllowed) {
+                            caption = Util.escapeHTML(caption);
+                            caption = caption.replaceAll("\\n", "<br />");
+                        }
+                        html += "<h1>" + caption + "</h1>";
                     }
                     if (notification.hasAttribute("message")) {
-                        html += "<p>"
-                                + notification.getStringAttribute("message")
-                                + "</p>";
+                        String message = notification
+                                .getStringAttribute("message");
+                        if (!htmlContentAllowed) {
+                            message = Util.escapeHTML(message);
+                            message = message.replaceAll("\\n", "<br />");
+                        }
+                        html += "<p>" + message + "</p>";
                     }
 
                     final String style = notification.hasAttribute("style") ? notification
index 989a8288e2bb3f38c1d76b9afe7ae9b24d6337d4..a16b6d585dfed4ca8e2b66060210e6f0fc6c575f 100644 (file)
@@ -663,6 +663,10 @@ public class Window extends Panel implements URIHandler, ParameterHandler,
                 if (n.getIcon() != null) {
                     target.addAttribute("icon", n.getIcon());
                 }
+                if (n.isHtmlContentAllowed()) {
+                    target.addAttribute(
+                            VView.NOTIFICATION_HTML_CONTENT_ALLOWED, true);
+                }
                 target.addAttribute("position", n.getPosition());
                 target.addAttribute("delay", n.getDelayMsec());
                 if (n.getStyleName() != null) {
@@ -1597,6 +1601,9 @@ public class Window extends Panel implements URIHandler, ParameterHandler,
      * Shows a notification message on the middle of the window. The message
      * automatically disappears ("humanized message").
      * 
+     * Care should be taken to to avoid XSS vulnerabilities as the caption is
+     * rendered as html.
+     * 
      * @see #showNotification(com.vaadin.ui.Window.Notification)
      * @see Notification
      * 
@@ -1612,6 +1619,9 @@ public class Window extends Panel implements URIHandler, ParameterHandler,
      * message depends on the type, which is one of the basic types defined in
      * {@link Notification}, for instance Notification.TYPE_WARNING_MESSAGE.
      * 
+     * Care should be taken to to avoid XSS vulnerabilities as the caption is
+     * rendered as html.
+     * 
      * @see #showNotification(com.vaadin.ui.Window.Notification)
      * @see Notification
      * 
@@ -1629,6 +1639,9 @@ public class Window extends Panel implements URIHandler, ParameterHandler,
      * description on the middle of the window. The message automatically
      * disappears ("humanized message").
      * 
+     * Care should be taken to to avoid XSS vulnerabilities as the caption and
+     * description are rendered as html.
+     * 
      * @see #showNotification(com.vaadin.ui.Window.Notification)
      * @see Notification
      * 
@@ -1648,6 +1661,9 @@ public class Window extends Panel implements URIHandler, ParameterHandler,
      * type, which is one of the basic types defined in {@link Notification},
      * for instance Notification.TYPE_WARNING_MESSAGE.
      * 
+     * Care should be taken to to avoid XSS vulnerabilities as the caption and
+     * description are rendered as html.
+     * 
      * @see #showNotification(com.vaadin.ui.Window.Notification)
      * @see Notification
      * 
@@ -1662,6 +1678,34 @@ public class Window extends Panel implements URIHandler, ParameterHandler,
         addNotification(new Notification(caption, description, type));
     }
 
+    /**
+     * Shows a notification consisting of a bigger caption and a smaller
+     * description. The position and behavior of the message depends on the
+     * type, which is one of the basic types defined in {@link Notification},
+     * for instance Notification.TYPE_WARNING_MESSAGE.
+     * 
+     * Care should be taken to avoid XSS vulnerabilities if html content is
+     * allowed.
+     * 
+     * @see #showNotification(com.vaadin.ui.Window.Notification)
+     * @see Notification
+     * 
+     * @param caption
+     *            The message caption
+     * @param description
+     *            The message description
+     * @param type
+     *            The type of message
+     * @param htmlContentAllowed
+     *            Whether html in the caption and description should be
+     *            displayed as html or as plain text
+     */
+    public void showNotification(String caption, String description, int type,
+            boolean htmlContentAllowed) {
+        addNotification(new Notification(caption, description, type,
+                htmlContentAllowed));
+    }
+
     /**
      * Shows a notification message.
      * 
@@ -1773,10 +1817,14 @@ public class Window extends Panel implements URIHandler, ParameterHandler,
         private int position = POSITION_CENTERED;
         private int delayMsec = 0;
         private String styleName;
+        private boolean htmlContentAllowed;
 
         /**
          * Creates a "humanized" notification message.
          * 
+         * Care should be taken to to avoid XSS vulnerabilities as the caption
+         * is by default rendered as html.
+         * 
          * @param caption
          *            The message to show
          */
@@ -1787,6 +1835,9 @@ public class Window extends Panel implements URIHandler, ParameterHandler,
         /**
          * Creates a notification message of the specified type.
          * 
+         * Care should be taken to to avoid XSS vulnerabilities as the caption
+         * is by default rendered as html.
+         * 
          * @param caption
          *            The message to show
          * @param type
@@ -1800,6 +1851,9 @@ public class Window extends Panel implements URIHandler, ParameterHandler,
          * Creates a "humanized" notification message with a bigger caption and
          * smaller description.
          * 
+         * Care should be taken to to avoid XSS vulnerabilities as the caption
+         * and description are by default rendered as html.
+         * 
          * @param caption
          *            The message caption
          * @param description
@@ -1813,6 +1867,9 @@ public class Window extends Panel implements URIHandler, ParameterHandler,
          * Creates a notification message of the specified type, with a bigger
          * caption and smaller description.
          * 
+         * Care should be taken to to avoid XSS vulnerabilities as the caption
+         * and description are by default rendered as html.
+         * 
          * @param caption
          *            The message caption
          * @param description
@@ -1821,8 +1878,31 @@ public class Window extends Panel implements URIHandler, ParameterHandler,
          *            The type of message
          */
         public Notification(String caption, String description, int type) {
+            this(caption, description, type, true);
+        }
+
+        /**
+         * Creates a notification message of the specified type, with a bigger
+         * caption and smaller description.
+         * 
+         * Care should be taken to to avoid XSS vulnerabilities if html is
+         * allowed.
+         * 
+         * @param caption
+         *            The message caption
+         * @param description
+         *            The message description
+         * @param type
+         *            The type of message
+         * @param htmlContentAllowed
+         *            Whether html in the caption and description should be
+         *            displayed as html or as plain text
+         */
+        public Notification(String caption, String description, int type,
+                boolean htmlContentAllowed) {
             this.caption = caption;
             this.description = description;
+            this.htmlContentAllowed = htmlContentAllowed;
             setType(type);
         }
 
@@ -1980,6 +2060,32 @@ public class Window extends Panel implements URIHandler, ParameterHandler,
         public String getStyleName() {
             return styleName;
         }
+
+        /**
+         * Sets whether html is allowed in the caption and description. If set
+         * to true, the texts are passed to the browser as html and the
+         * developer is responsible for ensuring no harmful html is used. If set
+         * to false, the texts are passed to the browser as plain text.
+         * 
+         * @param htmlContentAllowed
+         *            true if the texts are used as html, false if used as plain
+         *            text
+         */
+        public void setHtmlContentAllowed(boolean htmlContentAllowed) {
+            this.htmlContentAllowed = htmlContentAllowed;
+        }
+
+        /**
+         * Checks whether caption and description are interpreted as html or
+         * plain text.
+         * 
+         * @return true if the texts are used as html, false if used as plain
+         *         text
+         * @see #setHtmlContentAllowed(boolean)
+         */
+        public boolean isHtmlContentAllowed() {
+            return htmlContentAllowed;
+        }
     }
 
     /**
diff --git a/tests/src/com/vaadin/tests/components/notification/NotificationsHtmlAllowed.html b/tests/src/com/vaadin/tests/components/notification/NotificationsHtmlAllowed.html
new file mode 100644 (file)
index 0000000..a6a4022
--- /dev/null
@@ -0,0 +1,56 @@
+<?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">NotificationsHtmlAllowed</td></tr>
+</thead><tbody>
+<tr>
+       <td>open</td>
+       <td>/run/com.vaadin.tests.components.notification.NotificationsHtmlAllowed?restartApplication</td>
+       <td></td>
+</tr>
+<tr>
+       <td>click</td>
+       <td>vaadin=runcomvaadintestscomponentsnotificationNotificationsHtmlAllowed::/VVerticalLayout[0]/ChildComponentContainer[1]/VVerticalLayout[0]/ChildComponentContainer[3]/VButton[0]/domChild[0]/domChild[0]</td>
+       <td></td>
+</tr>
+<tr>
+       <td>screenCapture</td>
+       <td></td>
+       <td>html</td>
+</tr>
+<tr>
+       <td>closeNotification</td>
+       <td>//body/div[2]</td>
+       <td>0,0</td>
+</tr>
+<tr>
+       <td>mouseClick</td>
+       <td>vaadin=runcomvaadintestscomponentsnotificationNotificationsHtmlAllowed::/VVerticalLayout[0]/ChildComponentContainer[1]/VVerticalLayout[0]/ChildComponentContainer[2]/VCheckBox[0]/domChild[0]</td>
+       <td>66,2</td>
+</tr>
+<tr>
+       <td>click</td>
+       <td>vaadin=runcomvaadintestscomponentsnotificationNotificationsHtmlAllowed::/VVerticalLayout[0]/ChildComponentContainer[1]/VVerticalLayout[0]/ChildComponentContainer[3]/VButton[0]/domChild[0]/domChild[0]</td>
+       <td></td>
+</tr>
+<tr>
+       <td>screenCapture</td>
+       <td><br /></td>
+       <td>plain</td>
+</tr>
+<tr>
+       <td>closeNotification</td>
+       <td>//body/div[2]</td>
+       <td>0,0</td>
+</tr>
+</tbody></table>
+</body>
+</html>
diff --git a/tests/src/com/vaadin/tests/components/notification/NotificationsHtmlAllowed.java b/tests/src/com/vaadin/tests/components/notification/NotificationsHtmlAllowed.java
new file mode 100644 (file)
index 0000000..58f6c12
--- /dev/null
@@ -0,0 +1,50 @@
+package com.vaadin.tests.components.notification;\r
+\r
+import com.vaadin.tests.components.TestBase;\r
+import com.vaadin.ui.Button;\r
+import com.vaadin.ui.Button.ClickEvent;\r
+import com.vaadin.ui.Button.ClickListener;\r
+import com.vaadin.ui.CheckBox;\r
+import com.vaadin.ui.TextArea;\r
+import com.vaadin.ui.TextField;\r
+import com.vaadin.ui.Window.Notification;\r
+\r
+public class NotificationsHtmlAllowed extends TestBase implements ClickListener {\r
+\r
+    private TextArea messageField;\r
+    private CheckBox htmlAllowedBox;\r
+    private TextField captionField;\r
+\r
+    @Override\r
+    protected void setup() {\r
+        captionField = new TextField("Caption", "Hello <u>world</u>");\r
+        addComponent(captionField);\r
+        messageField = new TextArea("Message",\r
+                "Hello <i>world</i>\nWith a newline <br/>And a html line break");\r
+        messageField.setRows(10);\r
+        addComponent(messageField);\r
+        htmlAllowedBox = new CheckBox("Html content allowed", true);\r
+        addComponent(htmlAllowedBox);\r
+        Button showNotification = new Button("Show notification", this);\r
+        addComponent(showNotification);\r
+    }\r
+\r
+    @Override\r
+    protected String getDescription() {\r
+        return "Test case for htmlAllowed in notifications";\r
+    }\r
+\r
+    @Override\r
+    protected Integer getTicketNumber() {\r
+        return 6097;\r
+    }\r
+\r
+    public void buttonClick(ClickEvent event) {\r
+        Notification n = new Notification((String) captionField.getValue(),\r
+                (String) messageField.getValue(),\r
+                Notification.TYPE_HUMANIZED_MESSAGE,\r
+                htmlAllowedBox.booleanValue());\r
+        event.getButton().getWindow().showNotification(n);\r
+\r
+    }\r
+}\r