Skip to content

Commit 8e3d9cf

Browse files
committed
improve code a bit
1 parent e057da0 commit 8e3d9cf

3 files changed

Lines changed: 72 additions & 23 deletions

File tree

src/main/java/org/htmlunit/javascript/HtmlUnitScriptable.java

Lines changed: 0 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -342,22 +342,6 @@ protected static WindowOrWorkerGlobalScope getWindowOrWorkerGlobalScope(
342342
throw new RuntimeException("Unable to find WindowOrWorkerGlobalScope associated with " + s);
343343
}
344344

345-
/**
346-
* <span style="color:red">INTERNAL API - SUBJECT TO CHANGE AT ANY TIME - USE AT YOUR OWN RISK.</span><br>
347-
*
348-
* @return the window that is set as the top call scope
349-
*/
350-
protected static Window getWindowFromTopCallScope() throws RuntimeException {
351-
final Scriptable top = JavaScriptEngine.getTopCallScope();
352-
if (top instanceof TopLevel topLevel) {
353-
final ScriptableObject globalThis = topLevel.getGlobalThis();
354-
if (globalThis instanceof Window window) {
355-
return window;
356-
}
357-
}
358-
throw new RuntimeException("Unable to find window in scope");
359-
}
360-
361345
/**
362346
* Gets the browser version currently used.
363347
* @return the browser version

src/main/java/org/htmlunit/javascript/host/Location.java

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@
3535
import org.htmlunit.corejs.javascript.VarScope;
3636
import org.htmlunit.html.HtmlPage;
3737
import org.htmlunit.javascript.HtmlUnitScriptable;
38+
import org.htmlunit.javascript.JavaScriptEngine;
3839
import org.htmlunit.javascript.configuration.JsxClass;
3940
import org.htmlunit.javascript.configuration.JsxConstructor;
4041
import org.htmlunit.javascript.host.event.Event;
@@ -314,25 +315,29 @@ public String getHref() {
314315
* @see <a href="http://msdn.microsoft.com/en-us/library/ms533867.aspx">MSDN Documentation</a>
315316
*/
316317
public void setHref(final String newLocation) throws IOException {
317-
WebWindow webWindow = getWindowFromTopCallScope().getWebWindow();
318-
final HtmlPage page = (HtmlPage) webWindow.getEnclosedPage();
319318
if (newLocation.startsWith(JavaScriptURLConnection.JAVASCRIPT_PREFIX)) {
320319
final String script = newLocation.substring(11);
320+
final HtmlPage page = (HtmlPage) window_.getWebWindow().getEnclosedPage();
321321
page.executeJavaScript(script, "new location value", 1);
322322
return;
323323
}
324+
324325
try {
326+
// we need to get the caller window to get the calling page
327+
WebWindow webWindow = ((Window) JavaScriptEngine.getTopCallScope().getGlobalThis()).getWebWindow();
328+
final HtmlPage callingPage = (HtmlPage) webWindow.getEnclosedPage();
329+
325330
final BrowserVersion browserVersion = webWindow.getWebClient().getBrowserVersion();
326331

327-
URL url = page.getFullyQualifiedUrl(newLocation);
332+
URL url = callingPage.getFullyQualifiedUrl(newLocation);
328333
// fix for empty url
329334
if (StringUtils.isEmptyOrNull(newLocation)) {
330335
url = UrlUtils.getUrlWithNewRef(url, null);
331336
}
332337

333338
final WebRequest request = new WebRequest(url,
334339
browserVersion.getHtmlAcceptHeader(), browserVersion.getAcceptEncodingHeader());
335-
request.setRefererHeader(page.getUrl());
340+
request.setRefererHeader(callingPage.getUrl());
336341

337342
webWindow = window_.getWebWindow();
338343
webWindow.getWebClient().download(webWindow, "", request, true, null, "JS set location");

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

Lines changed: 63 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1093,7 +1093,65 @@ public void refererHeaderWhenSettingLocation() throws Exception {
10931093
* @throws Exception if the test fails
10941094
*/
10951095
@Test
1096-
@Alerts("§§URL§§menu.html")
1096+
@Alerts({"§§URL§§path1/newContent.html", "§§URL§§path1/menu.html"})
1097+
public void settingRelativeLocation() throws Exception {
1098+
final String html = DOCTYPE_HTML
1099+
+ "<html><head><title>Frameset</title></head>\n"
1100+
+ "<frameset rows='20%,80%'>\n"
1101+
+ " <frame src='path1/menu.html' name='menu'>\n"
1102+
+ " <frame src='path2/content.html' name='content'>\n"
1103+
+ "</frameset></html>";
1104+
1105+
final String menu = DOCTYPE_HTML
1106+
+ "<html><head><title>Menu</title></head>\n"
1107+
+ "<body>\n"
1108+
+ " <a id='link' href='newContent.html' target='content'>Link</a>\n"
1109+
+ " <a id='jsLink' href='#' "
1110+
+ "onclick=\"javascript:top.content.location='newContent.html';\">jsLink</a>\n"
1111+
+ "</body></html>";
1112+
1113+
final String content = DOCTYPE_HTML
1114+
+ "<html><head><title>Content</title></head><body><p>content</p></body></html>";
1115+
final String newContent = DOCTYPE_HTML
1116+
+ "<html><head><title>New Content</title></head><body><p>new content</p></body></html>";
1117+
1118+
final MockWebConnection conn = getMockWebConnection();
1119+
conn.setResponse(new URL(URL_FIRST, "path1/menu.html"), menu);
1120+
conn.setResponse(new URL(URL_FIRST, "path2/content.html"), content);
1121+
conn.setResponse(new URL(URL_FIRST, "path1/newContent.html"), newContent);
1122+
1123+
expandExpectedAlertsVariables(URL_FIRST);
1124+
final WebDriver driver = loadPage2(html);
1125+
1126+
assertEquals(3, conn.getRequestCount());
1127+
1128+
// click an anchor with href and target
1129+
driver.switchTo().frame(0);
1130+
driver.findElement(By.id("link")).click();
1131+
if (useRealBrowser()) {
1132+
Thread.sleep(400);
1133+
}
1134+
assertEquals(4, conn.getRequestCount());
1135+
assertEquals(getExpectedAlerts()[0], conn.getLastWebRequest().getUrl());
1136+
Map<String, String> lastAdditionalHeaders = conn.getLastAdditionalHeaders();
1137+
assertEquals(getExpectedAlerts()[1], lastAdditionalHeaders.get(HttpHeader.REFERER));
1138+
1139+
// click an anchor with onclick which sets frame.location
1140+
driver.findElement(By.id("jsLink")).click();
1141+
if (useRealBrowser()) {
1142+
Thread.sleep(400);
1143+
}
1144+
assertEquals(5, conn.getRequestCount());
1145+
assertEquals(getExpectedAlerts()[0], conn.getLastWebRequest().getUrl());
1146+
lastAdditionalHeaders = conn.getLastAdditionalHeaders();
1147+
assertEquals(getExpectedAlerts()[1], lastAdditionalHeaders.get(HttpHeader.REFERER));
1148+
}
1149+
1150+
/**
1151+
* @throws Exception if the test fails
1152+
*/
1153+
@Test
1154+
@Alerts({"§§URL§§newContent.html", "§§URL§§menu.html"})
10971155
public void refererHeaderWhenSettingFrameLocation() throws Exception {
10981156
final String html = DOCTYPE_HTML
10991157
+ "<html><head><title>Frameset</title></head>\n"
@@ -1132,17 +1190,19 @@ public void refererHeaderWhenSettingFrameLocation() throws Exception {
11321190
Thread.sleep(400);
11331191
}
11341192
assertEquals(4, conn.getRequestCount());
1193+
assertEquals(getExpectedAlerts()[0], conn.getLastWebRequest().getUrl());
11351194
Map<String, String> lastAdditionalHeaders = conn.getLastAdditionalHeaders();
1136-
assertEquals(getExpectedAlerts()[0], lastAdditionalHeaders.get(HttpHeader.REFERER));
1195+
assertEquals(getExpectedAlerts()[1], lastAdditionalHeaders.get(HttpHeader.REFERER));
11371196

11381197
// click an anchor with onclick which sets frame.location
11391198
driver.findElement(By.id("jsLink")).click();
11401199
if (useRealBrowser()) {
11411200
Thread.sleep(400);
11421201
}
11431202
assertEquals(5, conn.getRequestCount());
1203+
assertEquals(getExpectedAlerts()[0], conn.getLastWebRequest().getUrl());
11441204
lastAdditionalHeaders = conn.getLastAdditionalHeaders();
1145-
assertEquals(getExpectedAlerts()[0], lastAdditionalHeaders.get(HttpHeader.REFERER));
1205+
assertEquals(getExpectedAlerts()[1], lastAdditionalHeaders.get(HttpHeader.REFERER));
11461206
}
11471207

11481208
/**

0 commit comments

Comments
 (0)