]> source.dussan.org Git - vaadin-framework.git/commitdiff
Fix for #3589 - Window.executeJavaScript(). Removed DOM reference possibility.
authorArtur Signell <artur.signell@itmill.com>
Thu, 12 Nov 2009 13:55:27 +0000 (13:55 +0000)
committerArtur Signell <artur.signell@itmill.com>
Thu, 12 Nov 2009 13:55:27 +0000 (13:55 +0000)
svn changeset:9746/svn branch:6.2

src/com/vaadin/terminal/gwt/client/ui/VView.java
src/com/vaadin/ui/Window.java
tests/src/com/vaadin/tests/components/window/ExecuteJavaScript.java [new file with mode: 0644]
tests/src/com/vaadin/tests/tickets/Ticket3589.java [deleted file]

index 1c097adcf76be0e4dfdbc6e306b1f7a17251b3a9..bfa4a653bdb3509c067accf77d024abf6a0fc01f 100644 (file)
@@ -10,7 +10,6 @@ import java.util.Iterator;
 import java.util.LinkedHashSet;
 import java.util.Set;
 
-import com.google.gwt.core.client.JavaScriptObject;
 import com.google.gwt.dom.client.DivElement;
 import com.google.gwt.dom.client.Document;
 import com.google.gwt.event.logical.shared.ResizeEvent;
@@ -145,39 +144,20 @@ public class VView extends SimplePanel implements Container, ResizeHandler,
      }-*/;
 
     /**
-     * Evaluate given script in browser document.
+     * Evaluate the given script in the browser document.
      * 
      * @param script
-     *            Script to be executed with $1, $2, ... markers to be replaced
-     *            with argument references.
-     * @param args
-     *            JavaScript array of element references to be replaced in
-     *            script
+     *            Script to be executed.
      */
-    private static native void eval(String script, JavaScriptObject args)
+    private static native void eval(String script)
     /*-{
       try {
          if (script == null) return;
-         $wnd.vaadin.evalargs = args;
-         if (args != null) 
-             for (var i=1;i<= args.length;i++) 
-                script = script.replace("$"+i, "$wnd.vaadin.evalargs["+(i-1)+"]");
          $wnd.eval(script);
-         delete $wnd.vaadin.evalargs;
       } catch (e) {
       }
     }-*/;
 
