[prev in list] [next in list] [prev in thread] [next in thread] 

List:       htmlunit-develop
Subject:    [HtmlUnit] SF.net SVN: htmlunit:[6434] trunk/htmlunit/src
From:       mguillem () users ! sourceforge ! net
Date:       2011-05-26 21:59:27
Message-ID: E1QPiaV-0006PG-2p () sfp-svn-2 ! v30 ! ch3 ! sourceforge ! com
[Download RAW message or body]

Revision: 6434
          http://htmlunit.svn.sourceforge.net/htmlunit/?rev=6434&view=rev
Author:   mguillem
Date:     2011-05-26 21:59:26 +0000 (Thu, 26 May 2011)

Log Message:
-----------
JavaScript: fix string conversion of native functions.
Issue 3029093

Modified Paths:
--------------
    trunk/htmlunit/src/changes/changes.xml
    trunk/htmlunit/src/main/java/com/gargoylesoftware/htmlunit/BrowserVersionFeatures.java
  trunk/htmlunit/src/main/java/com/gargoylesoftware/htmlunit/javascript/JavaScriptEngine.java
  trunk/htmlunit/src/main/resources/com/gargoylesoftware/htmlunit/javascript/configuration/IE6.properties
  trunk/htmlunit/src/main/resources/com/gargoylesoftware/htmlunit/javascript/configuration/IE7.properties
  trunk/htmlunit/src/main/resources/com/gargoylesoftware/htmlunit/javascript/configuration/IE8.properties
  trunk/htmlunit/src/test/java/com/gargoylesoftware/htmlunit/html/HtmlInputTest.java
    trunk/htmlunit/src/test/java/com/gargoylesoftware/htmlunit/html/HtmlPageTest.java
    trunk/htmlunit/src/test/java/com/gargoylesoftware/htmlunit/javascript/JavaScriptEngineTest.java
  trunk/htmlunit/src/test/java/com/gargoylesoftware/htmlunit/javascript/NativeArrayTest.java
  trunk/htmlunit/src/test/java/com/gargoylesoftware/htmlunit/javascript/NativeFunctionTest.java
  trunk/htmlunit/src/test/java/com/gargoylesoftware/htmlunit/javascript/host/html/HTMLFrameElementTest.java


Added Paths:
-----------
    trunk/htmlunit/src/main/java/com/gargoylesoftware/htmlunit/javascript/FunctionWrapper.java
  trunk/htmlunit/src/main/java/com/gargoylesoftware/htmlunit/javascript/NativeFunctionToStringFunction.java


Modified: trunk/htmlunit/src/changes/changes.xml
===================================================================
--- trunk/htmlunit/src/changes/changes.xml	2011-05-26 18:42:31 UTC (rev 6433)
+++ trunk/htmlunit/src/changes/changes.xml	2011-05-26 21:59:26 UTC (rev 6434)
@@ -7,6 +7,9 @@
     <body>
         <release version="2.9" date="???" description="Bugfixes">
             <action type="fix" dev="mguillem">
+                JavaScript: fix string conversion of native functions.
+            </action>
+            <action type="fix" dev="mguillem">
                 Parsing: accept self closing &lt;iframe/&gt; tags when simulating \
IE.  </action>
             <action type="fix" dev="rbri" issue="3306491">

Modified: trunk/htmlunit/src/main/java/com/gargoylesoftware/htmlunit/BrowserVersionFeatures.java
 ===================================================================
--- trunk/htmlunit/src/main/java/com/gargoylesoftware/htmlunit/BrowserVersionFeatures.java	2011-05-26 \
                18:42:31 UTC (rev 6433)
+++ trunk/htmlunit/src/main/java/com/gargoylesoftware/htmlunit/BrowserVersionFeatures.java	2011-05-26 \
21:59:26 UTC (rev 6434) @@ -673,6 +673,11 @@
      */
     JS_LOCATION_HASH_IS_DECODED,
 
