Fix bug where some XHTML elements wrongly serialised concisely

This commit is contained in:
Grahame Grieve 2024-03-01 16:41:32 +11:00
parent a3fb1457a0
commit 7ba1511748
4 changed files with 25 additions and 13 deletions

View File

@ -39,6 +39,7 @@ import java.io.StringWriter;
import java.io.Writer;
import java.util.List;
import org.hl7.fhir.utilities.DebugUtilities;
import org.hl7.fhir.utilities.Utilities;
import org.hl7.fhir.utilities.xml.IXMLWriter;
import org.w3c.dom.Element;
@ -236,15 +237,18 @@ public class XhtmlComposer {
if (!pretty || noPrettyOverride)
indent = "";
// html self closing tags: http://xahlee.info/js/html5_non-closing_tag.html
boolean concise = node.getChildNodes().size() == 0;
if (node.hasEmptyExpanded() && node.getEmptyExpanded()) {
concise = false;
boolean concise = false;
if (!node.hasChildren()) {
if (this.xml) {
concise = true;
} else if (!(node.hasEmptyExpanded() && node.getEmptyExpanded()) &&
Utilities.existsInList(node.getName(), "area", "base", "br", "col", "command", "embed", "hr", "img", "input", "keygen", "link", "menuitem", "meta", "param", "source", "track", "wbr")) {
// In HTML5, only these elements can self-close
// https://developer.mozilla.org/en-US/docs/Glossary/Void_element
concise = true;
}
}
if (!xml && Utilities.existsInList(node.getName(), "area", "base", "br", "col", "command", "embed", "hr", "img", "input", "keygen", "link", "menuitem", "meta", "param", "source", "track", "wbr")) {
concise = true;
}
if (concise)
dst.append(indent + "<" + node.getName() + attributes(node) + "/>" + (pretty && !noPrettyOverride ? "\r\n" : ""));
else {

View File

@ -1024,6 +1024,14 @@ public class XhtmlNode extends XhtmlFluent implements IBaseXhtml {
return addTag("link").attribute("rel", rel).attribute("href", href);
}
public XhtmlNode ahOrNot(String href) {
if (href == null) {
return this;
}
XhtmlNode x = addTag("a").attribute("href", href);
return x;
}
public void wbr() {
addTag("wbr");

View File

@ -1,4 +1,4 @@
package org.hl7.fhir.utilities.tests;
package org.hl7.fhir.utilities.xhtml;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
@ -6,6 +6,7 @@ import java.io.ObjectOutputStream;
import org.hl7.fhir.exceptions.FHIRException;
import org.hl7.fhir.exceptions.FHIRFormatError;
import org.hl7.fhir.utilities.tests.BaseTestingUtilities;
import org.hl7.fhir.utilities.xhtml.NodeType;
import org.hl7.fhir.utilities.xhtml.XhtmlComposer;
import org.hl7.fhir.utilities.xhtml.XhtmlNode;
@ -145,9 +146,8 @@ public class XhtmlNodeTest {
String src = BaseTestingUtilities.loadTestResource("xhtml", "xhtml-empty-elements.xml");
XhtmlNode x = new XhtmlParser().parse(src, "xml");
String xml = new XhtmlComposer(false, false).compose(x);
Assertions.assertEquals(src.trim(), xml.trim());
String xml = new XhtmlComposer(true, false).compose(x);
Assertions.assertEquals("<xml xmlns=\"http://something\"><empty attr=\"1\"/><empty attr=\"2\"/></xml>", xml.trim());
}
@Test

View File

@ -20,7 +20,7 @@
<properties>
<guava_version>32.0.1-jre</guava_version>
<hapi_fhir_version>6.4.1</hapi_fhir_version>
<validator_test_case_version>1.5.0</validator_test_case_version>
<validator_test_case_version>1.5.1-SNAPSHOT</validator_test_case_version>
<jackson_version>2.16.0</jackson_version>
<junit_jupiter_version>5.9.2</junit_jupiter_version>
<junit_platform_launcher_version>1.8.2</junit_platform_launcher_version>