Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 8 additions & 8 deletions src/main/java/org/htmlunit/WebClient.java
Original file line number Diff line number Diff line change
Expand Up @@ -1386,16 +1386,16 @@ private WebResponse makeWebResponseForDataUrl(final WebRequest webRequest) throw

private static WebResponse makeWebResponseForAboutUrl(final WebRequest webRequest) throws MalformedURLException {
final URL url = webRequest.getUrl();
final String urlString = url.toExternalForm();
if (UrlUtils.ABOUT_BLANK.equalsIgnoreCase(urlString)) {
return new StringWebResponse("", UrlUtils.URL_ABOUT_BLANK);
if (UrlUtils.ABOUT.equals(url.getProtocol())) {
if ("blank".equalsIgnoreCase(url.getPath())) {
if (url.getRef() == null && url.getQuery() == null) {
return new StringWebResponse("", UrlUtils.URL_ABOUT_BLANK);
}
return new StringWebResponse("", url);
}
}

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

/**
Expand Down
14 changes: 11 additions & 3 deletions src/main/java/org/htmlunit/javascript/host/Location.java
Original file line number Diff line number Diff line change
Expand Up @@ -441,10 +441,18 @@ public void setHash(final String oldURL, String hash, final boolean triggerHashC
final boolean hasChanged = hash != null && !hash.equals(hash_);
hash_ = hash;

if (triggerHashChanged && hasChanged) {
if (hasChanged) {
final Window w = getWindow();
final Event event = new HashChangeEvent(w, Event.TYPE_HASH_CHANGE, oldURL, getHref());
w.executeEventLocally(event);
final Page page = w.getWebWindow().getEnclosedPage();
if (page != null) {
final WebRequest request = page.getWebResponse().getWebRequest();
request.setUrl(UrlUtils.toUrlSafe(getHref()));
}

if (triggerHashChanged) {
final Event event = new HashChangeEvent(w, Event.TYPE_HASH_CHANGE, oldURL, getHref());
w.executeEventLocally(event);
}
}
}

Expand Down
42 changes: 41 additions & 1 deletion src/test/java/org/htmlunit/javascript/host/LocationTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -295,7 +295,7 @@ public void setHash() throws Exception {

// Verify that we didn't reload the page.
assertTrue(page == page2);
assertEquals(URL_FIRST, conn.getLastWebRequest().getUrl());
assertEquals(1, conn.getRequestCount());
}

/**
Expand Down Expand Up @@ -382,6 +382,46 @@ public void locationWithTarget() throws Exception {
assertEquals(getExpectedAlerts(), alerts);
}

/**
* @throws Exception if the test fails
*/
@Test
public void setHashUpdatesPageUrl() throws Exception {
final WebClient webClient = getWebClient();
final MockWebConnection conn = new MockWebConnection();

final String html = DOCTYPE_HTML
+ "<html><head><title>Test</title></head><body>\n"
+ "<a id='a' onclick='location.hash=\"newHash\"'>go</a>\n"
+ "</body></html>";

conn.setResponse(URL_FIRST, html);
webClient.setWebConnection(conn);

final HtmlPage page = webClient.getPage(URL_FIRST);
assertNull(page.getUrl().getRef());

page.getHtmlElementById("a").click();
assertEquals("newHash", page.getUrl().getRef());
}

/**
* @throws Exception if the test fails
*/
@Test
public void setHashAboutBlank() throws Exception {
try (WebClient webClient = new WebClient()) {
HtmlPage page = webClient.getPage("about:blank");
assertNull(page.getUrl().getRef());

page.executeJavaScript("location.hash = 'foo'");
assertEquals("foo", page.getUrl().getRef());

page = (HtmlPage) page.refresh();
assertEquals("foo", page.getUrl().getRef());
}
}

/**
* @throws Exception if an error occurs
*/
Expand Down
Loading