+    /**
+     * Indicates if the String representation of a native function begins and ends \
with a \n. +     */
+    JS_NATIVE_FUNCTION_TOSTRING_NEW_LINE,
+
     /** Always "[object]". */
     JS_OBJECT_ONLY,
 

Added: trunk/htmlunit/src/main/java/com/gargoylesoftware/htmlunit/javascript/FunctionWrapper.java
 ===================================================================
--- trunk/htmlunit/src/main/java/com/gargoylesoftware/htmlunit/javascript/FunctionWrapper.java	 \
                (rev 0)
+++ trunk/htmlunit/src/main/java/com/gargoylesoftware/htmlunit/javascript/FunctionWrapper.java	2011-05-26 \
21:59:26 UTC (rev 6434) @@ -0,0 +1,105 @@
+/*
+ * Copyright (c) 2002-2011 Gargoyle Software Inc.
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package com.gargoylesoftware.htmlunit.javascript;
+
+import net.sourceforge.htmlunit.corejs.javascript.Context;
+import net.sourceforge.htmlunit.corejs.javascript.Function;
+import net.sourceforge.htmlunit.corejs.javascript.Scriptable;
+
+/**
+ * Wrapper for a {@link Function} delegating all calls to the wrapped instance.
+ * @author Marc Guillemot
+ * @version $Revision$
+ */
+class FunctionWrapper implements Function {
+    private final Function wrapped_;
+
+    FunctionWrapper(final Function wrapped) {
+        wrapped_ = wrapped;
+    }
+
+    public Object call(final Context cx, final Scriptable scope, final Scriptable \
thisObj, final Object[] args) { +        return wrapped_.call(cx, scope, thisObj, \
args); +    }
+
+    public String getClassName() {
+        return wrapped_.getClassName();
+    }
+
+    public Scriptable construct(final Context cx, final Scriptable scope, final \
Object[] args) { +        return wrapped_.construct(cx, scope, args);
+    }
+
+    public Object get(final String name, final Scriptable start) {
+        return wrapped_.get(name, start);
+    }
+
+    public Object get(final int index, final Scriptable start) {
+        return wrapped_.get(index, start);
+    }
+
+    public boolean has(final String name, final Scriptable start) {
+        return wrapped_.has(name, start);
+    }
+
+    public boolean has(final int index, final Scriptable start) {
+        return wrapped_.has(index, start);
+    }
+
+    public void put(final String name, final Scriptable start, final Object value) {
+        wrapped_.put(name, start, value);
+    }
+
+    public void put(final int index, final Scriptable start, final Object value) {
+        wrapped_.put(index, start, value);
+    }
+
+    public void delete(final String name) {
+        wrapped_.delete(name);
+    }
+
+    public void delete(final int index) {
+        wrapped_.delete(index);
+    }
+
+    public Scriptable getPrototype() {
+        return wrapped_.getPrototype();
+    }
+
+    public void setPrototype(final Scriptable prototype) {
+        wrapped_.setPrototype(prototype);
+    }
+
+    public Scriptable getParentScope() {
+        return wrapped_.getParentScope();
+    }
+
+    public void setParentScope(final Scriptable parent) {
+        wrapped_.setParentScope(parent);
+    }
+
+    public Object[] getIds() {
+        return wrapped_.getIds();
+    }
+
+    public Object getDefaultValue(final Class<?> hint) {
+        return wrapped_.getDefaultValue(hint);
+    }
+
+    public boolean hasInstance(final Scriptable instance) {
+        return wrapped_.hasInstance(instance);
+    }
+
+}


Property changes on: \
trunk/htmlunit/src/main/java/com/gargoylesoftware/htmlunit/javascript/FunctionWrapper.java
 ___________________________________________________________________
Added: svn:keywords
   + Author Date Id Revision
Added: svn:eol-style
   + native

