Skip to content
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 @@ -30,7 +30,11 @@ public static String entityToString(String input) {

try {
int codePoint = Integer.parseInt(value, base);
if (codePoint == 0) {
// The spec says that invalid Unicode code points are replaced by the REPLACEMENT
// CHARACTER. The surrogate range is not a set of valid code points.
if (codePoint == 0
|| (codePoint >= Character.MIN_SURROGATE
&& codePoint <= Character.MAX_SURROGATE)) {
return "\uFFFD";
}
return new String(Character.toChars(codePoint));
Expand Down
13 changes: 13 additions & 0 deletions commonmark/src/test/java/org/commonmark/test/SpecialInputTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,19 @@ public void nullCharacterEntityShouldBeReplaced() {
assertRendering("foo&#0;bar", "<p>foo\uFFFDbar</p>\n");
}

@Test
public void surrogateCharacterReferenceShouldBeReplaced() {
// The reference is well-formed, but a surrogate is not a valid Unicode code point, so it
// must be replaced by U+FFFD (same as cmark and commonmark.js)
assertRendering("&#xD800;", "<p>\uFFFD</p>\n");
assertRendering("&#55296;", "<p>\uFFFD</p>\n");
assertRendering("&#xDFFF;", "<p>\uFFFD</p>\n");
assertRendering("before&#xD800;after", "<p>before\uFFFDafter</p>\n");
// Code points outside the surrogate range are not affected
assertRendering("&#x1F600;", "<p>\uD83D\uDE00</p>\n");
assertRendering("&#x10FFFF;", "<p>\uDBFF\uDFFF</p>\n");
}

@Test
public void crLfAsLineSeparatorShouldBeParsed() {
assertRendering("foo\r\nbar", "<p>foo\nbar</p>\n");
Expand Down