Skip to content

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

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

Open
wants to merge 3 commits into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
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
Next Next commit
[bugfix] no newline after doctype with indent=no
fixes #4736
  • Loading branch information
line-o committed Jul 16, 2024
commit d826a541671de9a56441a347b9fe9244cfad3e48
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
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@

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 @@ -94,7 +95,7 @@ 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\"/>";
"<bookmap id=\"bookmap-1\">\n asdf\n </bookmap>";

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

Expand Down Expand Up @@ -177,6 +178,10 @@ public void xqueryUpdateNsTest() throws XMLDBException {
@Test
public void getDocTypeDefault() throws XMLDBException {
final Resource res = testCollection.getResource(TEST_XML_DOC_WITH_DOCTYPE_URI.lastSegmentString());
// FIXME (JL): local and remote collections apparently have different output properties set
// INDENT is set to "no" for remote collections that's why it is explicitly set to true here
// Also, setting INDENT to "no" does not work for local collections and only somewhat for remote ones.
testCollection.setProperty(INDENT, "yes");
assertEquals(XML_WITH_DOCTYPE, res.getContent());
}

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
Loading