Skip to content

Commit

Permalink
solves #665: Add option to disable the loading of frames
Browse files Browse the repository at this point in the history
  • Loading branch information
Roy Teeuwen committed Nov 15, 2023
1 parent aa30b59 commit 3bb8d98
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 1 deletion.
13 changes: 13 additions & 0 deletions src/main/java/org/htmlunit/WebClientOptions.java
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,7 @@ public class WebClientOptions implements Serializable {
private int webSocketMaxBinaryMessageBufferSize_ = -1;

private boolean isFetchPolyfillEnabled_;
private boolean isLoadFramesEnabled_ = true;

/**
* If set to {@code true}, the client will accept connections to any host, regardless of
Expand Down Expand Up @@ -858,4 +859,16 @@ public void setFetchPolyfillEnabled(final boolean enabled) {
public boolean isFetchPolyfillEnabled() {
return isFetchPolyfillEnabled_;
}

public boolean isLoadFramesEnabled() {
return isLoadFramesEnabled_;
}

/**
* Sets whether or not fetch frames in the HTML page as separate requests
* @param enabled true to enable loading of frames
*/
public void setLoadFramesEnabled(boolean enabled) {
this.isLoadFramesEnabled_ = enabled;
}
}
4 changes: 3 additions & 1 deletion src/main/java/org/htmlunit/html/HtmlPage.java
Original file line number Diff line number Diff line change
Expand Up @@ -281,7 +281,9 @@ public void initialize() throws IOException, FailingHttpStatusCodeException {

executeEventHandlersIfNeeded(Event.TYPE_DOM_DOCUMENT_LOADED);

loadFrames();
if (getWebClient().getOptions().isLoadFramesEnabled()) {
loadFrames();
}

// don't set the ready state if we really load the blank page into the window
// see Node.initInlineFrameIfNeeded()
Expand Down
25 changes: 25 additions & 0 deletions src/test/java/org/htmlunit/html/HtmlPageTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -1971,4 +1971,29 @@ public void getBaseUrl() throws Exception {
page = loadPage(getBrowserVersion(), html, null, new URL(URL_FIRST.toString() + path));
assertEquals(URL_FIRST.toExternalForm() + path, page.getBaseURL().toExternalForm());
}

/**
* @throws Exception if the test fails
*/
@Test
public void framesAreNotLoadedWhenDisabled() throws Exception {
final String firstContent
= "<html><head><title>First</title></head><body>\n"
+ "<iframe id='iframe1' src='" + URL_SECOND + "'>\n"
+ "</body></html>";
final String secondContent = "<html><head><title>Second</title></head><body></body></html>";
final WebClient client = getWebClientWithMockWebConnection();
client.getOptions().setLoadFramesEnabled(false);

final MockWebConnection webConnection = getMockWebConnection();
webConnection.setResponse(URL_FIRST, firstContent);
webConnection.setResponse(URL_SECOND, secondContent);

final HtmlPage page = client.getPage(URL_FIRST);
assertEquals("First", page.getTitleText());

final HtmlInlineFrame iframe = page.getHtmlElementById("iframe1");
assertEquals(URL_SECOND.toExternalForm(), iframe.getSrcAttribute());
assertNull(((HtmlPage) iframe.getEnclosedPage()).getTextContent());
}
}

0 comments on commit 3bb8d98

Please sign in to comment.