+
+ 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 '/>', so parsing the
+ output again moved the following siblings into the empty element.
NamedNodeMap supports named properties (the attribute names) for 'in', hasOwnProperty() and
Object.getOwnPropertyNames(); prototype properties (e.g. length) are no longer hidden by attributes
diff --git a/src/main/java/org/htmlunit/html/HtmlElement.java b/src/main/java/org/htmlunit/html/HtmlElement.java
index 0ac47e1832..7c4ca752b0 100644
--- a/src/main/java/org/htmlunit/html/HtmlElement.java
+++ b/src/main/java/org/htmlunit/html/HtmlElement.java
@@ -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;
@@ -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 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 attributeListeners_ = new ArrayList<>();
@@ -1269,6 +1274,17 @@ protected boolean isAttributeCaseSensitive() {
return false;
}
+ /**
+ * {@inheritDoc}
+ * An HTML parser does not close a non-void element at "/>".
+ * @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
* HTML 4.01
diff --git a/src/test/java/org/htmlunit/WebClient8Test.java b/src/test/java/org/htmlunit/WebClient8Test.java
index 478b1fb114..7045942d47 100644
--- a/src/test/java/org/htmlunit/WebClient8Test.java
+++ b/src/test/java/org/htmlunit/WebClient8Test.java
@@ -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;
@@ -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
+ + "foo"
+ + "