Modified: trunk/htmlunit/src/main/java/com/gargoylesoftware/htmlunit/javascript/JavaScriptEngine.java
 ===================================================================
--- trunk/htmlunit/src/main/java/com/gargoylesoftware/htmlunit/javascript/JavaScriptEngine.java	2011-05-26 \
                18:42:31 UTC (rev 6433)
+++ trunk/htmlunit/src/main/java/com/gargoylesoftware/htmlunit/javascript/JavaScriptEngine.java	2011-05-26 \
21:59:26 UTC (rev 6434) @@ -282,6 +282,8 @@
             removePrototypeProperties(window, "String", "toSource");
         }
 
+        NativeFunctionToStringFunction.installFix(window, \
webClient.getBrowserVersion()); +
         window.setPrototypes(prototypes);
         window.initialize(webWindow);
     }

Added: trunk/htmlunit/src/main/java/com/gargoylesoftware/htmlunit/javascript/NativeFunctionToStringFunction.java
 ===================================================================
--- trunk/htmlunit/src/main/java/com/gargoylesoftware/htmlunit/javascript/NativeFunctionToStringFunction.java	 \
                (rev 0)
+++ trunk/htmlunit/src/main/java/com/gargoylesoftware/htmlunit/javascript/NativeFunctionToStringFunction.java	2011-05-26 \
21:59:26 UTC (rev 6434) @@ -0,0 +1,71 @@
+/*
+ * Copyright (c) 2002-2011 Gargoyle Software Inc.
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package com.gargoylesoftware.htmlunit.javascript;
+
+import com.gargoylesoftware.htmlunit.BrowserVersion;
+import com.gargoylesoftware.htmlunit.BrowserVersionFeatures;
+
+import net.sourceforge.htmlunit.corejs.javascript.Context;
+import net.sourceforge.htmlunit.corejs.javascript.Function;
+import net.sourceforge.htmlunit.corejs.javascript.IdFunctionObject;
+import net.sourceforge.htmlunit.corejs.javascript.Scriptable;
+import net.sourceforge.htmlunit.corejs.javascript.ScriptableObject;
+
+/**
+ * Replacement (in fact a wrapper) for Rhino's native toString function on Function \
prototype + * allowing to produce the desired formatting.
+ * @author Marc Guillemot
+ * @version $Revision$
+ */
+class NativeFunctionToStringFunction extends FunctionWrapper {
+    private final String separator_;
+
+    NativeFunctionToStringFunction(final Function wrapped, final String separator) {
+        super(wrapped);
+        separator_ = separator;
+    }
+
+    /**
+     * {@inheritDoc}
+     */
+    public Object call(final Context cx, final Scriptable scope, final Scriptable \
thisObj, final Object[] args) { +        final String s = (String) super.call(cx, \
scope, thisObj, args); +
+        if (thisObj instanceof IdFunctionObject && s.contains("() { [native code for \
")) { +            final String functionName = ((IdFunctionObject) \
thisObj).getFunctionName(); +            return separator_ + "function " + \
functionName + "() {\n    [native code]\n}" + separator_; +        }
+        return s.trim();
+    }
+
+    /**
+     * Install the wrapper in place of the native toString function on Function's \
prototype. +     * @param window the scope
+     * @param browserVersion the simulated browser
+     */
+    static void installFix(final Scriptable window, final BrowserVersion \
browserVersion) { +        final ScriptableObject fnPrototype = (ScriptableObject) \
ScriptableObject.getClassPrototype(window, "Function"); +        final Function \
originalToString = (Function) ScriptableObject.getProperty(fnPrototype, "toString"); \
+        final String separator; +        if \
(browserVersion.hasFeature(BrowserVersionFeatures.JS_NATIVE_FUNCTION_TOSTRING_NEW_LINE)) \
{ +            separator = "\n";
+        }
+        else {
+            separator = "";
+        }
+        final Function newToString = new \
NativeFunctionToStringFunction(originalToString, separator); +        \
ScriptableObject.putProperty(fnPrototype, "toString", newToString); +    }
+}