-    /** Helper for creating empty javascript arrays */
-    private native static JavaScriptObject createArray() /*-{
-                                                         return new Array();
-                                                         }-*/;
-
-    /** Add an element to javascript array */
-    private native static void pushArray(JavaScriptObject array, Element e) /*-{
-                                                                            array.push(e);
-                                                                            }-*/;
-
     public void updateFromUIDL(UIDL uidl, ApplicationConnection client) {
         rendering = true;
 
@@ -300,16 +280,7 @@ public class VView extends SimplePanel implements Container, ResizeHandler,
                 actionHandler.updateActionMap(childUidl);
             } else if (tag == "execJS") {
                 String script = childUidl.getStringAttribute("script");
-                int argNum = childUidl.getChildCount();
-                JavaScriptObject args = createArray();
-                for (int i = 0; i < argNum; i++) {
-                    String id = childUidl.getChildUIDL(i).getStringAttribute(
-                            "ref");
-                    Paintable p = client.getPaintable(id);
-                    pushArray(args, p == null ? null : ((Widget) p)
-                            .getElement());
-                }
-                eval(script, args);
+                eval(script);
             } else if (tag == "notifications") {
                 for (final Iterator it = childUidl.getChildIterator(); it
                         .hasNext();) {
index 8de7a944f703a8cc5fcafb4602ba6b9a6991a7a3..d234f7aaaca731dc7d466a5963b8de2507bb7b43 100644 (file)
@@ -20,7 +20,6 @@ import com.vaadin.Application;
 import com.vaadin.terminal.DownloadStream;
 import com.vaadin.terminal.PaintException;
 import com.vaadin.terminal.PaintTarget;
-import com.vaadin.terminal.Paintable;
 import com.vaadin.terminal.ParameterHandler;
 import com.vaadin.terminal.Resource;
 import com.vaadin.terminal.Sizeable;
@@ -122,7 +121,6 @@ public class Window extends Panel implements URIHandler, ParameterHandler {
     private Focusable pendingFocus;
 
     private ArrayList<String> jsExecQueue = null;
-    private ArrayList<ArrayList<Paintable>> jsExecParamsQueue = null;
 
     /* ********************************************************************* */
 
@@ -527,22 +525,12 @@ public class Window extends Panel implements URIHandler, ParameterHandler {
 
         // Add executable javascripts if needed
         if (jsExecQueue != null) {
-            while (jsExecQueue.size() > 0) {
-                String script = jsExecQueue.get(0);
-                ArrayList<Paintable> args = jsExecParamsQueue.get(0);
+            for (String script : jsExecQueue) {
                 target.startTag("execJS");
                 target.addAttribute("script", script);
-                for (Paintable p : args) {
-                    target.startTag("arg");
-                    target.paintReference(p, "ref");
-                    target.endTag("arg");
-                }
                 target.endTag("execJS");
-                jsExecQueue.remove(0);
-                jsExecParamsQueue.remove(0);
             }
             jsExecQueue = null;
-            jsExecParamsQueue = null;
         }
 
         // Window position
@@ -1661,82 +1649,34 @@ public class Window extends Panel implements URIHandler, ParameterHandler {
      * Executes JavaScript in this window.
      * 
      * <p>
-     * This method allows one to inject javascript from the server to client.
-     * Client implementation is not required to implenment this functionality,
+     * This method allows one to inject javascript from the server to client. A
+     * client implementation is not required to implement this functionality,
      * but currently all web-based clients do implement this.
      * </p>
      * 
      * <p>
-     * Use of this method should be avoided. Executing javascript often leads to
-     * cross-browser compatibility issues and regressions that are hard to
-     * resolve. Instead it is recommended to create new widgets with google web
-     * toolkit. For more info on creating own, reusable client-side widgets in
-     * Java, read corresponding chapter on Book of Vaadin.
-     * </p>
-     * 
-     * <p>
-     * The script may contain markers indentified with "$x", where x is a one
-     * digit number. The number of markers must match to the number of optional
-     * Paintable reference parameters. The first marker is $1, second $2 and so
-     * on. If the markers are specified, the markers are replaced in the browser
-     * with references to outmost DOM elements connected to the given
-     * paintables.
-     * </p>
-     * 
-     * <p>
-     * Use example 1: Show a alert box in the current window<code>
-       mainWindow.executeJavaScript("alert('foo');");
-     * </code>
-     * </p>
-     * 
-     * <p>
-     * Use example 2: Print current window<code>
-       mainWindow.executeJavaScript("window.print()");
-     * </code>
-     * </p>
-     * 
-     * <p>
-     * Use example 3: Change labels background to yellow<code>
-        Label label = new Label("Label");
-        mainWindow.addComponent(label);
-        mainWindow.executeJavaScript("$1.style.backgroundColor='yellow';",label);
-     * </code>
-     * </p>
-     * 
-     * <p>
-     * Use example 4: Print contents of a label<code>
-        Label label = new Label("This label contains some report");
-        mainWindow.addComponent(label);
-        mainWindow.executeJavaScript("var w = window.open(); w.document.write($1.outerHTML); w.print();",label);
-     * </code>
+     * Executing javascript this way often leads to cross-browser compatibility
+     * issues and regressions that are hard to resolve. Use of this method
+     * should be avoided and instead it is recommended to create new widgets
+     * with GWT. For more info on creating own, reusable client-side widgets in
+     * Java, read the corresponding chapter in Book of Vaadin.
      * </p>
      * 
      * @param script
-     *            JavaScript snippet that will be executed and that might
-     *            optionally contain $1, $2, ... markers.
-     * @param paintables
-     *            References to number of visible paintables that correspond the
-     *            to markers listed in script
+     *            JavaScript snippet that will be executed.
      */
-    public void executeJavaScript(String script, Paintable... paintables) {
+    public void executeJavaScript(String script) {
 
         if (getParent() != null) {
             throw new UnsupportedOperationException(
                     "Only application level windows can execute javascript.");
         }
 
-        ArrayList<Paintable> ps = new ArrayList<Paintable>();
-        for (Paintable p : paintables) {
-            ps.add(p);
-        }
-
         if (jsExecQueue == null) {
             jsExecQueue = new ArrayList<String>();
-            jsExecParamsQueue = new ArrayList<ArrayList<Paintable>>();
         }
 
         jsExecQueue.add(script);
-        jsExecParamsQueue.add(ps);
 
         requestRepaint();
     }
diff --git a/tests/src/com/vaadin/tests/components/window/ExecuteJavaScript.java b/tests/src/com/vaadin/tests/components/window/ExecuteJavaScript.java
new file mode 100644 (file)
index 0000000..3d4394d
--- /dev/null
@@ -0,0 +1,46 @@
+package com.vaadin.tests.components.window;
+
+import com.vaadin.tests.components.AbstractTestCase;
+import com.vaadin.ui.Button;
+import com.vaadin.ui.Panel;
+import com.vaadin.ui.Window;
+import com.vaadin.ui.Button.ClickEvent;
+
+public class ExecuteJavaScript extends AbstractTestCase {
+
+    @Override
+    public void init() {
+        final Window mainWindow = new Window("Test");
+        setMainWindow(mainWindow);
+
+        for (final String script : new String[] { "alert('foo');",
+                "window.print()", "document.write('foo')" }) {
+            Panel p = new Panel("Example: " + script);
+            p.addComponent(createScriptButton(script));
+            mainWindow.addComponent(p);
+        }
+
+    }
+
+    private Button createScriptButton(final String script) {
+        Button b = new Button(script);
+        b.addListener(new Button.ClickListener() {
+
+            public void buttonClick(ClickEvent event) {
+                getMainWindow().executeJavaScript(script);
+            }
+        });
+
+        return b;
+    }
+
+    @Override
+    protected String getDescription() {
+        return "Test for the Window.executeJavaScript method. Click a button to execute the javascript";
+    }
+
+    @Override
+    protected Integer getTicketNumber() {
+        return 3589;
+    }
+}
diff --git a/tests/src/com/vaadin/tests/tickets/Ticket3589.java b/tests/src/com/vaadin/tests/tickets/Ticket3589.java
deleted file mode 100644 (file)
index f072f2d..0000000
+++ /dev/null
@@ -1,57 +0,0 @@
-package com.vaadin.tests.tickets;
-
-import com.vaadin.Application;
-import com.vaadin.ui.Button;
-import com.vaadin.ui.Label;
-import com.vaadin.ui.Panel;
-import com.vaadin.ui.TextField;
-import com.vaadin.ui.Window;
-import com.vaadin.ui.Button.ClickEvent;
-
-public class Ticket3589 extends Application {
-
-    @Override
-    public void init() {
-        final Window mainWindow = new Window("Test");
-        setMainWindow(mainWindow);
-
-        for (final String script : new String[] { "alert('foo');",
-                "window.print()", "document.write('foo')" }) {
-            Panel p = new Panel("Example: " + script);
-            p.addComponent(new Button("Run javascript",
-                    new Button.ClickListener() {
-
-                        public void buttonClick(ClickEvent event) {
-                            mainWindow.executeJavaScript(script);
-                        }
-                    }));
-            mainWindow.addComponent(p);
-        }
-
-        final String script = "$1.style.backgroundColor='yellow';$2.style.borderColor='red';";
-        Panel p = new Panel("Example: " + script);
-        final Label label = new Label("Label");
-        final TextField textfield = new TextField("TestField");
-        p.addComponent(label);
-        p.addComponent(textfield);
-        p.addComponent(new Button("Run javascript", new Button.ClickListener() {
-
-            public void buttonClick(ClickEvent event) {
-                mainWindow.executeJavaScript(script, label, textfield);
-            }
-        }));
-        mainWindow.addComponent(p);
-
-        final String script2 = "var w = window.open(); w.document.write($1.outerHTML); w.print();";
-        final Panel p2 = new Panel("Example: " + script2);
-        p2.addComponent(new Label("Only this panel will be printed..."));
-        p2.addComponent(new Button("Run javascript",
-                new Button.ClickListener() {
-                    public void buttonClick(ClickEvent event) {
-                        mainWindow.executeJavaScript(script2, p2);
-                    }
-                }));
-        mainWindow.addComponent(p2);
-
-    }
-}