From e200f7cbeb652a38e1c7c559e523875742fde48d Mon Sep 17 00:00:00 2001
From: Caroline Marques Lau <128255216+cmarqueslau@users.noreply.github.com>
Date: Tue, 10 Nov 2026 17:28:20 +0000
Subject: [PATCH] Fix invalid XML for emoji and math symbols in DocBook export
(#17765)
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
### Summary
When an entry contains emoji or some math symbols the DocBook exports
created invalid XML, so XML parsers could not read the file.
The reason is that `XMLChars` read the text one UTF-16 `char` at a time.
These characters use two `char`s, so each half was written as a separate
character reference. Now the formatter reads the text by Unicode code
point, so each character becomes one valid reference. Other characters
are escaped as before.
### Steps to test
1. Start JabRef and create a new library.
2. Add an entry of type "Article" with the title "Smile 😀 and alpha 𝛼
and é".
3. Select File -> Export -> Export all entries, choose DocBook 5.1 and
save the file with the .xml extension.
4. Open the file in a web browser. The browser should show the XML tree
with the title "Smile 😀 and alpha 𝛼 and é".
How it is know:
How it was before:
### Related issues and pull requests
Closes #17747
### AI usage
I used Claude Code (model claude-opus-5-5) as an assistant. It helped me
understand why surrogate pairs created invalid XML and suggested the
code change. I reviewed and adjusted the code, wrote the steps to test,
and ran the tests and the manual export myself.
### Checklist
- [x] I own the copyright of the code submitted and I license it under
the [MIT license](https://github.com/JabRef/jabref/blob/main/LICENSE)
- [x] If AI tools were used, I disclosed them in the "AI usage" section
and reviewed, understood, and take full ownership of all AI-generated
code
- [x] I manually tested my changes in running JabRef (always required)
- [x] I added JUnit tests for changes (if applicable)
- [x] I added screenshots in the PR description (if change is visible to
the user)
- [x] I added **one sentence (max 20 words)** to `CHANGELOG.md`
describing the change from the user's point of view (if the change is
visible to the user)
- [/] I checked the [user documentation](https://docs.jabref.org/) for
up to dateness and submitted a pull request to our [user documentation
repository](https://github.com/JabRef/user-documentation/tree/main/en)
---------
Co-authored-by: Carl Christian Snethlage <50491877+calixtus@users.noreply.github.com>
(cherry picked from commit 61dcea698b7bb3e9d3583d2d28449598dcc0ccdf)
---
CHANGELOG.md | 1 +
.../jabref/logic/layout/format/XMLChars.java | 10 ++--
.../logic/exporter/DocBook5ExporterTest.java | 18 +++++++
.../logic/layout/format/XMLCharsTest.java | 54 +++++++++++++++++++
4 files changed, 79 insertions(+), 4 deletions(-)
create mode 100644 jablib/src/test/java/org/jabref/logic/layout/format/XMLCharsTest.java
diff --git a/CHANGELOG.md b/CHANGELOG.md
index 6d4fb5ff2867..4b3127e66ade 100644
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -33,6 +33,7 @@ Note that this project **does not** adhere to [Semantic Versioning](https://semv
- We fixed an issue where back navigation selected a deleted entry instead of skipping it. [#17353](https://github.com/JabRef/jabref/issues/17353)
- We fixed the linked file tooltip showing the old file name after renaming or moving the file. [#9277](https://github.com/JabRef/jabref/issues/9277)
- We fixed an issue where citationcount, day, and dayfiled columns were sorted alphabetically instead of numerically. [#17459](https://github.com/JabRef/jabref/issues/17459)
+- 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)
## [6.0-beta.1] - 2026-09-21
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 40ff6fd0fa0d..b7901482ad1c 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());
+ }
+}