[HtmlUnit] SVN: [15540] trunk/htmlunit/src/main/java/com/gargoylesoftware/htmlunit

rbri--- via HtmlUnit-develop <[email protected]> Fri, 24 Aug 2018 16:53:37 +0000
Newsgroups gmane.comp.java.htmlunit.devel
Message-ID <[email protected]>
Revision: 15540
          http://sourceforge.net/p/htmlunit/code/15540
Author:   rbri
Date:     2018-08-24 16:53:34 +0000 (Fri, 24 Aug 2018)
Log Message:
-----------
next step in event refactoring (wip)

Modified Paths:
--------------
    trunk/htmlunit/src/main/java/com/gargoylesoftware/htmlunit/BrowserVersion.java
    trunk/htmlunit/src/main/java/com/gargoylesoftware/htmlunit/BrowserVersionFeatures.java
    trunk/htmlunit/src/main/java/com/gargoylesoftware/htmlunit/html/HtmlPage.java
    trunk/htmlunit/src/main/java/com/gargoylesoftware/htmlunit/javascript/host/event/BeforeUnloadEvent.java
    trunk/htmlunit/src/main/java/com/gargoylesoftware/htmlunit/javascript/host/event/EventListenersContainer.java

Modified: trunk/htmlunit/src/main/java/com/gargoylesoftware/htmlunit/BrowserVersion.java
===================================================================
--- trunk/htmlunit/src/main/java/com/gargoylesoftware/htmlunit/BrowserVersion.java	2018-08-23 13:38:51 UTC (rev 15539)
+++ trunk/htmlunit/src/main/java/com/gargoylesoftware/htmlunit/BrowserVersion.java	2018-08-24 16:53:34 UTC (rev 15540)
@@ -210,8 +210,8 @@
         EDGE.scriptAcceptHeader_ = "application/javascript, */*;q=0.8";
 
         // CHROME
-        CHROME.applicationVersion_ = "5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/68.0.3440.75 Safari/537.36";
-        CHROME.userAgent_ = "Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/68.0.3440.75 Safari/537.36";
+        CHROME.applicationVersion_ = "5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/68.0.3440.106 Safari/537.36";
+        CHROME.userAgent_ = "Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/68.0.3440.106 Safari/537.36";
 
         CHROME.applicationCodeName_ = "Mozilla";
         CHROME.vendor_ = "Google Inc.";

Modified: trunk/htmlunit/src/main/java/com/gargoylesoftware/htmlunit/BrowserVersionFeatures.java
===================================================================
--- trunk/htmlunit/src/main/java/com/gargoylesoftware/htmlunit/BrowserVersionFeatures.java	2018-08-23 13:38:51 UTC (rev 15539)
+++ trunk/htmlunit/src/main/java/com/gargoylesoftware/htmlunit/BrowserVersionFeatures.java	2018-08-24 16:53:34 UTC (rev 15540)
@@ -154,9 +154,9 @@
     @BrowserFeature(IE)
     DOM_NORMALIZE_REMOVE_CHILDREN,
 
-    /** Indicates handler return value is only used if returnValue is the default value. */
+    /** Indicates whether returnValue behaves HTML5-like with an empty string default. */
     @BrowserFeature({CHROME, FF})
-    EVENT_BEFORE_UNLOAD_USES_HANDLER_RETURN_ONLY_IF_FIRST,
+    EVENT_BEFORE_UNLOAD_RETURN_VALUE_IS_HTML5_LIKE,
 
     /** Triggers the onfocus onfocusin blur onfocusout events in this order. */
     @BrowserFeature(CHROME)
@@ -166,6 +166,10 @@
     @BrowserFeature(IE)
     EVENT_FOCUS_IN_FOCUS_OUT_BLUR,
 
+    /** Indicates whether returning 'null' from a property handler is meaningful. */
+    @BrowserFeature(IE)
+    EVENT_HANDLER_NULL_RETURN_IS_MEANINGFUL,
+
     /** Mouse events are triggered on disabled elements also. */
     @BrowserFeature(FF)
     EVENT_MOUSE_ON_DISABLED,

Modified: trunk/htmlunit/src/main/java/com/gargoylesoftware/htmlunit/html/HtmlPage.java
===================================================================
--- trunk/htmlunit/src/main/java/com/gargoylesoftware/htmlunit/html/HtmlPage.java	2018-08-23 13:38:51 UTC (rev 15539)
+++ trunk/htmlunit/src/main/java/com/gargoylesoftware/htmlunit/html/HtmlPage.java	2018-08-24 16:53:34 UTC (rev 15540)
@@ -98,6 +98,7 @@
 import net.sourceforge.htmlunit.corejs.javascript.Script;
 import net.sourceforge.htmlunit.corejs.javascript.Scriptable;
 import net.sourceforge.htmlunit.corejs.javascript.ScriptableObject;
