SF.net SVN: htmlunit: [1235] trunk/htmlunit/src

classic Classic list List threaded Threaded
1 message Options
Reply | Threaded
Open this post in threaded view
|

SF.net SVN: htmlunit: [1235] trunk/htmlunit/src

mguillem
Revision: 1235
          http://svn.sourceforge.net/htmlunit/?rev=1235&view=rev
Author:   mguillem
Date:     2006-09-17 08:15:57 -0700 (Sun, 17 Sep 2006)

Log Message:
-----------
JavaScript: allow script to set a "javascript:..." location (#1345136)

Modified Paths:
--------------
    trunk/htmlunit/src/java/com/gargoylesoftware/htmlunit/javascript/host/Window.java
    trunk/htmlunit/src/test/java/com/gargoylesoftware/htmlunit/javascript/host/LocationTest.java
    trunk/htmlunit/src/xdocs/changes.xml

Modified: trunk/htmlunit/src/java/com/gargoylesoftware/htmlunit/javascript/host/Window.java
===================================================================
--- trunk/htmlunit/src/java/com/gargoylesoftware/htmlunit/javascript/host/Window.java 2006-09-11 13:07:31 UTC (rev 1234)
+++ trunk/htmlunit/src/java/com/gargoylesoftware/htmlunit/javascript/host/Window.java 2006-09-17 15:15:57 UTC (rev 1235)
@@ -384,26 +384,33 @@
      * @throws IOException when location loading fails
      */
     public void jsxSet_location( final String newLocation ) throws IOException {
-        try {
-            // url should be resolved from the page in which the js is executed
-            // cf test FrameTest#testLocation
-            final HtmlPage page = (HtmlPage) getWindow(getStartingScope()).getWebWindow().getEnclosedPage();
-            final URL url = page.getFullyQualifiedUrl(newLocation);
+        // url should be resolved from the page in which the js is executed
+        // cf test FrameTest#testLocation
+        final HtmlPage page = (HtmlPage) getWindow(getStartingScope()).getWebWindow().getEnclosedPage();
 
-            getLog().debug(
-                "window.location=" + newLocation + " (" + url.toExternalForm()
-                        + "), for window named '" + webWindow_.getName() + "'");
-
-            webWindow_.getWebClient().getPage(webWindow_, new WebRequestSettings(url));
+        if (newLocation.startsWith("javascript:")) {
+            final String script = newLocation.substring(11);
+            page.executeJavaScriptIfPossible(script, "new location value", false, null);
         }
-        catch( final MalformedURLException e ) {
-            getLog().error("jsxSet_location(\""+newLocation+"\") Got MalformedURLException", e);
-            throw e;
+        else {
+            try {
+                final URL url = page.getFullyQualifiedUrl(newLocation);
+    
+                getLog().debug(
+                    "window.location=" + newLocation + " (" + url.toExternalForm()
+                            + "), for window named '" + webWindow_.getName() + "'");
+    
+                webWindow_.getWebClient().getPage(webWindow_, new WebRequestSettings(url));
+            }
+            catch( final MalformedURLException e ) {
+                getLog().error("jsxSet_location(\""+newLocation+"\") Got MalformedURLException", e);
+                throw e;
+            }
+            catch( final IOException e ) {
+                getLog().error("jsxSet_location(\""+newLocation+"\") Got IOException", e);
+                throw e;
+            }
         }
-        catch( final IOException e ) {
-            getLog().error("jsxSet_location(\""+newLocation+"\") Got IOException", e);
-            throw e;
-        }
     }
 
 

Modified: trunk/htmlunit/src/test/java/com/gargoylesoftware/htmlunit/javascript/host/LocationTest.java
===================================================================
--- trunk/htmlunit/src/test/java/com/gargoylesoftware/htmlunit/javascript/host/LocationTest.java 2006-09-11 13:07:31 UTC (rev 1234)
+++ trunk/htmlunit/src/test/java/com/gargoylesoftware/htmlunit/javascript/host/LocationTest.java 2006-09-17 15:15:57 UTC (rev 1235)
@@ -521,4 +521,23 @@
         assertEquals("bla bla", page.getWebResponse().getContentAsString());
     }
 
+    /**
+     * @throws Exception if the test fails
+     */
+    public void testJSLocation() throws Exception {
+
+        final String html =
+              "<html><head>\n"
+            + "  <script>\n"
+            + "      document.location.href = 'javascript:alert(\"foo\")';\n"
+            + "  </script>\n"
+            + "</head>\n"
+            + "<body></body></html>";
+
+        final String[] expectedAlerts = { "foo" };
+        final List collectedAlerts = new ArrayList();
+        loadPage(html, collectedAlerts);
+        
+        assertEquals(expectedAlerts, collectedAlerts);
+    }
 }

Modified: trunk/htmlunit/src/xdocs/changes.xml
===================================================================
--- trunk/htmlunit/src/xdocs/changes.xml 2006-09-11 13:07:31 UTC (rev 1234)
+++ trunk/htmlunit/src/xdocs/changes.xml 2006-09-17 15:15:57 UTC (rev 1235)
@@ -7,6 +7,9 @@
 
     <body>
         <release version="1.10-prerelease">
+            <action type="update" dev="mguillem" id="1345136">
+                JavaScript: allow script to set a "javascript:..." location.
+            </action>
             <action type="update" dev="mguillem" id="1495132">
                 JavaScript: use of HTMLElement.innerHTML should not cause registration of a WebWindow.
             </action>


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

-------------------------------------------------------------------------
Using Tomcat but need to do more? Need to support web services, security?
Get stuff done quickly with pre-integrated technology to make your job easier
Download IBM WebSphere Application Server v.1.0.1 based on Apache Geronimo
http://sel.as-us.falkag.net/sel?cmd=lnk&kid=120709&bid=263057&dat=121642
_______________________________________________
HtmlUnit-develop mailing list
[hidden email]
https://lists.sourceforge.net/lists/listinfo/htmlunit-develop