diff --git a/CHANGELOG.md b/CHANGELOG.md index 8f870ae449bf..310728007ac6 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -55,6 +55,7 @@ Note that this project **does not** adhere to [Semantic Versioning](https://semv ### Fixed +- We fixed an issue where the DocBook export wrote invalid XML for emoji and some math symbols. [#17747](https://github.com/JabRef/jabref/issues/17747) - We fixed an issue where clicking "Generate" in the AUX file import dialog before parsing caused an exception. - We fixed an issue where it was hard to distinguish between enabled/disabled status of search buttons in dark mode and added tooltips. [#11569](https://github.com/JabRef/jabref/issues/11569) - We fixed an issue where entries containing dollar signs, backslashes, or unset fields passed to `substring$` caused citation style preview and export to fail. [#12799](https://github.com/JabRef/jabref/issues/12799) diff --git a/jablib/src/main/java/org/jabref/logic/layout/format/XMLChars.java b/jablib/src/main/java/org/jabref/logic/layout/format/XMLChars.java index ba630e70e9c9..cea9665b298a 100644 --- a/jablib/src/main/java/org/jabref/logic/layout/format/XMLChars.java +++ b/jablib/src/main/java/org/jabref/logic/layout/format/XMLChars.java @@ -70,23 +70,25 @@ private String restFormat(String toFormat) { StringBuilder buffer = new StringBuilder(fieldText.length() * 2); - for (int i = 0; i < fieldText.length(); i++) { - int code = fieldText.charAt(i); + int i = 0; + while (i < fieldText.length()) { + int code = fieldText.codePointAt(i); // Checking the case when the character is already escaped // Just push "&#" to the buffer and keep going from the next char if ((code == 38) && (fieldText.charAt(i + 1) == 35)) { i += 2; buffer.append("&#"); - code = fieldText.charAt(i); + code = fieldText.codePointAt(i); } // TODO: Check whether > 125 is correct here or whether it should rather be >= if ((code > 125) || forceReplace[code]) { buffer.append("&#").append(code).append(';'); } else { - buffer.append((char) code); + buffer.appendCodePoint(code); } + i += Character.charCount(code); } fieldText = buffer.toString(); diff --git a/jablib/src/test/java/org/jabref/logic/exporter/DocBook5ExporterTest.java b/jablib/src/test/java/org/jabref/logic/exporter/DocBook5ExporterTest.java index c1591d095147..e591648690b6 100644 --- a/jablib/src/test/java/org/jabref/logic/exporter/DocBook5ExporterTest.java +++ b/jablib/src/test/java/org/jabref/logic/exporter/DocBook5ExporterTest.java @@ -9,6 +9,7 @@ import java.time.LocalDate; import java.util.List; +import javax.xml.parsers.DocumentBuilderFactory; import javax.xml.parsers.ParserConfigurationException; import javax.xml.transform.TransformerException; @@ -28,6 +29,8 @@ import org.junit.jupiter.api.parallel.ExecutionMode; import org.junit.jupiter.api.parallel.ResourceLock; import org.mockito.Answers; +import org.w3c.dom.Document; +import org.xml.sax.SAXException; import org.xmlunit.builder.Input; import org.xmlunit.builder.Input.Builder; import org.xmlunit.diff.DefaultNodeMatcher; @@ -35,6 +38,7 @@ import org.xmlunit.matchers.CompareMatcher; import static org.hamcrest.MatcherAssert.assertThat; +import static org.junit.jupiter.api.Assertions.assertEquals; import static org.mockito.Mockito.mock; @Execution(ExecutionMode.SAME_THREAD) @@ -86,4 +90,18 @@ void performExportForSingleEntry(@TempDir Path testFolder) throws IOException, S .normalizeWhitespace() .withNodeMatcher(new DefaultNodeMatcher(ElementSelectors.byNameAndText)).throwComparisonFailure()); } + + @Test + void titleWithEmojiIsValidXml(@TempDir Path testFolder) throws IOException, SaveException, ParserConfigurationException, TransformerException, SAXException { + Path path = testFolder.resolve("supplementaryCharacters.xml"); + BibEntry entry = new BibEntry(StandardEntryType.Book) + .withField(StandardField.TITLE, "Smile 😀 and 𝛼"); + + exporter.export(databaseContext, path, List.of(entry)); + + Document document = DocumentBuilderFactory.newInstance() + .newDocumentBuilder() + .parse(path.toFile()); + assertEquals("Smile 😀 and 𝛼", document.getElementsByTagName("citetitle").item(0).getTextContent()); + } } diff --git a/jablib/src/test/java/org/jabref/logic/layout/format/XMLCharsTest.java b/jablib/src/test/java/org/jabref/logic/layout/format/XMLCharsTest.java new file mode 100644 index 000000000000..2d8d3646dbf5 --- /dev/null +++ b/jablib/src/test/java/org/jabref/logic/layout/format/XMLCharsTest.java @@ -0,0 +1,54 @@ +package org.jabref.logic.layout.format; + +import java.io.IOException; +import java.io.Reader; +import java.util.stream.Stream; + +import javax.xml.parsers.DocumentBuilderFactory; +import javax.xml.parsers.ParserConfigurationException; + +import org.jabref.logic.layout.LayoutFormatter; + +import org.junit.jupiter.params.ParameterizedTest; +import org.junit.jupiter.params.provider.Arguments; +import org.junit.jupiter.params.provider.MethodSource; +import org.junit.jupiter.params.provider.ValueSource; +import org.w3c.dom.Document; +import org.xml.sax.InputSource; +import org.xml.sax.SAXException; + +import static org.junit.jupiter.api.Assertions.assertEquals; + +class XMLCharsTest { + + private final LayoutFormatter formatter = new XMLChars(); + + private static Stream format() { + return Stream.of( + Arguments.of("normal", "normal"), + Arguments.of("é", "é"), + Arguments.of("a < b > c", "a < b > c"), + Arguments.of("&", "&"), + Arguments.of("𝛼", "𝛼"), + Arguments.of("😀", "😀"), + Arguments.of("Smile 😀 and é", "Smile 😀 and é")); + } + + @ParameterizedTest + @MethodSource + void format(String expected, String input) { + assertEquals(expected, formatter.format(input)); + } + + @ParameterizedTest + @ValueSource(strings = {"normal", "é", "𝛼", "😀"}) + void formattedTextIsParsableXml(String input) throws ParserConfigurationException, SAXException, IOException { + String xml = "" + formatter.format(input) + ""; + + Document document = DocumentBuilderFactory.newInstance() + .newDocumentBuilder() + .parse(new InputSource(Reader.of(xml))); + + assertEquals(input, document.getDocumentElement().getTextContent()); + } +}