Property changes on: \
trunk/htmlunit/src/main/java/com/gargoylesoftware/htmlunit/javascript/NativeFunctionToStringFunction.java
 ___________________________________________________________________
Added: svn:keywords
   + Author Date Id Revision
Added: svn:eol-style
   + native

Modified: trunk/htmlunit/src/main/resources/com/gargoylesoftware/htmlunit/javascript/configuration/IE6.properties
 ===================================================================
--- trunk/htmlunit/src/main/resources/com/gargoylesoftware/htmlunit/javascript/configuration/IE6.properties	2011-05-26 \
                18:42:31 UTC (rev 6433)
+++ trunk/htmlunit/src/main/resources/com/gargoylesoftware/htmlunit/javascript/configuration/IE6.properties	2011-05-26 \
21:59:26 UTC (rev 6434) @@ -154,6 +154,7 @@
 JS_IFRAME_GET_WIDTH_NEGATIVE_VALUES
 JS_INNER_HTML_REDUCE_WHITESPACES
 JS_LENGTH_WITHOUT_PX
+JS_NATIVE_FUNCTION_TOSTRING_NEW_LINE
 JS_OBJECT_ONLY
 JS_OFFSET_PARENT_THROWS_NOT_ATTACHED
 JS_SELECT_ITEM_THROWS_IF_NEGATIVE

Modified: trunk/htmlunit/src/main/resources/com/gargoylesoftware/htmlunit/javascript/configuration/IE7.properties
 ===================================================================
--- trunk/htmlunit/src/main/resources/com/gargoylesoftware/htmlunit/javascript/configuration/IE7.properties	2011-05-26 \
                18:42:31 UTC (rev 6433)
+++ trunk/htmlunit/src/main/resources/com/gargoylesoftware/htmlunit/javascript/configuration/IE7.properties	2011-05-26 \
21:59:26 UTC (rev 6434) @@ -152,6 +152,7 @@
 JS_IFRAME_GET_WIDTH_NEGATIVE_VALUES
 JS_INNER_HTML_REDUCE_WHITESPACES
 JS_LENGTH_WITHOUT_PX
+JS_NATIVE_FUNCTION_TOSTRING_NEW_LINE
 JS_OBJECT_ONLY
 JS_OFFSET_PARENT_THROWS_NOT_ATTACHED
 JS_SELECT_ITEM_THROWS_IF_NEGATIVE

Modified: trunk/htmlunit/src/main/resources/com/gargoylesoftware/htmlunit/javascript/configuration/IE8.properties
 ===================================================================
--- trunk/htmlunit/src/main/resources/com/gargoylesoftware/htmlunit/javascript/configuration/IE8.properties	2011-05-26 \
                18:42:31 UTC (rev 6433)
+++ trunk/htmlunit/src/main/resources/com/gargoylesoftware/htmlunit/javascript/configuration/IE8.properties	2011-05-26 \
21:59:26 UTC (rev 6434) @@ -155,6 +155,7 @@
 JS_INNER_HTML_REDUCE_WHITESPACES
 JS_LENGTH_WITHOUT_PX
 JS_LOCATION_HASH_IS_DECODED
+JS_NATIVE_FUNCTION_TOSTRING_NEW_LINE
 JS_OBJECT_ONLY
 JS_OFFSET_PARENT_THROWS_NOT_ATTACHED
 JS_SELECT_ITEM_THROWS_IF_NEGATIVE

Modified: trunk/htmlunit/src/test/java/com/gargoylesoftware/htmlunit/html/HtmlInputTest.java
 ===================================================================
--- trunk/htmlunit/src/test/java/com/gargoylesoftware/htmlunit/html/HtmlInputTest.java	2011-05-26 \
                18:42:31 UTC (rev 6433)
