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

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

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

rbri
Revision: 14143
          http://sourceforge.net/p/htmlunit/code/14143
Author:   rbri
Date:     2017-04-20 20:24:33 +0000 (Thu, 20 Apr 2017)
Log Message:
-----------
document.baseURI calculation fixed for relative href

Issue 1870

Modified Paths:
--------------
    trunk/htmlunit/src/changes/changes.xml
    trunk/htmlunit/src/main/java/com/gargoylesoftware/htmlunit/html/HtmlPage.java
    trunk/htmlunit/src/test/java/com/gargoylesoftware/htmlunit/javascript/host/html/HTMLDocumentTest.java

Modified: trunk/htmlunit/src/changes/changes.xml
===================================================================
--- trunk/htmlunit/src/changes/changes.xml 2017-04-20 13:11:42 UTC (rev 14142)
+++ trunk/htmlunit/src/changes/changes.xml 2017-04-20 20:24:33 UTC (rev 14143)
@@ -8,6 +8,9 @@
 
     <body>
         <release version="2.27" date="???" description="GAE broken, Bugfixes">
+            <action type="fix" dev="rbri" issue="1870" due-to="Joerg Werner">
+                JavaScript: document.baseURI calculation fixed for relative href.
+            </action>
             <action type="fix" dev="asashour" issue="43482935" system="stackoverflow">
                 JavaScript: fix setting .innerHTML.
             </action>

Modified: trunk/htmlunit/src/main/java/com/gargoylesoftware/htmlunit/html/HtmlPage.java
===================================================================
--- trunk/htmlunit/src/main/java/com/gargoylesoftware/htmlunit/html/HtmlPage.java 2017-04-20 13:11:42 UTC (rev 14142)
+++ trunk/htmlunit/src/main/java/com/gargoylesoftware/htmlunit/html/HtmlPage.java 2017-04-20 20:24:33 UTC (rev 14143)
@@ -91,6 +91,7 @@
 import com.gargoylesoftware.htmlunit.javascript.host.event.Event2;
 import com.gargoylesoftware.htmlunit.javascript.host.html.HTMLDocument;
 import com.gargoylesoftware.htmlunit.protocol.javascript.JavaScriptURLConnection;
+import com.gargoylesoftware.htmlunit.util.UrlUtils;
 import com.gargoylesoftware.js.nashorn.api.scripting.ScriptObjectMirror;
 import com.gargoylesoftware.js.nashorn.internal.objects.Global;
 import com.gargoylesoftware.js.nashorn.internal.runtime.ScriptFunction;
