diff --git a/src/changes/changes.xml b/src/changes/changes.xml index 4451327fea..e8a23c1dfa 100644 --- a/src/changes/changes.xml +++ b/src/changes/changes.xml @@ -8,6 +8,12 @@ + + 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. <h3/>) 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). + 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) diff --git a/src/main/java/org/htmlunit/WebClientOptions.java b/src/main/java/org/htmlunit/WebClientOptions.java index eb0ae0cf01..36aed932a7 100644 --- a/src/main/java/org/htmlunit/WebClientOptions.java +++ b/src/main/java/org/htmlunit/WebClientOptions.java @@ -103,6 +103,7 @@ public class WebClientOptions implements Serializable { private boolean isFetchPolyfillEnabled_; + private boolean htmlFragmentSelfClosingTagsAllowed_; private boolean contentSecurityPolicyReportsEnabled_ = true; private boolean localFileAccessFromRemotePagesAllowed_; @@ -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

}), 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

}. 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. * diff --git a/src/main/java/org/htmlunit/html/parser/neko/HtmlUnitNekoHtmlParser.java b/src/main/java/org/htmlunit/html/parser/neko/HtmlUnitNekoHtmlParser.java index 8497a3645d..36d500806b 100644 --- a/src/main/java/org/htmlunit/html/parser/neko/HtmlUnitNekoHtmlParser.java +++ b/src/main/java/org/htmlunit/html/parser/neko/HtmlUnitNekoHtmlParser.java @@ -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 diff --git a/src/test/java/org/htmlunit/WebClient8Test.java b/src/test/java/org/htmlunit/WebClient8Test.java index 7045942d47..c7daaa4d78 100644 --- a/src/test/java/org/htmlunit/WebClient8Test.java +++ b/src/test/java/org/htmlunit/WebClient8Test.java @@ -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 + "
"; + + 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("

x

"); + assertEquals("

x

", copy.asXml()); + } + } + + /** + * @throws Exception if something goes wrong + */ + @Test + public void parseFragmentHtmlFragmentSelfClosingTagsAllowed() throws Exception { + final String html = DOCTYPE_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("

x

y


", copy.asXml()); + } + } + /** * @throws Exception if something goes wrong */ diff --git a/src/test/java/org/htmlunit/WebClientOptionsTest.java b/src/test/java/org/htmlunit/WebClientOptionsTest.java index e8b0201a94..f86f5dfe7d 100644 --- a/src/test/java/org/htmlunit/WebClientOptionsTest.java +++ b/src/test/java/org/htmlunit/WebClientOptionsTest.java @@ -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(), @@ -101,6 +103,7 @@ public void serializationChanged() throws Exception { original.setWebSocketMaxBinaryMessageSize(44); original.setFetchPolyfillEnabled(true); + original.setHtmlFragmentSelfClosingTagsAllowed(true); original.setContentSecurityPolicyReportsEnabled(false); original.setLocalFileAccessFromRemotePagesAllowed(true); @@ -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(),