+++ trunk/htmlunit/src/test/java/com/gargoylesoftware/htmlunit/html/HtmlInputTest.java	2011-05-26 \
21:59:26 UTC (rev 6434) @@ -243,7 +243,7 @@
      * @throws Exception if the test fails
      */
     @Test
-    @Alerts({ "\nfunction handler() {\n}\n", "null" })
+    @Alerts({ "function handler() {\n}", "null" })
     public void onchangeNull() throws Exception {
         final String html =
             "<html><head>\n"

Modified: trunk/htmlunit/src/test/java/com/gargoylesoftware/htmlunit/html/HtmlPageTest.java
 ===================================================================
--- trunk/htmlunit/src/test/java/com/gargoylesoftware/htmlunit/html/HtmlPageTest.java	2011-05-26 \
                18:42:31 UTC (rev 6433)
+++ trunk/htmlunit/src/test/java/com/gargoylesoftware/htmlunit/html/HtmlPageTest.java	2011-05-26 \
21:59:26 UTC (rev 6434) @@ -48,6 +48,7 @@
 import org.w3c.dom.NodeList;
 
 import com.gargoylesoftware.htmlunit.BrowserRunner;
+import com.gargoylesoftware.htmlunit.BrowserRunner.Alerts;
 import com.gargoylesoftware.htmlunit.BrowserRunner.Browser;
 import com.gargoylesoftware.htmlunit.BrowserRunner.NotYetImplemented;
 import com.gargoylesoftware.htmlunit.CollectingAlertHandler;
@@ -574,19 +575,16 @@
      * @throws Exception if the test fails
      */
     @Test
+    @Alerts("function () {\n}")
     public void testOnLoadHandler_ScriptNameRead() throws Exception {
-        final String htmlContent = "<html><head><title>foo</title>\n"
+        final String html = "<html><head><title>foo</title>\n"
             + "<script type='text/javascript'>\n"
             + "load=function(){};\n"
             + "onload=load;\n"
             + "alert(onload);\n"
             + "</script></head><body></body></html>";
-        final List<String> collectedAlerts = new ArrayList<String>();
-        final HtmlPage page = loadPage(htmlContent, collectedAlerts);
-        assertEquals("foo", page.getTitleText());
 
-        final String[] expectedAlerts = {"\nfunction () {\n}\n"};
-        assertEquals(expectedAlerts, collectedAlerts);
+        loadPageWithAlerts(html);
     }
 
     /**

Modified: trunk/htmlunit/src/test/java/com/gargoylesoftware/htmlunit/javascript/JavaScriptEngineTest.java
 ===================================================================
--- trunk/htmlunit/src/test/java/com/gargoylesoftware/htmlunit/javascript/JavaScriptEngineTest.java	2011-05-26 \
                18:42:31 UTC (rev 6433)
+++ trunk/htmlunit/src/test/java/com/gargoylesoftware/htmlunit/javascript/JavaScriptEngineTest.java	2011-05-26 \
21:59:26 UTC (rev 6434) @@ -1501,7 +1501,6 @@
      * @throws Exception if the test fails
      */
     @Test
-    @NotYetImplemented
     @Alerts("0")
     public void function_toStringValue() throws Exception {
         final String html = "<html><head><title>foo</title><script>\n"

Modified: trunk/htmlunit/src/test/java/com/gargoylesoftware/htmlunit/javascript/NativeArrayTest.java
 ===================================================================
--- trunk/htmlunit/src/test/java/com/gargoylesoftware/htmlunit/javascript/NativeArrayTest.java	2011-05-26 \
                18:42:31 UTC (rev 6433)
+++ trunk/htmlunit/src/test/java/com/gargoylesoftware/htmlunit/javascript/NativeArrayTest.java	2011-05-26 \
21:59:26 UTC (rev 6434) @@ -113,4 +113,21 @@
 
         loadPageWithAlerts2(html);
     }
+
+    /**
+     * @throws Exception if the test fails
+     */
+    @Test
+    @Alerts(FF = "function Array() {\n    [native code]\n}",
+            IE = "\nfunction Array() {\n    [native code]\n}\n")
+    public void constructorToString() throws Exception {
+        final String html
+            = "<html><head><script>\n"
+            + "alert([].constructor.toString());\n"
+            + "</script></head><body>\n"
+            + "</body></html>";
+
+        loadPageWithAlerts2(html);
+    }
+
 }