+import net.sourceforge.htmlunit.corejs.javascript.Undefined;
 
 /**
  * A representation of an HTML page returned from a server.
@@ -908,7 +909,7 @@
      */
     public ScriptResult executeJavaScript(String sourceCode, final String sourceName, final int startLine) {
         if (!getWebClient().getOptions().isJavaScriptEnabled()) {
-            return new ScriptResult(null, this);
+            return new ScriptResult(Undefined.instance, this);
         }
 
         if (StringUtils.startsWithIgnoreCase(sourceCode, JavaScriptURLConnection.JAVASCRIPT_PREFIX)) {

Modified: trunk/htmlunit/src/main/java/com/gargoylesoftware/htmlunit/javascript/host/event/BeforeUnloadEvent.java
===================================================================
--- trunk/htmlunit/src/main/java/com/gargoylesoftware/htmlunit/javascript/host/event/BeforeUnloadEvent.java	2018-08-23 13:38:51 UTC (rev 15539)
+++ trunk/htmlunit/src/main/java/com/gargoylesoftware/htmlunit/javascript/host/event/BeforeUnloadEvent.java	2018-08-24 16:53:34 UTC (rev 15540)
@@ -14,7 +14,8 @@
  */
 package com.gargoylesoftware.htmlunit.javascript.host.event;
 
-import static com.gargoylesoftware.htmlunit.BrowserVersionFeatures.EVENT_BEFORE_UNLOAD_USES_HANDLER_RETURN_ONLY_IF_FIRST;
+import static com.gargoylesoftware.htmlunit.BrowserVersionFeatures.EVENT_BEFORE_UNLOAD_RETURN_VALUE_IS_HTML5_LIKE;
+import static com.gargoylesoftware.htmlunit.BrowserVersionFeatures.EVENT_HANDLER_NULL_RETURN_IS_MEANINGFUL;
 import static com.gargoylesoftware.htmlunit.javascript.configuration.SupportedBrowser.CHROME;
 import static com.gargoylesoftware.htmlunit.javascript.configuration.SupportedBrowser.EDGE;
 import static com.gargoylesoftware.htmlunit.javascript.configuration.SupportedBrowser.FF;
@@ -79,7 +80,9 @@
     }
 
     private static Object getReturnValueDefault(final BrowserVersion browserVersion) {
-        if (browserVersion.isChrome() || browserVersion.isFirefox()) {
+        if (browserVersion.hasFeature(EVENT_BEFORE_UNLOAD_RETURN_VALUE_IS_HTML5_LIKE)) {
+            // Empty string default is specified by HTML5
+            // https://www.w3.org/TR/html5/browsers.html#the-beforeunloadevent-interface
             return "";
         }
         return Undefined.instance;
@@ -115,8 +118,10 @@
 
         final BrowserVersion browserVersion = getBrowserVersion();
 
-        if (!Undefined.isUndefined(returnValue) && (returnValue != null || browserVersion.isIE())) {
-            if (!browserVersion.hasFeature(EVENT_BEFORE_UNLOAD_USES_HANDLER_RETURN_ONLY_IF_FIRST)
+        // Most browsers ignore null return values of property handlers
+        if (returnValue != null || browserVersion.hasFeature(EVENT_HANDLER_NULL_RETURN_IS_MEANINGFUL)) {
+            // Chrome/Firefox only accept the return value if returnValue is equal to default
+            if (!browserVersion.hasFeature(EVENT_BEFORE_UNLOAD_RETURN_VALUE_IS_HTML5_LIKE)
                     || getReturnValueDefault(browserVersion).equals(getReturnValue())) {
                 setReturnValue(returnValue);
             }

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-08-23 13:38:51 UTC (rev 15539)
+++ trunk/htmlunit/src/main/java/com/gargoylesoftware/htmlunit/javascript/host/event/EventListenersContainer.java	2018-08-24 16:53:34 UTC (rev 15540)
@@ -344,8 +344,7 @@
                     final ScriptResult result =
                             page.executeJavaScriptFunction(function, thisObject, args, node);
                     // Return value is only honored for property handlers (Tested in Chrome/FF/IE11)
-                    if (isPropertyHandler) {
-                        allResult = result;
+                    if (isPropertyHandler && !ScriptResult.isUndefined(result)) {
                         event.handlePropertyHandlerReturnValue(result.getJavaScriptResult());
 
                         // This return value is now all but unused and can be refactored away


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