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

<body>
<release version="5.8.0" date="xxxx xx, 2026" description="Chrome/Edge 157, Bugfixes">
<action type="fix" dev="Ronny Shapiro">
asXml(): an empty non-void html element (e.g. h3, b, label, section) is written with an end tag
instead of as empty-element tag; the html parser does not close it at '/&gt;', so parsing the
output again moved the following siblings into the empty element.
<action type="add" dev="rbri">
NamedNodeMap supports named properties (the attribute names) for 'in', hasOwnProperty() and
Object.getOwnPropertyNames(); prototype properties (e.g. length) are no longer hidden by attributes
Expand Down
16 changes: 16 additions & 0 deletions src/main/java/org/htmlunit/html/HtmlElement.java
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
import java.util.List;
import java.util.Locale;
import java.util.Map;
import java.util.Set;

import org.htmlunit.ClipboardHandler;
import org.htmlunit.ElementNotFoundException;
Expand Down Expand Up @@ -154,6 +155,10 @@ public String value() {
/** Constant 'readonly'. */
protected static final String ATTRIBUTE_READONLY = "readonly";

/** The elements the HTML serialization writes without an end tag. */
private static final Set<String> VOID_ELEMENTS = Set.of("area", "base", "basefont", "bgsound", "br", "col",
"embed", "frame", "hr", "img", "input", "keygen", "link", "meta", "param", "source", "track", "wbr");

/** The listeners which are to be notified of attribute changes. */
private final List<HtmlAttributeChangeListener> attributeListeners_ = new ArrayList<>();

Expand Down Expand Up @@ -1269,6 +1274,17 @@ protected boolean isAttributeCaseSensitive() {
return false;
}

/**
* {@inheritDoc}
* An HTML parser does not close a non-void element at "/&gt;".
* @return {@code true} for non-void elements in the HTML namespace
*/
@Override
protected boolean isEmptyXmlTagExpanded() {
return Html.XHTML_NAMESPACE.equals(getNamespaceURI())
&& !VOID_ELEMENTS.contains(getLowercaseName());
}

/**
* Returns the value of the attribute {@code lang}. Refer to the
* <a href="http://www.w3.org/TR/html401/">HTML 4.01</a>
Expand Down
68 changes: 68 additions & 0 deletions src/test/java/org/htmlunit/WebClient8Test.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@

import org.htmlunit.html.DomElement;
import org.htmlunit.html.DomNodeList;
import org.htmlunit.html.Html;
import org.htmlunit.html.HtmlCheckBoxInput;
import org.htmlunit.html.HtmlInlineFrame;
import org.htmlunit.html.HtmlPage;
Expand Down Expand Up @@ -108,6 +109,73 @@ public void asXmlWhitespaceOnlyTextBetweenElements() throws Exception {
}
}

/**
* @throws Exception if something goes wrong
*/
@Test
public void asXmlEmptyElementWritesEndTag() throws Exception {
final String html = DOCTYPE_HTML
+ "<html><head><title>foo</title></head>"
+ "<body><div id='d'><h3></h3><br><p>x</p></div></body></html>";

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

/**
* @throws Exception if something goes wrong
*/
@Test
public void asXmlEmptyElementsRoundTrip() throws Exception {
final String html = DOCTYPE_HTML
+ "<html><head><title>foo</title></head>"
+ "<body><div id='source'><h3></h3><b></b><label></label><section></section>"
+ "<p>x</p><br><img src='a.png'></div>"
+ "<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 source = page.getElementById("source");
final DomElement copy = page.getElementById("copy");
copy.setInnerHtml(source.asXml());
assertEquals(source.asXml(), copy.getFirstElementChild().asXml());
}
}

/**
* @throws Exception if something goes wrong
*/
@Test
public void asXmlMixedCaseVoidElement() 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 br = page.createElementNS(Html.XHTML_NAMESPACE, "Br");
assertEquals("<br/>", br.asXml());

final DomElement copy = page.getElementById("copy");
copy.setInnerHtml(br.asXml());
assertEquals(1, copy.getChildElementCount());
}
}

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

try (WebClient webClient = new WebClient(getBrowserVersion(), false, null, -1)) {
final HtmlPage page = loadPage(webClient, html, null, URL_FIRST);
assertEquals("<div id=\"d\"><svg/><svg><circle/></svg></div>", page.getElementById("d").asXml());
}
}

/**
* @throws Exception if something goes wrong
*/
Expand Down
2 changes: 1 addition & 1 deletion src/test/java/org/htmlunit/html/HtmlElementTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -910,7 +910,7 @@ public void asXml_span() throws Exception {
final HtmlPage page = loadPage(html);
assertEquals("<?xml version=\"1.0\" encoding=\"ISO-8859-1\"?>\r\n"
+ "<html>\r\n"
+ " <head/>\r\n"
+ " <head></head>\r\n"
+ " <body>\r\n"
+ " <p><span>a</span><span>b</span></p></body>\r\n"
+ "</html>", page.asXml());
Expand Down
2 changes: 1 addition & 1 deletion src/test/java/org/htmlunit/html/HtmlFormTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -1201,7 +1201,7 @@ public void asXml_emptyTag() throws Exception {
final String xml =
"<?xml version=\"1.0\" encoding=\"ISO-8859-1\"?>\r\n"
+ "<html>\r\n"
+ " <head/>\r\n"
+ " <head></head>\r\n"
+ " <body>\r\n"
+ " <form></form>\r\n"
+ " <div>test</div>\r\n"
Expand Down
2 changes: 1 addition & 1 deletion src/test/java/org/htmlunit/html/HtmlOrderedListTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ public void asXml() throws Exception {

assertEquals("<?xml version=\"1.0\" encoding=\"ISO-8859-1\"?>\r\n"
+ "<html>\r\n"
+ " <head/>\r\n"
+ " <head></head>\r\n"
+ " <body>\r\n"
+ " <ol id=\"myNode\"></ol>\n"
+ "foo\n"
Expand Down
2 changes: 1 addition & 1 deletion src/test/java/org/htmlunit/html/HtmlPageTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -1048,7 +1048,7 @@ public void asXml_noscript() throws Exception {

final String expected = "<?xml version=\"1.0\" encoding=\"ISO-8859-1\"?>\r\n"
+ "<html>\r\n"
+ " <head/>\r\n"
+ " <head></head>\r\n"
+ " <body><noscript>"
+ "&lt;p&gt;&lt;strong&gt;your browser does not support JavaScript&lt;/strong&gt;&lt;/p&gt;"
+ "</noscript></body>\r\n"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ public void asXml() throws Exception {
assertEquals("<ul id=\"myNode\"></ul>", element.asXml());
assertEquals("<?xml version=\"1.0\" encoding=\"ISO-8859-1\"?>\r\n"
+ "<html>\r\n"
+ " <head/>\r\n"
+ " <head></head>\r\n"
+ " <body>\r\n"
+ " <ul id=\"myNode\"></ul>\n"
+ "foo\n"
Expand Down
Loading