Modified: trunk/htmlunit/src/test/java/com/gargoylesoftware/htmlunit/javascript/NativeFunctionTest.java
 ===================================================================
--- trunk/htmlunit/src/test/java/com/gargoylesoftware/htmlunit/javascript/NativeFunctionTest.java	2011-05-26 \
                18:42:31 UTC (rev 6433)
+++ trunk/htmlunit/src/test/java/com/gargoylesoftware/htmlunit/javascript/NativeFunctionTest.java	2011-05-26 \
21:59:26 UTC (rev 6434) @@ -97,4 +97,41 @@
 
         loadPageWithAlerts2(html);
     }
+
+    /**
+     * For the first test of this kind, we take a special case to have
+     * correct expectations for IE as IE (at least IE6) seems to just return
+     * the original string.
+     * @throws Exception if the test fails
+     */
+    @Test
+    @Alerts("function anonymous() {\n    var x = 1;\n}")
+    public void newFunctionToString() throws Exception {
+        final String html
+            = "<html><head><title>foo</title><script>\n"
+            + "var f1 = new Function('    var x = 1;');\n"
+            + "alert(f1);\n"
+            + "</script></head><body>\n"
+            + "</body></html>";
+
+        loadPageWithAlerts2(html);
+    }
+
+    /**
+     * @throws Exception if the test fails
+     */
+    @Test
+    @Alerts("function foo() {\n    return 1;\n}")
+    public void functionToString() throws Exception {
+        final String html
+            = "<html><head><title>foo</title><script>\n"
+            + "function foo() {\n"
+            + "    return 1;\n"
+            + "};\n"
+            + "alert(foo);\n"
+            + "</script></head><body>\n"
+            + "</body></html>";
+
+        loadPageWithAlerts2(html);
+    }
 }

Modified: trunk/htmlunit/src/test/java/com/gargoylesoftware/htmlunit/javascript/host/html/HTMLFrameElementTest.java
 ===================================================================
--- trunk/htmlunit/src/test/java/com/gargoylesoftware/htmlunit/javascript/host/html/HTMLFrameElementTest.java	2011-05-26 \
                18:42:31 UTC (rev 6433)
+++ trunk/htmlunit/src/test/java/com/gargoylesoftware/htmlunit/javascript/host/html/HTMLFrameElementTest.java	2011-05-26 \
21:59:26 UTC (rev 6434) @@ -239,7 +239,7 @@
      * @throws Exception if the test fails
      */
     @Test
-    @Alerts({ "\nfunction handler() {\n}\n", "null" })
+    @Alerts({ "function handler() {\n}", "null" })
     public void testOnloadNull() throws Exception {
         final String html =
             "<html><head>\n"


This was sent by the SourceForge.net collaborative development platform, the world's \
largest Open Source development site.

------------------------------------------------------------------------------
vRanger cuts backup time in half-while increasing security.
With the market-leading solution for virtual backup and recovery, 
you get blazing-fast, flexible, and affordable data protection.
Download your free trial now. 
http://p.sf.net/sfu/quest-d2dcopy1
_______________________________________________
HtmlUnit-develop mailing list
HtmlUnit-develop@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/htmlunit-develop


[prev in list] [next in list] [prev in thread] [next in thread] 

Configure | About | News | Add a list | Sponsored by KoreLogic