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

rbri--- via HtmlUnit-develop <[email protected]> Mon, 20 Aug 2018 17:30:07 +0000
Newsgroups gmane.comp.java.htmlunit.devel
Message-ID <[email protected]>
Revision: 15531
          http://sourceforge.net/p/htmlunit/code/15531
Author:   rbri
Date:     2018-08-20 17:30:03 +0000 (Mon, 20 Aug 2018)
Log Message:
-----------
next step in event refactoring (wip)

Modified Paths:
--------------
    trunk/htmlunit/src/main/java/com/gargoylesoftware/htmlunit/javascript/host/event/Event.java
    trunk/htmlunit/src/test/java/com/gargoylesoftware/htmlunit/javascript/host/Window3Test.java
    trunk/htmlunit/src/test/java/com/gargoylesoftware/htmlunit/javascript/host/event/EventTest.java

Modified: trunk/htmlunit/src/main/java/com/gargoylesoftware/htmlunit/javascript/host/event/Event.java
===================================================================
--- trunk/htmlunit/src/main/java/com/gargoylesoftware/htmlunit/javascript/host/event/Event.java	2018-08-20 06:39:52 UTC (rev 15530)
+++ trunk/htmlunit/src/main/java/com/gargoylesoftware/htmlunit/javascript/host/event/Event.java	2018-08-20 17:30:03 UTC (rev 15531)
@@ -14,7 +14,6 @@
  */
 package com.gargoylesoftware.htmlunit.javascript.host.event;
 
-import static com.gargoylesoftware.htmlunit.BrowserVersionFeatures.EVENT_FOCUS_FOCUS_IN_BLUR_OUT;
 import static com.gargoylesoftware.htmlunit.BrowserVersionFeatures.EVENT_ONLOAD_CANCELABLE_FALSE;
 import static com.gargoylesoftware.htmlunit.javascript.configuration.SupportedBrowser.CHROME;
 import static com.gargoylesoftware.htmlunit.javascript.configuration.SupportedBrowser.EDGE;
@@ -21,7 +20,6 @@
 import static com.gargoylesoftware.htmlunit.javascript.configuration.SupportedBrowser.FF;
 import static com.gargoylesoftware.htmlunit.javascript.configuration.SupportedBrowser.IE;
 
-import java.lang.reflect.Method;
 import java.util.LinkedList;
 
 import com.gargoylesoftware.htmlunit.ScriptResult;
@@ -35,6 +33,7 @@
 import com.gargoylesoftware.htmlunit.javascript.configuration.JsxSetter;
 
 import net.sourceforge.htmlunit.corejs.javascript.Context;
+import net.sourceforge.htmlunit.corejs.javascript.ScriptRuntime;
 import net.sourceforge.htmlunit.corejs.javascript.Scriptable;
 import net.sourceforge.htmlunit.corejs.javascript.ScriptableObject;
 import net.sourceforge.htmlunit.corejs.javascript.Undefined;
@@ -181,6 +180,7 @@
     private boolean stopPropagation_;
     private boolean stopImmediatePropagation_;
     private boolean preventDefault_;
+    private boolean returnValue_;
 
     /**
      * The current event phase. This is a W3C standard attribute. One of {@link #NONE},
@@ -229,6 +229,8 @@
         target_ = target;
         currentTarget_ = target;
         type_ = type;
+        returnValue_ = true;
+
         setParentScope(target);
         setPrototype(getPrototype(getClass()));
 
@@ -268,6 +270,7 @@
     public void eventCreated() {
         setBubbles(false);
         setCancelable(false);
+        returnValue_ = true;
     }
 
     /**
@@ -609,17 +612,7 @@
         type_ = type;
         bubbles_ = bubbles;
         cancelable_ = cancelable;
-        if (TYPE_BEFORE_UNLOAD.equals(type) && getBrowserVersion().hasFeature(EVENT_FOCUS_FOCUS_IN_BLUR_OUT)) {
-            try {
-                final Class<?> klass = getClass();
-                final Method readMethod = klass.getMethod("getReturnValue");
-                final Method writeMethod = klass.getMethod("setReturnValue", Object.class);
-                defineProperty("returnValue", null, readMethod, writeMethod, ScriptableObject.EMPTY);
-            }
-            catch (final Exception e) {
-                throw Context.throwAsScriptRuntimeEx(e);
-            }
-        }
+        returnValue_ = true;
     }
 
     /**
@@ -668,4 +661,20 @@
     public boolean processLabelAfterBubbling() {
         return false;
     }
+
+    /**
+     * @return the return value property
+     */
+    @JsxGetter(CHROME)
+    public Object getReturnValue() {
+        return returnValue_;
+    }
+
+    /**
+     * @param newValue the new return value
+     */
+    @JsxSetter(CHROME)
+    public void setReturnValue(final Object newValue) {
+        returnValue_ = ScriptRuntime.toBoolean(newValue);
+    }
 }

