[HtmlUnit] SVN: [15495] trunk/htmlunit/src

rbri--- via HtmlUnit-develop <[email protected]> Wed, 25 Jul 2018 17:38:53 +0000
Newsgroups gmane.comp.java.htmlunit.devel
Message-ID <[email protected]>
Revision: 15495
          http://sourceforge.net/p/htmlunit/code/15495
Author:   rbri
Date:     2018-07-25 17:38:50 +0000 (Wed, 25 Jul 2018)
Log Message:
-----------
event listeners had to be functions, other types are ignored
Issue 1976

Modified Paths:
--------------
    trunk/htmlunit/src/changes/changes.xml
    trunk/htmlunit/src/main/java/com/gargoylesoftware/htmlunit/javascript/host/ApplicationCache.java
    trunk/htmlunit/src/main/java/com/gargoylesoftware/htmlunit/javascript/host/MessagePort.java
    trunk/htmlunit/src/main/java/com/gargoylesoftware/htmlunit/javascript/host/Window.java
    trunk/htmlunit/src/main/java/com/gargoylesoftware/htmlunit/javascript/host/event/EventListenersContainer.java
    trunk/htmlunit/src/test/java/com/gargoylesoftware/htmlunit/javascript/host/event/EventListenersContainerTest.java
    trunk/htmlunit/src/test/java/com/gargoylesoftware/htmlunit/javascript/host/worker/WorkerTest.java

Modified: trunk/htmlunit/src/changes/changes.xml
===================================================================
--- trunk/htmlunit/src/changes/changes.xml	2018-07-22 17:37:11 UTC (rev 15494)
+++ trunk/htmlunit/src/changes/changes.xml	2018-07-25 17:38:50 UTC (rev 15495)
@@ -8,6 +8,9 @@
 
     <body>
         <release version="2.32" date="xx 2018" description="Bugfixes, FIREFOX_45 removed, FIREFOX_60 added">
+            <action type="fix" dev="rbri" issue="1976">
+                Event listeners had to be functions, other types are ignored.
+            </action>
             <action type="fix" dev="rbri">
                 Setting window.clientInformation is ignored in IE.
             </action>

Modified: trunk/htmlunit/src/main/java/com/gargoylesoftware/htmlunit/javascript/host/ApplicationCache.java
===================================================================
--- trunk/htmlunit/src/main/java/com/gargoylesoftware/htmlunit/javascript/host/ApplicationCache.java	2018-07-22 17:37:11 UTC (rev 15494)
+++ trunk/htmlunit/src/main/java/com/gargoylesoftware/htmlunit/javascript/host/ApplicationCache.java	2018-07-25 17:38:50 UTC (rev 15495)
@@ -29,8 +29,6 @@
 import com.gargoylesoftware.htmlunit.javascript.host.event.Event;
 import com.gargoylesoftware.htmlunit.javascript.host.event.EventTarget;
 