@@ -2376,7 +2377,7 @@
                         baseUrl = new URL(String.format("%s%s", url.toString(), href));
                     }
                     else {
-                        baseUrl = new URL(String.format("%s/%s", url.toString(), href));
+                        baseUrl = new URL(UrlUtils.resolveUrl(url, href));
                     }
                 }
                 catch (final MalformedURLException e) {

Modified: trunk/htmlunit/src/test/java/com/gargoylesoftware/htmlunit/javascript/host/html/HTMLDocumentTest.java
===================================================================
--- trunk/htmlunit/src/test/java/com/gargoylesoftware/htmlunit/javascript/host/html/HTMLDocumentTest.java 2017-04-20 13:11:42 UTC (rev 14142)
+++ trunk/htmlunit/src/test/java/com/gargoylesoftware/htmlunit/javascript/host/html/HTMLDocumentTest.java 2017-04-20 20:24:33 UTC (rev 14143)
@@ -21,6 +21,7 @@
 import static java.nio.charset.StandardCharsets.UTF_8;
 import static org.junit.Assert.fail;
 
+import java.net.URL;
 import java.text.ParseException;
 import java.text.SimpleDateFormat;
 import java.util.ArrayList;
@@ -2325,9 +2326,12 @@
     @Alerts(DEFAULT = "§§URL§§",
             IE = "undefined")
     public void baseURI_noBaseTag() throws Exception {
-        final String html = "<html><body><script>\n"
-            + "alert(document.baseURI);\n"
-            + "</script></body></html>";
+        final String html = "<html>\n"
+                + "<body>\n"
+                + "<script>\n"
+                + "  alert(document.baseURI);\n"
+                + "</script>\n"
+                + "</body></html>";
 
         loadPageWithAlerts2(html);
     }
@@ -2339,12 +2343,14 @@
     @Alerts(DEFAULT = "http://myotherwebsite.com/foo",
             IE = "undefined")
     public void baseURI_withBaseTag() throws Exception {
-        final String html = "<html><head>\n"
-            + "<base href='http://myotherwebsite.com/foo'>\n"
-            + "</head>\n"
-            + "<body><script>\n"
-            + "alert(document.baseURI);\n"
-            + "</script></body></html>";
+        final String html = "<html>\n"
+                + "<head>\n"
+                + "  <base href='http://myotherwebsite.com/foo'>\n"
+                + "</head>\n"
+                + "<body>\n"
+                + "<script>\n"
+                + "  alert(document.baseURI);\n"
+                + "</script></body></html>";
 
         loadPageWithAlerts2(html);
     }
@@ -2356,11 +2362,13 @@
     @Alerts(DEFAULT = "http://myotherwebsite.com/foo",
             IE = "undefined")
     public void baseURI_withBaseTagInBody() throws Exception {
-        final String html = "<html><body>\n"
-            + "<base href='http://myotherwebsite.com/foo'>\n"
-            + "<script>\n"
-            + "alert(document.baseURI);\n"
-            + "</script></body></html>";
+        final String html = "<html>\n"
+                + "<body>\n"
+                + "<base href='http://myotherwebsite.com/foo'>\n"
+                + "<script>\n"
+                + "  alert(document.baseURI);\n"
+                + "</script>\n"
+                + "</body></html>";
 
         loadPageWithAlerts2(html);
     }
@@ -2369,6 +2377,106 @@
      * @throws Exception if the test fails
      */
     @Test
+    @Alerts(DEFAULT = "http://localhost:12345/img/",
+            IE = "undefined")
+    public void baseURI_withBaseTag_absolutePath() throws Exception {
+        final String html = "<html>\n"
+                + "<head>\n"
+                + "  <base href='/img/'>\n"
+                + "</head>\n"
+                + "<body>\n"
+                + "<script>\n"
+                + "  alert(document.baseURI);\n"
+                + "</script>\n"
+                + "</body></html>";
+
+        loadPageWithAlerts2(html, new URL("http://localhost:12345/path/to/page.html"));
+    }
+
+    /**
+     * @throws Exception if the test fails
+     */
+    @Test
+    @Alerts(DEFAULT = "http://localhost:12345/path/to/img",
+            IE = "undefined")
+    public void baseURI_withBaseTag_relativePath() throws Exception {
+        final String html = "<html>\n"
+                + "<head>\n"
+                + "  <base href='img'>\n"
+                + "</head>\n"
+                + "<body>\n"
+                + "<script>\n"
+                + "  alert(document.baseURI);\n"
+                + "</script>\n"
+                + "</body></html>";
+
+        loadPageWithAlerts2(html, new URL("http://localhost:12345/path/to/page.html"));
+    }
+
+    /**
+     * @throws Exception if the test fails
+     */
+    @Test
+    @Alerts(DEFAULT = "http://localhost:12345/path/to/img/",
+            IE = "undefined")
+    public void baseURI_withBaseTag_relativePath_slash() throws Exception {
+        final String html = "<html>\n"
+                + "<head>\n"
+                + "  <base href='img/'>\n"
+                + "</head>\n"
+                + "<body>\n"
+                + "<script>\n"
+                + "  alert(document.baseURI);\n"
+                + "</script>\n"
+                + "</body></html>";
+
+        loadPageWithAlerts2(html, new URL("http://localhost:12345/path/to/page.html"));
+    }
+
+    /**
+     * @throws Exception if the test fails
+     */
+    @Test
+    @Alerts(DEFAULT = "http://localhost:12345/path/img",
+            IE = "undefined")
+    public void baseURI_withBaseTag_relativePath_parent() throws Exception {
+        final String html = "<html>\n"
+                + "<head>\n"
+                + "  <base href='../img'>\n"
+                + "</head>\n"
+                + "<body>\n"
+                + "<script>\n"
+                + "  alert(document.baseURI);\n"
+                + "</script>\n"
+                + "</body></html>";
+
+        loadPageWithAlerts2(html, new URL("http://localhost:12345/path/to/page.html"));
+    }
+
+    /**
+     * @throws Exception if the test fails
+     */
+    @Test
+    @Alerts(DEFAULT = "http://localhost:12345/img",
+            IE = "undefined")
+    public void baseURI_withBaseTag_relativePath_strange() throws Exception {
+        final String html = "<html>\n"
+                + "<head>\n"
+                + "  <base href='../../../../img'>\n"
+                + "</head>\n"
+                + "<body>\n"
+                + "<script>\n"
+                + "  alert(document.baseURI);\n"
+                + "</script>\n"
+                + "</body></html>";
+
+        loadPageWithAlerts2(html, new URL("http://localhost:12345/path/to/page.html"));
+    }
+
+    /**
+     * @throws Exception if the test fails
+     */
+    @Test
     @Alerts("true")
     @NotYetImplemented
     public void hasFocus() throws Exception {


------------------------------------------------------------------------------
Check out the vibrant tech community on one of the world's most
engaging tech sites, Slashdot.org! http://sdm.link/slashdot
_______________________________________________
HtmlUnit-develop mailing list
[hidden email]
https://lists.sourceforge.net/lists/listinfo/htmlunit-develop