Modified: trunk/htmlunit/src/test/java/com/gargoylesoftware/htmlunit/javascript/host/Window3Test.java
===================================================================
--- trunk/htmlunit/src/test/java/com/gargoylesoftware/htmlunit/javascript/host/Window3Test.java	2018-08-20 06:39:52 UTC (rev 15530)
+++ trunk/htmlunit/src/test/java/com/gargoylesoftware/htmlunit/javascript/host/Window3Test.java	2018-08-20 17:30:03 UTC (rev 15531)
@@ -2164,7 +2164,7 @@
             + "  d1.addEventListener('click', function () { log('d1 at click 2 capture') }, true)\n"
 
             + "  d2.addEventListener('click', function () { log('d2 at click 1') })\n"
-            + "  d2.onclick = function () { log('d2 onclick'); d2.parentNode.removeChild(d2) }\n"
+            + "  d2.onclick = function () { log('d2 onclick'); if (d2.parentNode) d2.parentNode.removeChild(d2) }\n"
             + "  d2.addEventListener('click', function () { log('d2 at click 1 capture') }, true)\n"
             + "  d2.addEventListener('click', function () { log('d2 at click 2') })\n"
             + "  d2.addEventListener('click', function () { log('d2 at click 2 capture') }, true)\n"

Modified: trunk/htmlunit/src/test/java/com/gargoylesoftware/htmlunit/javascript/host/event/EventTest.java
===================================================================
--- trunk/htmlunit/src/test/java/com/gargoylesoftware/htmlunit/javascript/host/event/EventTest.java	2018-08-20 06:39:52 UTC (rev 15530)
+++ trunk/htmlunit/src/test/java/com/gargoylesoftware/htmlunit/javascript/host/event/EventTest.java	2018-08-20 17:30:03 UTC (rev 15531)
@@ -969,4 +969,72 @@
 
         verifyAlerts(driver, getExpectedAlerts());
     }
+
+    /**
+     * @throws Exception if the test fails
+     */
+    @Test
+    @Alerts(DEFAULT = {"undefined", "undefined"},
+            CHROME = {"true", "boolean"})
+    public void returnValue() throws Exception {
+        final String html = HtmlPageTest.STANDARDS_MODE_PREFIX_
+            + "<html><head><title>foo</title><script>\n"
+            + "  function test() {\n"
+            + "    try {\n"
+            + "      var event = document.createEvent('Event');\n"
+            + "      alert(event.returnValue);\n"
+            + "      alert(typeof event.returnValue);\n"
+            + "    } catch (e) { alert('exception') }\n"
+            + "  }\n"
+            + "</script></head><body onload='test()'>\n"
+            + "</body></html>";
+
+        loadPageWithAlerts2(html);
+    }
+
+    /**
+     * @throws Exception if the test fails
+     */
+    @Test
+    @Alerts(DEFAULT = {"undefined", "undefined",
+                        "false", "boolean",
+                        "undefined", "undefined",
+                        "test", "string",
+                        "0", "number"},
+            CHROME = {"true", "boolean",
+                        "false", "boolean",
+                        "false", "boolean",
+                        "true", "boolean",
+                        "false", "boolean"})
+    public void returnValueSetter() throws Exception {
+        final String html = HtmlPageTest.STANDARDS_MODE_PREFIX_
+            + "<html><head><title>foo</title><script>\n"
+            + "  function test() {\n"
+            + "    try {\n"
+            + "      var event = document.createEvent('Event');\n"
+            + "      alert(event.returnValue);\n"
+            + "      alert(typeof event.returnValue);\n"
+
+            + "      event.returnValue = false;\n"
+            + "      alert(event.returnValue);\n"
+            + "      alert(typeof event.returnValue);\n"
+
+            + "      event.returnValue = undefined;\n"
+            + "      alert(event.returnValue);\n"
+            + "      alert(typeof event.returnValue);\n"
+
+            + "      event.returnValue = 'test';\n"
+            + "      alert(event.returnValue);\n"
+            + "      alert(typeof event.returnValue);\n"
+
+            + "      event.returnValue = 0;\n"
+            + "      alert(event.returnValue);\n"
+            + "      alert(typeof event.returnValue);\n"
+            + "    } catch (e) { alert('exception') }\n"
+            + "  }\n"
+            + "</script></head><body onload='test()'>\n"
+            + "</body></html>";
+
+        loadPageWithAlerts2(html);
+    }
 }


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