Skip to content
Merged
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
6 changes: 6 additions & 0 deletions src/changes/changes.xml
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,12 @@

<body>
<release version="5.9.0" date="xxxx xx, 2026" description="Firefox 160, Chrome/Edge 158, Fetch, Intl, CSP, CORS, Cookies, Bugfixes">
<action type="add" dev="Ronny Shapiro">
New option WebClientOptions.setHtmlFragmentSelfClosingTagsAllowed(); when enabled, the fragment parser
of html pages (e.g. setInnerHtml()) closes a non void element or iframe written as empty-element
tag (e.g. &lt;h3/&gt;) like up to version 5.5.0. This allows applications to read back markup written by
asXml() before 5.8.0. The option is disabled by default (like the browsers).
</action>
<action type="add" dev="rbri">
New option WebClientOptions.setLocalFileAccessFromRemotePagesAllowed(boolean) (default false).
If enabled, pages that were not loaded from a file are allowed again to load local files (file urls)
Expand Down
24 changes: 24 additions & 0 deletions src/main/java/org/htmlunit/WebClientOptions.java
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,7 @@ public class WebClientOptions implements Serializable {

private boolean isFetchPolyfillEnabled_;

private boolean htmlFragmentSelfClosingTagsAllowed_;
private boolean contentSecurityPolicyReportsEnabled_ = true;

private boolean localFileAccessFromRemotePagesAllowed_;
Expand Down Expand Up @@ -959,6 +960,29 @@ public boolean isFetchPolyfillEnabled() {
return isFetchPolyfillEnabled_;
}

/**
* Sets whether the fragment parser of html pages (e.g. {@code setInnerHtml()}) closes a non void element
* or iframe written as empty-element tag (e.g. {@code <h3/>}), like up to version 5.5.0. By default the
* trailing slash is ignored like in the browsers. A self-closed script is not affected.
* Enable this to read back markup written by {@code asXml()} before 5.8.0, which wrote empty elements
* like {@code <h3/>}. XHTML pages always honor the trailing slash.
*
* @param allowed {@code true} to close a non void element written as empty-element tag
*/
public void setHtmlFragmentSelfClosingTagsAllowed(final boolean allowed) {
htmlFragmentSelfClosingTagsAllowed_ = allowed;
}

/**
* Returns whether the fragment parser of html pages closes a non void element written as empty-element tag.
*
* @return {@code true} if the fragment parser of html pages closes a non void element written as
* empty-element tag
*/
public boolean isHtmlFragmentSelfClosingTagsAllowed() {
return htmlFragmentSelfClosingTagsAllowed_;
}

/**
* Enables/disables Geolocation support. By default, this property is disabled.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -105,8 +105,9 @@ public void parseFragment(final WebClient webClient, final DomNode parent, final
domBuilder.setInsideSvg(isSvgContext(context));

final boolean xhtml = page instanceof XHtmlPage;
domBuilder.setFeature(HTMLScanner.ALLOW_SELFCLOSING_TAGS, xhtml);
domBuilder.setFeature(HTMLScanner.ALLOW_SELFCLOSING_IFRAME, xhtml);
final boolean selfClosing = xhtml || webClient.getOptions().isHtmlFragmentSelfClosingTagsAllowed();
domBuilder.setFeature(HTMLScanner.ALLOW_SELFCLOSING_TAGS, selfClosing);
domBuilder.setFeature(HTMLScanner.ALLOW_SELFCLOSING_IFRAME, selfClosing);
domBuilder.setFeature(HTMLScanner.ALLOW_SELFCLOSING_SCRIPT, xhtml);

// build fragment context stack
Expand Down
32 changes: 32 additions & 0 deletions src/test/java/org/htmlunit/WebClient8Test.java
Original file line number Diff line number Diff line change
Expand Up @@ -176,6 +176,38 @@ public void asXmlEmptySvgElements() throws Exception {
}
}

/**
* @throws Exception if something goes wrong
*/
@Test
public void parseFragmentIgnoresTrailingSlashOfNonVoidElement() throws Exception {
final String html = DOCTYPE_HTML + "<html><body><div id='copy'></div></body></html>";

try (WebClient webClient = new WebClient(getBrowserVersion(), false, null, -1)) {
final HtmlPage page = loadPage(webClient, html, null, URL_FIRST);
final DomElement copy = page.getElementById("copy");
copy.setInnerHtml("<h3/><p>x</p>");
assertEquals("<div id=\"copy\"><h3><p>x</p></h3></div>", copy.asXml());
}
}

/**
* @throws Exception if something goes wrong
*/
@Test
public void parseFragmentHtmlFragmentSelfClosingTagsAllowed() throws Exception {
final String html = DOCTYPE_HTML + "<html><body><div id='copy'></div></body></html>";

try (WebClient webClient = new WebClient(getBrowserVersion(), false, null, -1)) {
webClient.getOptions().setHtmlFragmentSelfClosingTagsAllowed(true);
final HtmlPage page = loadPage(webClient, html, null, URL_FIRST);
final DomElement copy = page.getElementById("copy");
// markup written by asXml() before 5.8.0
copy.setInnerHtml("<h3/><p>x</p><iframe/><p>y</p><br/>");
assertEquals("<div id=\"copy\"><h3></h3><p>x</p><iframe></iframe><p>y</p><br/></div>", copy.asXml());
}
}

/**
* @throws Exception if something goes wrong
*/
Expand Down
5 changes: 5 additions & 0 deletions src/test/java/org/htmlunit/WebClientOptionsTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,8 @@ public void serialization() throws Exception {

assertEquals(original.isFetchPolyfillEnabled(), deserialized.isFetchPolyfillEnabled());

assertEquals(original.isHtmlFragmentSelfClosingTagsAllowed(),
deserialized.isHtmlFragmentSelfClosingTagsAllowed());
assertEquals(original.isContentSecurityPolicyReportsEnabled(),
deserialized.isContentSecurityPolicyReportsEnabled());
assertEquals(original.isLocalFileAccessFromRemotePagesAllowed(),
Expand Down Expand Up @@ -101,6 +103,7 @@ public void serializationChanged() throws Exception {
original.setWebSocketMaxBinaryMessageSize(44);

original.setFetchPolyfillEnabled(true);
original.setHtmlFragmentSelfClosingTagsAllowed(true);

original.setContentSecurityPolicyReportsEnabled(false);
original.setLocalFileAccessFromRemotePagesAllowed(true);
Expand Down Expand Up @@ -139,6 +142,8 @@ public void serializationChanged() throws Exception {

assertEquals(original.isFetchPolyfillEnabled(), deserialized.isFetchPolyfillEnabled());

assertEquals(original.isHtmlFragmentSelfClosingTagsAllowed(),
deserialized.isHtmlFragmentSelfClosingTagsAllowed());
assertEquals(original.isContentSecurityPolicyReportsEnabled(),
deserialized.isContentSecurityPolicyReportsEnabled());
assertEquals(original.isLocalFileAccessFromRemotePagesAllowed(),
Expand Down
Loading