-import net.sourceforge.htmlunit.corejs.javascript.Scriptable;
-
 /**
  * <p>A collection of offline resources as defined in the HTML5 spec.
  * Intended to support offline web applications.</p>
@@ -224,10 +222,7 @@
     }
 
     private void setHandlerForJavaScript(final String eventName, final Object handler) {
-        if (handler == null || handler instanceof Scriptable) {
-            getEventListenersContainer().setEventHandler(eventName, handler);
-        }
-        // Otherwise, fail silently.
+        getEventListenersContainer().setEventHandler(eventName, handler);
     }
 
     /**

Modified: trunk/htmlunit/src/main/java/com/gargoylesoftware/htmlunit/javascript/host/MessagePort.java
===================================================================
--- trunk/htmlunit/src/main/java/com/gargoylesoftware/htmlunit/javascript/host/MessagePort.java	2018-07-22 17:37:11 UTC (rev 15494)
+++ trunk/htmlunit/src/main/java/com/gargoylesoftware/htmlunit/javascript/host/MessagePort.java	2018-07-25 17:38:50 UTC (rev 15495)
@@ -32,7 +32,6 @@
 import com.gargoylesoftware.htmlunit.javascript.host.event.MessageEvent;
 
 import net.sourceforge.htmlunit.corejs.javascript.ContextFactory;
-import net.sourceforge.htmlunit.corejs.javascript.Function;
 
 /**
  * A JavaScript object for {@code MessagePort}.
@@ -82,10 +81,7 @@
     }
 
     private void setHandlerForJavaScript(final String eventName, final Object handler) {
-        if (handler == null || handler instanceof Function) {
-            getEventListenersContainer().setEventHandler(eventName, handler);
-        }
-        // Otherwise, fail silently.
+        getEventListenersContainer().setEventHandler(eventName, handler);
     }
 
     /**

Modified: trunk/htmlunit/src/main/java/com/gargoylesoftware/htmlunit/javascript/host/Window.java
===================================================================
--- trunk/htmlunit/src/main/java/com/gargoylesoftware/htmlunit/javascript/host/Window.java	2018-07-22 17:37:11 UTC (rev 15494)
+++ trunk/htmlunit/src/main/java/com/gargoylesoftware/htmlunit/javascript/host/Window.java	2018-07-25 17:38:50 UTC (rev 15495)
@@ -1194,7 +1194,7 @@
      */
     @JsxSetter
     public void setOnload(final Object onload) {
-        getEventListenersContainer().setEventHandler(Event.TYPE_LOAD, onload);
+        setHandlerForJavaScript(Event.TYPE_LOAD, onload);
     }
 
     /**
@@ -1203,7 +1203,7 @@
      */
     @JsxSetter
     public void setOnblur(final Object onblur) {
-        getEventListenersContainer().setEventHandler(Event.TYPE_BLUR, onblur);
+        setHandlerForJavaScript(Event.TYPE_BLUR, onblur);
     }
 
     /**
@@ -1377,10 +1377,7 @@
     }
 
     private void setHandlerForJavaScript(final String eventName, final Object handler) {
-        if (handler == null || handler instanceof Function) {
-            getEventListenersContainer().setEventHandler(eventName, handler);
-        }
-        // Otherwise, fail silently.
+        getEventListenersContainer().setEventHandler(eventName, handler);
     }
 
     /**

Modified: trunk/htmlunit/src/main/java/com/gargoylesoftware/htmlunit/javascript/host/event/EventListenersContainer.java
===================================================================
--- trunk/htmlunit/src/main/java/com/gargoylesoftware/htmlunit/javascript/host/event/EventListenersContainer.java	2018-07-22 17:37:11 UTC (rev 15494)
+++ trunk/htmlunit/src/main/java/com/gargoylesoftware/htmlunit/javascript/host/event/EventListenersContainer.java	2018-07-25 17:38:50 UTC (rev 15495)
@@ -57,7 +57,7 @@
     static class TypeContainer implements Serializable {
         private List<Scriptable> capturingListeners_;
         private List<Scriptable> bubblingListeners_;
-        private Object handler_;
+        private Function handler_;
 
         TypeContainer() {
             capturingListeners_ = Collections.unmodifiableList(new ArrayList<Scriptable>());
@@ -65,7 +65,7 @@
         }
 
         private TypeContainer(final List<Scriptable> capturingListeners,
-                    final List<Scriptable> bubblingListeners, final Object handler) {
+                    final List<Scriptable> bubblingListeners, final Function handler) {
             capturingListeners_ = Collections.unmodifiableList(new ArrayList<>(capturingListeners));
             bubblingListeners_ = Collections.unmodifiableList(new ArrayList<>(bubblingListeners));
             handler_ = handler;
@@ -206,10 +206,15 @@
      * @param value the new property
      */
     public void setEventHandler(final String eventType, final Object value) {
-        Object handler = value;
-        if (handler == Undefined.instance) {
+        final Function handler;
+
+        // Otherwise, ignore silently.
+        if (value == Undefined.instance || !(value instanceof Function)) {
             handler = null;
         }
+        else {
+            handler = (Function) value;
+        }
 
         final TypeContainer container = getTypeContainer(eventType);
         container.handler_ = handler;

Modified: trunk/htmlunit/src/test/java/com/gargoylesoftware/htmlunit/javascript/host/event/EventListenersContainerTest.java
===================================================================
--- trunk/htmlunit/src/test/java/com/gargoylesoftware/htmlunit/javascript/host/event/EventListenersContainerTest.java	2018-07-22 17:37:11 UTC (rev 15494)
+++ trunk/htmlunit/src/test/java/com/gargoylesoftware/htmlunit/javascript/host/event/EventListenersContainerTest.java	2018-07-25 17:38:50 UTC (rev 15495)
@@ -25,6 +25,7 @@
  * Tests for {@link EventListenersContainer}.
  *
  * @author Ahmed Ashour
+ * @author Ronald Brill
  */
 @RunWith(BrowserRunner.class)
 public class EventListenersContainerTest extends WebDriverTestCase {

Modified: trunk/htmlunit/src/test/java/com/gargoylesoftware/htmlunit/javascript/host/worker/WorkerTest.java
===================================================================
--- trunk/htmlunit/src/test/java/com/gargoylesoftware/htmlunit/javascript/host/worker/WorkerTest.java	2018-07-22 17:37:11 UTC (rev 15494)
+++ trunk/htmlunit/src/test/java/com/gargoylesoftware/htmlunit/javascript/host/worker/WorkerTest.java	2018-07-25 17:38:50 UTC (rev 15495)
@@ -14,6 +14,8 @@
  */
 package com.gargoylesoftware.htmlunit.javascript.host.worker;
 
+import static com.gargoylesoftware.htmlunit.BrowserRunner.TestedBrowser.IE;
+
 import java.net.URL;
 
 import org.junit.After;
@@ -23,6 +25,7 @@
 
 import com.gargoylesoftware.htmlunit.BrowserRunner;
 import com.gargoylesoftware.htmlunit.BrowserRunner.Alerts;
+import com.gargoylesoftware.htmlunit.BrowserRunner.NotYetImplemented;
 import com.gargoylesoftware.htmlunit.WebDriverTestCase;
 
 /**
@@ -29,6 +32,7 @@
  * Unit tests for {@code Worker}.
  *
  * @author Marc Guillemot
+ * @author Ronald Brill
  */
 @RunWith(BrowserRunner.class)
 public class WorkerTest extends WebDriverTestCase {
@@ -164,4 +168,57 @@
 
         loadPageWithAlerts2(html);
     }
+
+    /**
+     * @throws Exception if the test fails
+     */
+    @Test
+    @Alerts("function")
+    public void onmessageFunction() throws Exception {
+        final String html = "<html><body><script>\n"
+                + "  var myWorker = new Worker('worker.js');\n"
+                + "  myWorker.onmessage = function(e) {};\n"
+                + "  alert(typeof myWorker.onmessage);\n"
+                + "</script></body></html>\n";
+
+        loadPageWithAlerts2(html);
+    }
+
+    /**
+     * @throws Exception if the test fails
+     */
+    @Test
+    @Alerts(DEFAULT = "null",
+            IE = "exception Error")
+    @NotYetImplemented(IE)
+    public void onmessageNumber() throws Exception {
+        final String html = "<html><body><script>\n"
+                + "  var myWorker = new Worker('worker.js');\n"
+                + "  try {\n"
+                + "    myWorker.onmessage = 17;\n"
+                + "    alert(myWorker.onmessage);\n"
+                + "  } catch(e) { alert('exception ' + e.name); }\n"
+                + "</script></body></html>\n";
+
+        loadPageWithAlerts2(html);
+    }
+
+    /**
+     * @throws Exception if the test fails
+     */
+    @Test
+    @Alerts(DEFAULT = "null",
+            IE = "HtmlUnit")
+    @NotYetImplemented(IE)
+    public void onmessageString() throws Exception {
+        final String html = "<html><body><script>\n"
+                + "  var myWorker = new Worker('worker.js');\n"
+                + "  try {\n"
+                + "    myWorker.onmessage = 'HtmlUnit';\n"
+                + "    alert(myWorker.onmessage);\n"
+                + "  } catch(e) { alert('exception ' + e.name); }\n"
+                + "</script></body></html>\n";
+
+        loadPageWithAlerts2(html);
+    }
 }


------------------------------------------------------------------------------
Check out the vibrant tech community on one of the world's most
engaging tech sites, Slashdot.org! http://sdm.link/slashdot