Skip to content

Commit 5f73df3

Browse files
duonglaiquangrbri
authored andcommitted
WebClient: fix MalformedURLException when setting hash for about:blank
1 parent ee053eb commit 5f73df3

2 files changed

Lines changed: 25 additions & 8 deletions

File tree

src/main/java/org/htmlunit/WebClient.java

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1386,16 +1386,16 @@ private WebResponse makeWebResponseForDataUrl(final WebRequest webRequest) throw
13861386

13871387
private static WebResponse makeWebResponseForAboutUrl(final WebRequest webRequest) throws MalformedURLException {
13881388
final URL url = webRequest.getUrl();
1389-
final String urlString = url.toExternalForm();
1390-
if (UrlUtils.ABOUT_BLANK.equalsIgnoreCase(urlString)) {
1391-
return new StringWebResponse("", UrlUtils.URL_ABOUT_BLANK);
1389+
if (UrlUtils.ABOUT.equals(url.getProtocol())) {
1390+
if ("blank".equalsIgnoreCase(url.getPath())) {
1391+
if (url.getRef() == null && url.getQuery() == null) {
1392+
return new StringWebResponse("", UrlUtils.URL_ABOUT_BLANK);
1393+
}
1394+
return new StringWebResponse("", url);
1395+
}
13921396
}
13931397

1394-
final String urlWithoutQuery = StringUtils.substringBefore(urlString, "?");
1395-
if (!"blank".equalsIgnoreCase(StringUtils.substringAfter(urlWithoutQuery, UrlUtils.ABOUT_SCHEME))) {
1396-
throw new MalformedURLException(url + " is not supported, only about:blank is supported at the moment.");
1397-
}
1398-
return new StringWebResponse("", url);
1398+
throw new MalformedURLException(url + " is not supported, only about:blank is supported at the moment.");
13991399
}
14001400

14011401
/**

src/test/java/org/htmlunit/javascript/host/LocationTest.java

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -405,6 +405,23 @@ public void setHashUpdatesPageUrl() throws Exception {
405405
assertEquals("newHash", page.getUrl().getRef());
406406
}
407407

408+
/**
409+
* @throws Exception if the test fails
410+
*/
411+
@Test
412+
public void setHashAboutBlank() throws Exception {
413+
try (WebClient webClient = new WebClient()) {
414+
HtmlPage page = webClient.getPage("about:blank");
415+
assertNull(page.getUrl().getRef());
416+
417+
page.executeJavaScript("location.hash = 'foo'");
418+
assertEquals("foo", page.getUrl().getRef());
419+
420+
page = (HtmlPage) page.refresh();
421+
assertEquals("foo", page.getUrl().getRef());
422+
}
423+
}
424+
408425
/**
409426
* @throws Exception if an error occurs
410427
*/

0 commit comments

Comments
 (0)