Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[bugfix] omit newline after doctype with indent=no #5370

Open
wants to merge 3 commits into
base: develop
Choose a base branch
from
Open
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
Original file line number Diff line number Diff line change
Expand Up @@ -151,7 +151,7 @@ public void endDocument() throws TransformerException {
@Override
public void endDocumentType() throws TransformerException {
super.endDocumentType();
super.characters("\n");
indent();
sameline = false;
}

Expand Down Expand Up @@ -197,11 +197,7 @@ protected void pushWhitespacePreserve(final CharSequence value) {
protected void popWhitespacePreserve() {
if (!whitespacePreserveStack.isEmpty() && Math.abs(whitespacePreserveStack.peek()) > level) {
whitespacePreserveStack.pop();
if (whitespacePreserveStack.isEmpty() || whitespacePreserveStack.peek() >= 0) {
whitespacePreserve = false;
} else {
whitespacePreserve = true;
}
whitespacePreserve = !whitespacePreserveStack.isEmpty() && whitespacePreserveStack.peek() < 0;
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ public void setUp() throws Exception {

@Test
public void testAttributeWithBooleanValue() throws Exception {
final String expected = "<!DOCTYPE html>\n<input checked>";
final String expected = "<!DOCTYPE html><input checked>";
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

would it be useful to have an other test with the \n still there, to show the (non?) effect of the changes?

final QName elQName = new QName("input");
writer.startElement(elQName);
writer.attribute("checked", "checked");
Expand All @@ -54,7 +54,7 @@ public void testAttributeWithBooleanValue() throws Exception {

@Test
public void testAttributeWithNonBooleanValue() throws Exception {
final String expected = "<!DOCTYPE html>\n<input name=\"name\">";
final String expected = "<!DOCTYPE html><input name=\"name\">";
final QName elQName = new QName("input");
writer.startElement(elQName);
writer.attribute("name", "name");
Expand All @@ -66,7 +66,7 @@ public void testAttributeWithNonBooleanValue() throws Exception {

@Test
public void testAttributeQNameWithBooleanValue() throws Exception {
final String expected = "<!DOCTYPE html>\n<input checked>";
final String expected = "<!DOCTYPE html><input checked>";
final QName elQName = new QName("input");
final QName attrQName = new QName("checked");
writer.startElement(elQName);
Expand All @@ -79,7 +79,7 @@ public void testAttributeQNameWithBooleanValue() throws Exception {

@Test
public void testAttributeQNameWithNonBooleanValue() throws Exception {
final String expected = "<!DOCTYPE html>\n<input name=\"name\">";
final String expected = "<!DOCTYPE html><input name=\"name\">";
final QName elQName = new QName("input");
final QName attrQName = new QName("name");
writer.startElement(elQName);
Expand Down
69 changes: 62 additions & 7 deletions exist-core/src/test/java/org/exist/xmldb/SerializationTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -43,10 +43,12 @@
import org.xmlunit.builder.Input;
import org.xmlunit.diff.Diff;

import javax.xml.transform.OutputKeys;
import javax.xml.transform.Source;

import java.util.Arrays;

import static javax.xml.transform.OutputKeys.INDENT;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertNotNull;
Expand Down Expand Up @@ -93,8 +95,11 @@ public class SerializationTest {
private static final XmldbURI TEST_XML_DOC_WITH_DOCTYPE_URI = XmldbURI.create("test-with-doctype.xml");

private static final String XML_WITH_DOCTYPE =
"<!DOCTYPE bookmap PUBLIC \"-//OASIS//DTD DITA BookMap//EN\" \"bookmap.dtd\">\n" +
"<bookmap id=\"bookmap-1\"/>";
"""
<!DOCTYPE bookmap PUBLIC "-//OASIS//DTD DITA BookMap//EN" "bookmap.dtd">
<bookmap id="bookmap-1">
<title>The Title</title>
</bookmap>""";

private static final XmldbURI TEST_XML_DOC_WITH_XMLDECL_URI = XmldbURI.create("test-with-xmldecl.xml");

Expand All @@ -118,7 +123,7 @@ public static java.util.Collection<Object[]> data() {

private Collection testCollection;

private final String getBaseUri() {
private String getBaseUri() {
return baseUri.replace(PORT_PLACEHOLDER, Integer.toString(existWebServer.getPort()));
}

Expand Down Expand Up @@ -186,7 +191,7 @@ public void getDocTypeNo() throws XMLDBException {
try {
final Resource res = testCollection.getResource(TEST_XML_DOC_WITH_DOCTYPE_URI.lastSegmentString());
testCollection.setProperty(EXistOutputKeys.OUTPUT_DOCTYPE, "no");
assertEquals("<bookmap id=\"bookmap-1\"/>", res.getContent());
assertEquals("<bookmap id=\"bookmap-1\">\n <title>The Title</title>\n</bookmap>", res.getContent());
} finally {
if (prevOutputDocType != null) {
testCollection.setProperty(EXistOutputKeys.OUTPUT_DOCTYPE, prevOutputDocType);
Expand Down Expand Up @@ -215,7 +220,7 @@ public void getXmlDeclDefault() throws XMLDBException {
}

@Test
public void getXmlDeclNo() throws XMLDBException {
public void getOmitOriginalXmlDeclNo() throws XMLDBException {
final String prevOmitOriginalXmlDecl = testCollection.getProperty(EXistOutputKeys.OMIT_ORIGINAL_XML_DECLARATION);
try {
final Resource res = testCollection.getResource(TEST_XML_DOC_WITH_XMLDECL_URI.lastSegmentString());
Expand All @@ -229,7 +234,7 @@ public void getXmlDeclNo() throws XMLDBException {
}

@Test
public void getXmlDeclYes() throws XMLDBException {
public void getOmitOriginalXmlDeclYes() throws XMLDBException {
final String prevOmitOriginalXmlDecl = testCollection.getProperty(EXistOutputKeys.OMIT_ORIGINAL_XML_DECLARATION);
try {
final Resource res = testCollection.getResource(TEST_XML_DOC_WITH_XMLDECL_URI.lastSegmentString());
Expand All @@ -241,6 +246,52 @@ public void getXmlDeclYes() throws XMLDBException {
}
}
}
@Test
public void getOmitXmlDeclNo() throws XMLDBException {
final String prevOmitXmlDecl = testCollection.getProperty(OutputKeys.OMIT_XML_DECLARATION);
try {
final Resource res = testCollection.getResource(TEST_XML_DOC_WITH_XMLDECL_URI.lastSegmentString());
testCollection.setProperty(OutputKeys.OMIT_XML_DECLARATION, "no");
assertEquals(XML_WITH_XMLDECL, res.getContent());
} finally {
if (prevOmitXmlDecl != null) {
testCollection.setProperty(OutputKeys.OMIT_XML_DECLARATION, prevOmitXmlDecl);
}
}
}

@Test
public void getOmitXmlDeclYes() throws XMLDBException {
final String prevOmitXmlDecl = testCollection.getProperty(OutputKeys.OMIT_XML_DECLARATION);
try {
final Resource res = testCollection.getResource(TEST_XML_DOC_WITH_XMLDECL_URI.lastSegmentString());
testCollection.setProperty(OutputKeys.OMIT_XML_DECLARATION, "yes");
assertEquals(XML_WITH_XMLDECL, res.getContent());
} finally {
if (prevOmitXmlDecl != null) {
testCollection.setProperty(OutputKeys.OMIT_XML_DECLARATION, prevOmitXmlDecl);
}
}
}

@Test
public void getOmitAllXmlDeclYes() throws XMLDBException {
final String prevOmitXmlDecl = testCollection.getProperty(OutputKeys.OMIT_XML_DECLARATION);
final String prevOmitOriginalXmlDecl = testCollection.getProperty(EXistOutputKeys.OMIT_ORIGINAL_XML_DECLARATION);
try {
final Resource res = testCollection.getResource(TEST_XML_DOC_WITH_XMLDECL_URI.lastSegmentString());
testCollection.setProperty(OutputKeys.OMIT_XML_DECLARATION, "yes");
testCollection.setProperty(EXistOutputKeys.OMIT_ORIGINAL_XML_DECLARATION, "yes");
assertEquals("<bookmap id=\"bookmap-2\"/>", res.getContent());
} finally {
if (prevOmitXmlDecl != null) {
testCollection.setProperty(OutputKeys.OMIT_XML_DECLARATION, prevOmitXmlDecl);
}
if (prevOmitOriginalXmlDecl != null) {
testCollection.setProperty(EXistOutputKeys.OMIT_ORIGINAL_XML_DECLARATION, prevOmitOriginalXmlDecl);
}
}
}

private static void assertXMLEquals(final String expected, final Resource actual) throws XMLDBException {
final Source srcExpected = Input.fromString(expected).build();
Expand Down Expand Up @@ -271,7 +322,11 @@ public void setUp() throws XMLDBException {
final XMLResource res2 = testCollection.createResource(TEST_XML_DOC_WITH_XMLDECL_URI.lastSegmentString(), XMLResource.class);
res2.setContent(XML_WITH_XMLDECL);
testCollection.storeResource(res2);
}

// FIXME (JL): local and remote collections apparently have different output properties set
// local collections have INDENT set to "yes" whereas remote collections have "no" by default
testCollection.setProperty(INDENT, "yes");
}

@After
public void tearDown() throws XMLDBException {
Expand Down
33 changes: 17 additions & 16 deletions exist-core/src/test/xquery/xquery3/serialize.xql
Original file line number Diff line number Diff line change
Expand Up @@ -847,36 +847,39 @@ function ser:serialize-xml-134() {
};

declare
%test:assertEquals('<!DOCTYPE html> <option selected></option>')
%test:assertEquals('<!DOCTYPE html><option selected></option>')
function ser:serialize-html-5-boolean-attribute-names() {
<option selected="selected"/>
=> serialize($ser:opt-map-html5)
=> normalize-space()
serialize(<option selected="selected"/>, $ser:opt-map-html5)
};

declare
%test:assertEquals('<!DOCTYPE html> <br>')
%test:assertEquals('<!DOCTYPE html><br>')
function ser:serialize-html-5-empty-tags() {
<br/>
=> serialize($ser:opt-map-html5)
=> normalize-space()
serialize(<br/>, $ser:opt-map-html5)
};

(: test for https://github.com/eXist-db/exist/issues/4736 :)
declare
%test:assertEquals('<!DOCTYPE html> <html><body><style>ul > li { color:red; }</style><script>if (a < b) foo()</script></body></html>')
%test:assertEquals('<!DOCTYPE html>&#10;<html>&#10; <body>&#10; <p>hi</p>&#10; </body>&#10;</html>')
function ser:serialize-html-5-with-indent() {
serialize(<html><body><p>hi</p></body></html>,
map{ "method": "html", "version": "5.0", "indent": true() })
};

declare
%test:assertEquals('<!DOCTYPE html><html><body><style>ul > li { color:red; }</style><script>if (a < b) foo()</script></body></html>')
function ser:serialize-html-5-raw-text-elements-body() {
<html>
<body>
<style><![CDATA[ul > li { color:red; }]]></style>
<script><![CDATA[if (a < b) foo()]]></script>
</body>
</html>
=> serialize($ser:opt-map-html5)
=> normalize-space()
=> serialize($ser:opt-map-html5)
};

declare
%test:assertEquals('<!DOCTYPE html> <html><head><style>ul > li { color:red; }</style><script>if (a < b) foo()</script></head><body></body></html>')
%test:assertEquals('<!DOCTYPE html><html><head><style>ul > li { color:red; }</style><script>if (a < b) foo()</script></head><body></body></html>')
function ser:serialize-html-5-raw-text-elements-head() {
<html>
<head>
Expand All @@ -885,12 +888,11 @@ function ser:serialize-html-5-raw-text-elements-head() {
</head>
<body></body>
</html>
=> serialize($ser:opt-map-html5)
=> normalize-space()
=> serialize($ser:opt-map-html5)
};

declare
%test:assertEquals('<!DOCTYPE html> <html><head><title>XML &amp;gt; JSON</title></head><body><textarea>if (a &amp;lt; b) foo()</textarea></body></html>')
%test:assertEquals('<!DOCTYPE html><html><head><title>XML &amp;gt; JSON</title></head><body><textarea>if (a &amp;lt; b) foo()</textarea></body></html>')
function ser:serialize-html-5-needs-escape-elements() {
<html>
<head>
Expand All @@ -901,7 +903,6 @@ function ser:serialize-html-5-needs-escape-elements() {
</body>
</html>
=> serialize($ser:opt-map-html5)
=> normalize-space()
};

(: test for https://github.com/eXist-db/exist/issues/4702 :)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,15 +39,14 @@
import java.io.IOException;
import java.nio.file.Files;

import static java.nio.charset.StandardCharsets.UTF_8;
import static org.junit.Assert.*;
import static org.junit.Assert.assertEquals;

public class SerializationTest {

private static final String XML_WITH_DOCTYPE =
"<!DOCTYPE bookmap PUBLIC \"-//OASIS//DTD DITA BookMap//EN\" \"bookmap.dtd\">\n" +
"<bookmap id=\"bookmap-1\"/>";
"<!DOCTYPE bookmap PUBLIC \"-//OASIS//DTD DITA BookMap//EN\" \"bookmap.dtd\">" +
"<bookmap id=\"bookmap-1\"><title>The Title</title></bookmap>";
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why add a child element here, it doesn't seem that it has changed anything else about the test?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The child element was added to see the effect of indent=no to slightly more complex data.


private static final String XML_WITH_XMLDECL =
"<?xml version=\"1.1\" encoding=\"ISO-8859-1\" standalone=\"yes\"?>\n" +
Expand Down
Loading